00001 #ifndef CONVENIENCE_ROS_FUNCTIONS_ROSFUNCTIONS_H
00002 #define CONVENIENCE_ROS_FUNCTIONS_ROSFUNCTIONS_H
00003
00004 #include <baselib_binding/SharedPtr.h>
00005 #include <baselib_binding/Thread.h>
00006 #include <geometry_msgs/PoseStamped.h>
00007 #include <tf/transform_listener.h>
00008 #include <actionlib/client/simple_client_goal_state.h>
00009 #include <trajectory_msgs/JointTrajectory.h>
00010 #include <sensor_msgs/JointState.h>
00011 #include <rosbag/bag.h>
00012 #include <rosbag/view.h>
00013 #include <fstream>
00014
00015 #include <boost/foreach.hpp>
00016
00017
00018 #define DEFAULT_TF_CACHE_TIME 15
00019
00020 namespace convenience_ros_functions
00021 {
00022
00033 class ROSFunctions
00034 {
00035 public:
00036 typedef baselib_binding::shared_ptr<ROSFunctions>::type ROSFunctionsPtr;
00040 ROSFunctions(float tf_max_cache_time = DEFAULT_TF_CACHE_TIME);
00041
00042 ~ROSFunctions()
00043 {
00044 }
00045
00053 static void initSingleton();
00054 static void destroySingleton();
00055
00062 static ROSFunctionsPtr Singleton();
00063
00064 template<typename ROSMessage>
00065 static bool saveToFile(const ROSMessage& msg, const std::string& filename, bool asBinary);
00066
00067 template<typename ROSMessage>
00068 static bool readFromFile(const std::string& filename, ROSMessage& msg, bool isBinary);
00069
00070 template<typename ROSMessage>
00071 static bool saveToBagFile(const std::vector<ROSMessage>& msgs, const std::string& filename);
00072
00073 template<typename ROSMessage>
00074 static bool readFromBagFile(const std::string& filename, std::vector<ROSMessage>& msgs);
00075
00081 static void assignJointState(const sensor_msgs::JointState& target_joints,
00082 sensor_msgs::JointState& joint_state);
00083
00084
00094 static bool intersectJointState(const sensor_msgs::JointState& s1, const sensor_msgs::JointState& s2,
00095 sensor_msgs::JointState& result,
00096 bool init_s1, bool s2_is_subset);
00097
00104 static bool intersectJointStates(const sensor_msgs::JointState& s1,
00105 const sensor_msgs::JointState& s2,
00106 sensor_msgs::JointState& result, bool s2_is_subset);
00107
00108
00115 static bool equalJointPositionsSimple(const sensor_msgs::JointState& j1, const sensor_msgs::JointState& j2,
00116 const float pos_tolerance);
00117
00126 static int equalJointPositions(const sensor_msgs::JointState& j1, const sensor_msgs::JointState& j2,
00127 const float pos_tolerance);
00128
00129
00130 static bool getJointStateAt(int idx, const trajectory_msgs::JointTrajectory& traj, sensor_msgs::JointState& result);
00131
00132
00138 template<typename MJointState>
00139 static bool getPoseFromVirtualJointState(const std::vector<std::string>& joint_names,
00140 const MJointState& virtualJointState,
00141 const std::string& virtualJointName, geometry_msgs::PoseStamped& robotPose);
00142
00149 bool canGetTransform(const std_msgs::Header& p1, const std_msgs::Header& p2, bool latest, bool printError) const;
00154 bool canGetTransform(const std::string& f1, const std::string& f2, const ros::Time& useTime, bool printError) const;
00155
00162 bool waitForTransform(const std_msgs::Header& p1, const std_msgs::Header& p2,
00163 const float& timeout, bool latest, bool printError);
00168 bool waitForTransform(const std::string& f1, const std::string& f2,
00169 const ros::Time& useTime, const float& timeout, bool printError);
00170
00185 int equalPoses(const geometry_msgs::PoseStamped& p1, const geometry_msgs::PoseStamped& p2, float accuracy_pos,
00186 float accuracy_ori, bool useLatestTime, float maxWaitTransform, bool printErrors = true);
00187
00192 int poseDistance(const geometry_msgs::PoseStamped& p1, const geometry_msgs::PoseStamped& p2, float& posDist, float& angleDist,
00193 bool useLatestTime, float maxWaitTransform, bool printErrors);
00194
00200 int relativePose(const geometry_msgs::PoseStamped& origin, const geometry_msgs::PoseStamped& other,
00201 geometry_msgs::Pose& result, bool useLatestTime, float maxWaitTransform, bool printErrors = true);
00202
00211 int transformPose(const geometry_msgs::PoseStamped& p, const std::string& frame_id, geometry_msgs::PoseStamped& result,
00212 float maxWaitTransform, bool printErrors = true);
00213
00219 int getTransform(const std::string& f1, const std::string& f2, geometry_msgs::Pose& result,
00220 const ros::Time& useTime, float maxWaitTransform, bool printErrors = true);
00221
00225 static void applyTransform(const geometry_msgs::Pose& transform, geometry_msgs::Pose& pose);
00226
00230 template<typename GH, typename Res>
00231 static void mapToGoalHandle(const actionlib::SimpleClientGoalState& state, GH& goalHandle,
00232 const Res& res, const std::string& s);
00233
00239 template<typename GH, typename Res>
00240 static void effectOnGoalHandle(const actionlib::SimpleClientGoalState& state, GH& goalHandle,
00241 const Res& res, const std::string& s = "");
00245 template<typename GH>
00246 static void effectOnGoalHandle(const actionlib::SimpleClientGoalState& state, GH& goalHandle);
00247
00248 private:
00249 static int hasVal(const std::string& val, const std::vector<std::string>& vec);
00250
00251
00252 static baselib_binding::recursive_mutex slock;
00253 static ROSFunctionsPtr _singleton;
00254
00255
00256 tf::TransformListener tf_listener;
00257 };
00258
00259 #include <convenience_ros_functions/ROSFunctions.hpp>
00260
00261 }
00262
00263 #endif // CONVENIENCE_ROS_FUNCTIONS_ROSFUNCTIONS_H