Skip to content

Commit e8f4fc7

Browse files
committed
Merge branch 'friendlier-motor-programming-docs'
2 parents 43725f4 + a2e88ae commit e8f4fc7

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

kit/motor_board.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ The USB interface is isolated from the rest of the board to prevent damage to th
1414
Due to this isolation the board must have power applied to the power connector, from the motor rail on the power board, to function.
1515
If the board does not have power applied to the power connector then the kit will report that there is a problem with the motor board.
1616

17+
The motor board uses [pulse-width modulation][wiki-pwm] (PWM) to control the
18+
amount of power that is sent to the motors.
19+
20+
[wiki-pwm]: https://en.wikipedia.org/wiki/Pulse-width_modulation
21+
1722
Board Diagram
1823
-------------
1924

programming/sr/motors/index.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ R.motor_boards["srABC1"].something...
3939
Setting motor power
4040
-------------------
4141

42-
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 a value 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.
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.
44+
Fractional values (such as 0.42) can be used to specify less than 100% power.
45+
Negative values run the motor in the opposite direction.
4346

44-
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:
47+
The field to change the output power is `power`. As each Motor Board has two
48+
outputs you will need to specify which output you want to control:
4549

4650
~~~~~ python
4751
from sr.robot3 import *
@@ -57,21 +61,55 @@ R.motor_boards["srXYZ1"].motors[0].power = -1
5761

5862
# motor board srABC1, channel 1 to half power forward
5963
R.motor_boards["srABC1"].motors[1].power = 0.5
64+
~~~~~
65+
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
68+
off).
6069

70+
Therefore to stop your motors you must explicitly set the power output to zero:
71+
72+
~~~~~ python
6173
# motor board srXYZ1, channel 0 stopped
6274
R.motor_boards["srXYZ1"].motors[0].power = 0
6375

64-
# the following will put motor board srABC1, channel 1 at 25% power (forwards) for 2.5 seconds:
76+
# Put motor board srABC1, channel 1 at 25% power for 2.5 seconds:
6577
R.motor_boards["srABC1"].motors[1].power = 0.25
6678
time.sleep(2.5) # wait for 2.5 seconds
6779
R.motor_boards["srABC1"].motors[1].power = 0
6880
~~~~~
6981

82+
Since each output channel can be controlled separately, you can control several
83+
motors at once.
84+
85+
~~~~~ python
86+
# Set one motor to full power in one direction and
87+
# 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
90+
91+
# Wait a while (perhaps for the robot to move)
92+
time.sleep(3)
93+
94+
# Stop both motors
95+
R.motor_boards["srABC1"].motors[0].power = 0
96+
R.motor_boards["srABC1"].motors[1].power = 0
97+
~~~~~
98+
99+
<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.
103+
</div>
104+
105+
Getting the current motor power
106+
-------------------------------
107+
70108
You can read the current power value for a motor using the same field:
71109

72110
~~~~~ python
73111
# get the current output power of Motor Board srABC1, channel 0
74-
currentTarget = R.motor_boards["srABC1"].motors[0].power
112+
target = R.motor_boards["srABC1"].motors[0].power
75113
~~~~~
76114

77115
Stopping the motors
@@ -80,7 +118,8 @@ Stopping the motors
80118
When you set the motor power to 0, this signals the Motor Board to actively stop that motor from turning.
81119

82120
~~~~~ python
83-
# store the motor in a local variable because typing it out gets really boring
121+
# store the motor in a local variable because
122+
# typing it out gets really boring
84123
molly = R.motor_boards["srABC1"].motors[1]
85124

86125
# set the power to 100% for a second, then stop immediately

0 commit comments

Comments
 (0)