Basic sensor readout and radio comms

master
Jamie Munro 2 years ago
commit 2541f3e13a
  1. 10
      README.md
  2. 88
      controller.py
  3. 34
      receiver.py

@ -0,0 +1,10 @@
# Micropython on Micro:bit
To flash micro:bit with python program
```
uflash program.py
```
To access micro:bit serial port
```
sudo screen /dev/ttyACM0 115200
```

@ -0,0 +1,88 @@
from microbit import *
import radio
MODE = True #True for radio, False for serial
CONTROL_CHAR = "*"
RADIO_GROUP = 222
RADIO_CHANNEL = 5
RADIO_LENGTH = 36
DELAY = 4 #this puts us approximately in line with PS4 poll rate (not accounting for processing time) (250hz)
conf = {
"a": True,
"b": True,
"logo": True,
"accelerometer": True,
"light": True,
"heading": True,
"temperature": False,
"time": False,
}
def setup():
print("Starting...")
if MODE:
print("Turning radio on...")
radio.on()
radio.config(group=RADIO_GROUP, channel=RADIO_CHANNEL, length=RADIO_LENGTH)
if not compass.is_calibrated():
print("Calibrating compass...")
compass.calibrate
while True:
loop()
sleep(DELAY)
def loop():
if conf["a"]: a = button_a.is_pressed()
if conf["b"]: b = button_b.is_pressed()
if conf["logo"]: logo = pin_logo.is_touched()
if conf["accelerometer"]:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
if conf["light"]: light = display.read_light_level()
if conf["heading"]: heading = compass.heading()
if conf["temperature"]: temp = temperature()
if conf["time"]: time = running_time()
# print("-----------------------------------")
# if conf["a"]: print("a: ", a)
# if conf["b"]: print("b: ", b)
# if conf["logo"]: print("logo: ", logo)
# if conf["accelerometer"]:
# print("x: ", x)
# print("y: ", y)
# print("z: ", z)
# if conf["light"]: print("light: ", light)
# if conf["heading"]: print("heading: ", heading)
# if conf["temperature"]: print("temp: ", temp)
# if conf["time"]: print("time: ", time)
# print("-----------------------------------")
msg = ""
msg += CONTROL_CHAR
if conf["a"]: msg += "a" + str(int(a))
if conf["b"]: msg += "b" + str(int(b))
if conf["logo"]: msg += "c" + str(int(logo))
if conf["accelerometer"]:
msg += 'x{:+5d}'.format(x) + 'y{:+5d}'.format(y) + 'z{:+5d}'.format(z)
if conf["light"]: msg += 'l{:3d}'.format(light)
if conf["heading"]: msg += 'h{:3d}'.format(heading)
if MODE:
radio.send(msg)
else:
print(msg)
setup()

@ -0,0 +1,34 @@
from microbit import *
import radio
CONTROL_CHAR = "*"
RADIO_GROUP = 222
RADIO_CHANNEL = 5
RADIO_LENGTH = 36
MSG_LENGTH = RADIO_LENGTH - 3
DELAY = 0
def setup():
print("Starting...")
print("Turning radio on...")
radio.on()
radio.config(group=RADIO_GROUP, channel=RADIO_CHANNEL, length=RADIO_LENGTH)
while True:
loop()
sleep(DELAY)
def loop():
#get message from buffer
incoming = radio.receive()
#perform a bunch of sanity checks to insure this is a valid message
#TODO: investigate performance impact of sanity checks, maybe this should be done at app level
#TODO: investigate performance speed up of direct byte encoding instead of string manipulation
if incoming != None and incoming[0] == CONTROL_CHAR and len(incoming) == MSG_LENGTH:
print(incoming)
setup()
Loading…
Cancel
Save