-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafetyShutdown.py
executable file
·135 lines (108 loc) · 4.18 KB
/
safetyShutdown.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
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
#!/usr/bin/python3
#
# safetyShutdown.py SAFELY SHUTDOWN AT BATTERY
#
# Loop reading the battery voltage
# UNTIL voltage stays below LOW_BATTERY_V 4 times,
# then will force a shutdown.
#
# Will start wifi led blinking orange ~15 minutes before safety shutdown is executed
# when battery.SAFETY_SHUTDOWN_vBatt is 9.75v and battery.WARNING_LOW_vBatt is 10.25v
#
# Note: Program prints actual battery voltage which is 0.81v higher than GoPiGo3 reading
# due to reverse polarity protection diode, wires, and connections
#
import sys
import time
import signal
import os
from datetime import datetime
sys.path.insert(1,"/home/pi/GoPi5Go/plib")
from noinit_easygopigo3 import EasyGoPiGo3
import battery
import leds
import status
import speak
import lifeLog
# ######### CNTL-C #####
# Callback and setup to catch control-C and quit program
_funcToRun=None
def signal_handler(signal, frame):
print('\n** Control-C Detected')
if (_funcToRun != None):
_funcToRun()
sys.exit(0) # raise SystemExit exception
# Setup the callback to catch control-C
def set_cntl_c_handler(toRun=None):
global _funcToRun
_funcToRun = toRun
signal.signal(signal.SIGINT, signal_handler)
# ##### MAIN ######
def handle_ctlc():
global egpg
egpg.reset_all()
print("safetyShutdown.py: handle_ctlc() executed")
def main():
global egpg
# #### SET CNTL-C HANDLER
set_cntl_c_handler(handle_ctlc)
# #### Create instance of GoPiGo3 base class
egpg = EasyGoPiGo3(use_mutex=True,noinit=True)
batteryLowCount = 0
last_leg_count = 0
warning_led_on = False
leds.wifi_blinker_off(egpg)
str_to_log = "'Starting safetyShutdown.py at {:.2f} volts.'".format(egpg.volt()+battery.REV_PROTECT_DIODE)
os.system("/home/pi/GoPi5Go/utils/logMaintenance.py " + str_to_log)
doloop = True
while doloop:
try:
status.printStatus(egpg)
if (battery.too_low(egpg)):
batteryLowCount += 1
else:
batteryLowCount = 0
if (warning_led_on == True) and (battery.on_last_leg(egpg) == False):
last_leg_count -= 1
if last_leg_count < 1:
last_leg_count = 0
warning_led_on = False
leds.wifi_blinker_off(egpg)
# os.system("/home/pi/GoPi5Go/utils/logMaintenance.py 'safetyShutdown: voltage warning blinker deactivated '")
if (warning_led_on == False) and battery.on_last_leg(egpg):
last_leg_count += 1
if last_leg_count > 4:
warning_led_on = True
leds.wifi_blinker_on(egpg,color=leds.ORANGE)
os.system("/home/pi/GoPi5Go/utils/logMaintenance.py 'safetyShutdown: voltage warning blinker activated '")
last_leg_count = 150 # allow plenty of bouncing around the low mark
speak.shout("15 minute warning. Battery Voltage is {:.1f} volts. Need to be Docked".format(egpg.volt()+battery.REV_PROTECT_DIODE))
if (batteryLowCount > 3):
vBatt,_ = battery.vBatt_vReading(egpg)
print ("WARNING, WARNING, SHUTTING DOWN NOW")
print ("BATTERY %.2f volts BATTERY LOW - SHUTTING DOWN NOW" % vBatt)
egpg.reset_all()
time.sleep(1)
os.system("/home/pi/GoPi5Go/utils/logMaintenance.py 'SAFETY SHUTDOWN - BATTERY LOW'")
str_log_voltages = "** "+battery.voltages_string(egpg)+" **"
lifeLog.logger.info(str_log_voltages)
time.sleep(1)
# os.system("sudo shutdown +10") # for testing
os.system("sudo shutdown -h +2")
sys.exit(0)
time.sleep(10) # check battery status every 10 seconds
# important to make four checks low V quickly
except SystemExit:
doloop = False
except Exception as e:
print("safetyShutdown.py exception: ",type(e).__name__,",",str(e))
str_to_log="Continuing after Exception "+type(e).__name__+": "+str(e)
print(str_to_log)
lifeLog.logger.info(str_to_log)
#end try
#end while
print("safetyShutdown.py: exiting")
str_to_log="Normal Exit"
lifeLog.logger.info(str_to_log)
if __name__ == "__main__":
main()