Skip to content

Commit da1c123

Browse files
committed
review comments
1 parent 2f77c3f commit da1c123

File tree

1 file changed

+86
-88
lines changed

1 file changed

+86
-88
lines changed

tutorials/basic_motor_control.md

Lines changed: 86 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ title: Basic Motor Control
66
# Basic Motor Control
77

88
The aim of this tutorial is to get a motor turning with your kit.
9-
To complete this tutorial, you'll need the following:
109

11-
* The [Power Board](/docs/kit/power_board)
12-
* A battery (charged, of course)
13-
* A [Motor Board](/docs/kit/motor_board)
14-
* 2 large (7.5mm) green _CamCon_ connectors to connect power between the Power Board and Motor Board
15-
* 2 lengths of thicker gauge wire for powering the Motor Board from the Power Board (one should be black)
10+
To complete this tutorial, you'll need the following items from the kit:
11+
12+
* A charged battery
13+
* An assembled kit according to the [assembly guide]({{ site.baseurl }}/tutorials/assembly)
14+
* A small (5mm) green CamCon connector to plug the motor into the Motor Board
15+
* 2 lengths of wire for your motor
16+
* A small slotted/flat blade screwdriver (for the CamCon screws)
17+
18+
And additionally you will need to provide the following:
1619
* A motor (see specification below)
17-
* A small (5mm) green _CamCon_ connector to plug the motor into the Motor Board
18-
* 2 lengths of a suitable gauge of wire for your motor
19-
* 2 Micro USB cables
20-
* A USB hub
21-
* A USB A to B cable (used to connect to the USB hub to the brain)
22-
* The memory stick
2320
* A soldering Iron
2421
* Some solder wire
2522
* Wire strippers
26-
* A small slotted/flat blade screwdriver (for the _CamCon_ screws)
2723

28-
You should be familiar with the setup for most of the above now, so it's just the motor-related parts that need explaining.
29-
If you need help assembling the rest of the kit please look at the [guide]({{ site.baseurl }}/tutorials/assembly) on assembling the kit
24+
Before continuing with this guide, if you have yet to setup your kit, first follow the instructions on the [kit assembly]({{ site.baseurl }}/tutorials/assembly) page.
3025

3126

3227
## Motor Specification
@@ -35,7 +30,7 @@ There is a certain specification the motor(s) you use must meet.
3530
The criteria are as follows:
3631

3732
* 12V motor
38-
* A stall current of less than 10A (this is the current limit for the [Motor Boards](/docs/kit/motor_board))
33+
* A stall current of less than 10A (this is the maximum current a motor will draw when it can't turn)
3934

4035
<div class="info">
4136
When designing your robot you should bear in mind that while each Motor Board can deliver 10A on each output, all the power needs to go through the Power Board, which is limited to a combined total of 30A.
@@ -45,32 +40,19 @@ This means that across all the outputs for all the motors (as well as the rest o
4540

4641
## Connecting a Motor
4742

48-
To plug the motor into the kit, you'll need to solder an appropriate gauge of wire to the terminals on the motor, and connect the other ends to the _CamCon_ connector.
43+
To plug the motor into the kit, you'll need to solder two wires to the terminals on the motor, and connect the other ends to the CamCon connector.
44+
4945
Like so:
5046

5147
![Motor connected to CamCon]({{ site.baseurl }}/images/content/kit/motor_and_camcon.png)
5248

5349
<div class="info">
5450
You may want to insulate the motor's terminals with some insulation tape (or heat shrink tubing if you've got it) like in the image above.
51+
This will stop the wires accidentally touching and shorting out the power.
5552
</div>
5653

57-
Now you need to connect the motor to one of your Motor Boards.
58-
You'll need to connect the following:
59-
60-
* Your motor plugged into the motor 0 socket on the Motor Board
61-
* The micro USB cable from the Motor Board to the USB hub
62-
* The USB A to B cable from the USB hub to the Brain Board
63-
64-
This is almost ready, but the Motor Board also needs the power that it will be delivering to the motor.
65-
This is done by connecting together the two larger _CamCon_ connectors, using the other two lengths of wire.
66-
Be sure that the cable connects the positive output ("+") of the Power Board to the positive power input of the Motor Board, and likewise for the ground ("-") output &mdash;
67-
see the [Power Board](/docs/kit/power_board) and [Motor Board](/docs/kit/motor_board) documentation to see which is which.
68-
69-
<div class="info">
70-
You must always use black for ground (0V) connections (and only for these), so that it's clear which these are.
71-
</div>
72-
73-
You can now connect this into the Power Board on one end, and the Motor Board power connection on the other.
54+
This motor should then be connected to motor 0 socket on the Motor Board.
55+
To see which output is number 0, look at the [Motor Board]({{ site.baseurl }}/kit/motor_board) page.
7456

7557

7658
## Some Code
@@ -80,101 +62,108 @@ You will want to ensure the motor is secured when testing.
8062
A motor suddenly starting up during testing and moving across the table could cause a potential hazard.
8163
</div>
8264

83-
The example program we'll write will do a number of things with the motor:
84-
forwards and backwards, and different power settings, for example.
85-
Let's begin.
86-
87-
To start off with, we'll just make a motor move forwards, then backwards, and then repeat.
65+
The following sections will lead you through running some code on your robot which will cause your motor to spin.
66+
By combining these examples you should be able to achieve movement with your robot.
8867

8968

9069
### Forwards & Backwards
9170

92-
Doing this is actually very easy; the only thing you need to realise is that a positive number is forwards and a negative number is backwards.
71+
To start off with, we'll make a motor move forwards, then backwards, and then repeat.
72+
73+
The way we control motors is by providing a number between `-1` and `1` to describe its power and direction.
9374

9475
<div class="info">
9576
The actual direction of travel of a motor, when mounted on a robot, will depend on its orientation and the way in which the wires are connected to the Motor Board.
9677
If the motor appears to be going in the wrong direction, just swap the motor's positive and negative wires over.
9778
</div>
9879

99-
Here's the code:
80+
As a first example:
10081

10182
~~~~~ python
10283
from sr.robot3 import *
103-
import time
10484

10585
robot = Robot()
10686

10787
while True:
10888
robot.motor_board.motors[0].power = 0.5
109-
time.sleep(3.0)
89+
robot.sleep(3.0)
11090

11191
robot.motor_board.motors[0].power = 0
112-
time.sleep(5)
92+
robot.sleep(5)
11393

11494
robot.motor_board.motors[0].power = -0.5
115-
time.sleep(3.0)
95+
robot.sleep(3.0)
11696

11797
robot.motor_board.motors[0].power = 0
118-
time.sleep(5)
98+
robot.sleep(5)
11999
~~~~~
120100

121-
You're familiar with the first few lines; in fact, the only lines you may not be familiar with are the `robot.motor_board...` lines.
122-
For a comprehensive reference to the `motor` object, see the `sr.robot3` module's [Motors]({{ site.baseurl }}/programming/motors) page.
123-
But, to summarise:
124-
125-
<div class="info" markdown="1">
126-
`robot.motor_board.motors[0].power = x` will set the power of the motor connected to output 0 on the first [Motor Board]({{ site.baseurl }}/kit/motor_board) to `x`, where `x` is a value between `-1` and `1`.
127-
</div>
101+
The first few lines in this example are repeated throughout the docs, they import our `sr.robot3` library and then create our `robot`.
128102

129-
So, `robot.motor_board.motors[0].power = 0.5` sets the target power of the motor connected to output 0 to 50% forwards.
103+
The line:
104+
~~~~~ python
105+
robot.motor_board.motors[0].power = 0.5
106+
~~~~~
107+
sets the target power of the motor connected to output 0 to 50% forwards.
130108

131-
As you would expect, then, `robot.motor_board.motors[0].power = -0.5` will put the this motor into reverse at 50% power.
109+
Equally, the line:
110+
~~~~~ python
111+
robot.motor_board.motors[0].power = -0.5
112+
~~~~~
113+
will put the this motor into reverse at 50% power.
132114

133-
`robot.motor_board.motors[0].power = 0` will output no power to the motor and stop it.
115+
Finally setting the power to 0 will output no power and stop the motor.
134116

135-
So, if you put the above code on your robot, you should be able to see a motor spin forwards, stop, spin backwards, stop, and then repeat...
117+
So, if you put the above code on your robot, you should be able to see a motor spin forwards, stop, spin backwards, stop, and then repeat.
136118

137119
<div class="info" markdown="1">
138-
If you find that the motor doesn't turn when you run the above code, check that you've got all the cables connected to the right places, in particular note that the Motor Board has _two_ outputs.
120+
If you find that the motor doesn't turn when you run the above code, check that you've got all the cables connected to the right places, in particular note that the Motor Board has two outputs.
139121
</div>
140122

123+
For a comprehensive reference to the `motor` object, see the `sr.robot3` module's [Motors]({{ site.baseurl }}/programming/motors) page.
124+
141125

142126
### Changing the Speed
143127

144-
Now we're going to modify the program to vary the ramp (go from 0% to 100% slowly) the speed of the motor.
145-
Our aim is to do the forwards and backwards bit (as above), but, instead of setting the power to a fixed value we will ramp the power from 10% up to 50%, then back down to 10%, and then back to 0 (all in steps of 10%).
128+
Now we're going to modify the program to ramp (go from 0% to 100% slowly) the speed of the motor.
129+
Our aim is to do the forwards and backwards bit (as above), but, instead of setting the power to a fixed value we will gradually increase the power from 0% up to 50%, then back down to 0%.
146130

147131
Here's the code:
148132

149133
~~~~~ python
150134
from sr.robot3 import *
151-
import time
152135

153136
robot = Robot()
154137

155138
while True:
156139

157-
# ramp from 10% to 50%
158-
for pwr in range(20, 60, 10):
140+
# ramp from 0% to 50%
141+
for pwr in range(0, 60, 10):
159142
robot.motor_board.motors[0].power = pwr / 100 # Convert from percentage
160-
time.sleep(0.1)
143+
robot.sleep(0.1)
161144

162-
# ramp from 50% to 10%
163-
for pwr in range(50, 0, -10):
145+
# Drive at 50% power for a second
146+
robot.sleep(1)
147+
148+
# ramp from 50% to 0%
149+
for pwr in range(50, -10, -10):
164150
robot.motor_board.motors[0].power = pwr / 100 # Convert from percentage
165-
time.sleep(0.1)
151+
robot.sleep(0.1)
152+
153+
# Stop for a second
154+
robot.sleep(1)
166155

167-
# set power to 0 for a second
168-
robot.motor_board.motors[0].power = 0
169-
time.sleep(1)
170156
~~~~~
171157

172-
You have seen some of those bits of code before but the thing that may be new is the `for` loop.
158+
You have seen most of these lines of code in the previous example but we have now added a `for` loop.
159+
160+
The [`for`](https://docs.python.org/tutorial/controlflow.html#for-statements) loop accepts anything that has multiple items that Python can step through to get multiple values.
161+
One example of this is a `list`, which when printed appears in square brackets like so: `[1, 2, 3]`.
162+
This is called iteration.
173163

174-
The [`for`](https://docs.python.org/tutorial/controlflow.html#for-statements) loop accepts anything that Python can iterate over to get multiple values, such as a `list` (a `list`, when `print`ed, appears in square brackets like so: `[1, 2, 3]`).
164+
For a comprehensive introduction to to `list`s, have a look at [this tutorial](https://www.w3schools.com/python/python_lists.asp).
175165

176-
For a comprehensive introduction to to `list`s, have a look at [this WikiBooks article](https://en.wikibooks.org/wiki/Python_Programming/Lists).
177-
The `for` loop will iterate over the `list` (i.e. take each element in turn) and make it available in the loop's body as the variable after the the `for` keyword.
166+
The `for` loop will iterate over the `list`, taking each element in turn and making it available in the loop.
178167
Here's an example:
179168

180169
~~~~~ python
@@ -190,39 +179,48 @@ The above would output:
190179
3
191180
~~~~~
192181

193-
Then there's the [`range()`](https://docs.python.org/3/library/stdtypes.html#typesseq-range) object.
182+
Then there's the [`range()`](https://docs.python.org/3/library/stdtypes.html#typesseq-range) function.
194183

195-
`range()` objects represent a sequence of numbers.
196-
They can be constructed with one, two or three arguments to describe what the sequence of numbers should be.
184+
The `range()` function returns a sequence of numbers.
185+
The function has three forms that take one, two or three arguments to describe what the sequence of numbers should be.
197186

198187
The general format is:
199188
~~~~~ python
200189
range(start, stop, step)
201190
~~~~~
202-
If only one argument is provided then two of the arguments have default values, the sequence starts at `0` and the step is set to `1`.
203-
The provided argument only defines the stop point.
191+
192+
start
193+
: The value to start your sequence at
194+
195+
stop
196+
: The value which stops the sequence if the next step passes or equals this
197+
198+
step
199+
: The value which is added on each step of the sequence
200+
201+
If only one argument is provided, this defines the stop point. The other two arguments are set to default values, which is a sequence starting at `0` with a step size of `1`.
204202

205203
For example:
206204
~~~~~ python
207205
range(5) # [0, 1, 2, 3, 4]
208206
~~~~~
209207

208+
<div class="info">
209+
Note that the stop value is not included in the sequence.
210+
</div>
211+
210212
If two arguments are provided, then these represent the start and stop with the step being set to `1`.
211213

212214
For example:
213215
~~~~~ python
214216
range(2, 6) # [2, 3, 4, 5]
215217
~~~~~
216218

217-
Taking `range(10, 60, 10)`, as an example of all three arguments:
218-
219-
* Start at 10
220-
* Go up in steps of 10
221-
* For all values less than 60
219+
Taking `range(0, 60, 10)`, as an example of all three arguments:
220+
Starts at 0, going up in steps of 10, for all values less than 60.
222221

223-
Gives:
224222
~~~~~ python
225-
range(10, 60, 10) # [10, 20, 30, 40, 50]
223+
range(0, 60, 10) # [0, 10, 20, 30, 40, 50]
226224
~~~~~
227225

228226
Putting all of that together; in the motor control example we iterate over that sequence of numbers setting the motor power to each of those values in turn, with a delay of 100ms between.
@@ -232,8 +230,8 @@ You might want to run the code on your kit to see if it does what you expect it
232230

233231
## Next Steps
234232

235-
From here, you should be able to make your robot move about in a controlled manner.
233+
From here, you should be able to make your robot move about.
236234
See if you can make your robot drive forwards to a given point, stop, turn around and then return to its starting point.
237235

238236
You might also like to see if you can make the code example above more concise by creating functions for different types of movement, such as moving and turning.
239-
[This](https://www.tutorialspoint.com/python3/python_functions.htm) tutorial is good if you want to learn about how functions work.
237+
[This tutorial](https://www.tutorialspoint.com/python3/python_functions.htm) is good if you want to learn about how functions work.

0 commit comments

Comments
 (0)