00001 #include <ros/ros.h> 00002 #include <control_msgs/FollowJointTrajectoryAction.h> 00003 #include <actionlib/client/simple_action_client.h> 00004 00005 typedef actionlib::SimpleActionClient< control_msgs::FollowJointTrajectoryAction > TrajClient; 00006 00007 class RobotArm 00008 { 00009 private: 00010 // Action client for the joint trajectory action 00011 // used to trigger the arm movement action 00012 TrajClient* traj_client_; 00013 00014 public: 00016 RobotArm() 00017 { 00018 // tell the action client that we want to spin a thread by default 00019 traj_client_ = new TrajClient("/iri_wam_controller/follow_joint_trajectory", true); 00020 00021 // wait for action server to come up 00022 while(!traj_client_->waitForServer(ros::Duration(5.0))){ 00023 ROS_INFO("Waiting for the joint_trajectory_action server"); 00024 } 00025 } 00026 00028 ~RobotArm() 00029 { 00030 delete traj_client_; 00031 } 00032 00034 void startTrajectory(control_msgs::FollowJointTrajectoryGoal goal) 00035 { 00036 // When to start the trajectory: 1s from now 00037 goal.trajectory.header.stamp = ros::Time::now() + ros::Duration(1.0); 00038 traj_client_->sendGoal(goal); 00039 } 00040 00042 00047 control_msgs::FollowJointTrajectoryGoal armExtensionTrajectory() 00048 { 00049 //our goal variable 00050 control_msgs::FollowJointTrajectoryGoal goal; 00051 00052 int trajectory_points = 80; 00053 00054 // We will have two waypoints in this goal trajectory 00055 goal.trajectory.points.resize(trajectory_points); 00056 00057 // First trajectory point 00058 // Positions 00059 int ind = 0; 00060 goal.trajectory.points[ind].positions.resize(7); 00061 goal.trajectory.points[ind].positions[0] = 0.0; 00062 goal.trajectory.points[ind].positions[1] = 0.0; 00063 goal.trajectory.points[ind].positions[2] = 0.0; 00064 goal.trajectory.points[ind].positions[3] = 0.0; 00065 goal.trajectory.points[ind].positions[4] = 0.0; 00066 goal.trajectory.points[ind].positions[5] = 0.0; 00067 goal.trajectory.points[ind].positions[6] = 0.0; 00068 00069 ind += 1; 00070 for(int ind = 1; ind < trajectory_points; ind++) 00071 { 00072 goal.trajectory.points[ind].positions.resize(7); 00073 goal.trajectory.points[ind].positions[0] = 0.0; 00074 goal.trajectory.points[ind].positions[1] = (0.2*ind/trajectory_points); 00075 goal.trajectory.points[ind].positions[2] = 0.0; 00076 goal.trajectory.points[ind].positions[3] = (2.0*ind/trajectory_points); 00077 goal.trajectory.points[ind].positions[4] = 0.0; 00078 goal.trajectory.points[ind].positions[5] = 0.0; 00079 goal.trajectory.points[ind].positions[6] = 0.0; 00080 } 00081 00082 return goal; 00083 } 00084 00086 actionlib::SimpleClientGoalState getState() 00087 { 00088 return traj_client_->getState(); 00089 } 00090 00091 }; 00092 00093 int main(int argc, char** argv) 00094 { 00095 // Init the ROS node 00096 ros::init(argc, argv, "robot_driver"); 00097 00098 RobotArm arm; 00099 // Start the trajectory 00100 arm.startTrajectory(arm.armExtensionTrajectory()); 00101 // Wait for trajectory completion 00102 while(!arm.getState().isDone() && ros::ok()) 00103 { 00104 usleep(50000); 00105 } 00106 } 00107