3.7. Motors#

../../_images/maqueen_motor.jpg

The two motors onboard the Maqueen are controlled over the I2C protocol.

Note

We’ll ignore the I2C protocol for now, we’ll learn about it later in I2C.

3.7.1. Driving the Motors#

The motors can be independently controlled in both direction (0 and 1) and speed (0 to 255).

For your convenience we’ve provided some code to get started:

def motor_left(speed, direction):
    buf = bytearray(3)
    buf[0] = 0x00
    buf[1] = direction
    buf[2] = speed
    i2c.write(0x10, buf)


def motor_right(speed, direction):
    buf = bytearray(3)
    buf[0] = 0x02
    buf[1] = direction
    buf[2] = speed
    i2c.write(0x10, buf)

You will also need to initialise the I2C interface by putting this line at the top of your script:

i2c.init()

Examples#

To move forward at full speed use:

motor_left(255, 0)
motor_right(255, 0)

To stop do:

motor_left(0, 0)
motor_right(0, 0)

To turn on the spot clockwise do:

motor_left(255, 0)
motor_right(255, 1)