TPBot Samples for Python
3. TPBot Samples for Python#
3.1. Add Python File#
We need to add TPBot.py for programming. Click “Load/Save” and then click “Show Files (1)” to see more choices, click “Add file” to add TPBot.py from the unzipped package of EF_Produce_MicroPython-master.
Sample 1: Drive the car at a full speed.#
from microbit import * from TPBot import * tp = TPBOT() tp.set_motors_speed(100,100)
The speed of the left and right wheels is at 100, the car moves forward at the full speed.
Sample 2: Turn the headlights on in random colors#
from microbit import * from TPBot import * import random tp = TPBOT() while True: R = random.randint(0,255); G = random.randint(0,255); B = random.randint(0,255); tp.set_car_light(R,G,B) sleep(500)
The headlights light up in different colours at random.
Sample 3: Obstacles avoidance#
from microbit import * from TPBot import * tp = TPBOT() while True: i = tp.get_distance(0) if i>3 and i<30: tp.set_motors_speed(-50, 50) sleep(500) else: tp.set_motors_speed(50, 50)
The TPBot turns its direction once it detects any obstacle ahead of it.
The TPBot drives along with the black line.
Sample 5: Control the servo#
from microbit import * from TPBot import * tp = TPBOT() while True: tp.set_servo(1,180) sleep(1000) tp.set_servo(1,0) sleep(1000)
The servo connecting to S1 continues driving back and forth.