forked from pleiades-cluster/yearling-flight-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimu_task.py
67 lines (57 loc) · 2.57 KB
/
imu_task.py
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
# Task to obtain IMU sensor readings
from Tasks.template_task import Task
import msgpack
import os
from os import stat
SEND_DATA = False # make sure you have an antenna attached!
class task(Task):
priority = 5
frequency = 1/10 # once every 10s
name='imu'
color = 'green'
data_file = None
# we want to initialize the data file only once upon boot
# so perform our task init and use that as a chance to init the data files
def __init__(self,satellite):
super().__init__(satellite)
self.data_file=self.cubesat.new_file('/data/imu',binary=True)
async def main_task(self):
# take IMU readings
readings = {
'accel':self.cubesat.acceleration,
'mag': self.cubesat.magnetic,
'gyro': self.cubesat.gyro,
}
# store them in our cubesat data_cache object
self.cubesat.data_cache.update({'imu':readings})
# print the readings with some fancy formatting
self.debug('IMU readings (x,y,z)')
for imu_type in self.cubesat.data_cache['imu']:
self.debug('{:>5} {}'.format(imu_type,self.cubesat.data_cache['imu'][imu_type]),2)
# save data to the sd card, but only if we have a proper data file
if self.data_file is not None:
# save our readings using msgpack
with open(self.data_file,'ab') as f:
msgpack.pack(readings,f)
# check if the file is getting bigger than we'd like
if stat(self.data_file)[6] >= 256: # bytes
if SEND_DATA:
print('\nSend IMU data file: {}'.format(self.data_file))
with open(self.data_file,'rb') as f:
chunk = f.read(64) # each IMU readings is 64 bytes when encoded
while chunk:
# we could send bigger chunks, radio packet can take 252 bytes
self.cubesat.radio1.send(chunk)
print(chunk)
chunk = f.read(64)
print('finished\n')
else:
# print the unpacked data from the file
print('\nPrinting IMU data file: {}'.format(self.data_file))
with open(self.data_file,'rb') as f:
while True:
try: print('\t',msgpack.unpack(f))
except: break
print('finished\n')
# increment our data file number
self.data_file=self.cubesat.new_file('/data/imu')