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?
/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$gdb
$\endgroup$