A small mistake in the Solution of Exercise 2.1

Hi there,

I think there is a small mistake in the code.

while (x < x_limit) {
      x = rosbot.get_position(1);
      rosbot.move_forward(1.0);

The loop condition should be updated finally.

while (x < x_limit) {
      rosbot.move_forward(1.0);
      x = rosbot.get_position(1);

The Solution of Exercise 2.1

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

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");

  RosbotClass rosbot;
  
  rosbot.move_forward(5.0);

  float x_limit = 10.0;
  float x = rosbot.get_position(1);
  ROS_INFO_STREAM("X reached: " << x);
 
  while (x < x_limit) {
      x = rosbot.get_position(1);
      rosbot.move_forward(1.0);

  }
  rosbot.stop_moving();

  return 0;
}

Interesting point. Do you mind explaining why you think it should be so?

we init the x=x_0 by

float x = rosbot.get_position(1);

When entering the while loop, then execution of the first line

x = rosbot.get_position(1);

leads to the same value as before, x=x_0…
Then move the robot…

The second time when checking the condition x<x_limit, the x is still the previous value x_0 instead of x_1

Good point. Thanks for the clarification.

I’ll forward this to the team responsible, for review.

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