
bouton A+B pour avancer bouton A seul pour tourner à gauche bouton B seul pour tourner à droite
66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
import microbit
|
|
import radio
|
|
|
|
# https://www.firialabs.com/blogs/lab-notes/continuous-rotation-servos-with-python-and-the-micro-bit
|
|
|
|
class MicrobitCServo:
|
|
""" Microbit continuous servo controller """
|
|
|
|
def __init__(self, pin):
|
|
self.pin = pin
|
|
|
|
def clockwise(self):
|
|
self.pin.write_analog(1023 * 1.0 / 20)
|
|
|
|
def counterclockwise(self):
|
|
self.pin.write_analog(1023 * 2.0 / 20)
|
|
|
|
def stop(self):
|
|
# self.pin.write_analog(0)
|
|
self.pin.write_analog(1023 * 1.5 / 20)
|
|
|
|
class RingCar:
|
|
|
|
def __init__(self):
|
|
self.left_pin = microbit.pin1
|
|
self.right_pin = microbit.pin2
|
|
self.left = MicrobitCServo(self.left_pin)
|
|
self.right = MicrobitCServo(self.right_pin)
|
|
self.left.go = self.left.counterclockwise
|
|
self.right.go = self.right.clockwise
|
|
self.channel = 77
|
|
|
|
def forward(self):
|
|
self.left.go()
|
|
self.right.go()
|
|
|
|
def stop(self):
|
|
self.left.stop()
|
|
self.right.stop()
|
|
|
|
def go_left(self):
|
|
self.left.stop()
|
|
self.right.go()
|
|
microbit.display.show('a')
|
|
|
|
def go_right(self):
|
|
self.right.stop()
|
|
self.left.go()
|
|
microbit.display.show('b')
|
|
|
|
def radio_controlled(self):
|
|
radio.config(channel = self.channel)
|
|
radio.on()
|
|
while True:
|
|
order = radio.receive()
|
|
if order == 'go':
|
|
self.forward()
|
|
elif order == 'left':
|
|
self.go_left()
|
|
elif order == 'right':
|
|
self.go_right()
|
|
else:
|
|
self.stop()
|
|
|
|
car = RingCar()
|
|
car.radio_controlled() |