Introduction: Coding Randomness
One of the things that I find interesting playing around with robots is the concept of using a highly structured environment of a computer program to try and simulate random behavior. How can you program something that will truly act randomly? Of course, in the movie I Robot, Dr. Lanning used dual interacting processors to create something akin to random behavior. I only had one simple robot, not Sonny, powered by a single Arduino, not two positronic brains, but I gave it a shot anyway, and here's what I came up with! Hope you find it interesting, and if you do, please vote for my project.
The programs to follow were written for the robot I have in this Instructable - https://www.instructables.com/id/R3-Rolling-Red-Robot/ .
Step 1: The Basic Program
A file for the basic Arduino code that ran the robot originally is attached here. The robot has three ultrasonic sensors, one facing front and one each angled 45 degrees to the left and the right. If it senses an obstacle in front, it stops, turns the front wheel to the left, backs up for 3 seconds, turns the front wheel back straight ahead, and moves forward. If it senses something to the right, it turns to the left until it no longer senses anything on the right; and if it senses something to the left, it turns to the right until it no longer senses anything on the left.
Now imagine the robot is in a box. Since it always reacts the same to each encounter in which it senses an obstacle, the robot will fall into a pattern of repeating it's same motions over and over. And in fact, when I just let it go to run around my living room, it always seems to end up in a spot where it's doing exactly that - repeating the same pattern of movement. To quote Homer Simpson, "Boring!"
Step 2: One Random Action
So now let's throw a little unpredictability into the program. We can use a random number generator and a formula for determining if the number chosen is odd or even to virtually toss a coin. Then using if-else statements, you can determine what happens if the coin is heads or tails. Now I've read that computer based random number generators aren't really completely random. So while we may not really be generating random behavior, the robots behavior will change in ways that can't be predicted by observation, which is close enough to random for the purposes of this experiment.
In this version of the code, I inserted one random action. When the robot encounters an obstacle straight ahead, if the random number generated is even it will back up to the left. If the random number generated is odd, it will back up to the right. So each time the robot encounters an obstacle straight ahead, there should be a 50% chance of it either backing up to the left or to the right. Enough randomness to make it a bit more interesting!
Step 3: Two Random Actions
What if we want to add even more complexity to the robots seemingly random behavior? You can do three basic things. One is add some action of the robot that does not have anything to do with the basic movements outlined in the original code. For example, the robot could just just spin in a circle at intervals determined strictly by the random number generator coming up with a particular number. Or you could add another random action independent of the one we added earlier. For example, if the robot encounters an obstacle to the left, perhaps sometimes it acts as it would in the original code and move to the right, and perhaps other times it could turn completely around and move in the opposite direction. Or, lastly, you could add a random action that is interlaced with the first random action such as outlined in the last step, which could be independent of that action or always predictably linked to that action. This last possibility is what I have done here.
Now, when the robot encounters an obstacle straight ahead, it will first determine which way to turn it's wheel based upon whether the random number generated is odd or even as explained in the last step. To add complexity you could program the robot to run the motor backward for a particular length of time if it was turned left, and another particular length of time if it was turned right, thereby turning the robot to different degrees based on the direction of the turn. Or, you could do what I have done here, and make those two variables independent of each other. In other words, a random number is generated that will determine if the wheel is turned left or right, and another random number is generated to determine if the robot will back through that turn for 2.5 seconds or 5 seconds. This gives us 4 variations of how the robot will behave when it backs up after encountering an obstacle straight ahead.
Step 4: And So On, and So On...
You could do this for more and more actions, in any of the three categories of actions discussed, adding an ever increasing level of seemingly random behavior to your robot, or perhaps even behavior that seems intelligent - sometimes it can be hard to tell the difference between the two! There are lot's of possibilities. And if my Instructable has helped you to understand what could be possible just a bit better, then please do vote for me. Thanks!
We have a be nice policy.
Please be positive and constructive.