interactive_tf.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <interactive_markers/interactive_marker_server.h>
00003 #include <tf/transform_broadcaster.h>
00004 #include <geometry_msgs/Pose.h>
00005 #include <rosbag/bag.h>
00006 
00007 using namespace std;
00008 
00009 class InteractiveTF 
00010 {
00011 private:
00012     interactive_markers::InteractiveMarkerServer im_server_;
00013     tf::TransformBroadcaster tf_broad_;
00014     std::string parent_frame_, child_frame_;
00015     double rate_;
00016     ros::Timer tf_timer_;
00017     geometry_msgs::Pose marker_pose_;
00018     geometry_msgs::TransformStamped cur_tf_msg;
00019 public:
00020     InteractiveTF(const std::string& parent_frame, const std::string& child_frame, double rate = 100);
00021     void processTFControl(const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback);
00022     void addTFMarker();
00023     void publishTF(const ros::TimerEvent& event);
00024     void bagTF(const string& bag_name, const string& topic_name);
00025 };
00026 
00027 InteractiveTF::InteractiveTF(const std::string& parent_frame, 
00028                              const std::string& child_frame, double rate) :
00029     im_server_("transform_marker"),
00030     parent_frame_(parent_frame),
00031     child_frame_(child_frame),
00032     rate_(rate) 
00033 {
00034     marker_pose_.orientation.w = 1;
00035 }
00036 
00037 void InteractiveTF::addTFMarker() 
00038 {
00039     ros::NodeHandle nh;
00040     visualization_msgs::InteractiveMarker tf_marker;
00041     tf_marker.header.frame_id = parent_frame_;
00042     tf_marker.name = "tf_marker";
00043     tf_marker.scale = 0.2;
00044     visualization_msgs::InteractiveMarkerControl tf_control;
00045     tf_control.orientation_mode = visualization_msgs::InteractiveMarkerControl::FIXED;
00046     // x
00047     tf_control.orientation.x = 1; tf_control.orientation.y = 0;
00048     tf_control.orientation.z = 0; tf_control.orientation.w = 1;
00049     tf_control.name = "rotate_x";
00050     tf_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00051     tf_marker.controls.push_back(tf_control);
00052     tf_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00053     tf_marker.controls.push_back(tf_control);
00054     // y
00055     tf_control.orientation.x = 0; tf_control.orientation.y = 1;
00056     tf_control.orientation.z = 0; tf_control.orientation.w = 1;
00057     tf_control.name = "rotate_y";
00058     tf_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00059     tf_marker.controls.push_back(tf_control);
00060     tf_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00061     tf_marker.controls.push_back(tf_control);
00062     // z
00063     tf_control.orientation.x = 0; tf_control.orientation.y = 0;
00064     tf_control.orientation.z = 1; tf_control.orientation.w = 1;
00065     tf_control.name = "rotate_z";
00066     tf_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::ROTATE_AXIS;
00067     tf_marker.controls.push_back(tf_control);
00068     tf_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
00069     tf_marker.controls.push_back(tf_control);
00070     im_server_.insert(tf_marker, boost::bind(&InteractiveTF::processTFControl, this, _1));
00071     im_server_.applyChanges();
00072     tf_timer_ = nh.createTimer(ros::Duration(1.0 / rate_), &InteractiveTF::publishTF, this);
00073 }
00074 
00075 void InteractiveTF::processTFControl(
00076         const visualization_msgs::InteractiveMarkerFeedbackConstPtr& feedback) 
00077 {
00078     /*
00079     ROS_INFO_STREAM(feedback->pose.position.x << " " << 
00080                     feedback->pose.position.y << " " << 
00081                     feedback->pose.position.z << " " << 
00082                     feedback->pose.orientation.x << " " << 
00083                     feedback->pose.orientation.y << " " << 
00084                     feedback->pose.orientation.z << " " << 
00085                     feedback->pose.orientation.w << " ");
00086     */
00087     marker_pose_ = feedback->pose;
00088 }
00089 
00090 void InteractiveTF::publishTF(const ros::TimerEvent& event) 
00091 {
00092     geometry_msgs::TransformStamped tf_msg;
00093     tf_msg.header.stamp = ros::Time::now();
00094     tf_msg.header.frame_id = parent_frame_;
00095     tf_msg.child_frame_id = child_frame_;
00096     tf_msg.transform.translation.x = marker_pose_.position.x;
00097     tf_msg.transform.translation.y = marker_pose_.position.y;
00098     tf_msg.transform.translation.z = marker_pose_.position.z;
00099     tf_msg.transform.rotation.x = marker_pose_.orientation.x;
00100     tf_msg.transform.rotation.y = marker_pose_.orientation.y;
00101     tf_msg.transform.rotation.z = marker_pose_.orientation.z;
00102     tf_msg.transform.rotation.w = marker_pose_.orientation.w;
00103     tf_broad_.sendTransform(tf_msg);
00104     cur_tf_msg = tf_msg;
00105 }
00106 
00107 void InteractiveTF::bagTF(const string& bag_name, const string& topic_name) 
00108 {
00109     rosbag::Bag bag;
00110     bag.open(bag_name, rosbag::bagmode::Write);
00111     bag.write(topic_name, cur_tf_msg.header.stamp, cur_tf_msg);
00112     bag.close();
00113 }
00114 
00115 int main(int argc, char **argv)
00116 {
00117     ros::init(argc, argv, "interactive_tf", ros::init_options::AnonymousName);
00118     if(argc < 3 || argc > 6) {
00119         printf("Usage: interative_tf parent_frame child_frame [rate] [bag_file] [bag_topic]\n");
00120         return 1;
00121     }
00122     if(argc >= 4) {
00123         InteractiveTF itf(argv[1], argv[2], atof(argv[3]));
00124         itf.addTFMarker();
00125         ros::spin();
00126         if(argc >= 5) {
00127             string topic_name = "/itf_transform";
00128             if(argc >= 6)
00129                 topic_name = argv[5];
00130             itf.bagTF(argv[4], topic_name);
00131         }
00132     } else {
00133         InteractiveTF itf(argv[1], argv[2]);
00134         itf.addTFMarker();
00135         ros::spin();
00136     }
00137 
00138     return 0;
00139 }


interactive_tf
Author(s): Kelsey Hawkins
autogenerated on Wed Nov 27 2013 11:36:37