Jul-07-2020, 11:25 AM
I'm fairly new to python and I just can't come up with an adequate and elegant solution for my simple first program in python. I hope anyone more experienced can help. The program takes input from a (hid) joystick (using pygame) and outputs a standardized signal (SBUS) over UART to control a rc model/robot.
What I want to do/what the problem is: All is working fine, but I want be able to easily map a specific joystick axes to a specific SBUS channel. For now in a config file, in the future user selectable in a gui (PyQt based I think).
e.g. I want to do:
I'm not sure however how to do this in an easy way, with what datastructure and with the least amount of lines (e.g. in a for loop).
This is the part that I want to write better, in some sort of way with a dictionary or list for mapping joystick axis to sbus channels:
- takes input from a (hid) joystick axis (0 - many available) (in my case a yoke or steering wheel) (float of -1 to 1)
- maps the input value to a INT (int from 192 to 1792)
- write the specific channel to a particular SBUS channel (20 channels available) (it's a communication protocol used in the RC world)
- encode the SBUS frame to a bytearray and sent it out over serial
What I want to do/what the problem is: All is working fine, but I want be able to easily map a specific joystick axes to a specific SBUS channel. For now in a config file, in the future user selectable in a gui (PyQt based I think).
e.g. I want to do:
- map joystick axis 0 to sbus channel 3.
- map joystick axis 1 to sbus channel 1.
- map joystick axis 2 to sbus channel 0.
- etc
I'm not sure however how to do this in an easy way, with what datastructure and with the least amount of lines (e.g. in a for loop).
This is the part that I want to write better, in some sort of way with a dictionary or list for mapping joystick axis to sbus channels:
value_roll = joystick.get_axis(0) value_pitch = joystick.get_axis(1) value_throttle = joystick.get_axis(2) mapped_value_roll = map(value_roll,-1,1,192,1792) mapped_value_pitch = map(value_pitch, -1, 1, 1792, 192) mapped_value_throttle = map(value_throttle, -1, 1, 1792, 192) sbusOutput.set_channel(3, int(mapped_value_roll)) sbusOutput.set_channel(1, int(mapped_value_pitch)) sbusOutput.set_channel(0, int(mapped_value_throttle))I hope somebody can help me to get my head around an elegant way to do this. Thanks already!