c++ - Passing a variable between callback and main -


i'm using ros c++ , after receiving data topic in void callback(), need pass data variable in int main(). i've found out far can using boost shared pointer , should use "class in program callback member function". part of code far:

double pos_z; void callback(gazebo_msgs::linkstates msgs_ls) {    double pos_z = msgs_ls.pose[43].position.z    pos_z = pos_z + 1; } int main(int argc, char **argv) {    ros::init(...);    ros::nodehandle n;    ros::subscriber ls_sub = n.subscribe("/gazebo/link_states", 10, callback);    ros::serviceclient sls_client = n.serviceclient<gazebo_msgs::setlinkstate>("/gazebo/set_link_state");    gazebo_msgs::setlinkstate setlinkstate;    while (ros::ok))    {       setlinkstate.request.link_state.position.z = pos_z;       sls_client.call(setlinkstate);   } 

about shared pointer: boost::shared_ptr<double> a_ptr(&a, noop_delete auto noop_delete = [](double *){} don't understand how implement this. mentioned class callback member function unclear me. examples show implementations using c, , don't know if can use that. thank help.

the advice given :

use "class in program callback member function"

you :

class supercoolrobot { public:   supercoolrobot ():pos_z(0) {     // note : provide callback (a member) + state (this)     ls_sub = n.subscribe("/gazebo/link_states", 10, supercoolrobot::link_state_callback, this);   }    void link_state_callback(gazebo_msgs::linkstates msgs_ls) {     pos_z = msgs_ls.pose[43].position.z + 1;   }    void run() {     gazebo_msgs::setlinkstate setlinkstate;     while (ros::ok))     {       setlinkstate.request.link_state.position.z = pos_z;       sls_client.call(setlinkstate);     }   } protected:   // state here   ros::nodehandle n;   ros::subscriber ls_sub;   double pos_z; }; 

then main :

int main(int argc, char **argv) {   supercoolrobot robot;   robot.run(); } 

note don't know ros threw understood code. ajust of course.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -