|
The source code of all examples can be downloaded from a link in the right side bar. Moving robots are considered to be the prototype of robots, probably because it is more humanoid than other machines. Moreover self-driving robots are of great actuality because self-driving cars (and other vehicles like drones) will revolutionize mankind in the near future. The most elementary version of a self-driving car is a platform equipped with two electric motors and a caster wheel. Be activating the speed of the motors independently the vehicle can move forward, backward and turn. |
||
Example 1: Turn motor on/off with digital output |
Caution: Never connect a motor directly to the micro:bit pins. You can not power a DC motor directly from a micro:bit output pin because of the current limitation (5 mA) and more important because a motor acts like a solenoid, so turning a motor off generates voltage spikes caused by induction that may damage the micro:bit. Aim:
Remarks: |
Example 2: Regulate motor speed with PWM |
Aim:
Remarks: |
All the following examples require the Kitronik buggy chassis
and the corresponding motor driver and line follower boards.
|
|
Based on the principle to drive a motor as shown in the previous examples a small library module mbutils.py is written that is downloaded to the micro:bit's file system from the MBM distribution when the micro:bit is flashed. The current version can be downloaded from here. Reference code (may not be up-to-date): |
# mbutils.java # V1.0, Aug. 2017 from microbit import * ### motors/buggy motL = (pin16, pin0) motR = (pin8, pin12) bspeed = 40 def mot_rot(mot, speed): if speed > 0: mot[0].write_analog(10 * speed) mot[1].write_digital(0) elif speed < 0: mot[0].write_digital(0) mot[1].write_analog(-10 * speed) else: mot[0].write_digital(0) mot[1].write_digital(0) def buggy_setSpeed(speed): global bspeed bspeed = speed def buggy_forward(): mot_rot(motL, bspeed) mot_rot(motR, bspeed) |
def buggy_backward(): mot_rot(motL, -bspeed) mot_rot(motR, -bspeed) def buggy_stop(): mot_rot(motL, 0) mot_rot(motR, 0) def buggy_left(): mot_rot(motL, 0) mot_rot(motR, bspeed) def buggy_right(): mot_rot(motL, bspeed) mot_rot(motR, 0) def buggy_leftArc(reduce): mot_rot(motL, bspeed) mot_rot(motR, int(bspeed * reduce)) def buggy_rightArc(reduce): mot_rot(motL, int(bspeed * reduce)) mot_rot(motR, bspeed) ### Line Follower ldrL = pin2 ldrR = pin1 def isDark(ldr): return (ldr.read_analog() > 100) |
Example 3: Moving the buggy |
|
Example 4: Line follower |
|
Example 5: Remote control |
|