# Wandering Robot

The wandering robot algorithm is a great way to practice your obstacle avoidance skills. The idea is simple: you program the robot to randomly move without a goal or destination for as long as possible. The program can also be used for exploration or to map out environments.&#x20;

\


The basic algorithm goes as follows:

1. Random Direction: The robot randomly selects a direction to move (e.g., forward, backward, left, right).
2. Movement: The robot moves in the selected direction for a predetermined distance or time.
3. Obstacle Avoidance: If the robot encounters an obstacle, it randomly selects a new direction and tries again.
4. Repeat: The process is repeated indefinitely, creating a seemingly random wandering pattern.

Mapping this out in a flow chart:

<figure><img src="https://content.gitbook.com/content/v8hfOD2FAX4jGYqMD1k9/blobs/RyVcAb4y3lVMOyXHfL4P/image.png" alt=""><figcaption></figcaption></figure>

So let's implement it!

1. First start the program:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfJTNHE0cJQqDAqVAf43QjLK4183ucTHHWSu6xgf6nIP_khLrlHRIaIeKHcRpD5xCSLibIAZ2Kt7IpT-n3J4zEWeKvH0icFT4fQuAP1MCfIQYfa6Gk3ayxaWG5rRfeSVw52aNz7kHcxqYhUSkOrZ2lPjRO2?key=yoSCOrfTueqmM4D5vyxz5Q)

Don’t forget to reverse the direction of the right side motors when initializing. This will make programming a lot easier!

\


2. Next implement a random direction function.

To keep things simple, I will have the robot turn for a random amount of time before stopping. This will effectively spin the robot to a random orientation.

\
\


![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcmTaHrNiYn0LDPYghuNnJ5jTM0NJ8R4ZRN3i2IarjUTsmp8h7bDbrv9XmTIxPTUNBGZWv-mAPfr4jRNm4bAsMVkB_F4RSkizWcfP5D2PEJbqxsFa_RqwC30-dg19JiPn4mlvcvGPg60emqbK-rwxFKKzEU?key=yoSCOrfTueqmM4D5vyxz5Q)

&#x20;By having the right motors reverse when the left motors go forward, the robot can rotate in place. This is important as you don’t want the robot to get stuck while selecting a direction to proceed. The collision detection step isn’t active yet so getting stuck now will stop the robot.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcT4l8s_0mzpnzxT64u3LY7qj47FpiL-cjoFCUA1rOMJgnq1uhlbGCtsYXoO5sAplPZGwyA2kJrVazUaDin-6sf7fU5XMLnOayCTD9pTGJliHaZAst9LKH32Asa88pJJjIjf9ufwnSFfCBb2UYxhE-dlFg?key=yoSCOrfTueqmM4D5vyxz5Q)

Next, have the program sleep for a random amount of time. I choose between 0 and 2 seconds but this range can be anything.

\


3. Obstacle Detection

There are multiple different ways of implementing this and you are highly encouraged to explore different methods. One of the easiest is using the distance sensor. Since the robot will be moving forwards, most of the obstacles will be in front of the robot so a distance sensor works well in most cases.&#x20;

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXetnPRK7J4lDmGI60tt59eK7zlu3W06VL-I-FpMj_J9WHNrdI3hiK8NnLUyy6fkUKPF8DeGhBII8V4E-19_xJyodbttO7clwV8UW7dEiCfuksa7GY-cCnQvBZrlUxJdnuyq26d4uG2Jwa4SDOw9Xxas-wR9?key=yoSCOrfTueqmM4D5vyxz5Q)

Here I use an if do else logic block. You know collision is imminent if there is an object present within 20 cm of the robot. The 20 cm gap is necessary as you need to give the robot space to turn and avoid the obstacle.

\


If there is no obstacle, move the robot forward.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfQGsrDVV49A-oSpsOrxQadIvNx99crznRKxjlHbn_6D16D5hIEkrbWYZsYvDVx8lFSi7PxVFSuxSzCuEfpVsEA_LSTELyHUl6IBTCq7tJAlpjss6R_arfm2FqfhEAata-aRskXx45N012ZEXl2aRKux6jn?key=yoSCOrfTueqmM4D5vyxz5Q)

If there is an obstacle, we can use the random turn code from earlier as an obstacle avoidance method. Afterall, there the random turn doesn’t avoid the obstacle, it will be quickly detected and the robot will turn until no obstacle is found.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdroY_v8ir8IDY1iSb0_oQH1gj_Gi7ndRh4euFi11VR4XuWasllCV4IrrZMeyVEQAHCrz78sNp6ZxckZnmy4yyjT0KSiajW378fzxF1XacRUf3H2c7_xonUCicH20_nm96VT-bdWjhr3GHF_lRAHKaPD0I?key=yoSCOrfTueqmM4D5vyxz5Q)

Now just insert the Detect Obstacle function into the main program and run it on a loop. Voila! You have a working wandering robot code.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcuUAgLGoc0sYz6Kx52Em2DM5KUcdJpcO46MgcMTCge0VsCpBlIllF2UNJXM0Oo3FT1XlyTQqVZII3N0P-2r_YiBrZgDiqqBwDzagZua_Txvz4CWY-YaSZiLvp3hSu3rHyWqF-qkDcc9bLpbkYPDzad9qQP?key=yoSCOrfTueqmM4D5vyxz5Q)

This is just one out of many versions of this code. One variant has the robot repeatedly turn left and right to try to widen the detection range of the distance sensor. Try to challenge yourself to create something better!

\
