BrainBot Dance

BrainBot Dance

Now it’s time to give our BrainBot life! This first lesson will show us how to move our BrainBot.

Prerequiste

This lesson assumes you’ve already assembled the BrainBot and read the intro and BrainBot API, and that you also learned about variables, loops.


Getting Ready to Move

Before we program our BrainBot to move, let’s set up Button A to start it. Check out the Button lesson if you need a refresher. This way every time we need to change or modify our code and we plug in the BrainBot, it won’t start up and run away.

Remember we’ll have to add Using Statements to our code in C#. In Python, we’ll need to add an additional line at the top of ALL BrainBot programs to import its library ‘from BrainBot import *.

using static BrainPad.Controller;
using static BrainPad.BrainBot;

Print("Press A to START");

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

Print("Starting...");

//ADD BRAINBOT CODE HERE
from BrainPad import *
from BrainBot import *

Print("Press A to START")

# Wait for Button A press
btnA = Button(ButtonA, 0);
while In(btnA) == 0: 
    Wait(0.1); 

Print("Starting...");

#ADD BRAINBOT CODE HERE       

**In the remaining examples, we’ll only include the code that goes where ‘ADD BRAINBOT CODE HERE’ is shown.**

Stop()

This function doesn’t really need explaining and doesn’t take any arguments.

Move()

To move the BrainBot, we’ll use the Move() function. It requires 2 arguments, the left and right motor speeds. The range of these values are -100 to 100. With these two arguments we can move the BrainBot in any direction and turn.

Move(left motor speed, right motor speed);

Let’s start by moving our robot forward. To achieve this, we would set both motor speed values the same. This will make both wheels spin the same. Let’s start a speed of 40.

Print("I'm ALIVE");
Move(40,40);
Print("I'm Alive")
Move(40,40)

Moving Backwards

Can you guess how we would make the BrainBot move backwards? If you guessed use negative numbers, you would be correct! Let’s try this.

Print("I'm moving backwards");
Move(-40,-40);
Print("I'm moving backwards")
Move(-40,-40)

Turning

To turn the BrainBot, we use the exact same Move() function. This time, however, we only turn one wheel will while not turning the other wheel at all. Let’s make the BrainBot move forward for a short distance and then turn to the right. We’ll need to Wait() for a bit in between each Move() function call.

Note how Wait() is not commanding the robot to wait. Instead, it is for the BrainPad to wait before it tells the robot what to do next.

while(true) {
    Move(40,40);
    Wait(2);
    Move(40,0);
    Wait(0.45);
}
while True:
    Move(40,40)
    Wait(2)
    Move(40,0)
    Wait(0.45)

You may have noticed or tried to get the BrainBot to drive in a perfect square. This isn’t really possible with the simple code we’ve created and the lack of sensors to detect the environment around it. These many sensors are what make autonomous vehicles possible. We’ll use the on-board sensors to control the movement of the BrainBot even better in later lessons.

Why do you think our 2 Wait() functions have different durations? Try playing with them to observe how the BrainBot responds.

Spinning

If we wanted to make our BrainBot spin or turn very quickly, how do you think we could achieve this? We can do it all with the exact same Move() function and just make both wheels spin in opposite directions.

while(true){
Move(-40,40);
}
while True:
    Move(-40,40);

How would we make the robot spin in the opposite direction? Try making it spin one direction for 2 seconds and then spin in the other direction.


What’s Next?

Now that we know how to move the BrainBot, let’s get it flashy!


BrainStorm

Do we see now how complex programming a robot can be? Think about different types of robots used in the world. Some might only have to perform one simple task while others may do many things.