From 70b12a198e29ed9c2763c7c68e78ffaf0e2b79d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81re=CC=81mie=20DUCASTEL?= Date: Mon, 13 Jul 2020 13:33:21 +0200 Subject: [PATCH] Ring:Bit car : accelerometre MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Controle radio via l'accéleromètre du microbit télécommande --- ringbit_car.py | 23 +++++++++++++----- ringbit_controller.py | 55 +++++++++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/ringbit_car.py b/ringbit_car.py index 52ef7c5..aa32e57 100644 --- a/ringbit_car.py +++ b/ringbit_car.py @@ -28,37 +28,48 @@ class RingCar: self.right = MicrobitCServo(self.right_pin) self.left.go = self.left.counterclockwise self.right.go = self.right.clockwise - self.channel = 77 + self.left.backward = self.left.clockwise + self.right.backward = self.right.counterclockwise def forward(self): self.left.go() self.right.go() + microbit.display.show(microbit.Image.ARROW_S) + + def backward(self): + self.left.backward() + self.right.backward() + microbit.display.show(microbit.Image.ARROW_N) def stop(self): self.left.stop() self.right.stop() + microbit.display.clear() def go_left(self): self.left.stop() self.right.go() - microbit.display.show('a') + microbit.display.show(microbit.Image.ARROW_E) def go_right(self): self.right.stop() self.left.go() - microbit.display.show('b') + microbit.display.show(microbit.Image.ARROW_W) - def radio_controlled(self): - radio.config(channel = self.channel) + def radio_controlled(self, channel=77): + radio.config(channel=channel) radio.on() + microbit.display.scroll('OK {}'.format(channel)) while True: order = radio.receive() - if order == 'go': + if order == 'forward': self.forward() elif order == 'left': self.go_left() elif order == 'right': self.go_right() + if order == 'backward': + self.backward() else: self.stop() diff --git a/ringbit_controller.py b/ringbit_controller.py index 8b60f8e..e209977 100644 --- a/ringbit_controller.py +++ b/ringbit_controller.py @@ -1,16 +1,47 @@ # emetteur radio microbit -from microbit import * +import microbit 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(): +class RingCarRemote: + + def __init__(self, channel=77): + self.channel = channel + radio.config(channel=self.channel) + radio.on() + + def control_with_buttons(self): + microbit.display.scroll('boutons {}'.format(self.channel)) + while True: + if microbit.button_a.is_pressed() and microbit.button_b.is_pressed(): + self.send_forward() + elif microbit.button_a.is_pressed(): + self.send_left() + elif microbit.button_b.is_pressed(): + self.send_right() + + def control_with_accelerometer(self): + microbit.display.scroll('accel {}'.format(self.channel)) + while True: + if microbit.accelerometer.is_gesture('down'): + self.send_forward() + if microbit.accelerometer.is_gesture('left'): + self.send_left() + if microbit.accelerometer.is_gesture('right'): + self.send_right() + if microbit.accelerometer.is_gesture('up'): + self.send_backward() + + def send_left(self): radio.send('left') - elif button_b.is_pressed(): - radio.send('right') \ No newline at end of file + + def send_right(self): + radio.send('right') + + def send_forward(self): + radio.send('forward') + + def send_backward(self): + radio.send('backward') + +remote = RingCarRemote() +remote.control_with_accelerometer()