komodo_arm2.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import roslib;
00003 
00004 roslib.load_manifest('ric_robot')
00005 import rospy.core
00006 import math
00007 import sys
00008 import time
00009 import roslaunch
00010 import subprocess
00011 import shlex
00012 
00013 from dynamixel_msgs.msg import JointState as dxl_JointState
00014 from sensor_msgs.msg import JointState
00015 from math import *
00016 from ric_robot.srv import *
00017 from ric_robot.msg import *
00018 from std_msgs.msg import Float64, Bool
00019 
00020 
00021 TIME_OUT = 1000  # In milis
00022 
00023 
00024 def br_callback(data):
00025     global msg, ns
00026     msg.name[0] = data.name
00027     msg.position[0] = data.current_pos
00028     msg.velocity[0] = data.velocity
00029     msg.effort[0] = data.load
00030 
00031 
00032 def sh_callback(data):
00033     global msg, ns
00034     msg.name[1] = data.name
00035     msg.position[1] = data.current_pos
00036     msg.velocity[1] = data.velocity
00037     msg.effort[1] = data.load
00038 
00039 
00040 def e1_callback(data):
00041     global msg, ns
00042     msg.name[2] = data.name
00043     msg.position[2] = data.current_pos
00044     msg.velocity[2] = data.velocity
00045     msg.effort[2] = data.load
00046 
00047 
00048 def e2_callback(data):
00049     global msg, ns
00050     msg.name[3] = data.name
00051     msg.position[3] = data.current_pos
00052     msg.velocity[3] = data.velocity
00053     msg.effort[3] = data.load
00054 
00055 
00056 def wr_callback(data):
00057     global msg, ns
00058     msg.name[4] = data.name
00059     msg.position[4] = data.current_pos
00060     msg.velocity[4] = data.velocity
00061     msg.effort[4] = data.load
00062 
00063 
00064 def lf_callback(data):
00065     global msg, ns
00066     msg.name[5] = data.name
00067     msg.position[5] = data.current_pos
00068     msg.velocity[5] = data.velocity
00069     msg.effort[5] = data.load
00070 
00071 
00072 def rf_callback(data):
00073     global msg, ns
00074     msg.name[6] = data.name
00075     msg.position[6] = data.current_pos
00076     msg.velocity[6] = data.velocity
00077     msg.effort[6] = data.load
00078 
00079 
00080 def elev_callback(data):
00081     global msg, pre_pos, pre_epos, final_epos, ns
00082     msg.name[7] = data.name
00083     epos = data.current_pos * elev_rad2m
00084     msg.velocity[7] = data.velocity * elev_rad2m
00085     msg.effort[7] = data.load
00086     dp = epos - pre_epos
00087     if dp > 0.0005:
00088         final_epos -= math.pi * 2 * elev_rad2m
00089     elif dp < -0.0005:
00090         final_epos += math.pi * 2 * elev_rad2m
00091     pre_epos = epos
00092     final_epos += dp
00093     msg.position[7] = final_epos
00094 
00095 
00096 def handle_elev_set(req):
00097     global final_epos, elevpub, home_ok, elev_move
00098     if (home_ok == 1) or (home_ok == 0):
00099         home_ok = 2
00100         rospy.loginfo("Homing is done")
00101     elevpub.publish(0.0)
00102     elev_move = False
00103     # rospy.sleep(0.5)
00104     final_epos = req.pos
00105     rospy.loginfo("Elevator position is: %.3f", final_epos)
00106     return True
00107 
00108 
00109 def epos_callback(data):
00110     global elev_goal_pos, elev_move, elevpub, final_epos, epos_tol, max_elev_speed, home_ok, min_elev_pos, max_elev_pos
00111     if home_ok == 2:
00112         elev_goal_pos = data.pos
00113         if elev_goal_pos > (max_elev_pos + 0.000001) or elev_goal_pos < (min_elev_pos - 0.000001):
00114             rospy.loginfo("Required goal (%.3f) is outside of the elevator range of motion [%.3f-%.3f]", elev_goal_pos,
00115                           min_elev_pos, max_elev_pos)
00116         else:
00117             epos_err = elev_goal_pos - final_epos
00118             if abs(epos_err) > epos_tol:
00119                 elev_move = True
00120     elif home_ok == 1:
00121         rospy.logwarn("Cannot accept commands! Home service is running")
00122     elif home_ok == 0:
00123         rospy.logwarn("Cannot accept commands! Please run the home service first")
00124 
00125 
00126 def handle_elev_home(req):
00127     global home_ok, elev_move
00128     if (req.dir > 0):
00129         if (final_epos < max_elev_pos + 0.005):
00130             elev_move = False
00131             elevpub.publish(max_elev_speed / elev_rad2m)
00132             home_ok = 1
00133             rospy.loginfo("Elevator is homing up")
00134             return True
00135         else:
00136             return False
00137     elif (req.dir < 0):
00138         if (final_epos > min_elev_pos - 0.005):
00139             elev_move = False
00140             elevpub.publish(-max_elev_speed / elev_rad2m)
00141             home_ok = 1
00142             rospy.loginfo("Elevator is homing down")
00143             return True
00144         else:
00145             return False
00146     else:
00147         rospy.loginfo("Please choose homing direction (1 - up / -1 - down)")
00148         return False
00149 
00150 
00151 def upper_switch_callback(msg):
00152     global upper_press
00153     global watch_dog_time
00154 
00155     watch_dog_time = int(round(time.time() * 1000))
00156     if not upper_press and msg.data:
00157         req = set_elevatorRequest()
00158         req.pos = 0.407
00159         handle_elev_set(req)
00160         upper_press = True
00161         rospy.loginfo("Upper home switch activated")
00162     elif upper_press and not msg.data:
00163         upper_press = False
00164 
00165 
00166 def lower_switch_callback(msg):
00167     global lower_press
00168     global watch_dog_time
00169 
00170     watch_dog_time = int(round(time.time() * 1000))
00171 
00172     if not lower_press and msg.data:
00173         req = set_elevatorRequest()
00174         req.pos = -0.01
00175         handle_elev_set(req)
00176         lower_press = True
00177         rospy.loginfo("Lower home switch activated")
00178     elif lower_press and not msg.data:
00179         lower_press = False
00180 
00181 
00182 def komodo_arm():
00183     global pub, msg, ns
00184     global elev_rad2m
00185     global pre_epos, final_epos, elev_goal_pos, elev_move, epos_tol, max_elev_speed, min_elev_pos, max_elev_pos, elevpub
00186     global home_ok
00187     global upper_press, lower_press
00188     global watch_dog_time
00189 
00190     watch_dog_time = int(round(time.time() * 1000))     
00191 
00192     upper_press = False
00193     lower_press = False
00194 
00195     fr = 0
00196     ns = rospy.get_namespace()
00197     ns = ns[1:-1]
00198     elev_rad2m = 0.00175 / (math.pi * 2)
00199     min_elev_pos = 0.0  # TODO: get as parameter?
00200     max_elev_pos = 0.4  # TODO: get as parameter?
00201     home_ok = 0  # 0-not homed, 1-homing  2-homed
00202     epos_tol = 0.001
00203     max_elev_speed = 0.01
00204     elev_goal_pos = 0
00205     elev_move = False
00206     pre_epos = 0
00207     final_epos = 0
00208     msg = JointState()
00209     rospy.init_node('komodo_arm', anonymous=True)
00210     have_elevator = rospy.get_param('have_elevator')
00211     for i in range(7):
00212         msg.name.append("")
00213         msg.position.append(0.0)
00214         msg.velocity.append(0.0)
00215         msg.effort.append(0.0)
00216     if have_elevator:
00217         rospy.wait_for_service('/devsOnline')
00218         msg.name.append("")
00219         msg.position.append(0.0)
00220         msg.velocity.append(0.0)
00221         msg.effort.append(0.0)
00222         rospy.Subscriber("elevator_controller/state", dxl_JointState, elev_callback)
00223         elevpub = rospy.Publisher("elevator_controller/command", Float64, queue_size=1)
00224         set_serv = rospy.Service("elevator_controller/set_position", set_elevator, handle_elev_set)
00225         home_serv = rospy.Service("elevator_controller/home", home_elevator, handle_elev_home)
00226         rospy.Subscriber("elevator_controller/pos_command", ric_elevator_command, epos_callback)
00227         rospy.Subscriber("elevator_upper_switch", Bool, upper_switch_callback)
00228         rospy.Subscriber("elevator_lower_switch", Bool, lower_switch_callback)
00229         rospy.loginfo("Seting up elevator...")
00230     rospy.Subscriber("base_rotation_controller/state", dxl_JointState, br_callback)
00231     rospy.Subscriber("shoulder_controller/state", dxl_JointState, sh_callback)
00232     rospy.Subscriber("elbow1_controller/state", dxl_JointState, e1_callback)
00233     rospy.Subscriber("elbow2_controller/state", dxl_JointState, e2_callback)
00234     rospy.Subscriber("left_finger_controller/state", dxl_JointState, lf_callback)
00235     rospy.Subscriber("right_finger_controller/state", dxl_JointState, rf_callback)
00236     rospy.Subscriber("wrist_controller/state", dxl_JointState, wr_callback)
00237     pub = rospy.Publisher('joint_states', JointState, queue_size=1)
00238 
00239     quit_loop = False
00240 
00241     while not rospy.is_shutdown() and not quit_loop:
00242         msg.header.stamp = rospy.Time.now()
00243         pub.publish(msg)
00244         # rospy.loginfo("Elevator position is: %.3f",final_epos)
00245         if have_elevator:
00246             epos_err = elev_goal_pos - final_epos
00247             if elev_move == True and abs(epos_err) < epos_tol:
00248                 elev_move = False
00249                 rospy.loginfo("Elevator reached goal position: %.3f", elev_goal_pos)
00250                 elevpub.publish(0.0)
00251             elif elev_move == True:
00252                 if epos_err > 0:
00253                     speed = max_elev_speed
00254                 elif epos_err < 0:
00255                     speed = -max_elev_speed
00256                 elevpub.publish(speed / elev_rad2m)
00257             if int(round(time.time() * 1000)) - watch_dog_time > 1000:
00258                 quit_loop = True
00259                 subprocess.Popen(shlex.split("pkill -f ros"))
00260                 rospy.logerr("ric_board is stack, from elevator node")
00261         rospy.sleep(0.05)
00262 
00263 
00264 if __name__ == '__main__':
00265     komodo_arm()


ric_robot
Author(s): RoboTiCan
autogenerated on Fri May 20 2016 03:50:58