Skip to content

Commit 3e09a3a

Browse files
committed
Update the motor board docs for SR2022
1 parent fe1ec44 commit 3e09a3a

File tree

1 file changed

+44
-47
lines changed

1 file changed

+44
-47
lines changed

programming/sr/motors/index.md

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,72 +6,70 @@ title: Motors
66
Motors
77
======
88

9-
The `motors` object is used to control a collection of Motor Boards.
10-
Similar to `ruggeduinos` and `servos`, `motors` can be used like a list.
11-
To do something with the **first Motor Board**, you would use:
9+
The `motor_board` object is used to control a collection of Motor Boards.
10+
11+
When a single Motor Board is connected to your robot, you can control it
12+
using the `motor_board` object.
1213

1314
~~~~~ python
14-
R.motors[0].something...
15+
R.motor_board.something...
1516
~~~~~
1617

17-
...because indexes are 0-based (counting starts from 0, not 1).
18-
19-
When you have more than one Motor Board connected to your kit they will be ordered based upon their SR Part Code, as written on the underside of the board.
20-
21-
The SR Part Code of each detected Motor Board is also printed to the log when your robot starts.
18+
The serial number of each detected Motor Board is printed to the log when your robot starts.
2219
It will look something like this:
2320

2421
~~~~~ not-code
25-
Found the following devices:
26-
- Motors:
27-
0: Motor( serialnum = "SR0XJ1F" )
28-
1: Motor( serialnum = "SR0A123" )
22+
sr.robot3.robot INFO - Found Student Robotics Motor Board v4 - srABC1
2923
~~~~~
3024

31-
32-
However, like `ruggeduinos` and `servos`, `motors` is actually a dictionary.
33-
As a result, in `motors` you can also use the SR Part Code of the Motor Board as a key.
34-
For example, if you had a board that was labelled "SR0A123",
25+
If you have more than one Motor Board attached, the `motor_boards` object can be used to control a collection of Motor Board. It is a dictionary access via serial number, which is usually written on the board. For example, if you had a board whose serial number was "srABC1",
3526
you could do this instead:
3627

3728
~~~~~ python
38-
R.motors["SR0A123"].something...
29+
R.motor_boards["srABC1"].something...
3930
~~~~~
4031

32+
<div class="warning">
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.
36+
</div>
37+
38+
4139
Setting motor power
4240
-------------------
4341

4442
Motor power is controlled using [PWM](https://en.wikipedia.org/wiki/Pulse-width_modulation) with 100% power being a [duty cycle](https://en.wikipedia.org/wiki/Duty_cycle) of 1. You set the power with an integer value between -100 and 100 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:
4543

4644
~~~~~ python
47-
from sr.robot import *
45+
from sr.robot3 import *
4846
import time
4947

5048
R = Robot()
5149

52-
# motor board 0, channel 0 to full power forward
53-
R.motors[0].m0.power = 100
50+
# motor board srABC1, channel 0 to full power forward
51+
R.motor_boards["srABC1"].motors[0].power = 1
5452

55-
# motor board 1, channel 0 to full power reverse
56-
R.motors[1].m0.power = -100
53+
# motor board srXYZ1, channel 0 to full power reverse
54+
R.motor_boards["srXYZ1"].motors[0].power = -1
5755

58-
# motor board 0, channel 1 to half power forward
59-
R.motors[0].m1.power = 50
56+
# motor board srABC1, channel 1 to half power forward
57+
R.motor_boards["srABC1"].motors[1].power = 0.5
6058

61-
# motor board 1, channel 0 stopped
62-
R.motors[1].m0.power = 0
59+
# motor board srXYZ1, channel 0 stopped
60+
R.motor_boards["srXYZ1"].motors[0].power = 0
6361

64-
# the following will put motor board 0, channel 1 at 25% power (forwards) for 2.5 seconds:
65-
R.motors[0].m1.power = 25
62+
# the following will put motor board srABC1, channel 1 at 25% power (forwards) for 2.5 seconds:
63+
R.motor_boards["srABC1"].motors[1].power = 0.25
6664
time.sleep(2.5) # wait for 2.5 seconds
67-
R.motors[0].m1.power = 0
65+
R.motor_boards["srABC1"].motors[1].power = 0
6866
~~~~~
6967

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

7270
~~~~~ python
73-
# get the current output power of Motor Board 0, channel 0
74-
currentTarget = R.motors[0].m0.power
71+
# get the current output power of Motor Board srABC1, channel 0
72+
currentTarget = R.motor_boards["srABC1"].motors[1].power
7573
~~~~~
7674

7775
Stopping the motors
@@ -81,29 +79,28 @@ When you set the motor power to 0, this signals the Motor Board to actively stop
8179

8280
~~~~~ python
8381
# store the motor in a local variable because typing it out gets really boring
84-
molly = R.motors[0].m0
82+
molly = R.motor_boards["srABC1"].motors[1]
8583

86-
# set the power to 100 for a second, then stop immediately
87-
molly.power = 100
84+
# set the power to 100% for a second, then stop immediately
85+
molly.power = 1
8886
time.sleep(1)
8987
molly.power = 0
9088
~~~~~
9189

9290
However, you may also want to allow the motors to gently coast to a halt.
93-
This can be achieved by setting the `use_brake` field of the individual motor.
91+
This can be achieved by using the special `COAST` value.
9492

9593
~~~~~ python
96-
# set braking mode
97-
molly.use_brake = False
98-
99-
# set the power to 100 for a second, then coast to a stop
100-
molly.power = 100
94+
# set the power to 100% for a second, then coast to a stop
95+
molly.power = 1
10196
time.sleep(1)
102-
molly.power = 0
103-
104-
# Print the current braking mode (might be handy for debugging!)
105-
print("m0.use_brake = {0}".format(molly.use_brake))
97+
molly.power = COAST
10698
~~~~~
10799

108-
The `use_brake` value defaults to `True` and, just like the `power` value,
109-
will remain set until you change it.
100+
When the power of the motor is set to `0`, it is equivalent to setting
101+
the power to `BRAKE`.
102+
103+
~~~~~ python
104+
# set the motor to brake
105+
molly.power = BRAKE
106+
~~~~~

0 commit comments

Comments
 (0)