joint_controller.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 
36 __author__ = 'Antons Rebguns'
37 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns'
38 
39 __license__ = 'BSD'
40 __maintainer__ = 'Antons Rebguns'
41 __email__ = 'anton@email.arizona.edu'
42 
43 
44 import math
45 
46 import rospy
47 
49 
50 from dynamixel_controllers.srv import SetSpeed
51 from dynamixel_controllers.srv import TorqueEnable
52 from dynamixel_controllers.srv import SetComplianceSlope
53 from dynamixel_controllers.srv import SetComplianceMargin
54 from dynamixel_controllers.srv import SetCompliancePunch
55 from dynamixel_controllers.srv import SetTorqueLimit
56 
57 from std_msgs.msg import Float64
58 from dynamixel_msgs.msg import MotorStateList
59 from dynamixel_msgs.msg import JointState
60 
62  def __init__(self, dxl_io, controller_namespace, port_namespace):
63  self.running = False
64  self.dxl_io = dxl_io
65  self.controller_namespace = controller_namespace
66  self.port_namespace = port_namespace
67  self.joint_name = rospy.get_param(self.controller_namespace + '/joint_name')
68  self.joint_speed = rospy.get_param(self.controller_namespace + '/joint_speed', 1.0)
69  self.compliance_slope = rospy.get_param(self.controller_namespace + '/joint_compliance_slope', None)
70  self.compliance_margin = rospy.get_param(self.controller_namespace + '/joint_compliance_margin', None)
71  self.compliance_punch = rospy.get_param(self.controller_namespace + '/joint_compliance_punch', None)
72  self.torque_limit = rospy.get_param(self.controller_namespace + '/joint_torque_limit', None)
73 
74  self.__ensure_limits()
75 
76  self.speed_service = rospy.Service(self.controller_namespace + '/set_speed', SetSpeed, self.process_set_speed)
77  self.torque_service = rospy.Service(self.controller_namespace + '/torque_enable', TorqueEnable, self.process_torque_enable)
78  self.compliance_slope_service = rospy.Service(self.controller_namespace + '/set_compliance_slope', SetComplianceSlope, self.process_set_compliance_slope)
79  self.compliance_marigin_service = rospy.Service(self.controller_namespace + '/set_compliance_margin', SetComplianceMargin, self.process_set_compliance_margin)
80  self.compliance_punch_service = rospy.Service(self.controller_namespace + '/set_compliance_punch', SetCompliancePunch, self.process_set_compliance_punch)
81  self.torque_limit_service = rospy.Service(self.controller_namespace + '/set_torque_limit', SetTorqueLimit, self.process_set_torque_limit)
82 
83  def __ensure_limits(self):
84  if self.compliance_slope is not None:
85  if self.compliance_slope < DXL_MIN_COMPLIANCE_SLOPE: self.compliance_slope = DXL_MIN_COMPLIANCE_SLOPE
86  elif self.compliance_slope > DXL_MAX_COMPLIANCE_SLOPE: self.compliance_slope = DXL_MAX_COMPLIANCE_SLOPE
87  else: self.compliance_slope = int(self.compliance_slope)
88 
89  if self.compliance_margin is not None:
90  if self.compliance_margin < DXL_MIN_COMPLIANCE_MARGIN: self.compliance_margin = DXL_MIN_COMPLIANCE_MARGIN
91  elif self.compliance_margin > DXL_MAX_COMPLIANCE_MARGIN: self.compliance_margin = DXL_MAX_COMPLIANCE_MARGIN
92  else: self.compliance_margin = int(self.compliance_margin)
93 
94  if self.compliance_punch is not None:
95  if self.compliance_punch < DXL_MIN_PUNCH: self.compliance_punch = DXL_MIN_PUNCH
96  elif self.compliance_punch > DXL_MAX_PUNCH: self.compliance_punch = DXL_MAX_PUNCH
97  else: self.compliance_punch = int(self.compliance_punch)
98 
99  if self.torque_limit is not None:
100  if self.torque_limit < 0: self.torque_limit = 0.0
101  elif self.torque_limit > 1: self.torque_limit = 1.0
102 
103  def initialize(self):
104  raise NotImplementedError
105 
106  def start(self):
107  self.running = True
108  self.joint_state_pub = rospy.Publisher(self.controller_namespace + '/state', JointState, queue_size=1)
109  self.command_sub = rospy.Subscriber(self.controller_namespace + '/command', Float64, self.process_command)
110  self.motor_states_sub = rospy.Subscriber('motor_states/%s' % self.port_namespace, MotorStateList, self.process_motor_states)
111 
112  def stop(self):
113  self.running = False
114  self.joint_state_pub.unregister()
115  self.motor_states_sub.unregister()
116  self.command_sub.unregister()
117  self.speed_service.shutdown('normal shutdown')
118  self.torque_service.shutdown('normal shutdown')
119  self.compliance_slope_service.shutdown('normal shutdown')
120 
121  def set_torque_enable(self, torque_enable):
122  raise NotImplementedError
123 
124  def set_speed(self, speed):
125  raise NotImplementedError
126 
127  def set_compliance_slope(self, slope):
128  raise NotImplementedError
129 
130  def set_compliance_margin(self, margin):
131  raise NotImplementedError
132 
133  def set_compliance_punch(self, punch):
134  raise NotImplementedError
135 
136  def set_torque_limit(self, max_torque):
137  raise NotImplementedError
138 
139  def process_set_speed(self, req):
140  self.set_speed(req.speed)
141  return [] # success
142 
143  def process_torque_enable(self, req):
144  self.set_torque_enable(req.torque_enable)
145  return []
146 
148  self.set_compliance_slope(req.slope)
149  return []
150 
152  self.set_compliance_margin(req.margin)
153  return []
154 
156  self.set_compliance_punch(req.punch)
157  return []
158 
159  def process_set_torque_limit(self, req):
160  self.set_torque_limit(req.torque_limit)
161  return []
162 
163  def process_motor_states(self, state_list):
164  raise NotImplementedError
165 
166  def process_command(self, msg):
167  raise NotImplementedError
168 
169  def rad_to_raw(self, angle, initial_position_raw, flipped, encoder_ticks_per_radian):
170  """ angle is in radians """
171  #print 'flipped = %s, angle_in = %f, init_raw = %d' % (str(flipped), angle, initial_position_raw)
172  angle_raw = angle * encoder_ticks_per_radian
173  #print 'angle = %f, val = %d' % (math.degrees(angle), int(round(initial_position_raw - angle_raw if flipped else initial_position_raw + angle_raw)))
174  return int(round(initial_position_raw - angle_raw if flipped else initial_position_raw + angle_raw))
175 
176  def raw_to_rad(self, raw, initial_position_raw, flipped, radians_per_encoder_tick):
177  return (initial_position_raw - raw if flipped else raw - initial_position_raw) * radians_per_encoder_tick
178 
def raw_to_rad(self, raw, initial_position_raw, flipped, radians_per_encoder_tick)
def __init__(self, dxl_io, controller_namespace, port_namespace)
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