01. Simple Drive

Introduction

The first thing that you'll want to do with a robot vehicle is make it move. To do this we need to send the correct electrical signals to the motors.


Programming

The motors are connected to the micro:bit on the following pins,

  • LFT Motor: PWM pin 0, DIR pin 8
  • RGT Motor: PWM pin 1, DIR pin 12

When you want to drive the motors, you start by using write_digital() on the IDR (direction pins) with a 0 for forwards and a 1 for backwards. You then use write_analog() on the PWM pins to set the speed of the motor.

For example, to go forwards at full speed for 2 seconds and then stop, you would write,

pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_analog(1023)
pin1.write_analog(1023)
sleep(2000)
pin0.write_analog(0)
pin1.write_analog(0)

You can write a value from 0 to 1023 in the write_analog statements. The higher your value, the quicker you move forwards. This would be a little under 80% speed.

pin8.write_digital(0)
pin12.write_digital(0)
pin0.write_analog(800)
pin1.write_analog(800)

To reverse, first write a 1 to the direction pins of each motor. Then write a value from 0 to 1023 like before. This time, however, the smaller your number, the quicker you go backwards. Subtract your speed from 1023 to get the value you write. If we want to reverse at '800', the same speed backwards as we just went forwards, we would write,

pin8.write_digital(1)
pin12.write_digital(1)
pin0.write_analog(223)
pin1.write_analog(223)

If the value you write_analog() with is not the same for each motor, then the robot will turn in the direction of the motor going the slowest. If the difference between the two values is small, then the turn is gentle. If you have a large difference between the values, you get a sharper turn.


A Helper Function

It's easier to experiment with the way that the car responds to the numbers you send to it if you have a function control the setting of the pins. The following program uses a function to drive the robot in different ways. The comments show how each function call affects the movement of the robot.


from microbit import *

def Drive(lft,rgt):
    pin8.write_digital(0)
    pin12.write_digital(0)
    if lft<0:
        pin8.write_digital(1)
        lft = 1023 + lft
    if rgt<0:
        rgt = 1023 + rgt
        pin12.write_digital(1)
    pin0.write_analog(lft)
    pin1.write_analog(rgt)

def Beep():
    pin14.write_digital(1)
    sleep(200)
    pin14.write_digital(0)
    sleep(50)
    pin14.write_digital(1)
    sleep(200)
    pin14.write_digital(0)
    sleep(50)
    
while True:
    Beep()
    # Forwards
    Drive(800,800)
    sleep(1000)
    # Coast
    Drive(0,0)
    sleep(1000)
    # Backwards
    Drive(-800,-800)
    sleep(1000)
    # Coast
    Drive(0,0)
    sleep(1000)
    # Slow Turn Left
    Drive(400,800)
    sleep(1000)
    # Coast
    Drive(0,0)
    sleep(1000)
    # Sharp Right Turn
    Drive(800,0)
    sleep(1000)

Challenges

  1. Take the last example and adapt it to have the car move in a repeating pattern that takes it back to its starting point.

  2. Obstacle Course
    1. Set up an obstacle course with a start and an end point. 
    2. Write a program that drives the Bit:Bot to the finish in the quickest time. 
    3. You will notice that full speed is not always the quickest route to victory in a complicated obstacle course.

  3. You can still make things appear on the LED matrix when using the robot. 
    1. When you are chaining a series of actions together, you can change the display to show what you intended your code to do. 
    2. For simple movements, this can be an arrow pointing in the direction of travel. 
    3. For other manoeuvres, you will need to design your own images.

  4. Experiment with the numbers you write in the analog write statements. 
    1. Work out how long it takes to travel a given distance and how to turn 90° on the spot. 
    2. Work out your own routines to drive fowards and backwards your chosen unit. 
    3. Write two more for your left and right rotations. 
    4. Use your functions to make it easier to write programs that send the robot on long planned routes. 
    5. If you know how the car is going to move in small units, you will reduce the amount of trial and error needed to get the robot to follow a long route.


Comments