-
Notifications
You must be signed in to change notification settings - Fork 1
/
battery_task.py
66 lines (57 loc) · 2.56 KB
/
battery_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
66
# check for low battery condition
from Tasks.template_task import Task
import time
class task(Task):
priority = 3
frequency = 1/10 # once every 10s
name='vbatt'
color = 'orange'
async def main_task(self):
THRESHOLD = self.cubesat.cfg['lb']
LOW_BATTERY = True # default to true as fail-safe
vbatt=self.cubesat.battery_voltage
if vbatt > THRESHOLD:
LOW_BATTERY = False
# clear all our low battery flags and counter
if self.cubesat.f_lowbatt:
self.cubesat.f_lowbatt=False
self.cubesat.f_lowbtout=False
self.cubesat.c_lowb=0
elif not self.cubesat.f_lowbtout:
"""
LOW BATTERY CONDITION!!
we don't want to get stuck in low battery mode, so we need to
count how many times we hit this condition. we have to use an
NVM counter since RAM gets reset during deep sleep
"""
self.cubesat.c_lowb+=1
self.debug(f'incrementing c_lowb to {self.cubesat.c_lowb}')
if self.cubesat.c_lowb > 4:
# default 12hr deep sleep = 4*12 = 48hr before timeout
self.debug('c_lowb reached. setting lowbatt timeout flag')
self.cubesat.f_lowbtout=True
self.debug(f'{vbatt:.1f}V {'<' if LOW_BATTERY else '>'} threshold: {THRESHOLD:.1f}V')
########### ADVANCED ###########
# respond to a low power condition
if LOW_BATTERY:
self.cubesat.f_lowbatt=True
# if we've timed out, don't do anything
if self.cubesat.f_lowbtout:
self.debug('lowbatt timeout flag set! skipping...')
else:
self.debug('low battery detected!',2)
# stop all tasks
for t in self.cubesat.scheduled_tasks:
self.cubesat.scheduled_tasks[t].stop()
self.cubesat.powermode('minimum')
# configure for deep sleep, default 12hrs
if 'st' in self.cubesat.cfg:
_sleeptime = self.cubesat.cfg['st']
else:
_sleeptime=5
import alarm, board
_sleeptime = eval('0.432e'+str(_sleeptime)) # default 0.432e5 = 43200s = 12hrs
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + _sleeptime)
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
# board will deep sleep until time_alarm then reset
# no further code will run!!