]> defiant.homedns.org Git - arm_ros_conn.git/blobdiff - scripts/arm_ros_conn.py
added FollowJointTrajectory Action Server
[arm_ros_conn.git] / scripts / arm_ros_conn.py
index ff06cdf8264a28c5c0f749162e01cbf0d91176ab..42a3e5647558f664b6c376ca1077998d43434c00 100755 (executable)
@@ -2,24 +2,65 @@
 # -*- coding: iso-8859-15 -*-
 
 import rospy
-import tf
-from nav_msgs.msg import Odometry
+import arm
+import actionlib
+from sensor_msgs.msg import JointState
+from control_msgs.msg import FollowJointTrajectoryAction, FollowJointTrajectoryActionResult, FollowJointTrajectoryActionFeedback, FollowJointTrajectoryResult
+from actionlib_msgs.msg import GoalStatus
+from math import *
+
+lJointNames = ["base_to_link1", "link_1_2_joint", "link_2_3_joint", "gripper_joint_1", "gripper_joint_2", "left_gripper_joint", "right_gripper_joint"]
+
 
 class ARMRosConn():
+       _feedback = FollowJointTrajectoryActionFeedback()
+       _result = FollowJointTrajectoryActionResult()
+
        def __init__(self):
                rospy.init_node('arm')
 
-               rospy.Subscriber("odom", Odometry, self.odom_received)
-               self.tf_broadcaster = tf.broadcaster.TransformBroadcaster()
+               self.speed = 220
+               arm.switch(0)
+               arm.switch(2)
+               arm.set_hall_mode(3, 0)
+               arm.set_hall_mode(5, 0)
+               arm.set_tolerance(3, 0)
+               arm.set_tolerance(5, 0)
 
-               rate = rospy.Rate(10)
+               self._as = actionlib.SimpleActionServer("arm_controller/follow_joint_trajectory", FollowJointTrajectoryAction, execute_cb=self.execute_joint_trajectory, auto_start = False)
+               self._as.start()
+               self.pub_joint_states = rospy.Publisher("joint_states", JointState, queue_size=16)
+               self.run()
+
+       def run(self):
+               rate = rospy.Rate(20)
                while not rospy.is_shutdown():
+                       self.publish_joint_states()
                        rate.sleep()
+       
+       def publish_joint_states(self):
+               joint_state = JointState()
+               joint_state.header.stamp = rospy.Time.now()
+               joint_state.name = lJointNames
+               joint_state.position = [-arm.get_angle(0), arm.get_angle(1), -arm.get_angle(2), -arm.get_angle(3), arm.get_angle(4), 0.175-arm.get_angle(5)/2, 0.175-arm.get_angle(5)/2]
+               self.pub_joint_states.publish(joint_state)
+
+       def execute_joint_trajectory(self, goal):
+               for point in goal.trajectory.points:
+                       print goal.trajectory.joint_names
+                       print point.positions
+                       arm.to_angle(0, self.speed, -point.positions[goal.trajectory.joint_names.index(lJointNames[0])])
+                       arm.to_angle(1, self.speed, point.positions[goal.trajectory.joint_names.index(lJointNames[1])])
+                       arm.to_angle(2, self.speed, -point.positions[goal.trajectory.joint_names.index(lJointNames[2])])
+                       arm.to_angle(3, self.speed, -point.positions[goal.trajectory.joint_names.index(lJointNames[3])])
+                       arm.to_angle(4, self.speed, point.positions[goal.trajectory.joint_names.index(lJointNames[4])])
+                       self._feedback.status = GoalStatus.SUCCEEDED
+                       self._feedback.feedback.joint_names = goal.trajectory.joint_names 
+                       self._feedback.feedback.desired = point
+                       self._as.publish_feedback(self._feedback.feedback)
+               self._result.status = FollowJointTrajectoryResult.SUCCESSFUL
+               self._as.set_succeeded(self._result.result)
 
-       def odom_received(self, msg):
-               pos = (msg.pose.pose.position.x, msg.pose.pose.position.y, msg.pose.pose.position.z)
-               orientation = (msg.pose.pose.orientation.x, msg.pose.pose.orientation.y, msg.pose.pose.orientation.z, msg.pose.pose.orientation.w)
-               self.tf_broadcaster.sendTransform(pos, orientation, rospy.Time.now(), "base_link", "odom")
 
 if __name__ == '__main__':
        ARMRosConn()