Rosject Part 2, service server + subscriber

Hi @jmledran ,

There was no way I could have known that, because you did not post that code here. Glad you found it yourself and got it working!

Simple, use a while loop with the correct condition. I have finished this course and got the certificate - I used a while loop to get this working.

From my experience, I can tell you that your robot will not stop rotating. Here is the simple reason why.
The moment you publish a cmd_vel message with linear.x = 0.0 and angular.z = 0.2, the robot will keep doing that endlessly. So if you put that inside your while loop, until you stop the robot (by sending linear.x = 0 and angular.z = 0), your robot is going to keep spinning, even after it overshoots the point where it has to stop spinning.

The best way to get rid of this is to add stop_robot() method (just set all values of cmd_vel message to 0.0).
So you will turn your while loop like this:

while (frontal_scan_range != smallest_range):
    # make the robot spin
    self.publish_cmd_vel_message(linear_x=0.0, angular_z=0.2)
    # wait for the robot to spin a little
    self.rate.sleep()
    # stop the robot
    self.publish_cmd_vel_message(linear_x=0.0, angular_z=0.0)
    # get the frontal range reading again
    self.get_frontal_scan_range()

So this way, you are:

  1. making the robot spin for a short while
  2. stop the robot
  3. read the sensor values again to update the while loop condition

Another very important thing to note:
You MUST use value round off.
You will NEVER be able to match a value like 3.123456789123456.
But you CAN match a value like 3.123.
Too much presicion = Too much (unnecessary) computation -----> Waste of time and gives you no results.
(Remember that laser scan range is in meters, which means that at 3rd decimal place of precision you are at millimetres. Anything more than a millimetre precision is completely useless, meaning, 4 decimal places and above are just waste of time and computation [because you are working with robots and not atoms or molecules to require that precision]).

Let me know if you need more help. Hope I clarified your doubts.

Regards,
Girish

1 Like