![]() |
mapping joystick axis to specific 'channels' in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: mapping joystick axis to specific 'channels' in python (/thread-28154.html) |
mapping joystick axis to specific 'channels' in python - DashOrion - Jul-07-2020 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: 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! RE: mapping joystick axis to specific 'channels' in python - DashOrion - Jul-07-2020 Ok, so I now made it like this, mapping channels to joystick axis with a dictionary: sbusChannel_to_JoystickAxis = { 0: 3, 1: 5, 8: 2, } for sbusChannel, joystickaxis in sbusChannel_to_JoystickAxis.items(): rawValue = get_axis[joystickaxis] mapped_value = map(rawValue,-1,1,192,1792) sbusOutput.set_channel(sbusChannel, int(mapped_value))Is this the proper way and best way to do it? Or is there a better way? |