Advertisement
BourbonCreams

braccio_controller.py

Jul 21st, 2017
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import rospy
  4. import actionlib
  5. from control_msgs.msg import FollowJointTrajectoryAction
  6. from control_msgs.msg import FollowJointTrajectoryResult
  7. from trajectory_msgs.msg import JointTrajectory
  8. from sensor_msgs.msg import JointState
  9. import sys
  10. sys.path.append('/home/user/catkin_ws/src/braccio_arm/braccio_arduino/tests')
  11. import moving_braccio_pc
  12.  
  13.  
  14. joint_names = ['braccio_joint_1', 'braccio_joint_2', 'braccio_joint_3', 'braccio_joint_4', 'braccio_joint_5']
  15.  
  16. class RobotTrajectoryFollower(object):
  17.     RATE = 0.02
  18.  
  19.     def __init__(self, name):
  20.         self._action_name = name
  21.         self._as = actionlib.ActionServer(self._action_name+"/follow_joint_trajectory", FollowJointTrajectoryAction, self.on_goal, self.on_cancel, auto_start=False)
  22.         self.goal_handle = None
  23.         self.traj = None
  24.         self.lenpoints = None
  25.         self.index = 0
  26.  
  27.         self.current_positions = [0.0] * len(joint_names)
  28.  
  29.         self.pub_joint_states = rospy.Publisher('joint_states', JointState, queue_size=1)
  30.  
  31.         self.update_timer = rospy.Timer(rospy.Duration(self.RATE), self._update)
  32.  
  33.     def start(self):
  34.         self._as.start()
  35.         rospy.logwarn("The action server for braccio_controller has started")
  36.  
  37.     def on_goal(self, goal_handle):
  38.  
  39.         if self.goal_handle:
  40.             # If goal_handle doesn't exist, cancels the existing goal
  41.             self.goal_handle.set_canceled()
  42.             self.goal_handle = None
  43.  
  44.         # check that robot is connected
  45.  
  46.         # get joint_names
  47.         joint_names = goal_handle.get_goal().trajectory.joint_names
  48.         joint_positions = goal_handle.get_goal().trajectory.points
  49.         self.traj = goal_handle.get_goal().trajectory
  50.  
  51.         # TODO: Checks that the trajectory has velocities
  52.  
  53.         rospy.loginfo("Accepting goal!")
  54.         # Replaces the goal
  55.         self.goal_handle = goal_handle
  56.         self.traj = goal_handle.get_goal().trajectory
  57.         self.lenpoints = len(self.traj.points)
  58.         self.index = 0
  59.         self.goal_handle.set_accepted("Trajectory accepted!")
  60.         print self.traj
  61.         print type(self.traj)
  62.         text = str(self.traj)
  63.         text = text.split("\n")
  64.         positions = []
  65.         for line in text:
  66.             if "positions" in line:
  67.                 positions.append(line)
  68.  
  69.         last_command = []           # Save the last command
  70.         result = ""
  71.         for elem in positions:
  72.             start = elem.index("[")
  73.             end = elem.index("]")
  74.             to_be_appended = elem[start+1:end].split(",")
  75.             command = ['__ignored__']
  76.             for elem in to_be_appended:
  77.                 if 'e' in elem:
  78.                     command = command + [0]
  79.                 else:
  80.                     command = command + [float(elem)*(57.2958)+90]
  81.  
  82.             # variable result should be 'Done!'
  83.             result = moving_braccio_pc.main(command+['10'])       # Keep the gripper open
  84.             last_command = command
  85.  
  86.         result = moving_braccio_pc.main(last_command+['73'])      # Close the gripper to grasp the object
  87.  
  88.     def on_cancel(self, goal_handle):
  89.  
  90.         if goal_handle == self.goal_handle:
  91.             self.goal_handle.set_canceled()
  92.             self.goal_handle = None
  93.         else:
  94.             goal_handle.set_canceled()
  95.  
  96.     def _update(self, event):
  97.         #update robot state
  98.         now = rospy.get_rostime()
  99.  
  100.         msg = JointState()
  101.         msg.header.stamp = now
  102.         msg.header.frame_id = "From arduino webserver for braccio"
  103.         msg.name = joint_names
  104.  
  105.         if self.traj and self.goal_handle:
  106.             #rospy.logwarn("UPDATE ************************")
  107.             #rospy.logwarn(self.traj)
  108.  
  109.             if self.index == self.lenpoints:
  110.                 msg_success = 'Trajectory execution successfully completed'
  111.                 rospy.logwarn(msg_success)
  112.                 res = FollowJointTrajectoryResult()
  113.                 res.error_code=FollowJointTrajectoryResult.SUCCESSFUL
  114.                 self.goal_handle.set_succeeded(result=res, text=msg_success)
  115.                 self.goal_handle = None
  116.                 self.index = 0
  117.                 self.lenpoints = None
  118.                 self.traj = None
  119.             else:
  120.                 position = self.traj.points[self.index].positions
  121.                 self.index += 1
  122.                 self.current_positions = position
  123.  
  124.         msg.position = self.current_positions
  125.         #msg.effort = [0] * 5
  126.         self.pub_joint_states.publish(msg)
  127.  
  128.  
  129.  
  130. if __name__ == '__main__':
  131.     rospy.init_node('braccio_controller')
  132.     as_ = RobotTrajectoryFollower("braccio_controller")
  133.     as_.start()
  134.     rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement