| 10 | import os |
| 11 | |
| 12 | class JoyStickNode(LifecycleNode): |
| 13 | def __init__(self): |
| 14 | super().__init__('joystick_interface') |
| 15 | |
| 16 | |
| 17 | self.cmd_pub = None |
| 18 | self.buttons_pub = None |
| 19 | self.dead_man_pub = None |
| 20 | self.timer = None |
| 21 | self.joystick = None |
| 22 | |
| 23 | self.declare_parameter('device_input', '/dev/input/js0') |
| 24 | self.declare_parameter('linear_deadband', 0.02) |
| 25 | self.declare_parameter('angular_deadband', 0.02) |
| 26 | |
| 27 | # Topic name parameters |
| 28 | self.declare_parameter('cmd_vel_topic', 'cmd_vel_joy') |
| 29 | self.declare_parameter('button_state_topic', 'button_state') |
| 30 | self.declare_parameter('dead_man_switch_topic', 'dead_man_switch') |
| 31 | |
| 32 | # Button mapping parameters |
| 33 | self.declare_parameter('button_mapping.x_button', 0) |
| 34 | self.declare_parameter('button_mapping.a_button', 1) |
| 35 | self.declare_parameter('button_mapping.b_button', 2) |
| 36 | self.declare_parameter('button_mapping.y_button', 3) |
| 37 | self.declare_parameter('button_mapping.l1_button', 4) |
| 38 | self.declare_parameter('button_mapping.r1_button', 5) |
| 39 | self.declare_parameter('button_mapping.l2_button', 6) |
| 40 | self.declare_parameter('button_mapping.r2_button', 7) |
| 41 | self.declare_parameter('button_mapping.select_button', 8) |
| 42 | self.declare_parameter('button_mapping.start_button', 9) |
| 43 | self.declare_parameter('button_mapping.dead_man_button', 4) |
| 44 | |
| 45 | # Axis mapping parameters |
| 46 | self.declare_parameter('axis_mapping.linear_x_axis', 1) |
| 47 | self.declare_parameter('axis_mapping.linear_y_axis', 0) |
| 48 | self.declare_parameter('axis_mapping.angular_y_axis', 5) |
| 49 | self.declare_parameter('axis_mapping.angular_z_axis', 2) |
| 50 | |
| 51 | self.device_input = '' |
| 52 | self.linear_deadband = 0.0 |
| 53 | self.angular_deadband = 0.0 |
| 54 | self.cmd_vel_topic = '' |
| 55 | self.button_state_topic = '' |
| 56 | self.dead_man_switch_topic = '' |
| 57 | self.button_map = {} |
| 58 | self.axis_map = {} |
| 59 | |
| 60 | self.joystick_connected = False |
| 61 | |
| 62 | self.get_logger().info(f"Node '{self.get_name()}' created and in 'unconfigured' state.") |
| 63 | |
| 64 | |
| 65 | def on_configure(self, state: State) -> TransitionCallbackReturn: |
| 66 | self.get_logger().info("In on_configure()") |
| 67 | |
| 68 | # Get parameter values |
| 69 | self.device_input = self.get_parameter('device_input').get_parameter_value().string_value |