move_to_start.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import rospy as ros
5 
6 from actionlib import SimpleActionClient
7 from sensor_msgs.msg import JointState
8 from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
9 from control_msgs.msg import FollowJointTrajectoryAction, \
10  FollowJointTrajectoryGoal, FollowJointTrajectoryResult
11 
12 ros.init_node('move_to_start')
13 
14 action = ros.resolve_name('~follow_joint_trajectory')
15 client = SimpleActionClient(action, FollowJointTrajectoryAction)
16 ros.loginfo("move_to_start: Waiting for '" + action + "' action to come up")
17 client.wait_for_server()
18 
19 param = ros.resolve_name('~joint_pose')
20 pose = ros.get_param(param, None)
21 if pose is None:
22  ros.logerr('move_to_start: Could not find required parameter "' + param + '"')
23  sys.exit(1)
24 
25 topic = ros.resolve_name('~joint_states')
26 ros.loginfo("move_to_start: Waiting for message on topic '" + topic + "'")
27 joint_state = ros.wait_for_message(topic, JointState)
28 initial_pose = dict(zip(joint_state.name, joint_state.position))
29 
30 max_movement = max(abs(pose[joint] - initial_pose[joint]) for joint in pose)
31 
32 point = JointTrajectoryPoint()
33 point.time_from_start = ros.Duration.from_sec(
34  # Use either the time to move the furthest joint with 'max_dq' or 500ms,
35  # whatever is greater
36  max(max_movement / ros.get_param('~max_dq', 0.5), 0.5)
37 )
38 goal = FollowJointTrajectoryGoal()
39 
40 goal.trajectory.joint_names, point.positions = [list(x) for x in zip(*pose.items())]
41 point.velocities = [0] * len(pose)
42 
43 goal.trajectory.points.append(point)
44 goal.goal_time_tolerance = ros.Duration.from_sec(0.5)
45 
46 ros.loginfo('Sending trajectory Goal to move into initial config')
47 client.send_goal_and_wait(goal)
48 
49 result = client.get_result()
50 if result.error_code != FollowJointTrajectoryResult.SUCCESSFUL:
51  ros.logerr('move_to_start: Movement was not successful: ' + {
52  FollowJointTrajectoryResult.INVALID_GOAL:
53  """
54  The joint pose you want to move to is invalid (e.g. unreachable, singularity...).
55  Is the 'joint_pose' reachable?
56  """,
57 
58  FollowJointTrajectoryResult.INVALID_JOINTS:
59  """
60  The joint pose you specified is for different joints than the joint trajectory controller
61  is claiming. Does you 'joint_pose' include all 7 joints of the robot?
62  """,
63 
64  FollowJointTrajectoryResult.PATH_TOLERANCE_VIOLATED:
65  """
66  During the motion the robot deviated from the planned path too much. Is something blocking
67  the robot?
68  """,
69 
70  FollowJointTrajectoryResult.GOAL_TOLERANCE_VIOLATED:
71  """
72  After the motion the robot deviated from the desired goal pose too much. Probably the robot
73  didn't reach the joint_pose properly
74  """,
75  }[result.error_code])
76 
77 else:
78  ros.loginfo('move_to_start: Successfully moved into start pose')


franka_example_controllers
Author(s): Franka Emika GmbH
autogenerated on Mon Sep 19 2022 03:06:01