Python Forum
mapping joystick axis to specific 'channels' in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mapping joystick axis to specific 'channels' in python
#1
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.
  • 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!
Reply
#2
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding MIDI Channels to each Input zach1234 6 1,192 Apr-20-2023, 11:51 AM
Last Post: jefsummers
  Mapping a value to an interval JazonKuer 12 1,927 Mar-17-2023, 07:59 PM
Last Post: Gribouillis
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,774 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,299 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Pygame with joystick mmagner2022 1 1,667 Aug-06-2021, 07:56 PM
Last Post: SheeppOSU
  Mapping a range ebolisa 5 3,465 Jun-12-2021, 11:17 PM
Last Post: ebolisa
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,212 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,459 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  mapping-camera-coordinates-to-a-2d-floor-plan fauveboyxuuki 0 2,525 Dec-10-2019, 10:34 PM
Last Post: fauveboyxuuki
  Delete specific lines contain specific words mannyi 2 4,109 Nov-04-2019, 04:50 PM
Last Post: mannyi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020