ROSFunctions.hpp
Go to the documentation of this file.
00001 
00002 template<typename ROSMessage>
00003 bool ROSFunctions::saveToFile(const ROSMessage& msg, const std::string& filename, bool asBinary)
00004 {
00005 
00006     std::ios_base::openmode mode;
00007     if (asBinary) mode = std::ios::out | std::ios::binary;
00008     else mode = std::ios::out;
00009 
00010     std::ofstream ofs(filename.c_str(), mode);
00011 
00012     if (!ofs.is_open())
00013     {
00014         ROS_ERROR("File %s cannot be opened.", filename.c_str());
00015         return false;
00016     }
00017 
00018     if (asBinary)
00019     {
00020         uint32_t serial_size = ros::serialization::serializationLength(msg);
00021         boost::shared_array<uint8_t> obuffer(new uint8_t[serial_size]);
00022         ros::serialization::OStream ostream(obuffer.get(), serial_size);
00023         ros::serialization::serialize(ostream, msg);
00024         ofs.write((char*) obuffer.get(), serial_size);
00025     }
00026     else
00027     {
00028         ofs<<msg; 
00029     }
00030 
00031     ofs.close();
00032     return true;
00033 }
00034 
00035 template<typename ROSMessage>
00036 bool ROSFunctions::readFromFile(const std::string& filename, ROSMessage& msg, bool isBinary)
00037 {
00038 
00039     std::ios_base::openmode mode;
00040     if (isBinary) mode = std::ios::in | std::ios::binary;
00041     else mode = std::ios::in;
00042 
00043     std::ifstream ifs(filename.c_str(), mode);
00044 
00045     if (!ifs.is_open())
00046     {
00047         ROS_ERROR("File %s cannot be opened.", filename.c_str());
00048         return false;
00049     }
00050 
00051     ifs.seekg(0, std::ios::end);
00052     std::streampos end = ifs.tellg();
00053     ifs.seekg(0, std::ios::beg);
00054     std::streampos begin = ifs.tellg();
00055 
00056     uint32_t file_size = end - begin;
00057 
00058     boost::shared_array<uint8_t> ibuffer(new uint8_t[file_size]);
00059 
00060     ifs.read((char*) ibuffer.get(), file_size);
00061 
00062     ros::serialization::IStream istream(ibuffer.get(), file_size);
00063 
00064     ros::serialization::deserialize(istream, msg);
00065 
00066     ifs.close();
00067 
00068     return true;
00069 }
00070 
00071 
00072 template<typename ROSMessage>
00073 bool ROSFunctions::saveToBagFile(const std::vector<ROSMessage>& msgs, const std::string& filename)
00074 {
00075     rosbag::Bag bag(filename, rosbag::bagmode::Write);
00076     for (typename std::vector<ROSMessage>::const_iterator m = msgs.begin(); m != msgs.end(); ++m)
00077     {
00078         bag.write("filesave", ros::Time::now(), *m);
00079     }
00080     bag.close();
00081     return true;
00082 }
00083 
00084 template<typename ROSMessage>
00085 bool ROSFunctions::readFromBagFile(const std::string& filename, std::vector<ROSMessage>& msgs)
00086 {
00087     rosbag::Bag bag(filename, rosbag::bagmode::Read);
00088     rosbag::View view(bag, rosbag::TopicQuery("filesave"));
00089     BOOST_FOREACH(rosbag::MessageInstance const m, view)
00090     {
00091         typename ROSMessage::ConstPtr msg = m.instantiate<ROSMessage>();
00092         if (msg != NULL)
00093         {
00094             msgs.push_back(*msg);
00095         }
00096     }
00097     bag.close();
00098     return true;
00099 }
00100 
00101 template<typename MJointState>
00102 bool ROSFunctions::getPoseFromVirtualJointState(const std::vector<std::string>& joint_names, const MJointState& virtualJointState,
00103         const std::string& virtualJointName, geometry_msgs::PoseStamped& robotPose)
00104 {
00105     if (virtualJointName.empty()) return false;
00106 
00107     std::vector<std::string>::const_iterator it = std::find(joint_names.begin(), joint_names.end(), virtualJointName);
00108     if (it == joint_names.end()) return false;
00109 
00110     int idx = (it - joint_names.begin());
00111     geometry_msgs::Transform t = virtualJointState.transforms[idx];
00112     robotPose.pose.position.x = t.translation.x;
00113     robotPose.pose.position.y = t.translation.y;
00114     robotPose.pose.position.z = t.translation.z;
00115     robotPose.pose.orientation = t.rotation;
00116     return true;
00117 }
00118 
00119 template<typename GH, typename Res>
00120 void ROSFunctions::mapToGoalHandle(const actionlib::SimpleClientGoalState& state, GH& goalHandle,
00121                                    const Res& res, const std::string& s)
00122 {
00123     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00124     {
00125         goalHandle.setSucceeded(res, s);
00126     }
00127     else if (actionlib::SimpleClientGoalState::REJECTED)
00128     {
00129         goalHandle.setRejected(res, s);
00130     }
00131     else if ((state == actionlib::SimpleClientGoalState::RECALLED) ||
00132              (state == actionlib::SimpleClientGoalState::PREEMPTED))
00133     {
00134         goalHandle.setCanceled(res, s);
00135     }
00136     else if ((state == actionlib::SimpleClientGoalState::ACTIVE) ||
00137              (state == actionlib::SimpleClientGoalState::PENDING))
00138     {
00139         goalHandle.setAccepted();
00140     }
00141     else if ((state == actionlib::SimpleClientGoalState::LOST) ||
00142              (state == actionlib::SimpleClientGoalState::ABORTED))
00143     {
00144         goalHandle.setAborted(res, s);
00145     }
00146     else
00147     {
00148         ROS_ERROR("ROSFunctions: Unknown goal handle");
00149     }
00150 }
00151 
00152 template<typename GH, typename Res>
00153 void ROSFunctions::effectOnGoalHandle(const actionlib::SimpleClientGoalState& state, GH& goalHandle,
00154                                       const Res& res, const std::string& s)
00155 {
00156     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00157     {
00158         goalHandle.setSucceeded(res, s);
00159     }
00160     else if (actionlib::SimpleClientGoalState::REJECTED)
00161     {
00162         goalHandle.setAborted(res, s);
00163     }
00164     else if ((state == actionlib::SimpleClientGoalState::RECALLED) ||
00165              (state == actionlib::SimpleClientGoalState::PREEMPTED))
00166     {
00167         goalHandle.setCanceled(res, s);
00168     }
00169     else if ((state == actionlib::SimpleClientGoalState::ACTIVE) ||
00170              (state == actionlib::SimpleClientGoalState::PENDING))
00171     {
00172         goalHandle.setAccepted();
00173     }
00174     else if ((state == actionlib::SimpleClientGoalState::LOST) ||
00175              (state == actionlib::SimpleClientGoalState::ABORTED))
00176     {
00177         goalHandle.setAborted(res, s);
00178     }
00179     else
00180     {
00181         ROS_ERROR("ROSFunctions: Unknown goal handle");
00182     }
00183 }
00184 
00185 template<typename GH>
00186 void ROSFunctions::effectOnGoalHandle(const actionlib::SimpleClientGoalState& state, GH& goalHandle)
00187 {
00188     if (state == actionlib::SimpleClientGoalState::SUCCEEDED)
00189     {
00190         goalHandle.setSucceeded();
00191     }
00192     else if (actionlib::SimpleClientGoalState::REJECTED)
00193     {
00194         goalHandle.setAborted();
00195     }
00196     else if ((state == actionlib::SimpleClientGoalState::RECALLED) ||
00197              (state == actionlib::SimpleClientGoalState::PREEMPTED))
00198     {
00199         goalHandle.setCanceled();
00200     }
00201     else if ((state == actionlib::SimpleClientGoalState::ACTIVE) ||
00202              (state == actionlib::SimpleClientGoalState::PENDING))
00203     {
00204         goalHandle.setAccepted();
00205     }
00206     else if ((state == actionlib::SimpleClientGoalState::LOST) ||
00207              (state == actionlib::SimpleClientGoalState::ABORTED))
00208     {
00209         goalHandle.setAborted();
00210     }
00211     else
00212     {
00213         ROS_ERROR("ROSFunctions: Unknown goal handle");
00214     }
00215 }
00216 


convenience_ros_functions
Author(s): Jennifer Buehler
autogenerated on Sat Mar 2 2019 03:21:42