# ros node does not seem to do anything

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::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?

• 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. – Shahbaz Oct 14 '14 at 9:01
• Also, whenever there is a crash, just use gdb – Shahbaz Oct 14 '14 at 11:48

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);

rostopic echo /turtle1/cmd_vel