Skip to content

Commit be99a1c

Browse files
committed
Tutorial - basic movement
1 parent dc1df78 commit be99a1c

File tree

1 file changed

+94
-129
lines changed

1 file changed

+94
-129
lines changed

tutorials/basic_motor_control.md

Lines changed: 94 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,81 @@ layout: page
33
title: Basic Motor Control
44
---
55

6-
Basic Motor Control
7-
===================
6+
# Basic Motor Control
87

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

12-
* The [power board](/docs/kit/power_board)
11+
* The [Power Board](/docs/kit/power_board)
1312
* A battery (charged, of course)
14-
* A [motor board](/docs/kit/motor_board)
15-
* 2 large (7.5mm) green _CamCon_ connectors to plug the power board and motor board together
16-
* 2 lengths of a suitable gauge of wire for powering the motor board from the power board (one should be black/grey)
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)
1716
* A motor (see specification below)
18-
* A small (5mm) green _CamCon_ connector to plug the motor and motor board together
17+
* A small (5mm) green _CamCon_ connector to plug the motor into the Motor Board
1918
* 2 lengths of a suitable gauge of wire for your motor
20-
* A Micro USB cable
19+
* 2 Micro USB cables
2120
* A USB hub
22-
* A standard USB cable
21+
* A USB A to B cable (used to connect to the USB hub to the brain)
2322
* The memory stick
2423
* A soldering Iron
2524
* Some solder wire
2625
* Wire strippers
2726
* A small slotted/flat blade screwdriver (for the _CamCon_ screws)
2827

29-
You should be familiar with the setup for most of the above now, so it's just
30-
the motor-related parts that need explaining.
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
3130

3231

33-
Motor Specification
34-
-------------------
32+
## Motor Specification
3533

3634
There is a certain specification the motor(s) you use must meet.
3735
The criteria are as follows:
3836

3937
* 12V motor
40-
* A stall current of less than 10A (this is the current limit for the [motor boards](/docs/kit/motor_board))
38+
* A stall current of less than 10A (this is the current limit for the [Motor Boards](/docs/kit/motor_board))
4139

4240
<div class="info">
43-
When designing your robot you should bear in mind that while each motor board can deliver 10A on each output,
44-
all the power needs to go through the power board, which is limited to 30A overall.
45-
This means that across all the outputs for all the motors (as well as the rest of your kit),
46-
you can only draw up to 30A at any time.
41+
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.
42+
This means that across all the outputs for all the motors (as well as the rest of your kit), you can only draw up to 30A at any time.
4743
</div>
4844

49-
Connecting a Motor
50-
------------------
5145

52-
To plug the motor into the kit,
53-
you'll need to solder an appropriate gauge of wire to the terminals on the motor,
54-
and connect the other ends to the _CamCon_ connector.
46+
## Connecting a Motor
47+
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.
5549
Like so:
5650

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

59-
<div class="info">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.</div>
53+
<div class="info">
54+
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.
55+
</div>
6056

61-
Now you need to connect the motor to one of your motor boards.
57+
Now you need to connect the motor to one of your Motor Boards.
6258
You'll need to connect the following:
6359

64-
* Your motor into the motor 0 socket on the motor board
65-
* The micro USB cable from the motor board to the USB hub
66-
* The standard USB cable from the USB hub to the power board
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
6763

68-
This is almost ready, but the motor board also needs the power that it will be delivering to the motor.
64+
This is almost ready, but the Motor Board also needs the power that it will be delivering to the motor.
6965
This is done by connecting together the two larger _CamCon_ connectors, using the other two lengths of wire.
70-
Be sure that the cable connects the positive motor rail output ("+") of the power board to the positive power input of the motor board, and likewise for the ground ("-") output &mdash;
71-
see the [power board](/docs/kit/power_board) and [motor board](/docs/kit/motor_board)
72-
documentation to see which is which.
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.
7368

7469
<div class="info">
75-
You must always use black or grey for ground (0V) connections (and only for these), so that it's clear where these are.
70+
You must always use black for ground (0V) connections (and only for these), so that it's clear which these are.
7671
</div>
7772

78-
You can now connect this into the power board on one end, and the motor board power connection on the other.
73+
You can now connect this into the Power Board on one end, and the Motor Board power connection on the other.
7974

80-
Some Code
81-
--------
75+
76+
## Some Code
8277

8378
<div class="warning">
84-
You might want to ensure the motor won't take the kit anywhere when you
85-
press the run button when testing some of the below code (unless it's in a robot, of course).
79+
You will want to ensure the motor is secured when testing.
80+
A motor suddenly starting up during testing and moving across the table could cause a potential hazard.
8681
</div>
8782

8883
The example program we'll write will do a number of things with the motor:
@@ -96,11 +91,9 @@ To start off with, we'll just make a motor move forwards, then backwards, and th
9691

9792
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.
9893

99-
<div class="warning">
100-
The actual direction of travel of a motor, when mounted on a robot,
101-
will depend on its orientation and the way in which the wires are connected to the motor board.
102-
If the motor appears to be going in the wrong direction,
103-
just swap the motor's positive and negative wires over.
94+
<div class="info">
95+
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.
96+
If the motor appears to be going in the wrong direction, just swap the motor's positive and negative wires over.
10497
</div>
10598

10699
Here's the code:
@@ -109,118 +102,79 @@ Here's the code:
109102
from sr.robot3 import *
110103
import time
111104

112-
R = Robot()
105+
robot = Robot()
113106

114107
while True:
115-
116-
R.motor_board.motors[0].power = 0.5
108+
robot.motor_board.motors[0].power = 0.5
117109
time.sleep(3.0)
118110

119-
R.motor_board.motors[0].power = 0
120-
time.sleep(1.4)
111+
robot.motor_board.motors[0].power = 0
112+
time.sleep(5)
121113

122-
R.motor_board.motors[0].power = -0.5
123-
time.sleep(1)
114+
robot.motor_board.motors[0].power = -0.5
115+
time.sleep(3.0)
124116

125-
R.motor_board.motors[0].power = 0
126-
time.sleep(4)
117+
robot.motor_board.motors[0].power = 0
118+
time.sleep(5)
127119
~~~~~
128120

129-
You're familiar with the first few lines; in fact, the only lines you may not be familiar with are the `R.motor_board...` lines.
130-
For a comprehensive reference to the `motor` object, see the `sr.robot3` module's [Motors](/docs/programming/motors) page.
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.
131123
But, to summarise:
132124

133125
<div class="info" markdown="1">
134-
`R.motor_board.motors[0].power = x` will set the power of the motor connected to output 0 (the `motors[0]` part)
135-
on the first [motor board](/docs/kit/motor_board) (the `motor_board` part)
136-
plugged in to a USB hub to `x`, where `x` is a value between `-1` and `1`,
137-
inclusive &mdash; in other words: `-1` &le; `x` &le; `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`.
138127
</div>
139128

140-
So, `R.motor_board.motors[0].power = 0.5` sets the target power of the motor connected to output 0 on the first [motor board](/docs/kit/motor_board)
141-
plugged in to a USB hub to 50% forwards (i.e. a duty-cycle of 0.5 forwards).
129+
So, `robot.motor_board.motors[0].power = 0.5` sets the target power of the motor connected to output 0 to 50% forwards.
142130

143-
As you would expect, then, `R.motor_board.motors[0].power = -0.5` will put the this motor into reverse at 50% power.
144-
`R.motor_board.motors[0].power = 0` will output no power to the motor and stop it.
131+
As you would expect, then, `robot.motor_board.motors[0].power = -0.5` will put the this motor into reverse at 50% power.
145132

146-
So, if you put the above code on your robot,
147-
you should be able to see a motor spin forwards, stop, spin backwards, stop, and then repeat...
133+
`robot.motor_board.motors[0].power = 0` will output no power to the motor and stop it.
134+
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...
148136

149137
<div class="info" markdown="1">
150-
If you find that the motor doesn't turn when you run the above code,
151-
check that you've got all the cables connected to the right places,
152-
in particular note that the motor board has _two_ outputs.
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.
153139
</div>
154140

141+
155142
### Changing the Speed
156143

157-
Now we're going to modify the program to vary the speed of the motor.
158-
Our aim is to do the forwards and backwards bit (as above), but, before we loop round again,
159-
ramp the power up to 70%, then down to -70%, and then back to 0 (all in steps of 10%).
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%).
160146

161147
Here's the code:
162148

163149
~~~~~ python
164150
from sr.robot3 import *
165151
import time
166152

167-
R = Robot()
153+
robot = Robot()
168154

169155
while True:
170156

171-
R.motor_board.motors[0].power = 0.5
172-
time.sleep(3.0)
173-
174-
R.motor_board.motors[0].power = 0
175-
time.sleep(1.4)
176-
177-
R.motor_board.motors[0].power = -0.5
178-
time.sleep(1)
179-
180-
R.motor_board.motors[0].power = 0
181-
time.sleep(4)
182-
183-
# ^^ code from before ^^
184-
185-
# power up to 70% (from 10%)
186-
for pwr in range(10, 80, 10):
187-
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
157+
# ramp from 10% to 50%
158+
for pwr in range(20, 60, 10):
159+
robot.motor_board.motors[0].power = pwr / 100 # Convert from percentage
188160
time.sleep(0.1)
189161

190-
# power down from 70% (to 10%)
191-
for pwr in range(70, 0, -10):
192-
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
162+
# ramp from 50% to 10%
163+
for pwr in range(50, 0, -10):
164+
robot.motor_board.motors[0].power = pwr / 100 # Convert from percentage
193165
time.sleep(0.1)
194166

195167
# set power to 0 for a second
196-
R.motor_board.motors[0].power
168+
robot.motor_board.motors[0].power = 0
197169
time.sleep(1)
198-
199-
# power "up" to -70% (from -10%)
200-
for pwr in range(-10, -80, -10):
201-
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
202-
time.sleep(0.1)
203-
204-
# power "down" to -10% (from -70%)
205-
for pwr in range(-70, 0, 10):
206-
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
207-
time.sleep(0.1)
208-
209-
# set power to 0 for a second
210-
R.motor_board.motors[0].power = 0
211-
time.sleep(1)
212-
213170
~~~~~
214171

215-
Again, as you've seen most of that before, it shouldn't be too difficult to get your head around.
216-
The `for` loop may be new, however.
172+
You have seen some of those bits of code before but the thing that may be new is the `for` loop.
217173

218-
The [`for`](https://docs.python.org/tutorial/controlflow.html#for-statements)
219-
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]`).
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]`).
220175

221176
For a comprehensive introduction to to `list`s, have a look at [this WikiBooks article](https://en.wikibooks.org/wiki/Python_Programming/Lists).
222-
The `for` loop will iterate over the `list` (i.e. take each element in turn)
223-
and make it available in the loop's body as the variable after the the `for` keyword.
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.
224178
Here's an example:
225179

226180
~~~~~ python
@@ -238,30 +192,41 @@ The above would output:
238192

239193
Then there's the [`range()`](https://docs.python.org/3/library/stdtypes.html#typesseq-range) object.
240194

241-
`range()` objects represent a sequence of numbers. They can be constructed with
242-
one, two or three arguments to describe what the sequence of numbers should be.
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.
243197

244-
If only one argument is provided then the sequence starts at `0` and stops just
245-
before the given number. Otherwise the first argument is the number to start at,
246-
the second argument is the number to stop before and the (optional) third
247-
argument is how big the steps should be (by default steps are `1`).
198+
The general format is:
199+
~~~~~ python
200+
range(start, stop, step)
201+
~~~~~
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.
204+
205+
For example:
206+
~~~~~ python
207+
range(5) # [0, 1, 2, 3, 4]
208+
~~~~~
209+
210+
Taking `range(10, 60, 10)`, as a second example:
248211

249-
So, based on this, `range(3)` would represent the sequence `0`, `1`, `2` because
250-
it is shorthand for `range(0, 3, 1)`.
212+
* Start at 10
213+
* Go up in steps of 10
214+
* For all values less than 60
251215

252-
So, taking `range(10, 80, 10)`, for example, would output `10` as the first element,
253-
then `20, 30, ...` up until `x=10+i*10` for some `i` where `i` ensures `x < stop` (which, in this case, is `80`).
254-
So, the `80` we've used could equally have been `77` or even `71` and the outputted sequence would still be `10`, `20`, `30`, `40`, `50`, `60`, `70`.
216+
Gives:
217+
~~~~~ python
218+
range(10, 60, 10) # [10, 20, 30, 40, 50]
219+
~~~~~
255220

256-
Putting all of that together should mean you understand the above code.
221+
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.
222+
This will give a slow ramp (~500ms) rather than a step in power straight to 50%.
257223
You might want to run the code on your kit to see if it does what you expect it to.
258224

259225

260-
Next Steps
261-
----------
226+
## Next Steps
262227

263228
From here, you should be able to make your robot move about in a controlled manner.
264229
See if you can make your robot drive forwards to a given point, stop, turn around and then return to its starting point.
265230

266-
You might also like to see if you can make the larger code example above more concise by creating functions for the repetitive parts.
267-
[This](https://www.tutorialspoint.com/python3/python_functions.htm) tutorial seems good if you're interested.
231+
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.
232+
[This](https://www.tutorialspoint.com/python3/python_functions.htm) tutorial is good if you want to learn about how functions work.

0 commit comments

Comments
 (0)