Leçon 1, Chapitre 1
En cours

Robot intelligent : Contourner les obstacles – Logique programmation

Yann KIDSHAKER 18 mars 2025

Logic

Time to decode the logic!

The ultrasonic distance sensor will check how far the object nearest to the robot is in every direction with the help of the servo motor and then decide whether it is safe to move ahead or it should take a detour. Let’s dig a bit deeper and start from the very beginning:

The robot has just been powered up. But first, we need something that controls the time that the sensor takes to turn right or left, because WE cannot control that. Therefore, we create a variable DelayTime and initialise it. This variable makes sure that the robot turns only as much as is required in the given direction; not any less, not anymore.

  • Now, the robot begins its task by checking if there is any obstacle in front of the robot at a distance less than, or equal to 20 cm. If no, then the robot will continue moving forward. But if an obstacle is present, then it will stop and check other directions.Move forwardCover distance of 20cm
  • Let’s first consider the case where the sensor points in the 45° direction; this means that the servo motor angle is set to 45°.  The sensor will check whether there is any obstacle at a distance less than or equal to 50 cm in that direction. If there is no obstacle present, the robot will then turn 45° towards its right and then continue moving forward; otherwise, it will check a different direction. servo will be set at 45move forward at 45
  • Next, the robot checks the 135° direction. If the coast is clear, the robot will, this time, turn 45° towards its left. The reason behind turning right in the first case, and left in the second is as follows:check at 135moves forward at 135

According to the servo motor’s configuration, 90° means facing forward. Therefore, 45° is in the right, and 135° in the left, which is nothing but 45°  in the left, from the forward position.servo_angle

  • Let’s get back to the sensor now. If it senses the presence of obstacles in the 135° direction well, it must check yet another direction; this time, it will check the direction. The procedure remains the same. If no obstacle present, then the robot will turn right; yes, right, because it is to the right of 45°.check at 0turn right
  • If the coast isn’t clear in the  direction, it will check the remaining direction, that is in the 180° direction; the same procedure this time as well.check at 180turns left

There is, however, one feature of interest: in the cases of and 180°, instead of turning for a time equal to DelayTime, like in the cases of 45° and 135°, the robot turns for a time equal to twice the DelayTime. This is so because in the cases of 45° and 135°, the difference between the forward position, i.e. 90° and the other two directions is 45°. But, in the cases of and 180°, the difference is 90°. Therefore, twice the time.

  • Now, what if none of the directions are free of obstacles? The robot will have no choice other than taking a reverse. And that is exactly what is does.reversetakes turncontinues in opposite direction

Flow Chart

Below is the complete flowchart:

flowchart

That was all that you need to know be able to write the script, which is, like before, the next part. Good luck!