Skip to main content

Motor Board API

The Motor Board can control up to two motors.

Accessing the Motor Board

The Motor Board can be accessed by importing motors from sbot.

from sbot import motors

Setting motor power

Control of your motors is achieved by setting a power output from one of the channels on your Motor Boards. Valid values are between -1 and 1 inclusive. Fractional values (such as 0.42) can be used to specify less than 100% power. Negative values run the motor in the opposite direction.

As each Motor Board has two outputs you will need to specify which output you want to control:

# Set channel 0 to full power forward
motors.set_power(0, 1)

# Set channel 0 to full power reverse
motors.set_power(0, -1)

# Set channel 1 to half power forward
motors.set_power(1, 0.5)
warning

Setting a value outside of the range -1 to 1 will raise an exception and your code will crash.

The Motor Board will continue to output the requested power until it is told otherwise or until power to the board is removed (usually when your code ends and the robot turns off).

Therefore to stop your motors you must explicitly set the power output to zero:

# Set channel 1 at 25% power for 2.5 seconds:
motors.set_power(1, 0.25)
utils.sleep(2.5) # wait for 2.5 seconds
motors.set_power(1, 0)

Since each output channel can be controlled separately, you can control several motors at once.

# Set one motor to full power in one direction and
# another to full power in the other:
motors.set_power(0, 1)
motors.set_power(1, -1)

# Wait a while for the robot to move
utils.sleep(3)

# Stop both motors
motors.set_power(0, 0)
motors.set_power(1, 0)
info

You will need to work out for your robot which values (positive or negative) result in it moving in each direction. If you want to swap the direction of a motor you can swap the wires connecting the motor to the Motor Board.

Getting the previously set motor power

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

# Print the output power of the Motor Board channel 0
>>> motors.get_power(0)
0

Special Values

In addition to the numeric values, there are two special constants that can be used:

  • BRAKE
  • COAST

In order to use these, they must be imported from the sbot module like so:

from sbot import BRAKE, COAST

BRAKE will stop the motors from turning, and thus stop your robot as quick as possible.

info

BRAKE does the same thing as setting the power to 0.

from sbot import motors, BRAKE

motors.set_power(0, BRAKE)

COAST will stop applying power to the motors. This will mean they continue moving under the momentum they had before and slowly come to a stop.

from sbot import motors, COAST

motors.set_power(1, COAST)

Motor Status

Several functions are provided to manage the Motor Board.

Get Motor Current

You can get the current being drawn by either motor by passing a motor number to the get_motor_current function.

current = motors.get_motor_current(0)

The function returns the current being drawn in Amps as a float.

warning

Sudden large changes in the motor speed setting (e.g. -1 to 0, 1 to -1 etc.) may trigger the over-current protection of the power board and your robot will shut down with a distinct beeping noise and/or a red light next to the power board output that is powering the motor board.

Check Fault Status

For various reasons, such as a motor drawing too much current, the motor board can enter a 'fault' state. You can use the in_fault function to check for faults on either output.

m0_has_fault = motors.in_fault(0)

if m0_has_fault:
print("Fault on motor 0 :(")

Reset Motor Board

You can clear any faults by resetting the motor board.

motors.reset()