Unpredicted behaviour of real robot

is it normal that my robot works fine in simulation but it just goes straight into the wall in rosject?
this is the code:

#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/laser_scan.hpp"
#include "geometry_msgs/msg/twist.hpp"

class DrivingNode : public rclcpp::Node
{
public:
    DrivingNode()
        : Node("Driving_node")
    {
        // create laser subscriber
        laser_sub_ = create_subscription<sensor_msgs::msg::LaserScan>(
            "/scan", 10, std::bind(&DrivingNode::laser_sub_callback, this, std::placeholders::_1));

        // create velocity publisher
        vel_pub_ = create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 10);
        vel_timer_ = create_wall_timer(std::chrono::milliseconds(500),
            std::bind(&DrivingNode::vel_timer_callback, this));
    }

private:
    void laser_sub_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg)
    {
        if (msg != nullptr)
        {
            distance_to_wall_ = msg->ranges[0];  // collect the data from ray represents right side of the robot
            distance_front_ = msg->ranges[msg->ranges.size() / 2];  // collect the data from ray represents front of the robot
        }
    }

    void vel_timer_callback()
    {
        auto twist = std::make_shared<geometry_msgs::msg::Twist>();

        // If the distance to the wall is bigger than 0.3m, you need to make the robot approach the wall a little,
        // by adding some rotational speed to the robot
        if (distance_to_wall_ > 0.3)
        {
            twist->angular.z = -0.2;
        }

        // If the distance to the wall is smaller than 0.2m, you need to move the robot away from the wall,
        // by adding rotational speed in the opposite direction
        if (distance_to_wall_ < 0.2)
        {
            twist->angular.z = 0.2;
        }

        if (distance_front_ < 0.5)
        {
            twist->angular.z = 0.8;
            // twist->linear.x = 0.3;
        }

        // If the distance to the wall is between 0.2m and 0.3m, just keep the robot moving forward
        twist->linear.x = 0.2;
        vel_pub_->publish(*twist);
    }

    rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr laser_sub_;
    rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr vel_pub_;
    rclcpp::TimerBase::SharedPtr vel_timer_;
    float distance_to_wall_{0.0};
    float distance_front_{0.0};
};

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<DrivingNode>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}

same thin happens with python code, both codes work in simulation only

Hi @Hegazi ,

If this is your rosject, then you have to implement the MultiThreadedExecutor for your program.
The reason being - your timer callback and your laser scan callback must work simultaneously (that is, in parallel, together). You can achieve parallel callback execution only with MultiThreadedExecutor.
By default, if you do not state any Executor in your program explicitly, then SingleThreadedExecutor is used. And your timer callback will block your laser scan callback.

Also change vel_pub_->publish(*twist); to vel_pub_->publish(twist);.

This should fix your problem. Let me know if your still have issues.

Regards,
Girish

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.