Motors

Back to docs

The motors object is used to control a collection of Motor Boards. Similar to servos, motors can be used like a list. To do something with the first Motor Board, you would use:

R.motors[0].something...

…because indexes are 0-based (counting starts from 0, not 1).

When you have more than one Motor Board connected to your kit they will be ordered based upon their I2C address, which will need to be rewritten if more than 1 board is being used.

SETTING MOTOR POWER

Motor power is controlled using PWM with 100% power being a duty cycle of 1. You set the power with an integer value between -70 and 70 inclusive (where a negative value puts the motor in reverse). The field to change the output power is power. As each Motor Board has two outputs you will need to specify which output you want to control. m0 is physical port M1 on the Brain Box, and m1 is physical port M2:

Here’s an example:

from robot import *
import time
R = Robot()
# motor board 0, channel 0 to full power forward
R.motors[0].m0.power = 70
# motor board 1, channel 0 to full power reverse
R.motors[1].m0.power = -70
# motor board 0, channel 1 to half power forward
R.motors[0].m1.power = 45
# motor board 1, channel 0 stopped
R.motors[1].m0.power = 0
# the following will put motor board 0, channel 1 at half power (backwards) for 2.5 seconds:
R.motors[0].m1.power = -45
time.sleep(2.5)       # wait for 2.5 seconds
R.motors[0].m1.power = 0

You can read the current power value for a motor using the same field:

# get the current output power of Motor Board 0, channel 0

currentTarget = R.motors[0].m0.power

STOPPING THE MOTORS

When you set the motor power to 0, this signals the Motor Board to actively stop that motor from turning.

# store the motor in a local variable because typing it out gets really boring
molly = R.motors[0].m0
# set the power to 70 for a second, then stop immediately
molly.power = 70
time.sleep(1)
molly.power = 0

 

Last Updated On November 24, 2017