Package joy :: Module safety
[hide private]
[frames] | no frames]

Source Code for Module joy.safety

 1  """ 
 2  Python module joy.safety 
 3   
 4  Contains classes implementing the safety protocols enforced by JoyApp 
 5   
 6  Main classes: 
 7    SafetyError -- exception class for indicating safety violations 
 8    BatteryVoltage -- a safety check that uses get_voltage calls and 
 9      enforces a lower bound on voltage 
10  """ 
11  from loggit import progress 
12   
13 -class SafetyError( RuntimeError ):
14 """ 15 Error used to indicate termination because of an unsafe working 16 condition being detected 17 """ 18 pass
19 20
21 -class BatteryVoltage( object ):
22 """ 23 Concrete class managing battery safety via a collection of objects 24 that support the get_voltage method. 25 """
26 - def __init__(self, vmin, sensors, pollRate = 1.0):
27 self.sensors = sensors 28 self.vmin = vmin 29 self.last = 0,None 30 self.rate = pollRate 31 progress("BatteryVoltage will test V<%g every %g seconds" % (vmin,pollRate))
32
33 - def poll( self, now ):
34 """Polls voltage sensors listed in self.sensors 35 for voltage measurements at the rate specified by self.rate 36 37 If a measurement is ready, it is stored in self.last 38 as a (time,voltage) pair, and this pair is returned. 39 40 Otherwise, returns (None,None) 41 42 If the voltage is below vmin -- raises a SafetyError 43 """ 44 if not self.sensors: 45 return None,None 46 t,v = self.last 47 if now - t < self.rate: 48 return None,None 49 s = self.sensors.pop(0) 50 v = s.get_voltage() 51 self.sensors.append(s) 52 self.last = now, v 53 if v<self.vmin: 54 msg = "DANGER: voltage %g is below %g -- shutting down" % (v,self.vmin) 55 raise SafetyError(msg) 56 ###progress("Voltage %g" % v ) 57 return self.last
58