3. Maqueen motors
3.1. Set up the buggy
- class MaqueenMotors
- Set up the buggy’s motors for use.Use
buggy = maqueen.MaqueenMotors()
to use the motor on the buggy.
from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
3.2. Duration parameter
None
. If the duration is None
or is not specified the motor will run continuously until another command is sent to it.buggy.forwards(1, duration=5000)
the buggy stops after 5 sec.buggy.forwards(1)
the buggy runs forwards continuously.3.3. Independent motor control
left_motor(speed=1, duration=None)
runs the left motor.right_motor(speed=1, duration=None)
runs the right motor.stop_left()
stops the left motor.stop_right()
stops the right motor.3.3.1. left_motor
- left_motor(speed=1, duration=None)
- Make the left motor run.
speed
values are integers from -5 to 5.Defaultspeed
is 1.If speed < 0 the motor turns the wheel backwards.duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping.
left_motor()
and left_motor(1)
and left_motor(speed=1)
all set the speed to 1.left_motor(2, 3000)
and left_motor(2, duration=3000)
and left_motor(speed=2, duration=3000)
all run the left motor at speed 2 for 3 sec.left_motor(5)
, runs the left motor at full speed.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.left_motor(5)
left_motor(2, 5000)
, runs the left motor at speed 2 for 5 sec.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.left_motor(2, 5000)
3.3.2. right_motor
- right_motor(speed=1, duration=None)
- Make the left motor run.
speed
values are integers from -5 to 5.Defaultspeed
is 1.If speed < 0 the motor turns the wheel backwards.duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping.
right_motor()
and right_motor(1)
and right_motor(speed=1)
all set the speed to 1.right_motor(2, 4000)
and right_motor(2, duration=4000)
and right_motor(speed=2, duration=4000)
all run the right motor at speed 2 for 4sec.right_motor(speed=4, duration=3000)
, runs the right motor at speed 4 for 3 sec.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.right_motor(speed=4, duration=3000)
right_motor(-5)
, runs the right motor backwards at full speed.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.right_motor(-5)
3.3.3. stop_left
- stop_left()
- Stop the left motor.
from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.left_motor()
sleep(2000)
buggy.stop_left()
3.3.4. stop_right
- stop_right()
- Stop the right motor.
from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.right_motor(4)
sleep(2000)
buggy.stop_right()
3.4. Stop both motors
- stop()
- Stop both motors.
from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.left_motor(5)
buggy.right_motor(2)
sleep(1500)
buggy.stop()
Tasks
Write code to drive the left motor at speed 2 for 1 second, stop it, run the right motor at speed 2 for 1 sec then stop it.
Write code to drive the right motor at speed 3 while the left motor runs at speed 2 for 3 sec then stop it.
Write code to drive the left motor at speed 3 while the right motor runs at speed 2 for 3 sec then stop it.
Write code that drives the left side faster than the right side then the right side faster the left side so that it zig zags for 5 sec then stop it.
Write code so that the buggy repetitively zig zags forwards for 5 zigs and zags then forwards forwards for 5 zigs and zags.
Modify the zig zag code so that it uses variables for the 2 motor speeds, the number of zig zags forwards and backward, and the time for each zig and zag.
3.5. forwards and backwards
forwards(speed=1, duration=None)
backwards(speed=1, duration=None)
3.5.1. forwards
- forwards(speed=1, duration=None)
- Drive the buggy forward.
speed
values are integers from 0 to 5.Defaultspeed
is 1.duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.
from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.forwards(speed=5, duration=4000)
3.5.2. backwards
- backwards(speed=1, duration=None)
- Drive the buggy backwards.
speed
values are integers from 0 to 5.Defaultspeed
is 1.duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping.
from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.backwards(5, 3000)
Tasks
Write code to drive the buggy forwards at speed 3 for 3 seconds.
Write code to drive the buggy backwards at speed 1 for 2 seconds.
Write code to drive the buggy forwards at speed 1 for 3 seconds then backwards at speed 1 for 3 seconds.
3.6. Turning
left(tightness=5, duration=None)
right(tightness=5, duration=None)
3.6.1. left
- left(tightness=5, duration=None)
- Drive the buggy to the left.
tightness
values are integers from 1 to 5Defaulttightness
is 5 (a tight turn).duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping, until another command is sent to the motor.
left(tightness=5, duration=4000)
, turns the buggy left through a tight turn for 4 secs.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.left(tightness=5, duration=4000)
Tasks
Write code to drive the buggy to the left at tightness 3 for 5 seconds.
Write code to drive the buggy to the left at tightness 1 for 5 seconds.
Write code to drive the buggy to the left at increasing tightness. Use a for-loop to change the tightness from 1 to 5, with each turn lasting for 2 seconds.
3.6.2. right
- right(tightness=5, duration=None)
- Drive the buggy to the right.
tightness
values are integers from 1 to 5Defaulttightness
is 5 (a tight turn).duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping, until another command is sent to the motor.
right(5, 4000)
, turns the buggy right through a tight turn for 4 secs.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.right(5, 4000)
Tasks
Write code to drive the buggy to the right at tightness 4 for 2 seconds.
Write code to drive the buggy to the right at tightness 1 for 2 seconds.
Write code to drive the buggy to the right at decreasing tightness. Use a for-loop to change the tightness from 5 to 1, with each turn lasting for 2 seconds.
3.7. Spinning
spin_left(speed=1, duration=None)
spin_right(speed=1, duration=None)
3.7.1. spin left
- spin_left(speed=1, duration=None)
- Spin the buggy on the spot.
speed
values are integers from 0 to 5.Defaultspeed
is 1.duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping, until another command is sent to the motor.
spin_left()
and spin_left(1)
and spin_left(speed=1)
all spin the buggy to the left at speed 1.spin_left(3, 2000)
and spin_left(3, duration=2000)
and spin_left(speed=3, duration=2000)
all spin the buggy to the left at speed 3 for 2 secs.3.7.2. spin right
- spin_right(speed=1, duration=None)
- Spin the buggy on the spot.
speed
values are integers from 0 to 5.Defaultspeed
is 1.duration
values are integers above 0.Defaultduration
is None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping, until another command is sent to the motor.
spin_right(2, 4000)
, spins the buggy to the right at speed 2 for 4 secs.from microbit import *
import maqueen
# setup buggy
buggy = maqueen.MaqueenMotors()
buggy.spin(2, 'right', 4000)
Tasks
Write code to spin the buggy to the left at speed 4 for 5 seconds.
Write code to spin the buggy to the right at speed 2 for 3 seconds.
Write code to spin the buggy to the left for 3 seconds then to right for 3 seconds at speed 4.
Write code to drive the buggy in a polygonal path (many straight sides) by combining short drives forwards with short spins.