You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The aim of this tutorial is to get a motor turning with your kit.
10
9
To complete this tutorial, you'll need the following:
11
10
12
-
* The [power board](/docs/kit/power_board)
11
+
* The [Power Board](/docs/kit/power_board)
13
12
* 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)
17
16
* 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
19
18
* 2 lengths of a suitable gauge of wire for your motor
20
-
*A Micro USB cable
19
+
*2 Micro USB cables
21
20
* 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)
23
22
* The memory stick
24
23
* A soldering Iron
25
24
* Some solder wire
26
25
* Wire strippers
27
26
* A small slotted/flat blade screwdriver (for the _CamCon_ screws)
28
27
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
31
30
32
31
33
-
Motor Specification
34
-
-------------------
32
+
## Motor Specification
35
33
36
34
There is a certain specification the motor(s) you use must meet.
37
35
The criteria are as follows:
38
36
39
37
* 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))
41
39
42
40
<divclass="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.
47
43
</div>
48
44
49
-
Connecting a Motor
50
-
------------------
51
45
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.
55
49
Like so:
56
50
57
51

58
52
59
-
<divclass="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
+
<divclass="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>
60
56
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.
62
58
You'll need to connect the following:
63
59
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
67
63
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.
69
65
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 —
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 —
67
+
see the [Power Board](/docs/kit/power_board) and [Motor Board](/docs/kit/motor_board) documentation to see which is which.
73
68
74
69
<divclass="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.
76
71
</div>
77
72
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.
79
74
80
-
Some Code
81
-
--------
75
+
76
+
## Some Code
82
77
83
78
<divclass="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.
86
81
</div>
87
82
88
83
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
96
91
97
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.
98
93
99
-
<divclass="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
+
<divclass="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.
104
97
</div>
105
98
106
99
Here's the code:
@@ -109,118 +102,79 @@ Here's the code:
109
102
from sr.robot3 import*
110
103
import time
111
104
112
-
R= Robot()
105
+
robot= Robot()
113
106
114
107
whileTrue:
115
-
116
-
R.motor_board.motors[0].power =0.5
108
+
robot.motor_board.motors[0].power =0.5
117
109
time.sleep(3.0)
118
110
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)
121
113
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)
124
116
125
-
R.motor_board.motors[0].power =0
126
-
time.sleep(4)
117
+
robot.motor_board.motors[0].power =0
118
+
time.sleep(5)
127
119
~~~~~
128
120
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.
131
123
But, to summarise:
132
124
133
125
<divclass="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 — in other words: `-1`≤`x`≤`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`.
138
127
</div>
139
128
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.
142
130
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.
145
132
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...
148
136
149
137
<divclass="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.
153
139
</div>
154
140
141
+
155
142
### Changing the Speed
156
143
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%).
160
146
161
147
Here's the code:
162
148
163
149
~~~~~python
164
150
from sr.robot3 import*
165
151
import time
166
152
167
-
R= Robot()
153
+
robot= Robot()
168
154
169
155
whileTrue:
170
156
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 inrange(10, 80, 10):
187
-
R.motor_board.motors[0].power = pwr /100# Convert from percentage
157
+
# ramp from 10% to 50%
158
+
for pwr inrange(20, 60, 10):
159
+
robot.motor_board.motors[0].power = pwr /100# Convert from percentage
188
160
time.sleep(0.1)
189
161
190
-
#power down from 70% (to 10%)
191
-
for pwr inrange(70, 0, -10):
192
-
R.motor_board.motors[0].power = pwr /100# Convert from percentage
162
+
#ramp from 50% to 10%
163
+
for pwr inrange(50, 0, -10):
164
+
robot.motor_board.motors[0].power = pwr /100# Convert from percentage
193
165
time.sleep(0.1)
194
166
195
167
# set power to 0 for a second
196
-
R.motor_board.motors[0].power
168
+
robot.motor_board.motors[0].power=0
197
169
time.sleep(1)
198
-
199
-
# power "up" to -70% (from -10%)
200
-
for pwr inrange(-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 inrange(-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
-
213
170
~~~~~
214
171
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.
217
173
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]`).
220
175
221
176
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.
224
178
Here's an example:
225
179
226
180
~~~~~python
@@ -238,30 +192,41 @@ The above would output:
238
192
239
193
Then there's the [`range()`](https://docs.python.org/3/library/stdtypes.html#typesseq-range) object.
240
194
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.
243
197
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:
248
211
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
251
215
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
+
~~~~~
255
220
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%.
257
223
You might want to run the code on your kit to see if it does what you expect it to.
258
224
259
225
260
-
Next Steps
261
-
----------
226
+
## Next Steps
262
227
263
228
From here, you should be able to make your robot move about in a controlled manner.
264
229
See if you can make your robot drive forwards to a given point, stop, turn around and then return to its starting point.
265
230
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