class MoveTurtlebot {
private:
ros::Publisher pub;
ros::NodeHandle nhMove;
public:
MoveTurtlebot() {
pub = nhMove.advertise<geometry_msgs::Twist>("/cmd/vel", 1);
};
void laser_callback(const sensor_msgs::LaserScanConstPtr &msg) {
ROS_INFO("I am inside laser_callback");
ros::Rate rate(1);
geometry_msgs::Twist move;
move.linear.x = 0.2;
move.angular.z = 0.2;
pub.publish(move);
rate.sleep();
}
};
int main(int argc, char **argv) {
ros::init(argc, argv, "turtlebot_subscriber_node");
ros::NodeHandle nh;
ros::Subscriber sub;
MoveTurtlebot moveTurtlebot;
sub = nh.subscribe("/kobuki/laser/scan", 1, &MoveTurtlebot::laser_callback,
&moveTurtlebot);
ros::spin();
return 0;
}
This is the output when I run it:
[ INFO] [1630232440.720399695, 13778.072000000]: I am inside laser_callback
[ INFO] [1630232441.726862869, 13779.073000000]: I am inside laser_callback
[ INFO] [1630232442.745110063, 13780.073000000]: I am inside laser_callback
[ INFO] [1630232443.758681972, 13781.073000000]: I am inside laser_callback
[ INFO] [1630232444.771686571, 13782.073000000]: I am inside laser_callback
[ INFO] [1630232445.805162872, 13783.075000000]: I am inside laser_callback
I really do not understand what I did wrong here, when I try and compile this, the laser_callback
is working just fine and I think I have publish inside the correct topic but the thing is my robot is not moving at all. Not even an inch!
So I tried checking if it is not publishing correctly by using the following command:
rostopic echo -n1 /cmd/vel
Here is the output:
linear:
x: 0.2
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.2
So, I am publishing it correctly and I dont know what I did wrong in this case.