2. Smart Cutebot Samples for Python


2.1. Add Python File

Download to unzip it: EF_Produce_MicroPython-master Go to Python editor

../../_images/cutebot-py-01.png

We need to add Cutebot.py for programming. Click “Load/Save” and then click “Show Files (1)” to see more choices, click “Add file” to add Cutebot.py from the unzipped package of EF_Produce_MicroPython-master.

../../_images/cutebot-py-02.png ../../_images/cutebot-py-03.png ../../_images/cutebot-py-04.png

2.2. Samples

2.3. Sample 1: Drive the car at a full speed.

from microbit import *
from Cutebot import *
ct = CUTEBOT()
ct.set_motors_speed(100, 100)

2.4. Result

  • The speed of the left and right wheels is at 100, the car moves forward at the full speed.

2.5. Sample 2: Turn the headlights on

from microbit import *
from Cutebot import *
ct = CUTEBOT()
ct.set_car_light(left, 0, 90, 90)
ct.set_car_light(right, 200, 200, 0)

2.6. Result

  • The two headlights light up in different colours.

2.7. Sample 3: Obstacles avoidance

from microbit import *
from Cutebot import *

dis = CUTEBOT()

while(True):
    i = dis.get_distance(0)
    if i>3 and i<20:
        dis.set_motors_speed(-50, 50)
        sleep(500)
    else:
        dis.set_motors_speed(50, 50)

2.8. Result

  • The Cutebot turns its direction once it detects any obstacle ahead of it.

2.9. Sample 4: Line-tracking

from microbit import *
from Cutebot import *

dis = CUTEBOT()

while(True):
    i = dis.get_tracking()
    if i == 10:
        dis.set_motors_speed(10, 50)
    if i == 1:
        dis.set_motors_speed(50, 10)   
    if i == 11:
        dis.set_motors_speed(25, 25)  

2.10. Result

  • The Cutebot drives along with the black line.

2.11. Sample 5: Control the servo

from microbit import *
from Cutebot import *

dis = CUTEBOT()

while(True):
    dis.set_servo(0,180)
    sleep(1000)
    dis.set_servo(0,0)
    sleep(1000)

2.12. Result

  • The servo connecting to S1 continues driving back and forth.

2.13. Relevant Case


2.14. Technique File