Unit2_exercise loops program running forever

No matter what conditions I give, the robot move backwards till the wall and the program is running forever. Any help?

Hi @aasyujak23 ,

Welcome to this Community!

A simpler method is to press Ctrl+C on the terminal that runs the program. If this fails, you can do two things.

  1. Reset your terminal that runs the program by pressing the red X button on the terminal tab.
  2. Reload the robot simulation by pressing the second button on the top-right of the simulation screen. The button with one arrow shown as a circle. [If this fails you can click on the third button with two circular arrows].

This should cut off the program from running forever.

Regards,
Girish

1 Like

Hi Girish,

Thanks for your answer!
I however, ultimately terminated the program. But my main concern is no matter what values I give for X_limit, the robot tends to move all the way backwards till the wall and it continues further as well leaving no end for the program. Is there any error with the exercise itself or is it the solution?

Hi @aasyujak23 ,

I am not sure if there is any problem with the solution. I do not usually look into the solutions unless I am really stuck or cannot understand the concept.

Maybe you can post your solution and I can have a look at it and provide you a solution.

Regards,
Girish

same issue for me also. I attaching my code below. I didn’t find any error. Please let me know if there are any error in my code. If else, what would be the reason?

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

using namespace std;

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

  RosbotClass rosbot;
  int s;
  cout<<"Select a seconds for simulation from 1 to 10 :";
  cin>>s;
  rosbot.move_forward(s);
  

  float x_0 = rosbot.get_position(1);
  ROS_INFO_STREAM("X reached: " << x_0);

  int x_lim;
  cout<<"Select a number for setting the limit from 1 to 7 :";
  cin>>x_lim;

  while (x_0 < x_lim)
  { 
    float x_0 = rosbot.get_position(1);
    rosbot.move_forward(1);
    //ROS_INFO_STREAM("X reached: " << x_0);
  }
  rosbot.stop_moving();
  float x_01 = rosbot.get_position(1);
  ROS_INFO_STREAM("Robot Stopped becuase,X reached: " << x_01 <<"m");
  return 0;
  }

Hi @tomkavuvilayil ,

Welcome to this Community!

If the robot is moving backwards for you also with the rosbot.move_forward() function call implemented, then I believe that there might be an error in the RosbotClass itself.
It could be a simple copy-paste error in the move_forward() function, where the values are same as move_backward() function (if there is one).

Check the move_...() functions in that class.
You should see something like linear.x and angular.z values.
For move_forward(), linear.x will be positive (> 0.0) and angular.z will be 0.0.
For move_forward(), linear.x will be negative (< 0.0) and angular.z will be 0.0.
If above (2 lines) is the case in the RosbotClass then there is no problem in the provided program.
Otherwise, please inform here and someone can fix that for you.

Regards,
Girish