demo_tf_listener.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2016, University of Colorado, Boulder
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of PickNik LLC nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Dave Coleman
00036    Desc:
00037 */
00038 
00039 #ifndef TF_KEYBOARD_CAL_DEMO_TF_LISTENER_H
00040 #define TF_KEYBOARD_CAL_DEMO_TF_LISTENER_H
00041 
00042 // ROS
00043 #include <ros/ros.h>
00044 #include <tf/transform_listener.h>
00045 
00046 // Conversions
00047 #include <tf_conversions/tf_eigen.h>
00048 #include <eigen_conversions/eigen_msg.h>
00049 
00050 // Visualize
00051 #include <rviz_visual_tools/rviz_visual_tools.h>
00052 
00053 namespace tf_keyboard_cal
00054 {
00055 class DemoTFListener
00056 {
00057 public:
00061   DemoTFListener() : name_("demo_tf_listener"), nh_("~")
00062   {
00063     visual_tools_.reset(new rviz_visual_tools::RvizVisualTools("world", "/demo_tf_listener/markers"));
00064     visual_tools_->deleteAllMarkers();
00065 
00066     ROS_INFO_STREAM_NAMED(name_, "DemoTFListener Ready.");
00067   }
00068 
00069   void runLoop()
00070   {
00071     // Allow time for listener to load
00072     ros::Duration(1.0).sleep();
00073 
00074     const bool verbose = false;
00075 
00076     ros::Rate rate(10.0);
00077     while (ros::ok())
00078     {
00079       Eigen::Affine3d pose;
00080 
00081       getPose("/world", "/thing", pose);
00082 
00083       // Show pose in console
00084       if (verbose)
00085         std::cout << "Position: \n" << pose.translation() << std::endl;
00086 
00087       // Visualize pose
00088       visual_tools_->publishXArrow(pose);
00089 
00090       rate.sleep();
00091     }
00092   }
00093 
00094   bool getPose(const std::string& from_frame, const std::string& to_frame, Eigen::Affine3d &pose)
00095   {
00096       tf::StampedTransform tf_transform;
00097       try
00098       {
00099         tf_.lookupTransform(from_frame, to_frame, ros::Time(0), tf_transform);
00100       }
00101       catch (tf::TransformException ex)
00102       {
00103         ROS_ERROR("%s", ex.what());
00104         return false;
00105       }
00106 
00107       // Convert to eigen
00108       tf::transformTFToEigen(tf_transform, pose);
00109       return true;
00110   }
00111 
00112 private:
00113   // --------------------------------------------------------
00114   // The short name of this class
00115   std::string name_;
00116 
00117   // A shared node handle
00118   ros::NodeHandle nh_;
00119 
00120   // For visualizing things in rviz
00121   rviz_visual_tools::RvizVisualToolsPtr visual_tools_;
00122 
00123   tf::TransformListener tf_;
00124 
00125 };  // end class
00126 
00127 // Create boost pointers for this class
00128 typedef boost::shared_ptr<DemoTFListener> DemoTFListenerPtr;
00129 typedef boost::shared_ptr<const DemoTFListener> DemoTFListenerConstPtr;
00130 
00131 }  // namespace tf_keyboard_cal
00132 #endif  // TF_KEYBOARD_CAL_DEMO_TF_LISTENER_H
00133 
00134 int main(int argc, char** argv)
00135 {
00136   // Initialize ROS
00137   ros::init(argc, argv, "demo_tf_listener");
00138   ROS_INFO_STREAM_NAMED("main", "Starting DemoTFListener...");
00139 
00140   // Allow the action server to recieve and send ros messages
00141   ros::AsyncSpinner spinner(2);
00142   spinner.start();
00143 
00144   // Initialize main class
00145   tf_keyboard_cal::DemoTFListener server;
00146   server.runLoop();
00147 
00148   // Shutdown
00149   ROS_INFO_STREAM_NAMED("main", "Shutting down.");
00150   ros::shutdown();
00151 
00152   return 0;
00153 }


tf_keyboard_cal
Author(s): Dave Coleman , Andy McEvoy
autogenerated on Fri Mar 18 2016 03:24:31