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)
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)
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