Quizz 1 where to write main

Hey everyone!
This is my first project ever and I have an issue.
I still need an intuition where to write the main function of my code. The project is simple just avoid the sphere with the help of laser scan and cmd vel.

I created the subscriber and publisher required and I can move forward and read the laser provided in the right left and center direction.

The only problem that I still have is writing the main function for the following logic:

  1. If the laser reading in front of the robot is higher than one meter (there is no obstacle closer than one meter in front of the robot), the robot will move forward.
  2. If the laser reading in front of the robot is lower than one meter (there is an obstacle closer than one meter in front of the robot), the robot will turn left.
  3. If the laser reading on the right side of the robot is lower than one meter (there is an obstacle closer than one meter on the right side of the robot), the robot will turn left.
  4. If the laser reading on the left side of the robot is lower than one meter (there is an obstacle closer than one meter on the left side of the robot), the robot will turn right.

I need your assistance in guiding me for having an intuition and understanding how to build my package and use the nodes that I created to perform this logic.

Thanks in advance!

Hi @Charbel_Beaini!

I understand the situation. Here are some tips.

Not sure how your code is structured - whether you are using a class (recommended) or the procedure-based approach, but this should work in general.

  • Put the code for controlling the robot in the subscriber callback, where you have access to the laser data, and make sure you can access your publisher from that callback.
    • If using a class, make sure the publisher is an instance variable you can access anywhere in the method of the class.
    • If using the procedural approach, the publisher should be a global variable accessible anywhere in the code.

This is my code right now:

#include “rclcpp/logging.hpp”
#include “rclcpp/rclcpp.hpp”
#include “sensor_msgs/msg/laser_scan.hpp”
#include “geometry_msgs/msg/twist.hpp”
#include

using std::placeholders::_1;
using namespace std::chrono_literals;

class LaserSubscriber : public rclcpp::Node {
public:
LaserSubscriber() : Node(“sub_laser”) {

subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>(
    "scan", 10, std::bind(&LaserSubscriber::laser_callback, this, _1));

publisher_ = this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);

timer_ = this->create_wall_timer(
  500ms, std::bind(&LaserSubscriber::laser_callback, this));

}

void laser_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg) {

//Split laser into 3 equal slices 
 int ranges_len = (msg->angle_max - msg->angle_min) / msg->angle_increment;
 int split_size = ranges_len / 3;

// Split sensor data into three areas and extract smallest distance
float right_distance = *std::min_element(msg->ranges.begin(), msg->ranges.begin()+split_size);
float front_distance = *std::min_element(msg->ranges.begin()+split_size, msg->ranges.begin()+2*split_size);
float left_distance = *std::min_element(msg->ranges.begin()+2*split_size, msg->ranges.begin()+ranges_len);

//Print data 
RCLCPP_INFO(this->get_logger(), "Left: '%f'", left_distance);
RCLCPP_INFO(this->get_logger(), "Middle: '%f'", front_distance);
RCLCPP_INFO(this->get_logger(), "Right: '%f'", right_distance);

//Conditions to publish data accordingly

auto message = geometry_msgs::msg::Twist();
if(front_distance>1){
message.linear.x=0.2;
publisher_->publish(message);
message.linear.z=0.2;
publisher_->publish(message);
}

}

rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr subscription_;
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
};

int main(int argc, char *argv) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared());
rclcpp::shutdown();
return 0;
}

Is it the right way to proceed?
And I am facing this error that I don’t understand:

Looks like the right way to proceed.

The error is with your wall timer - please check the error message closely to identify the line and what the error means.

C++ is not my thing, but I wonder if you need the wall time in the first place. If needed, then it looks like it was not properly set up.

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