BrainBot Autonomous

BrainBot Autonomous

Self-driving cars and robots don’t actually drive by themselves without many lines of code and sensors. You can create a simple self-driving BrainBot that will follow a line. This is one of the first steps in understanding how AI (Artificial Intelligence) works.

Prerequisite

This assumes you’ve already completed all of the other BrainBot lessons, understand basic coding principles, and are ready to give the BrainBot some ‘Brains’.

Remember you’ll also need to add the BrainBot NuGet if you’re programming in C#. MicroPython has it built in. so you’ll just need to add the import statement.

using static BrainPad.Controller;
using static BrainPad.BrainBot;
from BrainPad import *
from BrainBot import *

Ground Sensors

The BrainBot is equipped with two ground sensors for detecting ground reflection. They are located underneath the BrainBot on either side of the front wheel.

The ground sensors use the ground to reflect back an infrared beam back to a light sensor located next to them. If you look closely at them, you’ll see a separator between both. This is so it only gets the value from the reflection off the ground and not spill over from the beam itself. The infrared light sensor may look black to our eyes, but it is designed to filter out all other light except the infrared.

GroundSensor() function

The GroundSensor() function requires no arguments and uses one function to check the value of both sensors and return the appropriate value below.

Returned valueMeaning
0 Black line on both sensors (No white reflection is detected).
1 Black line on the left side. (White reflection is detected on the right sensor).
2Black line on the right side (White reflection is detected on the left sensor).
3No Black line on either sensors (White reflection is detected on both the left and the right sensors).

Reading the Sensors

First you can start by programming the sensors to detect the ground below and display the proper value on the screen.

while (true) {
    var value = GroundSensor();
    Print(value);
}
while True:
    value = GroundSensor()
    Print(value)
 

Try running the code and place the robot on a white paper then lift off the paper or place on a black surface.


Invisible Fence

This is a very simple AI program that impresses everyone! Find a while/light surface and verify that the sensors on the robots “see” the surface properly, meaning you read 0 from the sensor. Then use black electrical tape to draw a shape on the ground, any shape you like. The robot will now be dancing inside the shape.

add code

It might try to escape so keep in eye on it!


Detecting an Edge

Now that we understand the values the sensors are returning, we can create a program to detect when it comes to an edge (a line) and stops. You can use the line map included inside the BrainBot kit.

Since we are going to start moving the BrainBot, let’s add a button to start it so the BrainBot doesn’t run away while we’re testing and deploying the program, just like we did in the robot dance lesson.

//Wait for Button A press
Print("Press A to Start");
var btnA = Button(ButtonA, 0);
while(In(btnA) == 0) {
    Wait(0.1);
}

while (true) {
    var value = GroundSensor();
    Print(value);
    if (value == 3) {
        Move(30, 30);
    }else {
        Stop();
    }
}
#Wait for Button A press
Print("Press A to Start")
btn = Button(ButtonA, 0)
while In(btnA == 0):
    Wait(0.1)

Move(30,30)
while True:
    value = GroundSensor()
    if value == 3:
        Move(30,30)
    else:
        Stop()
 

 

We’ve detected the line and then stopped the BrainBot! You may have noticed that the BrainBot might move past the line before completely stopping. This is because it only stopped the motors and the BrainBot, which continues to coast a bit. To achieve a quicker stop, you could make the motors turn backwards very quickly and then call Stop().

Depending on where you positioned the BrainBot, the sensors might detect the logo in middle and then stop since it is also black.

Changing Direction

Change the argument in the if statement to make the BrainBot back up, turn and then go forward again. We’ll only show the code inside the while loop in the examples below.

while (true) {
    var value = GroundSensor();
    Print(value);
    if (value == 3) {
        Move(30, 30);
    }
    else {
        Move(-30, -30);
        Wait(0.4);
        Move(30, -30);
        Wait(0.4);
    }
}
while True: thisis wrong
    var value = GroundSensor()
    if value == 3:
        Move(30, 30)
    else:
        Stop()
        Move(-30, -30)
        Wait(0.4)
        Move(30, -30)
        Wait(0.4)
 
 
 

Follow the Line

We are ready to take our robot’s intelligence to the next level! How about making a line follower? We will have to make a few changes inside our while loop to detect the line and keep the robot on the line.

while(true){
    var value = GroundSensor()
    if (value == 3){
        Move(25, 25);
    }
    if (value == 2){
        Move(25, 0);
    }
    if (value == 1){
        Move(0, 25);
    }
    Print(value);
}
while True:
    var value = GroundSensor()
    if value == 3:
        Move(25, 25)
    if value == 2:
        Move(25, 0)
    if value == 1:
        Move(0, 25)
    Print(value)
  
 
 
 

Place the BrainBot front wheel on the black line on the line map and then hit the A button to start the BrainBot. The BrainBot now follows the line and adjusts as it moves off the line to correct itself.


BrainStorm

Now that you understand how both the distance sensor and the ground sensor work, can you think of ways to use both to create even more autonomy? What about how a robot might operate in a warehouse or plant floor? Autonomous vehicles are much more complex and require even more sensors and millions of lines of code to detect objects and people within it’s path.

Content Licensing
Newsletter

Twitter Feed
Hot off the press!