leftMotor.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/JointState.h>
00003 #include <std_msgs/Float64.h>
00004 #include <string>
00005 
00006 // publisher joint state from vrep to joint state topic in ros
00007 ros::Publisher pub_toRos;
00008 // publisher from ros motor controler to vrep motor controler
00009 ros::Publisher pub_toVrep;
00010 // robot's index in ROS ecosystem (if multiple instances)
00011 std::string tf_prefix;
00012 
00013 void stateCallback(const sensor_msgs::JointState::ConstPtr &msg) {
00014 
00015   sensor_msgs::JointState msg_out;
00016   std_msgs::Header header;         // creating header
00017   header.stamp = ros::Time::now(); // current time of data collection
00018   header.frame_id = tf_prefix + "/base/joint0";
00019   // filling new output msg with data from vrep
00020   msg_out.header = move(header);
00021   msg_out.effort = msg->effort;
00022   msg_out.position = msg->position;
00023   msg_out.velocity = msg->velocity;
00024   // publish float 64 of joint velocity meassured in Vrep
00025   pub_toRos.publish(move(msg_out));
00026 }
00027 
00028 void velCallback(const std_msgs::Float64::ConstPtr &msg) {
00029   // publish float 64 of value velocity of left wheel
00030   pub_toVrep.publish(msg);
00031 }
00032 
00033 
00034 
00035 int main(int argc, char **argv) {
00036   // init node name as accelerometer
00037   ros::init(argc, argv, "leftMotor");
00038   // set relative node namespace
00039   ros::NodeHandle n("~");
00040 
00041   // init parameters
00042   // resolve `tf_prefix` and `vrep_index` in parent namespaces
00043   int vrep_no = 0; // robot's index in vrep (if multiple instances)
00044 
00045   std::string tf_prefix_path;
00046   if (n.searchParam("tf_prefix", tf_prefix_path))
00047   {
00048     n.getParam(tf_prefix_path, tf_prefix);
00049   }
00050 
00051   std::string vrep_index_path;
00052   if (n.searchParam("vrep_index", vrep_index_path))
00053   {
00054     n.getParam(vrep_index_path, vrep_no);
00055   }
00056   
00057   // read msg from vrep topic joint state and publishing it to ROS
00058   ros::Subscriber sub_from_vrep=n.subscribe("/vrep/i" + std::to_string(vrep_no) +
00059                                             "_Pioneer_p3dx_leftMotor/getState",
00060                                         1000, stateCallback);
00061   pub_toRos = n.advertise<sensor_msgs::JointState>("getState", 1000);
00062   ROS_INFO("HAL(VREP): Left motor state msg publisher node initialized");
00063 
00064   // publishing velocity from ROS to vrep
00065   pub_toVrep = n.advertise<std_msgs::Float64>(
00066       "/vrep/i" + std::to_string(vrep_no) + "_Pioneer_p3dx_leftMotor/setVel",
00067       1000);
00068   ros::Subscriber sub_from_ros = n.subscribe("setVel", 1000, velCallback);
00069   ROS_INFO("HAL(VREP): Left motor publisher to vrep: velocity msg initialized");
00070 
00071   // run event loop
00072   ros::spin();
00073   return 0;
00074 }


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