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
Copy file name to clipboardExpand all lines: tutorials/basic_motor_control.md
+86-88Lines changed: 86 additions & 88 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,27 +6,22 @@ title: Basic Motor Control
6
6
# Basic Motor Control
7
7
8
8
The aim of this tutorial is to get a motor turning with your kit.
9
-
To complete this tutorial, you'll need the following:
10
9
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:
16
19
* 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
23
20
* A soldering Iron
24
21
* Some solder wire
25
22
* Wire strippers
26
-
* A small slotted/flat blade screwdriver (for the _CamCon_ screws)
27
23
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.
30
25
31
26
32
27
## Motor Specification
@@ -35,7 +30,7 @@ There is a certain specification the motor(s) you use must meet.
35
30
The criteria are as follows:
36
31
37
32
* 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)
39
34
40
35
<divclass="info">
41
36
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
45
40
46
41
## Connecting a Motor
47
42
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
+
49
45
Like so:
50
46
51
47

52
48
53
49
<divclass="info">
54
50
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.
55
52
</div>
56
53
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 —
67
-
see the [Power Board](/docs/kit/power_board) and [Motor Board](/docs/kit/motor_board) documentation to see which is which.
68
-
69
-
<divclass="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.
74
56
75
57
76
58
## Some Code
@@ -80,101 +62,108 @@ You will want to ensure the motor is secured when testing.
80
62
A motor suddenly starting up during testing and moving across the table could cause a potential hazard.
81
63
</div>
82
64
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.
88
67
89
68
90
69
### Forwards & Backwards
91
70
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.
93
74
94
75
<divclass="info">
95
76
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
77
If the motor appears to be going in the wrong direction, just swap the motor's positive and negative wires over.
97
78
</div>
98
79
99
-
Here's the code:
80
+
As a first example:
100
81
101
82
~~~~~python
102
83
from sr.robot3 import*
103
-
import time
104
84
105
85
robot = Robot()
106
86
107
87
whileTrue:
108
88
robot.motor_board.motors[0].power =0.5
109
-
time.sleep(3.0)
89
+
robot.sleep(3.0)
110
90
111
91
robot.motor_board.motors[0].power =0
112
-
time.sleep(5)
92
+
robot.sleep(5)
113
93
114
94
robot.motor_board.motors[0].power =-0.5
115
-
time.sleep(3.0)
95
+
robot.sleep(3.0)
116
96
117
97
robot.motor_board.motors[0].power =0
118
-
time.sleep(5)
98
+
robot.sleep(5)
119
99
~~~~~
120
100
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
-
<divclass="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`.
128
102
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.
130
108
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.
132
114
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.
134
116
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.
136
118
137
119
<divclass="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.
139
121
</div>
140
122
123
+
For a comprehensive reference to the `motor` object, see the `sr.robot3` module's [Motors]({{ site.baseurl }}/programming/motors) page.
124
+
141
125
142
126
### Changing the Speed
143
127
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%.
146
130
147
131
Here's the code:
148
132
149
133
~~~~~python
150
134
from sr.robot3 import*
151
-
import time
152
135
153
136
robot = Robot()
154
137
155
138
whileTrue:
156
139
157
-
# ramp from 10% to 50%
158
-
for pwr inrange(20, 60, 10):
140
+
# ramp from 0% to 50%
141
+
for pwr inrange(0, 60, 10):
159
142
robot.motor_board.motors[0].power = pwr /100# Convert from percentage
160
-
time.sleep(0.1)
143
+
robot.sleep(0.1)
161
144
162
-
# ramp from 50% to 10%
163
-
for pwr inrange(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 inrange(50, -10, -10):
164
150
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)
166
155
167
-
# set power to 0 for a second
168
-
robot.motor_board.motors[0].power =0
169
-
time.sleep(1)
170
156
~~~~~
171
157
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.
173
163
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).
175
165
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.
178
167
Here's an example:
179
168
180
169
~~~~~python
@@ -190,39 +179,48 @@ The above would output:
190
179
3
191
180
~~~~~
192
181
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.
194
183
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.
197
186
198
187
The general format is:
199
188
~~~~~python
200
189
range(start, stop, step)
201
190
~~~~~
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`.
204
202
205
203
For example:
206
204
~~~~~python
207
205
range(5) # [0, 1, 2, 3, 4]
208
206
~~~~~
209
207
208
+
<divclass="info">
209
+
Note that the stop value is not included in the sequence.
210
+
</div>
211
+
210
212
If two arguments are provided, then these represent the start and stop with the step being set to `1`.
211
213
212
214
For example:
213
215
~~~~~python
214
216
range(2, 6) # [2, 3, 4, 5]
215
217
~~~~~
216
218
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.
222
221
223
-
Gives:
224
222
~~~~~python
225
-
range(10, 60, 10) # [10, 20, 30, 40, 50]
223
+
range(0, 60, 10) # [0, 10, 20, 30, 40, 50]
226
224
~~~~~
227
225
228
226
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
232
230
233
231
## Next Steps
234
232
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.
236
234
See if you can make your robot drive forwards to a given point, stop, turn around and then return to its starting point.
237
235
238
236
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