3. MOVEMotor motors
3.1. Set up the buggy
- class MOVEMotorMotors
buggy = MOVEMotor.MOVEMotorMotors() to be able to use the motor methods on the buggy.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
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(10, duration=5000) the buggy stops after 5 sec.buggy.forwards(10) 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.
speedvalues are integers or floats (decimals) from -10 to 10.Defaultspeedis 1.If speed < 0 the motor turns the wheel backwards.durationvalues are integers above 0.Defaultdurationis 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, 1000) and left_motor(2, duration=1000) and left_motor(speed=2, duration=1000) all run the left motor at speed to 2 for 1 sec.left_motor(5), runs the left motor at about half speed.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.left_motor(5)
left_motor(2, 5000), runs the left motor at speed 2 for 5 sec.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.left_motor(2, 5000)
3.3.2. right_motor
- right_motor(speed=1, duration=None)
- Make the right motor run.
speedvalues are integers or floats (decimals) from -10 to 10.Defaultspeedis 1.If speed < 0 the motor turns the wheel backwards.durationvalues are integers above 0.Defaultdurationis 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, 1000) and right_motor(2, duration=1000) and right_motor(speed=2, duration=1000) all run the right motor at speed 2 for 1sec.right_motor(speed=4, duration=3000), runs the right motor at speed 4 for 3 sec.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.right_motor(speed=4, duration=3000)
right_motor(-10), runs the right motor backwards at full speed.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.right_motor(-10)
3.3.3. stop_left
- stop_left()
- Stop the left motor.
from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.left_motor()
sleep(1000)
buggy.stop_left()
3.3.4. stop_right
- stop_right()
- Stop the right motor.
from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.right_motor(4)
sleep(2000)
buggy.stop_right()
3.4. Stop both motors
- stop()
- Stop both motors.
from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
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 backwards 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. Straight line control
forwards(speed=1, duration=None, decrease_left=0, decrease_right=0)backwards(speed=1, duration=None, decrease_left=0, decrease_right=0)decrease_left is used to reduce the motor speed on the left side in case the buggy drifts to the right due to the left motor being slightly faster than the right.decrease_right is used to reduce the motor speed on the right side in case the buggy drifts to the left due to the right motor being slightly faster than the left.decrease_left and decrease_right values used to give a straight line are best found by experimentation.3.5.1. forward
- forwards(speed=1, duration=None, decrease_left=0, decrease_right=0)
- Drive the buggy forward.
speedvalues are integers or floats (decimals) from 0 to 10.Defaultspeedis 1.durationvalues are integers above 0.Defaultdurationis None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping.decrease_leftanddecrease_righttake numbers from 0 to 20. These are converted to a percentage of the maximum analog motor speed of 255 (speed setting 10) so they have similar effect at any speed.decrease_leftanddecrease_rightdefault values are 0.
forwards(10, None, 6) and forwards(10, None, 6, 0) and forwards(speed=10, decrease_left=6) all set the speed to 10 with the left wheel slowed by roughly 2% (6/255).from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.forwards(speed=10, duration=5000, decrease_left=6, decrease_right=0)
3.5.2. backward
- backwards(speed=1, duration=None, decrease_left=0, decrease_right=0)
- Drive the buggy backwards.
speedvalues are integers or floats (decimals) from 0 to 10.Defaultspeedis 1.durationvalues are integers above 0.Defaultdurationis None.The motor will stop after a given duration in milliseconds.If the duration is None, the motor runs without stopping.decrease_leftanddecrease_righttake numbers from 0 to 20. These are converted to a percentage of the maximum analog motor speed of 255 (speed setting 10) so they have similar effect at any speed.decrease_leftanddecrease_rightdefault values are 0.
backwards(10, None, 0, 3) and backwards(speed=10, decrease_right=3) both set the speed to 10 with the right wheel slowed by roughly 1% (3/255).forwards(8, 4000, 0, 3); instead values are in their specified order.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.backwards(8, 4000, 0, 3)
Tasks
Write code to drive the buggy forward, as close as possible to a straight line, by experimenting with the
decrease_leftanddecrease_rightvalues.Write code to drive the buggy forwards and backwards, as close as possible to a straight line, by experimenting with the
decrease_leftanddecrease_rightvalues.
3.6. Turning
left(speed=1, radius=25, duration=None)right(speed=1, radius=25, duration=None)3.6.1. left
- left(speed=1, radius=25, duration=None)
- Drive the buggy to the left.
speedvalues are integers or floats (decimals) from -10 to 10.speedvalues above 0 drive the buggy forwards to the left.speedvalues below 0 drive the buggy backwards to the left.Defaultspeedis 1.radiusvalues are 4 to 800 (in cm)Defaultradiusis 25 (in cm).durationvalues are integers above 0.Defaultdurationis 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() and left(1, 25) and left(speed=1, radius=25) all set the speed to 1 with a left turn of radius 25cm.left(2, 50, 1000) and left(2, radius=50, duration=1000) and left(speed=2, radius=50, duration=1000) all set the speed to 2 with a left turn of radius 50cm for 1sec.left(speed=3, radius=20, duration=4000), drives the buggy forwards at speed 3 while it turns left in a circular path of approximate radius 20 cm for 4 secs.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.left(speed=3, radius=20, duration=4000)
Tasks
Write code to drive the buggy to the left at speed 2 in small circles of 10 cm radius.
Write code to drive the buggy to the left at speed 5 in medium circles of 50 cm radius.
Write code to drive the buggy to the left at speed 8 in circles of 20, 40 and 60 cm radius for 5 seconds each. Use a for-loop and a list of the radii.
3.6.2. right
- right(speed=1, radius=25, duration=None)
- Drive the buggy to the right.
speedvalues are integers or floats (decimals) from -10 to 10.speedvalues above 0 drive the buggy forwards to the right.speedvalues below 0 drive the buggy backwards to the right.Defaultspeedis 1.radiusvalues are 4 to 800 (in cm)Defaultradiusis 25 (in cm).durationvalues are integers above 0.Defaultdurationis 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() and right(1, 25) and right(speed=1, radius=25) all set the speed to 1 with radius 25cm.right(2, 50, 1000) and right(2, radius=50, duration=1000) and right(speed=2, radius=50, duration=1000) all set the speed to 2 with a right turn of radius 50cm for 1sec.right(speed=2, radius=40, duration=3000), drives the buggy forwards at speed 2 while it turns right in a circular path of approximate radius 40 cm for 3 secs.from microbit import *
import MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.right(speed=2, radius=40, duration=3000)
Tasks
Write code to drive the buggy to the right at speed 4 in small circles of 5 cm radius.
Write code to drive the buggy to the right at speed 7 in medium circles of 80 cm radius.
Write code to drive the buggy to the right at speed 10 in circles of increasing size. Use a range function to increase the radius every 4 seconds from 10 to 100 in steps of 10.
3.7. Spinning
spin_left(speed=1, duration=None)spin_right(speed=1, duration=None)- spin_left(speed=1, duration=None)
- Spin the buggy on the spot, to the left.
speedvalues are integers or floats (decimals) from 0 to 10.Defaultspeedis 1.durationvalues are integers above 0.Defaultdurationis 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.- spin_right(speed=1, duration=None)
- Spin the buggy on the spot, to the right.
speedvalues are integers or floats (decimals) from 0 to 10.Defaultspeedis 1.durationvalues are integers above 0.Defaultdurationis 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 MOVEMotor
# setup buggy
buggy = MOVEMotor.MOVEMotorMotors()
buggy.spin_right(2, 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 6 for 3 seconds.
Write code to spin the buggy to the left for 3 seconds then to right for 3 seconds at speed 6.
Write code to drive the buggy in a polygonal path (many straight sides) by combining short drives forwards with short spins.