1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Donkey Carのプロポ対応その3(Donkey car設定編)

Posted at

はじめに

既存のジョイスティック種別(CONTROLLER_TYPE)と混乱しないようにするために、新しくArduinoHIDという種別を追加して説明します。

myconfig.pyの変更

CONTROLLER_TYPE='ArduinoHID'               #(ArduinoHID|ps3|ps4|xbox|nimbus|wiiu|F710)

controller.pyの変更

コントローラタイプの追加

二行追加

def get_js_controller(cfg):
    cont_class = None
    if cfg.CONTROLLER_TYPE == "ps3":
        cont_class = PS3JoystickController
    elif cfg.CONTROLLER_TYPE == "ps4":
        cont_class = PS4JoystickController
    elif cfg.CONTROLLER_TYPE == "nimbus":
        cont_class = NimbusController
    elif cfg.CONTROLLER_TYPE == "xbox":
        cont_class = XboxOneJoystickController
    elif cfg.CONTROLLER_TYPE == "wiiu":
        cont_class = WiiUController
    elif cfg.CONTROLLER_TYPE == "F710":
        cont_class = LogitechJoystickController
    elif cfg.CONTROLLER_TYPE == "ArduinoHID":
        cont_class = ArduinoHIDJoystickController
    else:
        raise("Unknown controller type: " + cfg.CONTROLLER_TYPE)

ArduinoHIDのインターフェイスの設定

stickの番号は、Arduinoのジョイスティック定義と一致しているので、変えなくても大丈夫だと思います。ボタンはArduinoのボタン番号(0, 1, 2, ...)に0x120を足したものとなっています。

class ArduinoHIDJoystick(Joystick):
    '''
    An interface to a Arduino HID joystick available at /dev/input/js0
    '''
    def __init__(self, *args, **kwargs):
        super(ArduinoHIDJoystick, self).__init__(*args, **kwargs)

        self.axis_names = {
            0x00 : 'left_stick_horz',
            0x04 : 'right_stick_vert',
        }

        self.button_names = {
            0x120 : 'mode',    #288
            0x121 : 'R1',      #289
            0x122 : 'L1',      #290
        }

ArduinoHIDのクラスの定義

この例では、左側スティックのX軸、右側スティックのY軸、および3つのボタンにDonkey Carの動作を割り当てています。

class ArduinoHIDJoystickController(JoystickController):
    '''
    A Controller object that maps inputs to actions
    '''
    def __init__(self, *args, **kwargs):
        super(ArduinoHIDJoystickController, self).__init__(*args, **kwargs)

    def init_js(self):
        '''
        attempt to init joystick
        '''
        try:
            self.js = ArduinoHIDJoystick(self.dev_fn)
            if not self.js.init():
                self.js = None
        except FileNotFoundError:
            print(self.dev_fn, "not found.")
            self.js = None
        return self.js is not None


    def init_trigger_maps(self):
        '''
        init set of mapping from buttons to function calls for RC controller
        '''

        self.button_down_trigger_map = {
            'mode' : self.toggle_mode,
            'L1' : self.increase_max_throttle,
            'R1' : self.decrease_max_throttle,
        }

        self.axis_trigger_map = {
            'left_stick_horz' : self.set_steering,
            'right_stick_vert' : self.set_throttle,
        }

終わりに

近藤科学のMC-8のプロポを試したのみで本格的なプロポは実際に持っていないため、もし何か間違いがあったら、指摘のほどお願いします。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?