mirror of
https://github.com/alankrantas/microbit-micropython-cookbook.git
synced 2026-01-04 11:21:08 +01:00
Create controller.py
This commit is contained in:
44
rc_car/controller.py
Normal file
44
rc_car/controller.py
Normal file
@@ -0,0 +1,44 @@
|
||||
# For the micro:bit RC car controller
|
||||
|
||||
RADIO_CHANNEL = 42 # radio channel: 0~255
|
||||
|
||||
from microbit import display, Image, accelerometer, sleep
|
||||
from math import pi, atan2, sqrt
|
||||
import radio
|
||||
|
||||
radio.config(group=RADIO_CHANNEL)
|
||||
radio.on()
|
||||
|
||||
def rotationPitch():
|
||||
return atan2(
|
||||
accelerometer.get_y(),
|
||||
sqrt(accelerometer.get_x() ** 2 + accelerometer.get_z() ** 2)
|
||||
) * (180 / pi)
|
||||
|
||||
def rotationRoll():
|
||||
return atan2(
|
||||
accelerometer.get_x(),
|
||||
sqrt(accelerometer.get_y() ** 2 + accelerometer.get_z() ** 2)
|
||||
) * (180 / pi)
|
||||
|
||||
while True:
|
||||
pitch = rotationPitch()
|
||||
roll = rotationRoll()
|
||||
|
||||
if pitch < -30:
|
||||
display.show(Image.ARROW_N)
|
||||
radio.send('forward')
|
||||
elif pitch > 30:
|
||||
display.show(Image.ARROW_S)
|
||||
radio.send('backward')
|
||||
elif roll > 30:
|
||||
display.show(Image.ARROW_E)
|
||||
radio.send('left')
|
||||
elif roll < -30:
|
||||
display.show(Image.ARROW_W)
|
||||
radio.send('right')
|
||||
else:
|
||||
display.show(Image.SQUARE)
|
||||
radio.send('stop')
|
||||
|
||||
sleep(250)
|
||||
Reference in New Issue
Block a user