reem_traj_publisher.cpp
Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <string>
00003 #include <vector>
00004 
00005 #include <boost/bind.hpp>
00006 
00007 #include <ros/ros.h>
00008 #include <sensor_msgs/JointState.h>
00009 #include <trajectory_msgs/JointTrajectory.h>
00010 
00011 class TrajectoryPublisher
00012 {
00013 public:
00014   TrajectoryPublisher()
00015    : approach_time_(3.0), // NOTE: Magic value
00016      approach_count_(0)
00017   {
00018     controlled_joints_.push_back("torso_1_joint");
00019     controlled_joints_.push_back("torso_2_joint");
00020     controlled_joints_.push_back("head_1_joint");
00021     controlled_joints_.push_back("head_2_joint");
00022     controlled_joints_.push_back("arm_left_1_joint");
00023     controlled_joints_.push_back("arm_left_2_joint");
00024     controlled_joints_.push_back("arm_left_3_joint");
00025     controlled_joints_.push_back("arm_left_4_joint");
00026     controlled_joints_.push_back("arm_left_5_joint");
00027     controlled_joints_.push_back("arm_left_6_joint");
00028     controlled_joints_.push_back("arm_left_7_joint");
00029     controlled_joints_.push_back("arm_right_1_joint");
00030     controlled_joints_.push_back("arm_right_2_joint");
00031     controlled_joints_.push_back("arm_right_3_joint");
00032     controlled_joints_.push_back("arm_right_4_joint");
00033     controlled_joints_.push_back("arm_right_5_joint");
00034     controlled_joints_.push_back("arm_right_6_joint");
00035     controlled_joints_.push_back("arm_right_7_joint");
00036 
00037     loop_rate_ = 2; // TODO: Get from param server
00038 
00039     ros::NodeHandle nh;
00040     traj_publisher_          = nh.advertise<trajectory_msgs::JointTrajectory>("/command", 1);
00041     joint_states_subscriber_ = nh.subscribe("/joint_states_cmd",
00042                                             1, // Buffer size
00043                                             &TrajectoryPublisher::publishTrajectory, this);
00044   }
00045 private:
00046   std::vector<std::string> controlled_joints_;
00047   ros::Publisher  traj_publisher_;
00048   ros::Subscriber joint_states_subscriber_;
00049   int loop_rate_;
00050   double approach_time_;
00051   int approach_count_;
00052 
00053 void publishTrajectory(const sensor_msgs::JointState& joint_states_cmd)
00054 {
00055   const double current_approach_time = approach_time_ - static_cast<double>(approach_count_) / static_cast<double>(loop_rate_);
00056   ros::Duration approach_delta;
00057   if (current_approach_time > 0.0)
00058   {
00059     approach_delta = ros::Duration(current_approach_time);
00060     ++approach_count_;
00061   }
00062   else
00063   {
00064     approach_delta = ros::Duration(0.0);
00065   }
00066 
00067   trajectory_msgs::JointTrajectory traj;
00068   const ros::Duration delta(0.2 / loop_rate_); // NOTE: Magic value
00069 
00070   traj.header.stamp = joint_states_cmd.header.stamp + delta;
00071   traj.points.resize(1);
00072 
00073   for(unsigned int k = 0; k < joint_states_cmd.name.size(); ++k)
00074   {
00075     if(std::find(controlled_joints_.begin(), controlled_joints_.end(), joint_states_cmd.name[k] ) != controlled_joints_.end())
00076     {
00077       traj.joint_names.push_back(joint_states_cmd.name[k]);
00078       traj.points.front().positions.push_back(joint_states_cmd.position[k]);
00079       traj.points.front().velocities.push_back(joint_states_cmd.velocity[k]);
00080     }
00081   }
00082   traj.points.front().time_from_start = ros::Duration(1.0 / loop_rate_) + delta + approach_delta;
00083   traj_publisher_.publish(traj);
00084 }
00085 };
00086 
00087 int main(int argc, char** argv)
00088 {
00089   ros::init(argc, argv, "reem_traj_publisher");
00090   TrajectoryPublisher traj_publisher;
00091   ros::Rate loopRate(10);
00092 
00093   // Main loop
00094   while (ros::ok())
00095   {
00096     ros::spinOnce();
00097     loopRate.sleep();
00098   }
00099 
00100   return EXIT_SUCCESS;
00101 }


reem_teleop_coordinator
Author(s): Marcus Liebhardt
autogenerated on Mon Jan 6 2014 11:40:12