simple_arm.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import rospy
3 import serial
4 import struct
5 import numpy as np
6 
7 from sensor_msgs.msg import Joy
8 
9 class SimpleArm:
10  def __init__(self):
11  self.velocities = {
12  'grip': 0,
13  'wrist_roll': 0,
14  'wrist_pitch': 0,
15  'upper_elbow': 0,
16  'lower_elbow': 0,
17  'base_yaw': 0
18  }
19  self.positions = {
20  'camera': 0
21  }
22  baudrate = rospy.get_param('~baudrate', 9600)
23  self.serialDev = serial.Serial(baudrate=baudrate)
24  self.serialDev.port = rospy.get_param("~serial_device")
25  self.serialDev.open()
26  self.arm_sub = rospy.Subscriber("/joy_arm", Joy, self.arm_joy_callback)
27 
28  def write_serial(self):
29  # Execute arm position
30  rospy.loginfo('velocities:%s\n' % self.velocities)
31  rospy.loginfo('positions:%s\n' % self.positions)
32  encoded_position = struct.pack("<fffffff",
33  self.velocities['grip'],
34  self.velocities['wrist_roll'],
35  self.velocities['wrist_pitch'],
36  self.velocities['upper_elbow'],
37  self.velocities['lower_elbow'],
38  self.velocities['base_yaw'],
39  self.positions['camera']
40  )
41  self.serialDev.write(encoded_position)
42 
43  def arm_joy_callback(self, data):
44  self.velocities['grip'] = 0
45  self.velocities['wrist_roll'] = 0
46  self.velocities['wrist_pitch'] = 0
47  self.velocities['upper_elbow'] = 0
48  self.velocities['lower_elbow'] = 0
49  self.velocities['base_yaw'] = 0
50 
51  # button 12 = hold button to double motor speed
52  SPEED_MULTIPLIER = 1
53  if data.buttons[11]:
54  SPEED_MULTIPLIER = 2
55  # button 10 = hold button to half motor speed
56  if data.buttons[9]:
57  SPEED_MULTIPLIER = 0.5
58 
59  # big stick twist = base clockwise
60  # big stick twist = base counter-clockwise
61  self.velocities['base_yaw'] = data.axes[2] / 5.0 * SPEED_MULTIPLIER
62 
63  # big stick forward = lower elbow up
64  # big stick back = lower elbow down
65  self.velocities['lower_elbow'] = data.axes[1] / 3.0 * SPEED_MULTIPLIER
66 
67  # thumb stick forward = upper elbow down
68  # thumb stick back = upper elbow up
69  self.velocities['upper_elbow'] = data.axes[5] / 5.0 * SPEED_MULTIPLIER
70 
71  # paddle forward = arm camera up
72  # paddle back = arm camera down
73  self.positions['camera'] = np.interp(data.axes[3],[-1,1],[7,77]) # this value is angular postion in degrees
74 
75  # button 9 = wrist clockwise
76  # button 11 = wrist counter-clockwise
77  if data.buttons[8]:
78  self.velocities['wrist_roll'] = 0.35 * SPEED_MULTIPLIER
79  if data.buttons[10]:
80  self.velocities['wrist_roll'] = -0.35 * SPEED_MULTIPLIER
81 
82  # button 6 = wrist down
83  # button 4 = wrist up
84  if data.buttons[4]:
85  self.velocities['wrist_pitch'] = 0.15 * SPEED_MULTIPLIER
86  if data.buttons[2]:
87  self.velocities['wrist_pitch'] = -0.15 * SPEED_MULTIPLIER
88 
89  # trigger = close gripper
90  # thumb button = open gripper
91  if data.buttons[0]:
92  self.velocities['grip'] = 1
93  if data.buttons[1]:
94  self.velocities['grip'] = -1
95 
96  # MOVE ARM
97  self.write_serial()
98 
99 
100 def main():
101  rospy.init_node("simple_arm")
102  controller = SimpleArm()
103  rospy.spin()
def arm_joy_callback(self, data)
Definition: simple_arm.py:43


simple_arm
Author(s): Daniel Snider , Matthew Mirvish
autogenerated on Mon Jun 10 2019 15:05:31