From 9f0cf3fece157458e9a141bd62ef85f3fc9e55dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81re=CC=81mie=20DUCASTEL?= Date: Sun, 12 Jul 2020 21:30:59 +0200 Subject: [PATCH] ringbit remote controlled car MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bouton A+B pour avancer bouton A seul pour tourner à gauche bouton B seul pour tourner à droite --- ringbit_car.py | 66 +++++++++++++++++++++++++++++++++++++++++++ ringbit_controller.py | 16 +++++++++++ 2 files changed, 82 insertions(+) create mode 100644 ringbit_car.py create mode 100644 ringbit_controller.py diff --git a/ringbit_car.py b/ringbit_car.py new file mode 100644 index 0000000..52ef7c5 --- /dev/null +++ b/ringbit_car.py @@ -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() \ No newline at end of file diff --git a/ringbit_controller.py b/ringbit_controller.py new file mode 100644 index 0000000..8b60f8e --- /dev/null +++ b/ringbit_controller.py @@ -0,0 +1,16 @@ +# emetteur radio microbit +from microbit import * +import radio + +radio.config(channel=77) +radio.on() + +display.scroll('on') + +while True: + if button_a.is_pressed() and button_b.is_pressed(): + radio.send('go') + elif button_a.is_pressed(): + radio.send('left') + elif button_b.is_pressed(): + radio.send('right') \ No newline at end of file