Skip to content

Commit 92e3b82

Browse files
authored
Merge pull request #253 from srobo/dgt/sr2022-update-motor-code-in-tutorial
Update the motor tutorial for SR2022
2 parents 001d0ee + 7a47e0f commit 92e3b82

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

tutorials/basic_motor_control.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,41 +107,41 @@ Doing this is actually very easy; the only thing you need to realise is that a p
107107
Here's the code:
108108

109109
~~~~~ python
110-
from sr.robot import *
110+
from sr.robot3 import *
111111
import time
112112

113113
R = Robot()
114114

115115
while True:
116116

117-
R.motors[0].m0.power = 50
117+
R.motor_board.motors[0].power = 0.5
118118
time.sleep(3.0)
119119

120-
R.motors[0].m0.power = 0
120+
R.motor_board.motors[0].power = 0
121121
time.sleep(1.4)
122122

123-
R.motors[0].m0.power = -50
123+
R.motor_board.motors[0].power = -0.5
124124
time.sleep(1)
125125

126-
R.motors[0].m0.power = 0
126+
R.motor_board.motors[0].power = 0
127127
time.sleep(4)
128128
~~~~~
129129

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

134134
<div class="info">
135-
`R.motors[0].m0.power = x` will set the power of the motor connected to output 0 (the `m0` part)
136-
on the first [motor board](/docs/kit/motor_board) (the `motors[0]` part)
137-
plugged in to a USB hub to `x`, where `x` is a value between `-100` and `100`,
138-
inclusive &mdash; in other words: `-100` &le; `x` &le; `100`.
135+
`R.motor_board.motors[0].power = x` will set the power of the motor connected to output 0 (the `motors[0]` part)
136+
on the first [motor board](/docs/kit/motor_board) (the `motor_board` part)
137+
plugged in to a USB hub to `x`, where `x` is a value between `-1` and `1`,
138+
inclusive &mdash; in other words: `-1` &le; `x` &le; `1`.
139139
</div>
140140

141-
So, `R.motors[0].m0.power = 50` sets the target power of the motor connected to output 0 on the first [motor board](/docs/kit/motor_board)
141+
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)
142142
plugged in to a USB hub to 50% forwards (i.e. a duty-cycle of 0.5 forwards).
143-
As you would expect, then, `R.motors[0].m0.power = -50` will put the this motor into reverse at 50% power.
144-
`R.motors[0].m0.power = 0` will output no power to the motor and stop it.
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.
145145

146146
So, if you put the above code on your robot,
147147
you should be able to see a motor spin forwards, stop, spin backwards, stop, and then repeat...
@@ -161,53 +161,53 @@ Our aim is to do the forwards and backwards bit (as above), but, before we loop
161161
Here's the code:
162162

163163
~~~~~ python
164-
from sr.robot import *
164+
from sr.robot3 import *
165165
import time
166166

167167
R = Robot()
168168

169169
while True:
170170

171-
R.motors[0].m0.power = 50
171+
R.motor_board.motors[0].power = 0.5
172172
time.sleep(3.0)
173173

174-
R.motors[0].m0.power = 0
174+
R.motor_board.motors[0].power = 0
175175
time.sleep(1.4)
176176

177-
R.motors[0].m0.power = -50
177+
R.motor_board.motors[0].power = -0.5
178178
time.sleep(1)
179179

180-
R.motors[0].m0.power = 0
180+
R.motor_board.motors[0].power = 0
181181
time.sleep(4)
182182

183183
# ^^ code from before ^^
184184

185-
# power up to 70 (from 10)
185+
# power up to 70% (from 10%)
186186
for pwr in range(10, 80, 10):
187-
R.motors[0].m0.power = pwr
187+
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
188188
time.sleep(0.1)
189189

190-
# power down from 70 (to 10)
190+
# power down from 70% (to 10%)
191191
for pwr in range(70, 0, -10):
192-
R.motors[0].m0.power = pwr
192+
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
193193
time.sleep(0.1)
194194

195195
# set power to 0 for a second
196-
R.motors[0].m0.power = 0
196+
R.motor_board.motors[0].power
197197
time.sleep(1)
198198

199-
# power "up" to -70 (from -10)
199+
# power "up" to -70% (from -10%)
200200
for pwr in range(-10, -80, -10):
201-
R.motors[0].m0.power = pwr
201+
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
202202
time.sleep(0.1)
203203

204-
# power "down" to -10 (from -70)
204+
# power "down" to -10% (from -70%)
205205
for pwr in range(-70, 0, 10):
206-
R.motors[0].m0.power = pwr
206+
R.motor_board.motors[0].power = pwr / 100 # Convert from percentage
207207
time.sleep(0.1)
208208

209209
# set power to 0 for a second
210-
R.motors[0].m0.power = 0
210+
R.motor_board.motors[0].power = 0
211211
time.sleep(1)
212212

213213
~~~~~

0 commit comments

Comments
 (0)