ringbit remote controlled car

bouton A+B pour avancer
bouton A seul pour tourner à gauche
bouton B seul pour tourner à droite
This commit is contained in:
Jérémie DUCASTEL
2020-07-12 21:30:59 +02:00
parent 40bf11cd7c
commit 9f0cf3fece
2 changed files with 82 additions and 0 deletions

66
ringbit_car.py Normal file
View File

@ -0,0 +1,66 @@
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()