Unable to access rosbot.get_laser(0)) in C++ micro project

In C++ basics course, micro project( solution provided by course below), we are asked to escape a cafe using laser reading and /cmd topic.

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>

#include <math.h>
#include <string>

using namespace std;

class RosbotMove {
public:
  RosbotClass rosbot;
  void get_out();
  float calc_distance(float x_0, float y_0, float x_1, float y_1);
};

void RosbotMove::get_out() {
//   ROS_INFO_STREAM(rosbot.get_laser(0));
  rosbot.move_forward(1);
  while (rosbot.get_laser(0) > 1.75) {
    ROS_INFO_STREAM("Laser frontal reading: " << rosbot.get_laser(0));
    rosbot.move_forward(1);
  }
  rosbot.turn("clockwise", 3);
  rosbot.move_forward(2);
  rosbot.turn("counterclockwise", 3);

  // Get initial position
  float x_0 = rosbot.get_position(1);
  float y_0 = rosbot.get_position(2);
  float x_1 = x_0;
  float y_1 = y_0;
  float dist = calc_distance(x_0, y_0, x_1, y_1);
  while (dist < 8.00) {
    // Update current position
    x_1 = rosbot.get_position(1);
    y_1 = rosbot.get_position(2);
    dist = calc_distance(x_0, y_0, x_1, y_1);
    ROS_INFO_STREAM("Distance travelled: " << dist);
    // Keep moving
    rosbot.move_forward(1);
  }
  rosbot.turn("clockwise", 3);
  rosbot.move_forward(5);
  ROS_INFO_STREAM("Success!!!");
}

float RosbotMove::calc_distance(float x_0, float y_0, float x_1, float y_1) {
  return sqrt(pow((x_0 - x_1), 2) + pow((x_0 - x_1), 2));
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "Rosbot_move_node");
  RosbotMove rosbot_moves;
  rosbot_moves.get_out();
}


Now, even though we have the line ROS_INFO_STREAM("Laser frontal reading: " << rosbot.get_laser(0));, no data is published into the shell. As you can see here, we are not seeing any output of the line: ROS_INFO_STREAM("Laser frontal reading: " << rosbot.get_laser(0));

If i try to access the laser reading using ROS_INFO_STREAM(rosbot.get_laser(0));, the program crahes saying

try running the rosbot.move() before the get_laser() function.
I found out that the get_laser() function only works after the rosbot has moved.

Also, the get_laser reading might be less than 1.75 try printing it out before the while loop.

Alternatively reset your gazebo and rosbot screen to the top right corner then run the program again. You might have moved the robot before.

1 Like

Thanks for replying.

The solution you mentioned worked, but what’s the logic behind it? why would the laser reading only work after move() function is called?

Things were pretty straight forward in python.

Supposedly the move() function initializes that ability to produce laser readings. This should be an error before a static robot should still be able to read obstacles. In a scenario of dynamic obstacles and static robot.

I agree with you man. Things are much simpler in python.

1 Like

I was hoping to stick with only python, but you need to know how to work with ROS in C++ to understand the c++ files used in ROS Control.

1 Like

and middleware control in general. Just bear with C++.

1 Like

This topic was automatically closed after 20 hours. New replies are no longer allowed.