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 ros::Publisher publisher;
00009 std::string tf_prefix;
00010
00011 void callback_val(const std_msgs::String::ConstPtr &msg) {
00012 std::vector<double> axis_data;
00013 std::string data = msg->data;
00014
00015 std::string value = "";
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
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
00033 geometry_msgs::Vector3Stamped msg_out;
00034 std_msgs::Header header;
00035 header.stamp = ros::Time::now();
00036 header.frame_id=tf_prefix + "/accelerometerSensors/sensor0";
00037
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
00044 publisher.publish( move(msg_out));
00045 }
00046
00047 int main(int argc, char **argv) {
00048
00049 ros::init(argc, argv, "accelerometerSensors");
00050
00051 ros::NodeHandle n("~");
00052
00053
00054
00055 int vrep_no = 0;
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
00070 ros::Subscriber sub_val = n.subscribe("/vrep/i" + std::to_string(vrep_no) +
00071 "_Pioneer_p3dx_Accelerometer",
00072 1000, callback_val);
00073
00074 publisher = n.advertise<geometry_msgs::Vector3Stamped>("sensor0/axis_data", 1000);
00075 ROS_INFO("HAL(VREP): Accelerometer node initialized");
00076
00077
00078 ros::spin();
00079 return 0;
00080 }