1
// http://www.ros.org/wiki/pr2_controllers/Tutorials/Moving%20the%20arm%20using%20the%20Joint%20Trajectory%20Action
4
#include <pr2_controllers_msgs/JointTrajectoryAction.h>
5
#include <actionlib/client/simple_action_client.h>
7
typedef actionlib::SimpleActionClient< pr2_controllers_msgs::JointTrajectoryAction > TrajClient;
12
// Action client for the joint trajectory action
13
// used to trigger the arm movement action
14
TrajClient* traj_client_;
17
//! Initialize the action client and wait for action server to come up
20
// tell the action client that we want to spin a thread by default
21
traj_client_ = new TrajClient("r_arm_controller/joint_trajectory_action", true);
23
// wait for action server to come up
24
while(!traj_client_->waitForServer(ros::Duration(5.0))){
25
ROS_INFO("Waiting for the joint_trajectory_action server");
29
//! Clean up the action client
35
//! Sends the command to start a given trajectory
36
void startTrajectory(pr2_controllers_msgs::JointTrajectoryGoal goal)
38
// When to start the trajectory: 1s from now
39
goal.trajectory.header.stamp = ros::Time::now(); //+ ros::Duration(1.0);
40
traj_client_->sendGoal(goal);
43
//! Wait for currently running trajectory to finish
44
void waitTrajectory() {
45
while(!getState().isDone() && ros::ok()) { usleep(50000); }
48
//! Generates a simple trajectory to move the arm.
49
/*! Note that this trajectory contains three waypoints, joined together
50
as a single trajectory. Alternatively, each of these waypoints could
51
be in its own trajectory - a trajectory can have one or more waypoints
52
depending on the desired application.
54
pr2_controllers_msgs::JointTrajectoryGoal arm_movement()
57
pr2_controllers_msgs::JointTrajectoryGoal goal;
59
// First, the joint names, which apply to all waypoints
60
goal.trajectory.joint_names.push_back("ShoulderJRotate");
61
goal.trajectory.joint_names.push_back("ShoulderJSwing");
62
goal.trajectory.joint_names.push_back("ElbowJSwing");
63
goal.trajectory.joint_names.push_back("ElbowJRotate");
64
goal.trajectory.joint_names.push_back("WRJ2");
65
goal.trajectory.joint_names.push_back("WRJ1");
67
// Set number of waypoints in this goal trajectory
68
goal.trajectory.points.resize(3);
70
// First trajectory point
73
goal.trajectory.points[ind].positions.resize(6);
74
goal.trajectory.points[ind].positions[0] = 0.0;
75
goal.trajectory.points[ind].positions[1] = 0.0;
76
goal.trajectory.points[ind].positions[2] = 1.57;
77
goal.trajectory.points[ind].positions[3] = -0.78;
78
goal.trajectory.points[ind].positions[4] = 0.0;
79
goal.trajectory.points[ind].positions[5] = 0.0;
81
// Points also have velocities
82
goal.trajectory.points[ind].velocities.resize(6);
83
goal.trajectory.points[ind].velocities[0] = 0.0;
84
goal.trajectory.points[ind].velocities[1] = 0.0;
85
goal.trajectory.points[ind].velocities[2] = 0.0;
86
goal.trajectory.points[ind].velocities[3] = 0.0;
87
goal.trajectory.points[ind].velocities[4] = 0.0;
88
goal.trajectory.points[ind].velocities[5] = 0.0;
90
// To be reached 4.0 second after starting along the trajectory
91
goal.trajectory.points[ind].time_from_start = ros::Duration(10.0);
93
// 2nd trajectory point
95
goal.trajectory.points[ind].positions.resize(6);
96
goal.trajectory.points[ind].positions[0] = 0.4;
97
goal.trajectory.points[ind].positions[1] = 0.78;
98
goal.trajectory.points[ind].positions[2] = 0.78;
99
goal.trajectory.points[ind].positions[3] = 0.78;
100
goal.trajectory.points[ind].positions[4] = 0.1;
101
goal.trajectory.points[ind].positions[5] = 0.1;
103
// Points also have velocities
104
goal.trajectory.points[ind].velocities.resize(6);
105
goal.trajectory.points[ind].velocities[0] = 0.3;
106
goal.trajectory.points[ind].velocities[1] = 0.0;
107
goal.trajectory.points[ind].velocities[2] = 0.0;
108
goal.trajectory.points[ind].velocities[3] = 0.0;
109
goal.trajectory.points[ind].velocities[4] = 0.0;
110
goal.trajectory.points[ind].velocities[5] = 0.0;
112
// To be reached 4.0 second after starting along the trajectory
113
goal.trajectory.points[ind].time_from_start = ros::Duration(16.0);
114
// 3rd trajectory point
116
goal.trajectory.points[ind].positions.resize(6);
117
goal.trajectory.points[ind].positions[0] = 0.6;
118
goal.trajectory.points[ind].positions[1] = 0.48;
119
goal.trajectory.points[ind].positions[2] = 0.78;
120
goal.trajectory.points[ind].positions[3] = 0.78;
121
goal.trajectory.points[ind].positions[4] = 0.1;
122
goal.trajectory.points[ind].positions[5] = 0.1;
124
// Points also have velocities
125
goal.trajectory.points[ind].velocities.resize(6);
126
goal.trajectory.points[ind].velocities[0] = 0.0;
127
goal.trajectory.points[ind].velocities[1] = 0.0;
128
goal.trajectory.points[ind].velocities[2] = 0.0;
129
goal.trajectory.points[ind].velocities[3] = 0.0;
130
goal.trajectory.points[ind].velocities[4] = 0.0;
131
goal.trajectory.points[ind].velocities[5] = 0.0;
133
// To be reached 4.0 second after starting along the trajectory
134
goal.trajectory.points[ind].time_from_start = ros::Duration(20.0);
139
//! Returns the current state of the action
140
actionlib::SimpleClientGoalState getState()
142
return traj_client_->getState();
147
int main(int argc, char** argv)
150
ros::init(argc, argv, "shadow_trajectory_driver");
153
sj.startTrajectory(sj.arm_movement());