Ring:Bit car : accelerometre

Controle radio via l'accéleromètre du microbit télécommande
This commit is contained in:
Jérémie DUCASTEL
2020-07-13 13:33:21 +02:00
parent 9f0cf3fece
commit 70b12a198e
2 changed files with 60 additions and 18 deletions

View File

@ -28,37 +28,48 @@ class RingCar:
self.right = MicrobitCServo(self.right_pin) self.right = MicrobitCServo(self.right_pin)
self.left.go = self.left.counterclockwise self.left.go = self.left.counterclockwise
self.right.go = self.right.clockwise self.right.go = self.right.clockwise
self.channel = 77 self.left.backward = self.left.clockwise
self.right.backward = self.right.counterclockwise
def forward(self): def forward(self):
self.left.go() self.left.go()
self.right.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): def stop(self):
self.left.stop() self.left.stop()
self.right.stop() self.right.stop()
microbit.display.clear()
def go_left(self): def go_left(self):
self.left.stop() self.left.stop()
self.right.go() self.right.go()
microbit.display.show('a') microbit.display.show(microbit.Image.ARROW_E)
def go_right(self): def go_right(self):
self.right.stop() self.right.stop()
self.left.go() self.left.go()
microbit.display.show('b') microbit.display.show(microbit.Image.ARROW_W)
def radio_controlled(self): def radio_controlled(self, channel=77):
radio.config(channel = self.channel) radio.config(channel=channel)
radio.on() radio.on()
microbit.display.scroll('OK {}'.format(channel))
while True: while True:
order = radio.receive() order = radio.receive()
if order == 'go': if order == 'forward':
self.forward() self.forward()
elif order == 'left': elif order == 'left':
self.go_left() self.go_left()
elif order == 'right': elif order == 'right':
self.go_right() self.go_right()
if order == 'backward':
self.backward()
else: else:
self.stop() self.stop()

View File

@ -1,16 +1,47 @@
# emetteur radio microbit # emetteur radio microbit
from microbit import * import microbit
import radio import radio
radio.config(channel=77) class RingCarRemote:
radio.on()
display.scroll('on') def __init__(self, channel=77):
self.channel = channel
radio.config(channel=self.channel)
radio.on()
while True: def control_with_buttons(self):
if button_a.is_pressed() and button_b.is_pressed(): microbit.display.scroll('boutons {}'.format(self.channel))
radio.send('go') while True:
elif button_a.is_pressed(): 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') radio.send('left')
elif button_b.is_pressed():
def send_right(self):
radio.send('right') radio.send('right')
def send_forward(self):
radio.send('forward')
def send_backward(self):
radio.send('backward')
remote = RingCarRemote()
remote.control_with_accelerometer()