joint_position_controller_dual_motor.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2010-2011, Antons Rebguns.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of University of Arizona nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 from __future__ import division
36 
37 
38 __author__ = 'Antons Rebguns'
39 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns'
40 __credits__ = 'Cara Slutter'
41 
42 __license__ = 'BSD'
43 __maintainer__ = 'Antons Rebguns'
44 __email__ = 'anton@email.arizona.edu'
45 
46 
47 import rospy
48 
50 from dynamixel_controllers.joint_controller import JointController
51 
52 from dynamixel_msgs.msg import JointState
53 
55  def __init__(self, dxl_io, controller_namespace, port_namespace):
56  JointController.__init__(self, dxl_io, controller_namespace, port_namespace)
57 
58  self.master_id = rospy.get_param(self.controller_namespace + '/motor_master/id')
59  self.master_initial_position_raw = rospy.get_param(self.controller_namespace + '/motor_master/init')
60  self.master_min_angle_raw = rospy.get_param(self.controller_namespace + '/motor_master/min')
61  self.master_max_angle_raw = rospy.get_param(self.controller_namespace + '/motor_master/max')
62 
63  self.slave_id = rospy.get_param(self.controller_namespace + '/motor_slave/id')
64  self.slave_offset = rospy.get_param(self.controller_namespace + '/motor_slave/calibration_offset', 0)
65 
67 
68  self.joint_state = JointState(name=self.joint_name, motor_ids=[self.master_id, self.slave_id])
69 
70  def initialize(self):
71  # verify that the expected motor is connected and responding
72  available_ids = rospy.get_param('dynamixel/%s/connected_ids' % self.port_namespace, [])
73  if not (self.master_id in available_ids and
74  self.slave_id in available_ids):
75  rospy.logwarn('The specified motor id is not connected and responding.')
76  rospy.logwarn('Available ids: %s' % str(available_ids))
77  rospy.logwarn('Specified ids: %d %d' % (self.master_id, self.slave_id))
78  return False
79 
80  self.RADIANS_PER_ENCODER_TICK = rospy.get_param('dynamixel/%s/%d/radians_per_encoder_tick' % (self.port_namespace, self.master_id))
81  self.ENCODER_TICKS_PER_RADIAN = rospy.get_param('dynamixel/%s/%d/encoder_ticks_per_radian' % (self.port_namespace, self.master_id))
82 
83  if self.flipped:
86  else:
89 
90  self.ENCODER_RESOLUTION = rospy.get_param('dynamixel/%s/%d/encoder_resolution' % (self.port_namespace, self.master_id))
92  self.VELOCITY_PER_TICK = rospy.get_param('dynamixel/%s/%d/radians_second_per_encoder_tick' % (self.port_namespace, self.master_id))
93  self.MAX_VELOCITY = rospy.get_param('dynamixel/%s/%d/max_velocity' % (self.port_namespace, self.master_id))
95 
96  if self.compliance_slope is not None: self.set_compliance_slope(self.compliance_slope)
97  if self.compliance_margin is not None: self.set_compliance_margin(self.compliance_margin)
98  if self.compliance_punch is not None: self.set_compliance_punch(self.compliance_punch)
99  if self.torque_limit is not None: self.set_torque_limit(self.torque_limit)
100 
101  self.joint_max_speed = rospy.get_param(self.controller_namespace + '/joint_max_speed', self.MAX_VELOCITY)
102 
103  if self.joint_max_speed < self.MIN_VELOCITY: self.joint_max_speed = self.MIN_VELOCITY
104  elif self.joint_max_speed > self.MAX_VELOCITY: self.joint_max_speed = self.MAX_VELOCITY
105 
106  if self.joint_speed < self.MIN_VELOCITY: self.joint_speed = self.MIN_VELOCITY
107  elif self.joint_speed > self.joint_max_speed: self.joint_speed = self.joint_max_speed
108 
109  self.set_speed(self.joint_speed)
110 
111  return True
112 
113  def pos_rad_to_raw(self, angle):
114  if angle < self.master_min_angle:
115  angle = self.master_min_angle
116  elif angle > self.master_max_angle:
117  angle = self.master_max_angle
118  mcv_master = self.rad_to_raw(angle, self.master_initial_position_raw, self.flipped, self.ENCODER_TICKS_PER_RADIAN)
119  mcv_slave = self.MAX_POSITION - mcv_master + self.slave_offset
120  if mcv_slave < 0:
121  mcv_slave = 0
122  elif mcv_slave > self.MAX_POSITION:
123  mcv_slave = self.MAX_POSITION
124  return (mcv_master, mcv_slave)
125 
126  def spd_rad_to_raw(self, spd_rad):
127  if spd_rad < self.MIN_VELOCITY:
128  spd_rad = self.MIN_VELOCITY
129  elif spd_rad > self.joint_max_speed:
130  spd_rad = self.joint_max_speed
131  # velocity of 0 means maximum, make sure that doesn't happen
132  return max(1, int(round(spd_rad / self.VELOCITY_PER_TICK)))
133 
134  def set_torque_enable(self, torque_enable):
135  mcv_master = (self.master_id, torque_enable)
136  mcv_slave = (self.slave_id, torque_enable)
137  self.dxl_io.set_multi_torque_enabled([mcv_master, mcv_slave])
138 
139  def set_speed(self, speed):
140  if speed < self.MIN_VELOCITY: speed = self.MIN_VELOCITY
141  elif speed > self.joint_max_speed: speed = self.joint_max_speed
142  speed_raw = int(round(speed / self.VELOCITY_PER_TICK))
143  mcv_master = (self.master_id, speed_raw if speed_raw > 0 else 1)
144  mcv_slave = (self.slave_id, mcv_master[1])
145  self.dxl_io.set_multi_speed([mcv_master, mcv_slave])
146 
147  def set_compliance_slope(self, slope):
148  if slope < DXL_MIN_COMPLIANCE_SLOPE: slope = DXL_MIN_COMPLIANCE_SLOPE
149  elif slope > DXL_MAX_COMPLIANCE_SLOPE: slope = DXL_MAX_COMPLIANCE_SLOPE
150  mcv_master = (self.master_id, slope, slope)
151  mcv_slave = (self.slave_id, slope, slope)
152  self.dxl_io.set_multi_compliance_slopes([mcv_master, mcv_slave])
153 
154  def set_compliance_margin(self, margin):
155  if margin < DXL_MIN_COMPLIANCE_MARGIN: margin = DXL_MIN_COMPLIANCE_MARGIN
156  elif margin > DXL_MAX_COMPLIANCE_MARGIN: margin = DXL_MAX_COMPLIANCE_MARGIN
157  else: margin = int(margin)
158  mcv_master = (self.master_id, margin, margin)
159  mcv_slave = (self.slave_id, margin, margin)
160  self.dxl_io.set_multi_compliance_margins([mcv_master, mcv_slave])
161 
162  def set_compliance_punch(self, punch):
163  if punch < DXL_MIN_PUNCH: punch = DXL_MIN_PUNCH
164  elif punch > DXL_MAX_PUNCH: punch = DXL_MAX_PUNCH
165  else: punch = int(punch)
166  mcv_master = (self.master_id, punch)
167  mcv_slave = (self.slave_id, punch)
168  self.dxl_io.set_multi_punch([mcv_master, mcv_slave])
169 
170  def set_torque_limit(self, max_torque):
171  if max_torque > 1: max_torque = 1.0
172  elif max_torque < 0: max_torque = 0.0 # turn off motor torque
173  raw_torque_val = int(DXL_MAX_TORQUE_TICK * max_torque)
174  mcv_master = (self.master_id, raw_torque_val)
175  mcv_slave = (self.slave_id, raw_torque_val)
176  self.dxl_io.set_multi_torque_limit([mcv_master, mcv_slave])
177 
178  def process_motor_states(self, state_list):
179  if self.running:
180  states = {}
181 
182  for state in state_list.motor_states:
183  if state.id in [self.master_id, self.slave_id]: states[state.id] = state
184 
185  if self.master_id in states and self.slave_id in states:
186  state = states[self.master_id]
187  self.joint_state.motor_temps = [state.temperature, states[self.slave_id].temperature]
188  self.joint_state.goal_pos = self.raw_to_rad(state.goal, self.master_initial_position_raw, self.flipped, self.RADIANS_PER_ENCODER_TICK)
189  self.joint_state.current_pos = self.raw_to_rad(state.position, self.master_initial_position_raw, self.flipped, self.RADIANS_PER_ENCODER_TICK)
190  self.joint_state.error = state.error * self.RADIANS_PER_ENCODER_TICK
191  self.joint_state.velocity = state.speed * self.VELOCITY_PER_TICK
192  self.joint_state.load = state.load
193  self.joint_state.is_moving = state.moving
194  self.joint_state.header.stamp = rospy.Time.from_sec(state.timestamp)
195  self.joint_state_pub.publish(self.joint_state)
196 
197  def process_command(self, msg):
198  angle = msg.data
199  if angle < self.master_min_angle: angle = self.master_min_angle
200  elif angle > self.master_max_angle: angle = self.master_max_angle
201  mcv_master = (self.master_id, self.rad_to_raw(angle, self.master_initial_position_raw, self.flipped, self.ENCODER_TICKS_PER_RADIAN))
202  mcv_slave = [self.slave_id, self.MAX_POSITION - mcv_master[1] + self.slave_offset]
203  if mcv_slave[1] < 0: mcv_slave[1] = 0
204  elif mcv_slave[1] > self.MAX_POSITION: mcv_slave[1] = self.MAX_POSITION
205  self.dxl_io.set_multi_position([mcv_master, mcv_slave])
def raw_to_rad(self, raw, initial_position_raw, flipped, radians_per_encoder_tick)
def rad_to_raw(self, angle, initial_position_raw, flipped, encoder_ticks_per_radian)


dynamixel_controllers
Author(s): Antons Rebguns, Cody Jorgensen, Cara Slutter
autogenerated on Tue May 12 2020 03:10:59