ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

I think it's more common to use subscribers instead of threads, as mentioned in the comments. I'll share an example code fragment for your reference.

// global variables definition
double vx;
double vy;
double vth;

// add callback function
void callback_function(const geometry_msgs::Twist::ConstPtr& msg)
{
    vx = msg->linear.x;
    vy = 0.0;
    vth = msg->angular.z;
}

int main(int argc, char** argv){
// ...
    double x = 0.0;
    double y = 0.0;
    double th = 0.0;

    // add subscribe defenition
    ros::Subscriber sub = n.subscribe("twist_topic", 1, callback_function);

    // initialize global variables
    vx = 0.0;
    vy = 0.0;
    vth = 0.0;

    ros::Time current_time, last_time;
// ...
    last_time = current_time;
    ros::spinOnce();  // enable callback
    r.sleep();