0
$\begingroup$

I have the following code for the ros turtlesim:

 #include <ros/ros.h>
#include <std_msgs/String.h> 
#include "geometry_msgs/Twist.h"
#include <stdio.h>     
#include <stdlib.h>     
#include <time.h>       
void disruptcb(geometry_msgs::Twist msg) {
    ros::NodeHandle pubHandle;
    ros::Publisher publisher = pubHandle.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000);
    ros::Rate loop_rate(2);

        double dist1=(rand()%100);
        double dist2=(rand()%100);
        std::cout<<dist1<<std::endl;
        dist1=dist1;
        dist2=dist2; 
        msg.linear.x+=dist1;
        msg.angular.z+=dist2; 
        std::cout<<msg<<std::endl;
        ROS_INFO("hello" );
        publisher.publish(msg);

}
int main(int argc,char** argv){
    srand(time(NULL));
    ros::init(argc,argv, "things_going_wrong");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("/ros_1/cmd_vel",1000,&disruptcb);
    ros::spin(); 
}

the idea behind this code is to introduce a random error to then practice error recovery in my code but this node does not appear to do anything at all. I do know that my other nodes are running but this one just doesn't appear to do anything, it doesn't exit it just hangs. Anybody know how to fix this?

$\endgroup$
2
  • 1
    $\begingroup$ Does CTRL-C close it? If so, it hasn't hung, but it simply has nothing to do, maybe because there is no message from /ros_1/cmd_vel. Nevertheless, one problem is that you create and destroy your publisher on every call. I'm not sure if ROS would actually work ok like this, but in the very least it's inefficient. It's also confusing if you're trying to subscribe to it from commandline etc. $\endgroup$
    – Shahbaz
    Oct 14, 2014 at 9:01
  • $\begingroup$ Also, whenever there is a crash, just use gdb $\endgroup$
    – Shahbaz
    Oct 14, 2014 at 11:48

1 Answer 1

2
$\begingroup$

I have tried your code. When publishing a message on "/ros_1/cmd_vel" your disruptcb will be called.

rostopic pub /ros_1/cmd_vel geometry_msgs/Twist '{linear: {x: 1, y: 2, z: 3}, angular: {x: 1,y: 2,z: 3}}'

But I can't connect to your outcome message "turtle1/cmd_vel". Try to advertise it inside the main function:

ros::Publisher* publisher;
void disruptcb(geometry_msgs::Twist msg) {
    ...
    publisher->publish(msg);
}                   
int main(int argc,char** argv){
    ros::init(argc,argv, "things_going_wrong");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("/ros_1/cmd_vel",1000,&disruptcb);
    publisher = new ros::Publisher(nh.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1000));
    ros::spin();
    delete publisher;
}

Then outcome message will also be observed:

rostopic echo /turtle1/cmd_vel
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.