joint_position_controller_dual_motor.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 #
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2010-2011, Antons Rebguns.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of University of Arizona nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 from __future__ import division
00036 
00037 
00038 __author__ = 'Antons Rebguns'
00039 __copyright__ = 'Copyright (c) 2010-2011 Antons Rebguns'
00040 __credits__ = 'Cara Slutter'
00041 
00042 __license__ = 'BSD'
00043 __maintainer__ = 'Antons Rebguns'
00044 __email__ = 'anton@email.arizona.edu'
00045 
00046 
00047 import rospy
00048 
00049 from dynamixel_driver.dynamixel_const import *
00050 from dynamixel_controllers.joint_controller import JointController
00051 
00052 from dynamixel_msgs.msg import JointState
00053 
00054 class JointPositionControllerDual(JointController):
00055     def __init__(self, dxl_io, controller_namespace, port_namespace):
00056         JointController.__init__(self, dxl_io, controller_namespace, port_namespace)
00057         
00058         self.master_id = rospy.get_param(self.controller_namespace + '/motor_master/id')
00059         self.master_initial_position_raw = rospy.get_param(self.controller_namespace + '/motor_master/init')
00060         self.master_min_angle_raw = rospy.get_param(self.controller_namespace + '/motor_master/min')
00061         self.master_max_angle_raw = rospy.get_param(self.controller_namespace + '/motor_master/max')
00062         
00063         self.slave_id = rospy.get_param(self.controller_namespace + '/motor_slave/id')
00064         self.slave_offset = rospy.get_param(self.controller_namespace + '/motor_slave/calibration_offset', 0)
00065         
00066         self.flipped = self.master_min_angle_raw > self.master_max_angle_raw
00067         
00068         self.joint_state = JointState(name=self.joint_name, motor_ids=[self.master_id, self.slave_id])
00069 
00070     def initialize(self):
00071         # verify that the expected motor is connected and responding
00072         available_ids = rospy.get_param('dynamixel/%s/connected_ids' % self.port_namespace, [])
00073         if not (self.master_id in available_ids and
00074                 self.slave_id in available_ids):
00075             rospy.logwarn('The specified motor id is not connected and responding.')
00076             rospy.logwarn('Available ids: %s' % str(available_ids))
00077             rospy.logwarn('Specified ids: %d %d' % (self.master_id, self.slave_id))
00078             return False
00079             
00080         self.RADIANS_PER_ENCODER_TICK = rospy.get_param('dynamixel/%s/%d/radians_per_encoder_tick' % (self.port_namespace, self.master_id))
00081         self.ENCODER_TICKS_PER_RADIAN = rospy.get_param('dynamixel/%s/%d/encoder_ticks_per_radian' % (self.port_namespace, self.master_id))
00082         
00083         if self.flipped:
00084             self.master_min_angle = (self.master_initial_position_raw - self.master_min_angle_raw) * self.RADIANS_PER_ENCODER_TICK
00085             self.master_max_angle = (self.master_initial_position_raw - self.master_max_angle_raw) * self.RADIANS_PER_ENCODER_TICK
00086         else:
00087             self.master_min_angle = (self.master_min_angle_raw - self.master_initial_position_raw) * self.RADIANS_PER_ENCODER_TICK
00088             self.master_max_angle = (self.master_max_angle_raw - self.master_initial_position_raw) * self.RADIANS_PER_ENCODER_TICK
00089             
00090         self.ENCODER_RESOLUTION = rospy.get_param('dynamixel/%s/%d/encoder_resolution' % (self.port_namespace, self.master_id))
00091         self.MAX_POSITION = self.ENCODER_RESOLUTION - 1
00092         self.VELOCITY_PER_TICK = rospy.get_param('dynamixel/%s/%d/radians_second_per_encoder_tick' % (self.port_namespace, self.master_id))
00093         self.MAX_VELOCITY = rospy.get_param('dynamixel/%s/%d/max_velocity' % (self.port_namespace, self.master_id))
00094         self.MIN_VELOCITY = self.VELOCITY_PER_TICK
00095         
00096         if self.compliance_slope is not None: self.set_compliance_slope(self.compliance_slope)
00097         if self.compliance_margin is not None: self.set_compliance_margin(self.compliance_margin)
00098         if self.compliance_punch is not None: self.set_compliance_punch(self.compliance_punch)
00099         if self.torque_limit is not None: self.set_torque_limit(self.torque_limit)
00100         
00101         self.joint_max_speed = rospy.get_param(self.controller_namespace + '/joint_max_speed', self.MAX_VELOCITY)
00102         
00103         if self.joint_max_speed < self.MIN_VELOCITY: self.joint_max_speed = self.MIN_VELOCITY
00104         elif self.joint_max_speed > self.MAX_VELOCITY: self.joint_max_speed = self.MAX_VELOCITY
00105         
00106         if self.joint_speed < self.MIN_VELOCITY: self.joint_speed = self.MIN_VELOCITY
00107         elif self.joint_speed > self.joint_max_speed: self.joint_speed = self.joint_max_speed
00108         
00109         self.set_speed(self.joint_speed)
00110         
00111         return True
00112 
00113     def pos_rad_to_raw(self, angle):
00114         if angle < self.master_min_angle: 
00115             angle = self.master_min_angle
00116         elif angle > self.master_max_angle: 
00117             angle = self.master_max_angle
00118         mcv_master = self.rad_to_raw(angle, self.master_initial_position_raw, self.flipped, self.ENCODER_TICKS_PER_RADIAN)
00119         mcv_slave = self.MAX_POSITION - mcv_master + self.slave_offset
00120         if mcv_slave < 0: 
00121             mcv_slave = 0
00122         elif mcv_slave > self.MAX_POSITION: 
00123             mcv_slave = self.MAX_POSITION
00124         return (mcv_master, mcv_slave)
00125         
00126     def spd_rad_to_raw(self, spd_rad):
00127         if spd_rad < self.MIN_VELOCITY: 
00128             spd_rad = self.MIN_VELOCITY
00129         elif spd_rad > self.joint_max_speed: 
00130             spd_rad = self.joint_max_speed
00131         # velocity of 0 means maximum, make sure that doesn't happen
00132         return max(1, int(round(spd_rad / self.VELOCITY_PER_TICK)))
00133         
00134     def set_torque_enable(self, torque_enable):
00135         mcv_master = (self.master_id, torque_enable)
00136         mcv_slave = (self.slave_id, torque_enable)
00137         self.dxl_io.set_multi_torque_enabled([mcv_master, mcv_slave])
00138 
00139     def set_speed(self, speed):
00140         if speed < self.MIN_VELOCITY: speed = self.MIN_VELOCITY
00141         elif speed > self.joint_max_speed: speed = self.joint_max_speed
00142         speed_raw = int(round(speed / self.VELOCITY_PER_TICK))
00143         mcv_master = (self.master_id, speed_raw if speed_raw > 0 else 1)
00144         mcv_slave = (self.slave_id, mcv_master[1])
00145         self.dxl_io.set_multi_speed([mcv_master, mcv_slave])
00146 
00147     def set_compliance_slope(self, slope):
00148         if slope < DXL_MIN_COMPLIANCE_SLOPE: slope = DXL_MIN_COMPLIANCE_SLOPE
00149         elif slope > DXL_MAX_COMPLIANCE_SLOPE: slope = DXL_MAX_COMPLIANCE_SLOPE
00150         mcv_master = (self.master_id, slope, slope)
00151         mcv_slave = (self.slave_id, slope, slope)
00152         self.dxl_io.set_multi_compliance_slopes([mcv_master, mcv_slave])
00153 
00154     def set_compliance_margin(self, margin):
00155         if margin < DXL_MIN_COMPLIANCE_MARGIN: margin = DXL_MIN_COMPLIANCE_MARGIN
00156         elif margin > DXL_MAX_COMPLIANCE_MARGIN: margin = DXL_MAX_COMPLIANCE_MARGIN
00157         else: margin = int(margin)
00158         mcv_master = (self.master_id, margin, margin)
00159         mcv_slave = (self.slave_id, margin, margin)
00160         self.dxl_io.set_multi_compliance_margins([mcv_master, mcv_slave])
00161 
00162     def set_compliance_punch(self, punch):
00163         if punch < DXL_MIN_PUNCH: punch = DXL_MIN_PUNCH
00164         elif punch > DXL_MAX_PUNCH: punch = DXL_MAX_PUNCH
00165         else: punch = int(punch)
00166         mcv_master = (self.master_id, punch)
00167         mcv_slave = (self.slave_id, punch)
00168         self.dxl_io.set_multi_punch([mcv_master, mcv_slave])
00169 
00170     def set_torque_limit(self, max_torque):
00171         if max_torque > 1: max_torque = 1.0
00172         elif max_torque < 0: max_torque = 0.0  # turn off motor torque
00173         raw_torque_val = int(DXL_MAX_TORQUE_TICK * max_torque)
00174         mcv_master = (self.master_id, raw_torque_val)
00175         mcv_slave = (self.slave_id, raw_torque_val)
00176         self.dxl_io.set_multi_torque_limit([mcv_master, mcv_slave])
00177 
00178     def process_motor_states(self, state_list):
00179         if self.running:
00180             states = {}
00181             
00182             for state in state_list.motor_states:
00183                 if state.id in [self.master_id, self.slave_id]: states[state.id] = state
00184                                
00185             if self.master_id in states and self.slave_id in states:
00186                 state = states[self.master_id]
00187                 self.joint_state.motor_temps = [state.temperature, states[self.slave_id].temperature]
00188                 self.joint_state.goal_pos = self.raw_to_rad(state.goal, self.master_initial_position_raw, self.flipped, self.RADIANS_PER_ENCODER_TICK)
00189                 self.joint_state.current_pos = self.raw_to_rad(state.position, self.master_initial_position_raw, self.flipped, self.RADIANS_PER_ENCODER_TICK)
00190                 self.joint_state.error = state.error * self.RADIANS_PER_ENCODER_TICK
00191                 self.joint_state.velocity = state.speed * self.VELOCITY_PER_TICK
00192                 self.joint_state.load = state.load
00193                 self.joint_state.is_moving = state.moving
00194                 self.joint_state.header.stamp = rospy.Time.from_sec(state.timestamp)
00195                 self.joint_state_pub.publish(self.joint_state)
00196 
00197     def process_command(self, msg):
00198         angle = msg.data
00199         if angle < self.master_min_angle: angle = self.master_min_angle
00200         elif angle > self.master_max_angle: angle = self.master_max_angle
00201         mcv_master = (self.master_id, self.rad_to_raw(angle, self.master_initial_position_raw, self.flipped, self.ENCODER_TICKS_PER_RADIAN))
00202         mcv_slave = [self.slave_id, self.MAX_POSITION - mcv_master[1] + self.slave_offset]
00203         if mcv_slave[1] < 0: mcv_slave[1] = 0
00204         elif mcv_slave[1] > self.MAX_POSITION: mcv_slave[1] = self.MAX_POSITION
00205         self.dxl_io.set_multi_position([mcv_master, mcv_slave])


dynamixel_controllers
Author(s): Antons Rebguns, Cody Jorgensen, Cara Slutter
autogenerated on Sun Oct 5 2014 23:32:36