15 from sensor_msgs.msg
import Joy
20 ''' Class to connect to and publish the Korg NanoKontrol midi device 25 ''' connect to midi device and set up ros 28 devices = pygame.midi.get_count()
30 rospy.logerr(
"No MIDI devices detected")
32 rospy.loginfo(
"Found %d MIDI devices" % devices)
34 input_dev = int(rospy.get_param(
"~input_dev",
35 pygame.midi.get_default_input_id))
37 rospy.loginfo(
"Using input device %d" % input_dev)
42 if not rospy.has_param(
'~modes'):
43 rospack = rospkg.RosPack()
44 paramlist = rosparam.load_file(rospack.get_path(
'korg_nanokontrol') +
45 '/config/nanokontrol_config.yaml')
46 for params, ns
in paramlist:
47 rosparam.upload_params(ns, params)
49 self.
modes = rospy.get_param(
'~modes')
57 self.
pub = rospy.Publisher(
'joy', Joy, queue_size=10, latch=
True)
64 ''' run the update hook (poll the midi controller, publish the message) 66 while self.controller.poll():
67 data = self.controller.read(1)
74 ''' Set up the message for the correct mode 78 self.msg.axes = [0]*len(self.
modes[mode][
'control_axis'])
79 self.msg.buttons = [0]*len(self.
modes[mode][
'control_buttons'])
80 self.msg.buttons.append(mode)
83 ''' Given data, try to guess the mode we are in 89 if (control[0] & 0xF0) == 176:
90 control_id = control[1] | ((control[0] & 0x0F) << 8)
94 for index, control_mode
in enumerate(self.
modes):
95 if control_id
in control_mode[
'control_axis']:
96 if candidate
is not None:
101 if control_id
in control_mode[
'control_buttons']:
102 if candidate
is not None:
107 rospy.loginfo(
"determining mode..")
118 ''' Given data, interpret into Joy message and publish 121 self.msg.header.stamp = rospy.Time.now()
128 if (control[0] & 0xF0) == 176:
129 control_id = control[1] | ((control[0] & 0x0F) << 8)
131 control_axis = self.
modes[self.
mode][
'control_axis']
132 control_buttons = self.
modes[self.
mode][
'control_buttons']
134 if control_id
in control_axis:
136 control_val = self.
clip(-1.0, 1.0,
137 float(control[2] - 63) / 63.0)
139 control_val = self.
clip(0.0, 1.0,
140 float(control[2]) / 127)
142 axis = control_axis.index(control_id)
143 self.msg.axes[axis] = control_val
146 if control_id
in control_buttons:
147 button = control_buttons.index(control_id)
149 self.msg.buttons[button] = 1
151 self.msg.buttons[button] = 0
155 elif control[0] == 79:
160 self.pub.publish(self.
msg)
def clip(self, min, max, val)
def guess_mode(self, data)