Skip to content

Commit 7e4fcac

Browse files
authored
SR2024 - Update Section - Motor board API
2 parents e96a2a5 + 61ebb2c commit 7e4fcac

File tree

1 file changed

+71
-60
lines changed

1 file changed

+71
-60
lines changed

programming/motors.md

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,83 @@ title: Motor Board API
66
Motor Board API
77
===============
88

9-
The `motor_board` object is used to control a collection of Motor Boards.
9+
The kit can control multiple motors simultaneously.
10+
Each Motor Board can control two motors.
11+
See the [Motor Board](/docs/kit/motor_board) hardware page for more details.
1012

11-
When a single Motor Board is connected to your robot, you can control it
12-
using the `motor_board` object.
13+
14+
Accessing the Motor Board
15+
-------------------------
16+
17+
If there is exactly one Motor Board connected to your robot, it can be accessed using the `motor_board` property of the `Robot` object.
1318

1419
~~~~~ python
15-
R.motor_board.something...
20+
from sr.robot3 import *
21+
robot = Robot()
22+
23+
my_motor_board = robot.motor_board
1624
~~~~~
1725

18-
The serial number of each detected Motor Board is printed to the log when your robot starts.
26+
If you have more than one Motor Board attached, you need to specify which one you want to control.
27+
This is done using the serial number of the board.
28+
29+
The serial number is physically written on each board and the serial of any connected board will be printed to your log when the robot starts.
1930
It will look something like this:
2031

2132
~~~~~ not-code
22-
sr.robot3.robot INFO - Found Student Robotics Motor Board v4 - srABC1
33+
sr.robot3.robot - INFO - Found MotorBoard, serial: srABC1
34+
sr.robot3.robot - INFO - Found MotorBoard, serial: srXYZ1
2335
~~~~~
2436

25-
If you have more than one Motor Board attached, you need to specify which one you want to control. This is done using the serial number of the board. For example: if you had a board that was detected as "srABC1",
26-
you could do this instead:
37+
You can then access the boards like this:
2738

2839
~~~~~ python
29-
R.motor_boards["srABC1"].something...
40+
from sr.robot3 import *
41+
robot = Robot()
42+
43+
my_motor_board = robot.motor_boards["srABC1"]
44+
my_other_motor_board = robot.motor_boards["srXYZ1"]
3045
~~~~~
3146

3247
<div class="warning" markdown="1">
33-
When you have more than one Motor board connected to your kit,
34-
you must use `R.motor_boards` and index by serial number. This is so
35-
that the kit knows which Motor Board you want to control.
48+
When you have more than one Motor Board connected to your kit, you can't use `robot.motor_board`.
49+
This is because the kit needs to know which Motor Board you want to control.
3650
</div>
3751

3852

3953
Setting motor power
4054
-------------------
4155

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

4761
The field to change the output power is `power`. As each Motor Board has two
4862
outputs you will need to specify which output you want to control:
4963

5064
~~~~~ python
51-
from sr.robot3 import *
52-
import time
53-
54-
R = Robot()
55-
5665
# motor board srABC1, channel 0 to full power forward
57-
R.motor_boards["srABC1"].motors[0].power = 1
66+
robot.motor_boards["srABC1"].motors[0].power = 1
5867

5968
# motor board srXYZ1, channel 0 to full power reverse
60-
R.motor_boards["srXYZ1"].motors[0].power = -1
69+
robot.motor_boards["srXYZ1"].motors[0].power = -1
6170

6271
# motor board srABC1, channel 1 to half power forward
63-
R.motor_boards["srABC1"].motors[1].power = 0.5
72+
robot.motor_boards["srABC1"].motors[1].power = 0.5
6473
~~~~~
6574

66-
The motor board will continue to output the requested power until it is told
67-
otherwise or until power to the board is removed (usually when the robot turns
75+
The Motor Board will continue to output the requested power until it is told
76+
otherwise or until power to the board is removed (usually when your code ends and the robot turns
6877
off).
6978

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

7281
~~~~~ python
73-
# motor board srXYZ1, channel 0 stopped
74-
R.motor_boards["srXYZ1"].motors[0].power = 0
75-
7682
# Put motor board srABC1, channel 1 at 25% power for 2.5 seconds:
77-
R.motor_boards["srABC1"].motors[1].power = 0.25
83+
robot.motor_boards["srABC1"].motors[1].power = 0.25
7884
time.sleep(2.5) # wait for 2.5 seconds
79-
R.motor_boards["srABC1"].motors[1].power = 0
85+
robot.motor_boards["srABC1"].motors[1].power = 0
8086
~~~~~
8187

8288
Since each output channel can be controlled separately, you can control several
@@ -85,63 +91,68 @@ motors at once.
8591
~~~~~ python
8692
# Set one motor to full power in one direction and
8793
# another to full power in the other:
88-
R.motor_boards["srABC1"].motors[0].power = 1
89-
R.motor_boards["srABC1"].motors[1].power = -1
94+
robot.motor_boards["srABC1"].motors[0].power = 1
95+
robot.motor_boards["srABC1"].motors[1].power = -1
9096

91-
# Wait a while (perhaps for the robot to move)
97+
# Wait a while for the robot to move
9298
time.sleep(3)
9399

94100
# Stop both motors
95-
R.motor_boards["srABC1"].motors[0].power = 0
96-
R.motor_boards["srABC1"].motors[1].power = 0
101+
robot.motor_boards["srABC1"].motors[0].power = 0
102+
robot.motor_boards["srABC1"].motors[1].power = 0
97103
~~~~~
98104

99105
<div class="info" markdown="1">
100-
You will need to work out for your robot which values (positive or negative)
101-
result in it moving in each direction. This will depend on how you have
102-
positioned your motors as well as how they have been wired to the motor board.
106+
You will need to work out for your robot which values (positive or negative) result in it moving in each direction.
107+
If you want to swap the direction of a motor you can swap the wires connecting the motor to the Motor Board.
103108
</div>
104109

110+
105111
Getting the current motor power
106112
-------------------------------
107113

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

110116
~~~~~ python
111-
# get the current output power of Motor Board srABC1, channel 0
112-
target = R.motor_boards["srABC1"].motors[0].power
117+
# Print the output power of Motor Board srABC1, channel 0
118+
print(robot.motor_boards["srABC1"].motors[0].power)
113119
~~~~~
114120

115-
Stopping the motors
116-
-------------------
117121

118-
When you set the motor power to 0, this signals the Motor Board to actively stop that motor from turning.
122+
Special Values
123+
--------------
124+
125+
In addition to the numeric values, there are two special constants that can be used:
126+
- `BRAKE`
127+
- `COAST`
128+
129+
`BRAKE` will stop the motors from turning, and thus stop your robot as quick as possible.
130+
131+
<div class="info" markdown="1">
132+
`BRAKE` does the same thing as setting the power to `0`.
133+
</div>
119134

120135
~~~~~ python
121-
# store the motor in a local variable because
122-
# typing it out gets really boring
123-
molly = R.motor_boards["srABC1"].motors[1]
124-
125-
# set the power to 100% for a second, then stop immediately
126-
molly.power = 1
127-
time.sleep(1)
128-
molly.power = 0
136+
# Stop the motor as quick as possible
137+
robot.motor_boards["srABC1"].motors[0].power = BRAKE
129138
~~~~~
130139

131-
However, you may also want to allow the motors to gently coast to a halt.
132-
This can be achieved by using the special `COAST` value.
140+
`COAST` will stop applying power to the motors.
141+
This will mean they continue moving under the momentum they had before and slowly come to a stop.
133142

134143
~~~~~ python
135-
# set the power to 100% for a second, then coast to a stop
136-
molly.power = 1
137-
time.sleep(1)
138-
molly.power = COAST
144+
# Slowly coast to a stop
145+
robot.motor_boards["srABC1"].motors[0].power = COAST
139146
~~~~~
140147

141-
When the power of the motor is set to `0`, it is equivalent to setting
142-
the power to `BRAKE`.
148+
149+
Motor currents
150+
--------------
151+
152+
The Motor Board can also measure the current being drawn by each of the ports on the board.
153+
This value is measured in amps.
143154

144155
~~~~~ python
145-
# set the motor to brake
146-
molly.power = BRAKE
156+
# Print the current in amps of motor 0 on board srABC1
157+
print(robot.motor_boards["srABC1"].motors[0].current)
147158
~~~~~

0 commit comments

Comments
 (0)