accelerometer.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <std_msgs/String.h>
00003 #include <geometry_msgs/Vector3Stamped.h>
00004 #include <string>
00005 #include <vector>
00006 
00007 // publisher of Accelerometer data
00008 ros::Publisher publisher;
00009 std::string tf_prefix;  // robot's index in ROS ecosystem (if multiple instances)
00010 
00011 void callback_val(const std_msgs::String::ConstPtr &msg) {
00012   std::vector<double> axis_data;
00013   std::string data = msg->data;
00014   // linear parser of data in format x;x;x were x is a float
00015   std::string value = ""; // partial data (x)
00016   for (size_t i = 0; i < data.length(); ++i) {
00017     if (data[i] == ';') {
00018       axis_data.push_back(std::stof(value));
00019       value.clear();
00020       ++i;
00021     }
00022     value += data[i];
00023   }
00024   // last element is added
00025   if (!value.empty())
00026     axis_data.push_back(std::stof(value));
00027   if (axis_data.size() > 3) {
00028     ROS_ERROR("HAL(VREP): Accelerometer received more than three values(axis) "
00029               "from vrep");
00030     axis_data.resize(3);
00031   }
00032   // creating new message
00033   geometry_msgs::Vector3Stamped msg_out;
00034   std_msgs::Header header; // creating header
00035   header.stamp = ros::Time::now(); // current time of data collection
00036   header.frame_id=tf_prefix + "/accelerometerSensors/sensor0";
00037   //  fill msg
00038   msg_out.header = move(header);
00039   msg_out.vector.x=axis_data[0];
00040   msg_out.vector.y=axis_data[1];
00041   msg_out.vector.z=axis_data[2];
00042   
00043   // publish vector of floats. One float for each axis X,Y,Z
00044   publisher.publish( move(msg_out)); 
00045 }
00046 
00047 int main(int argc, char **argv) {
00048   // init node name as accelerometer
00049   ros::init(argc, argv, "accelerometerSensors");
00050   // set relative node namespace
00051   ros::NodeHandle n("~");
00052 
00053   // init parameters
00054   // resolve `tf_prefix` and `vrep_index` in parent namespaces
00055   int vrep_no = 0; // robot's index in vrep (if multiple instances)
00056 
00057   std::string tf_prefix_path;
00058   if (n.searchParam("tf_prefix", tf_prefix_path))
00059   {
00060     n.getParam(tf_prefix_path, tf_prefix);
00061   }
00062 
00063   std::string vrep_index_path;
00064   if (n.searchParam("vrep_index", vrep_index_path))
00065   {
00066     n.getParam(vrep_index_path, vrep_no);
00067   }
00068 
00069   // read msg from vrep topic
00070   ros::Subscriber sub_val = n.subscribe("/vrep/i" + std::to_string(vrep_no) +
00071                                             "_Pioneer_p3dx_Accelerometer",
00072                                         1000, callback_val);
00073   // creates publisher in format ./sensor0/axis_data
00074   publisher = n.advertise<geometry_msgs::Vector3Stamped>("sensor0/axis_data", 1000);
00075   ROS_INFO("HAL(VREP): Accelerometer node initialized");
00076 
00077   // run event loop
00078   ros::spin();
00079   return 0;
00080 }


p3dx_hal_vrep
Author(s):
autogenerated on Sat Jun 8 2019 20:22:32