-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
160 lines (129 loc) · 5.42 KB
/
Copy pathmain.py
File metadata and controls
160 lines (129 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Controller for the RadiaCode-10X Radiation Detector. Receives and stores data
from the device, takes spectrogram at set intervals. Radiacode control based on
https://github.com/cdump/radiacode
"""
import machine
import time
import os
import sdcard
from radiacode import RadiaCode
from radiacode.transports.bluetooth import DeviceNotFound as DeviceNotFoundBT
from radiacode import DoseRateDB, RareData, RawData, RealTimeData, Event
BLUETOOTH_MAC = "52:43:06:60:17:dd"
SPECTRUM_DURATION_MS = 60 * 1000
WATCHDOG_TIMEOUT = 8388
def on_wdt_reset():
print("Watchdog reset")
time.sleep_ms(1000)
if machine.reset_cause() == machine.WDT_RESET:
on_wdt_reset()
led = machine.Pin("LED", machine.Pin.OUT)
# wdt = machine.WDT(timeout=WATCHDOG_TIMEOUT)
def heartbeat():
led.toggle()
# wdt.feed() # reset watchdog timer
heartbeat() # -----------------------------------------------------------------------------------------------
while True:
print(f"Connecting to Radiacode via Bluetooth (MAC address: {BLUETOOTH_MAC})")
heartbeat() # ---------------------------------------------------------------------------------------------------------
# find and connect to RadiaCode
try:
rc = RadiaCode(bluetooth_mac=BLUETOOTH_MAC)
except DeviceNotFoundBT as e:
print(e)
continue
heartbeat() # ---------------------------------------------------------------------------------------------------------
try:
print("Setting up SD card...")
spi = machine.SPI()
spi.init()
sd = sdcard.SDCard(spi, machine.Pin.board.GP17)
print("Mounting filesystem...")
vfs = os.VfsFat(sd)
os.mount(vfs, "/fs")
it = 0
data_file_path = ""
# find the next available data_.txt file name
while True:
try:
heartbeat() # -------------------------------------------------------------------------------------
data_file_path = f"/fs/data{it}.txt"
f_info = os.stat(data_file_path)
it += 1
except OSError:
break # the name is unused
# create data file
data_file = open(data_file_path, "a")
data_file.close()
print(f"Created data file {data_file_path}")
except Exception as e:
print("ERROR: ", e)
continue
heartbeat() # ----------------------------------------------------------------------------------------------------------
try:
serial = rc.serial_number()
print(f"### Serial number: {serial}")
print("--------")
fw_version = rc.fw_version()
print(f"### Firmware: {fw_version}")
print("--------")
spectrum = rc.spectrum()
print(f"### Spectrum: {spectrum}")
print("--------")
start = time.ticks_ms()
while True:
heartbeat() # ----------------------------------------------------------------------------------------------------
# infinite loop, check if there is data to process and read it if there is
for v in rc.data_buf():
print(v.dt.isoformat(), v)
data_file = open(data_file_path, "a")
data_file.write(f"{time.ticks_ms()}; ")
t = type(v)
# store class fields for each return option
if t == DoseRateDB:
data_file.write(
f"DoseRateDB; {v.dt}; {v.count}; {v.count_rate}; {v.dose_rate}; {v.dose_rate_err}; {v.flags};\n"
)
elif t == RareData:
data_file.write(
f"RareData; {v.dt}; {v.duration}; {v.dose}; {v.temperature}; {v.charge_level}; {v.flags};\n"
)
elif t == RealTimeData:
data_file.write(
f"RealTimeData; {v.dt}; {v.count_rate}; {v.count_rate_err}; {v.dose_rate}; {v.dose_rate_err}; {v.flags}; {v.real_time_flags};\n"
)
elif t == RawData:
data_file.write(
f"RawData; {v.dt}; {v.count_rate}; {v.dose_rate};\n"
)
elif t == Event:
print("fake write")
# data_file.write(
# f"Event; {v.dt}; {v.event.name}; {v.event_param1}; {v.flags};\n"
# )
# flush and close to reduce caching
data_file.flush()
data_file.close()
if time.ticks_ms() - start > SPECTRUM_DURATION_MS:
heartbeat() # ---------------------------------------------------------------------------------------------------
start = time.ticks_ms()
# read the spectrogram
spectrum = rc.spectrum()
data_file = open(data_file_path, "a")
data_file.write(
f"{time.ticks_ms()}; Spectrum; {spectrum.duration}; {spectrum.a0}; {spectrum.a1}; {spectrum.a2}; {spectrum.counts};\n"
)
data_file.flush()
data_file.close()
print(f"{spectrum.duration} Spectrum: {spectrum}")
# restart it
# rc.spectrum_reset()
except Exception:
# try to close data file if possible
try:
data_file.close()
except Exception:
pass
machine.reset()
continue