Skip to content

Instantly share code, notes, and snippets.

@ricardodeazambuja
Last active August 12, 2020 19:08
Show Gist options
  • Select an option

  • Save ricardodeazambuja/80a08c2dfa80e6f4de3d8d8dd923fe5c to your computer and use it in GitHub Desktop.

Select an option

Save ricardodeazambuja/80a08c2dfa80e6f4de3d8d8dd923fe5c to your computer and use it in GitHub Desktop.
ADXL377 datalogging using Adafruit Feather M0 Express (almost 8 seconds of data with sample rate between 100 and 120Hz, 4 between 200Hz and 210Hz)
import storage
storage.remount("/", readonly=True) # to enable the code to save data to the flash this needs to be False
# After the system is reset with the above line set to False, it will not allow to update
# anything using the virtual USB drive. Therefore it's necessary to connect using the serial USB (minicom),
# enter the REPL (contr+c to cancel code.py if it's running and any key) and paste the line below:
# import os; os.remove("/boot.py"); fs=open("/boot.py", "w"); fs.write("import storage\nstorage.remount(\"/\", readonly=True)"); fs.close()
#
# Usage
#
# The system will blink 3 times (1s each) and it will turn on the red led and start recording. After recording
# it will save the data inside "/data/acc_xxx.bin" with the names incrementing automatically (or it will create
# the first file 001, but the data directory MUST exist).
# The virtual USB drive only updates after ejecting and resetting the uC. After that, just copy and past the files.
# Data is recorded directly using struct.pack('<Qlll'...) so you just need to keep reading until the end of the file:
#
# values = []
# with open(filename, 'rb') as fp:
# while True:
# data = fp.read(20) # Qlll => 20 bytes
# if (not data) or (len(data)<20):
# break
# values.append(struct.unpack('<Qlll',data))
# values = np.array(values)
#
from time import monotonic_ns, sleep
from board import A1, A2, A3, D13
from digitalio import DigitalInOut, Direction
from analogio import AnalogIn
from struct import pack_into
import micropython as mp
from gc import collect, disable, mem_free
from os import listdir
collect()
DCT = mp.const(6) # in seconds
CDBS = mp.const(6) # in seconds
BF = mp.const(910)
DS = mp.const(20)
DD = mp.const("/data/")
# Built in red LED
led = DigitalInOut(D13)
led.direction = Direction.OUTPUT
for i in range(CDBS):
led.value = not led.value
sleep(1)
# ADXL377 should be connected to these pins:
x = AnalogIn(A1)
y = AnalogIn(A2)
z = AnalogIn(A3)
fl = listdir(DD)
if fl:
fl = fl[-1][:-4]
nf = DD + fl[:-3] + "{:03d}.bin".format(int(fl[-3:])+1)
del fl
else:
nf = mp.const(DD+"acc_001.bin")
try:
disable()
collect()
tmp = bytearray(DS*BF)
tmp_view = memoryview(tmp)
collect()
r = range(BF)
with open(nf, "xb", buffering=0) as fp:
led.value = True
total_time_us = int(DCT*1E9 + monotonic_ns())
collect()
curr_time = monotonic_ns()
collect()
while curr_time < total_time_us:
collect()
for i in r:
curr_time = monotonic_ns()
collect()
pack_into('<Qlll',tmp_view,i*DS,curr_time,x.value,y.value,z.value)
collect()
fp.write(tmp_view)
finally:
led.value = False
#
# It goes from 214 to 230Hz and saves ~4s
#
from time import monotonic_ns, sleep
from board import A1, A2, A3, D13
from digitalio import DigitalInOut, Direction
from analogio import AnalogIn
from struct import pack_into
import micropython as mp
from gc import collect, disable, mem_free
from os import listdir
collect()
DCT = mp.const(4) # in seconds
CDBS = mp.const(6) # in seconds
BF = mp.const(900)
DS = mp.const(20)
DD = mp.const("/data/")
# Built in red LED
led = DigitalInOut(D13)
led.direction = Direction.OUTPUT
for i in range(CDBS):
led.value = not led.value
sleep(1)
# ADXL377 should be connected to these pins:
x = AnalogIn(A1)
y = AnalogIn(A2)
z = AnalogIn(A3)
fl = listdir(DD)
if fl:
fl = fl[-1][:-4]
nf = DD + fl[:-3] + "{:03d}.bin".format(int(fl[-3:])+1)
del fl
else:
nf = mp.const(DD+"acc_001.bin")
try:
disable()
collect()
tmp = bytearray(DS*BF)
tmp_view = memoryview(tmp)
collect()
r = range(BF)
collect()
with open(nf, "xb", buffering=0) as fp:
led.value = True
total_time_us = int(DCT*1E9 + monotonic_ns())
collect()
curr_time = monotonic_ns()
collect()
while curr_time < total_time_us:
collect()
for i in r:
pack_into('<Qlll',tmp_view,i*DS,monotonic_ns(),x.value,y.value,z.value)
collect()
fp.write(tmp_view)
curr_time = monotonic_ns()
finally:
led.value = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment