gyroscope.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 
00008 // publisher of Accelerometer data
00009 ros::Publisher publisher;
00010 std::string tf_prefix;
00011 
00012 void callback_val(const std_msgs::String::ConstPtr & msg) {
00013     std::vector<double> axis_data;
00014     std::string data=msg->data;
00015     // linear parser of data in format x;x;x were x is a float
00016     std::string value=""; // partial data (x)
00017     for(size_t i=0; i<data.length();++i){
00018         if(data[i] == ';'){
00019             // in input data is 0,111 change to 0.111
00020             std::replace(value.begin(),value.end(), ',','.');
00021             axis_data.push_back(std::stof(value));
00022             value.clear();
00023             ++i;
00024         }
00025         value+=data[i];
00026     }
00027     // last element is added
00028     if(!value.empty()) axis_data.push_back(std::stof(value));
00029     if (axis_data.size() > 3){
00030         ROS_ERROR("HAL(VREP): Gyroscope received more than three values(axis) from vrep");
00031         axis_data.resize(3);
00032     }
00033     // creating new message
00034     geometry_msgs::Vector3Stamped msg_out;
00035     std_msgs::Header header; // creating header
00036     header.stamp = ros::Time::now(); // current time of data collection
00037     header.frame_id = tf_prefix + "/gyroSensors/sensor0";
00038     //  fill msg
00039     msg_out.header = move(header);
00040     msg_out.vector.x=axis_data[0];
00041     msg_out.vector.y=axis_data[1];
00042     msg_out.vector.z=axis_data[2];
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, "gyroSensors");
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_GyroSensor", 1000, callback_val);
00072     // creates publisher in format ./sensor0/axis_data
00073     publisher = n.advertise<geometry_msgs::Vector3Stamped>("sensor0/axis_data", 1000);
00074     ROS_INFO("HAL(VREP): Gyroscope node initialized");
00075     
00076     // run event loop
00077     ros::spin();
00078     return 0;
00079 }


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