Check when a topic is not published with a callback in C++
hi to all,
I have a callback function in my ROS Node that is used to set the velocity on the motors. I noticed that if I stop publishing the velocity topic, the callback function continues to make the motors spin at the previous velocity until I publish again the topic by setting speed to 0.
What is the best solution to check when the topic is not published anymore and automatically set 0 inside the callback?
This is my callback:
void velocity_callback(const roboclaw::RoboclawMotorVelocity &msg) {
int mot1_vel1_sps = msg.mot1_vel1_sps;
int mot2_vel1_sps = msg.mot2_vel1_sps;
int mot1_vel2_sps = msg.mot1_vel2_sps;
int mot2_vel2_sps = msg.mot2_vel2_sps;
if(roboclaw_duty_m1m2(rc, address_2, mot1_vel2_sps, mot2_vel2_sps) != ROBOCLAW_OK )
{
fprintf(stderr, "problem communicating with roboclaw, terminating\n");
}
if(roboclaw_duty_m1m2(rc, address, mot1_vel1_sps, mot2_vel1_sps) != ROBOCLAW_OK )
{
fprintf(stderr, "problem communicating with roboclaw, terminating\n");
}
}
This is how I publish the topic for testing:
rostopic pub -r 5 /cmd_vel_steering roboclaw/RoboclawMotorVelocity "{index0: 0, t1_vel1_sps: 0, mot2_vel1_sps: 20}"
This is how I call the callback in my main():
ros::Subscriber velocity_sub = n.subscribe(std::string("cmd_vel_steering"), 1, &velocity_callback);
If I stop using rostopic, then velocity_callback() continues to make the motors spin with the previous received velocity.
Thank you!
That is by design.
The callback gets triggered when the subscriber receives a message. Meaning if the topic is not being published on, then the callback will not get triggered.
I was already aware about this behavior. The problem is: how can I modify my code in order to be able to set velocity=0 when the callback is not triggered anymore?