simple_trajectory_compare.cpp
Go to the documentation of this file.
1 
27 #include <ros/ros.h>
28 #include <control_msgs/FollowJointTrajectoryAction.h>
30 
32 
33 class ShadowTrajectory
34 {
35 private:
36  // Action client for the joint trajectory action
37  // used to trigger the arm movement action
39 
40 public:
41  // ! Initialize the action client and wait for action server to come up
43  {
44  // tell the action client that we want to spin a thread by default
45  traj_client_ = new TrajClient("r_arm_controller/joint_trajectory_action", true);
46 
47  // wait for action server to come up
48  while (!traj_client_->waitForServer(ros::Duration(5.0)))
49  {
50  ROS_INFO("Waiting for the joint_trajectory_action server");
51  }
52  }
53 
54  // ! Clean up the action client
56  {
57  delete traj_client_;
58  }
59 
60  // ! Sends the command to start a given trajectory
61  void startTrajectory(control_msgs::FollowJointTrajectoryGoal goal)
62  {
63  // When to start the trajectory: 1s from now
64  goal.trajectory.header.stamp = ros::Time::now() + ros::Duration(1.0);
65  traj_client_->sendGoal(goal);
66  }
67 
68  // ! Wait for currently running trajectory to finish
70  {
71  while (!getState().isDone() && ros::ok())
72  {
73  usleep(50000);
74  }
75  }
76 
77  // ! Generates a simple trajectory to move two fingers on the hand.
83  control_msgs::FollowJointTrajectoryGoal arm_movement()
84  {
85  // our goal variable
86  control_msgs::FollowJointTrajectoryGoal goal;
87  // First, the joint names, which apply to all waypoints
88  goal.trajectory.joint_names.push_back("ShoulderJRotate");
89  goal.trajectory.joint_names.push_back("ShoulderJSwing");
90  goal.trajectory.joint_names.push_back("ElbowJSwing");
91  goal.trajectory.joint_names.push_back("ElbowJRotate");
92  goal.trajectory.joint_names.push_back("wrj2");
93  goal.trajectory.joint_names.push_back("wrj1");
94 
95  // Set number of waypoints in this goal trajectory
96  goal.trajectory.points.resize(3);
97 
98  // First trajectory point
99  // Positions
100  int ind = 0;
101  goal.trajectory.points[ind].positions.resize(6);
102  goal.trajectory.points[ind].positions[0] = 0.0;
103  goal.trajectory.points[ind].positions[1] = 0.0;
104  goal.trajectory.points[ind].positions[2] = 1.57;
105  goal.trajectory.points[ind].positions[3] = -0.78;
106  goal.trajectory.points[ind].positions[4] = 0.0;
107  goal.trajectory.points[ind].positions[5] = 0.0;
108 
109  // Points also have velocities
110  goal.trajectory.points[ind].velocities.resize(6);
111  goal.trajectory.points[ind].velocities[0] = 0.0;
112  goal.trajectory.points[ind].velocities[1] = 0.0;
113  goal.trajectory.points[ind].velocities[2] = 0.0;
114  goal.trajectory.points[ind].velocities[3] = 0.0;
115  goal.trajectory.points[ind].velocities[4] = 0.0;
116  goal.trajectory.points[ind].velocities[5] = 0.0;
117 
118  // To be reached 4.0 second after starting along the trajectory
119  goal.trajectory.points[ind].time_from_start = ros::Duration(10.0);
120 
121  // 2nd trajectory point
122  ind += 1;
123  goal.trajectory.points[ind].positions.resize(6);
124  goal.trajectory.points[ind].positions[0] = 0.4;
125  goal.trajectory.points[ind].positions[1] = 0.78;
126  goal.trajectory.points[ind].positions[2] = 0.78;
127  goal.trajectory.points[ind].positions[3] = 0.78;
128  goal.trajectory.points[ind].positions[4] = 0.1;
129  goal.trajectory.points[ind].positions[5] = 0.1;
130 
131  // Points also have velocities
132  goal.trajectory.points[ind].velocities.resize(6);
133  goal.trajectory.points[ind].velocities[0] = 0.3;
134  goal.trajectory.points[ind].velocities[1] = 0.0;
135  goal.trajectory.points[ind].velocities[2] = 0.0;
136  goal.trajectory.points[ind].velocities[3] = 0.0;
137  goal.trajectory.points[ind].velocities[4] = 0.0;
138  goal.trajectory.points[ind].velocities[5] = 0.0;
139 
140  // To be reached 4.0 second after starting along the trajectory
141  goal.trajectory.points[ind].time_from_start = ros::Duration(16.0);
142  // 3rd trajectory point
143  ind += 1;
144  goal.trajectory.points[ind].positions.resize(6);
145  goal.trajectory.points[ind].positions[0] = 0.6;
146  goal.trajectory.points[ind].positions[1] = 0.48;
147  goal.trajectory.points[ind].positions[2] = 0.78;
148  goal.trajectory.points[ind].positions[3] = 0.78;
149  goal.trajectory.points[ind].positions[4] = 0.1;
150  goal.trajectory.points[ind].positions[5] = 0.1;
151 
152  // Points also have velocities
153  goal.trajectory.points[ind].velocities.resize(6);
154  goal.trajectory.points[ind].velocities[0] = 0.0;
155  goal.trajectory.points[ind].velocities[1] = 0.0;
156  goal.trajectory.points[ind].velocities[2] = 0.0;
157  goal.trajectory.points[ind].velocities[3] = 0.0;
158  goal.trajectory.points[ind].velocities[4] = 0.0;
159  goal.trajectory.points[ind].velocities[5] = 0.0;
160 
161  // To be reached 4.0 second after starting along the trajectory
162  goal.trajectory.points[ind].time_from_start = ros::Duration(20.0);
163 
164  return goal;
165  }
166 
167  // ! Returns the current state of the action
169  {
170  return traj_client_->getState();
171  }
172 };
173 
174 int main(int argc, char **argv)
175 {
176  // Init the ROS node
177  ros::init(argc, argv, "shadow_trajectory_driver");
178 
179  ShadowTrajectory sj;
180 
181  sj.startTrajectory(sj.arm_movement());
182  sj.waitTrajectory();
183 }
184 
185 // vim: sw=2:ts=2
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
int main(int argc, char **argv)
actionlib::SimpleActionClient< control_msgs::FollowJointTrajectoryAction > TrajClient
actionlib::SimpleClientGoalState getState()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
void startTrajectory(control_msgs::FollowJointTrajectoryGoal goal)
control_msgs::FollowJointTrajectoryGoal arm_movement()
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
static Time now()
SimpleClientGoalState getState() const


sr_mechanism_controllers
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:58