How to update the new values of angular and linear in order to make the robot move accordingly

I am in topics_quiz in unit . idk how to continue. How can i pass the new updated members of Vector3 in order to move the robot. ? This is the code of src:

#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
#include <geometry_msgs/Twist.h>
//float32 linX , angZ;
void counterCallBack(const sensor_msgs::LaserScan::ConstPtr& msg){
    ROS_INFO("the distance to an obstacle is : %d",msg->ranges[360]);
    if (msg->ranges[360]> 1){
            linear.x=0.1;
            angular.z=0.0;
    }
    if (msg->ranges[360]<1){
            linear.x=0.0;
            angular.z=0.2;
    }
    if(msg->ranges[0]<1){
            linear.x=0.0;
            angular.z=0.2;
    }
    if (msg->ranges[719]<1){
            linear.x=0.0;
            angular.z=-0.2;
    }

}
int main(int argc,char** argv){
        ros::init(argc,argv,"topics_quiz_node");
        ros::NodeHandle nh;
        ros::Subscriber sub= nh1.subscribe("kobuki/laser/scan",1000,counterCallBack);
        ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("cmd_vel",1000);
        ROS_INFO("hedha msg men 3and node");

        return 0;
}

Hi @mouidsakka01 ,

You are updating the linear and angular speeds in the callback function, this is fine.
But you are not publishing the twist message onto the /cmd_vel topic!
Also, you have not declared a twist message variable that you can modify before publishing it.

Add these changes to your code, then everything should work fine.

Regards,
Girish

Thank you for your answer!
I have just updated script.cpp :

#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
#include <geometry_msgs/Twist.h>
//float32 linX , angZ;
void counterCallBack(const sensor_msgs::LaserScan::ConstPtr& msg){
ROS_INFO(“the distance to an obstacle is : %d”,msg->ranges[360]);
if (msg->ranges[360]> 1){
linear.x=0.1;
angular.z=0.0;
}
if (msg->ranges[360]<1){
linear.x=0.0;
angular.z=0.2;
}
if(msg->ranges[0]<1){
linear.x=0.0;
angular.z=0.2;
}
if (msg->ranges[719]<1){
linear.x=0.0;
angular.z=-0.2;
}

}
int main(int argc,char** argv){
ros::init(argc,argv,“topics_quiz_node”);
ros::NodeHandle nh;
ros::NodeHandle nh1;
ros::Subscriber sub= nh.subscribe(“kobuki/laser/scan”,1000,counterCallBack);
ros::Publisher pub = nh1.advertise<geometry_msgs::Twist>(“/cmd_vel”,1000);
ROS_INFO(“hedha msg men 3and node”);

    return 0;
}

But I get the following errors:
/home/user/catkin_ws/src/topics_quiz/src/script.cpp:8:6: error: ‘linear’ was not declared in this scope;did you mean ‘linkat’?
8 | linear.x=0.1;
| ^~~~~~
| linkat
/home/user/catkin_ws/src/topics_quiz/src/script.cpp:9:6: error: ‘angular’ was not declared in this scope
9 | angular.z=0.0;
| ^~~~~~~
/home/user/catkin_ws/src/topics_quiz/src/script.cpp:12:6: error: ‘linear’ was not declared in this scope; did you mean ‘linkat’?
12 | linear.x=0.0;
| ^~~~~~
| linkat
/home/user/catkin_ws/src/topics_quiz/src/script.cpp:13:6: error: ‘angular’ was not declared in this scope
13 | angular.z=0.2;
| ^~~~~~~
/home/user/catkin_ws/src/topics_quiz/src/script.cpp:16:6: error: ‘linear’ was not declared in this scope; did you mean ‘linkat’?
16 | linear.x=0.0;
| ^~~~~~
| linkat
/home/user/catkin_ws/src/topics_quiz/src/script.cpp:17:6: error: ‘angular’ was not declared in this scope
17 | angular.z=0.2;

Hi @mouidsakka01 ,

It is not surprising that you are getting those errors.

It is because:

  1. You have not defined / declared a variable to hold your Twist message for /cmd_vel topic.
  2. You are not publishing the modified speed values inside your callback as Twist messages into your /cmd_vel publisher.

Look into your own code, where have you declared linear and angular variables? Those variables are just “hanging in there” in your code! Fix this, you will be fine!

Regards,
Girish