def main(): lock = Lock("/root/csdc3/src/utils/heaterShutDownLock.tmp") lock.acquire() print("Lock has been acquired by ShutAllBatteryHeaters.py") sleep(600) lock.release() print("Lock has been released by ShutAllBatteryHeaters.py")
def __init__(self): self.controlStatus = False self.payloadLock = Lock("/root/csdc3/src/utils/payloadLock.tmp") self.sensorReadingLock = Lock("/root/csdc3/src/utils/sensorReadingLock.tmp") self.heaterShutDownLock = Lock("/root/csdc3/src/utils/heaterShutDownLock.tmp") self.pastReadingAboveThresh = True self.thresholdVoltage = 12
def f(i): l = Lock("/home/justin/Desktop/lock.tmp") l.acquire() try: print('Thread', i, 'has lock') sleep(2) finally: l.release()
def __init__(self, experiment, max_time=PAYLOAD_MIN_SPACE, \ max_loadcell=PAYLOAD_MAX_LOADCELL, sampling_freq=PAYLOAD_SAMPLING_FREQ, \ max_temp=MAX_ACTUATE_TEMP, heater_period=HEATER_ON_TIME, actuate_time=PAYLOAD_ACTUATE_TIME): self.experiment = experiment if self.experiment == 1: self.heater = PAYLOAD_HTR_B_GPIO self.temp_sensor = TEMP_PAYLOAD_B else: self.heater = PAYLOAD_HTR_A_GPIO self.temp_sensor = TEMP_PAYLOAD_A self.max_time = max_time self.max_loadcell = max_loadcell self.sampling_freq = sampling_freq self.heater_period = heater_period self.max_temp = max_temp self.actuate_time = actuate_time self.lock = Lock("/root/csdc3/src/utils/payloadLock.tmp")
class PowerMonitor: def __init__(self): self.controlStatus = False self.payloadLock = Lock("/root/csdc3/src/utils/payloadLock.tmp") self.sensorReadingLock = Lock("/root/csdc3/src/utils/sensorReadingLock.tmp") self.heaterShutDownLock = Lock("/root/csdc3/src/utils/heaterShutDownLock.tmp") self.pastReadingAboveThresh = True def check_health(self): """ Determines whether battery chargers must be set manually """ # Check if sensors are reading data in the system # if areSensorsAcquiringData(): # return # Check if ShutAllBatteryHeaters is running if self.heaterShutDownLock.isLocked(): # Shut all battery heaters off print('Battery heaters must remain shut off') self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) for heater in heaterIdentifers: SensorManager.gpio_output(heater, OFF) return # # Get temperature inputs # tempIdentifiers = (TEMP_BAT_1,) # TEMP_BAT_2, TEMP_BAT_3, TEMP_BAT_4) # tempValues = [] # for iden in tempIdentifiers: # SensorManager.init_temp_sensor(iden) # valueList = [] # # Get median of 5 value readings to remove outliers # for i in range(0,5): # valueList.append(SensorManager.read_temp_sensor(iden)) # tempValue = median(valueList) # print(tempValue) # SensorManager.stop_temp_sensor(iden) # # Keep final value of sensor # tempValues.append(tempValue) # # # Get status identifiers # statusIdentifiers = (PSS_HTR_STAT_1_GPIO, PSS_HTR_STAT_2_GPIO,\ # PSS_HTR_STAT_3_GPIO, PSS_HTR_STAT_4_GPIO) # statusValues = [] # for iden in statusIdentifiers: # statusValues.append(SensorManager.gpio_input(iden,0)) batteryTempAndStatusDict = BatteryHeatersReader() tempValues = [item["temp"] for item in batteryTempAndStatusDict] statusValues = [item["heaters"] for item in batteryTempAndStatusDict] # Define manual heater identifiers heaterIdentifers = (PSS_HTR_EN_1_GPIO, PSS_HTR_EN_2_GPIO,\ PSS_HTR_EN_3_GPIO, PSS_HTR_EN_4_GPIO) print('Status value: ' + str(statusValues[0])) print('Is analog:', SensorManager.gpio_input(PSS_HTR_MUX_SEL_GPIO, time.time())) # Check if payload is running if self.isPayloadAcquiringData(): # Shut all battery heaters off print('Payload is running... shutting off all battery heaters') self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) for heater in heaterIdentifers: SensorManager.gpio_output(heater, OFF) return # Find out if analog or OBC is in control for i in range(0,len(tempValues)): if (self.temp_threshold(tempValues[i], 'GT') and statusValues[i] == 1)\ or (self.temp_threshold(tempValues[i], 'LT') and statusValues[i] == 0): # OBC will take control self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) break else: # Analog will take control self.controlStatus = False SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, ON) # Perform OBC control if required if self.controlStatus == True: for i in range(0,len(tempValues)): if tempValues[i] != None: if self.temp_threshold(tempValues[i], 'GT') and statusValues[i] == 0: print('Case 1: Temp > threshold, heaters off, no action required') elif self.temp_threshold(tempValues[i], 'GT') and statusValues[i] == 1: print('Case 2: Temp > threshold, heaters on, OBC must shut off heater') SensorManager.gpio_output(heaterIdentifers[i], OFF) elif self.temp_threshold(tempValues[i], 'LT') and statusValues[i] == 0: print('Case 3: Temp < threshold, heaters off, OBC must activate heater') if self.is_battery_safe(): SensorManager.gpio_output(heaterIdentifers[i], ON) elif self.temp_threshold(tempValues[i], 'LT') and statusValues[i] == 1: print('Case 4: Temp < threshold, heaters on, no action required') def temp_threshold(self, tempValue, sign): """ Returns boolean for crossed temperature threshold """ tempRef = 12 thresholdPct = 0.1 if sign == 'GT': result = tempValue > tempRef*(1+thresholdPct) elif sign == 'LT': result = tempValue < tempRef*(1-thresholdPct) else: raise Exception("Invalid parameter: must be 'GT' or 'LT'") return result def is_battery_safe(self): """ Determines if heaters have enough power to heat batteries """ return True def isPayloadAcquiringData(self): """ Determines whether the payload experiment is running """ return self.payloadLock.isLocked() def areSensorsAcquiringData(self): """ Determines if there are sensor readings in progress """ return self.sensorReadingLock.isLocked()
def checkLock(): l = Lock("/home/justin/Desktop/lock.tmp") if l.isLocked(): print("Lock is locked") else: print("Lock is NOT locked")
class PowerMonitor: def __init__(self): self.controlStatus = False self.payloadLock = Lock("/root/csdc3/src/utils/payloadLock.tmp") self.sensorReadingLock = Lock("/root/csdc3/src/utils/sensorReadingLock.tmp") self.heaterShutDownLock = Lock("/root/csdc3/src/utils/heaterShutDownLock.tmp") self.pastReadingAboveThresh = True self.thresholdVoltage = 12 def check_health(self): """ Determines whether battery chargers must be set manually """ # Check if sensors are reading data in the system # if areSensorsAcquiringData(): # return print('Threshold voltage:', self.thresholdVoltage) # Get temperature inputs tempIdentifiers = (TEMP_BAT_1) #, TEMP_BAT_2, TEMP_BAT_3, TEMP_BAT_4) tempValues = [] for iden in tempIdentifiers: SensorManager.init_temp_sensor(iden) valueList = [] # Get median of 5 value readings to remove outliers for i in range(0,5): valueList.append(SensorManager.read_temp_sensor(iden)) tempValue = median(valueList) print('Current temperature:', tempValue) SensorManager.stop_temp_sensor(iden) # Keep final value of sensor tempValues.append(tempValue) # Get status identifiers statusIdentifiers = (PSS_HTR_STAT_1_GPIO, PSS_HTR_STAT_2_GPIO,\ PSS_HTR_STAT_3_GPIO, PSS_HTR_STAT_4_GPIO) statusValues = [] for iden in statusIdentifiers: statusValues.append(SensorManager.gpio_input(iden,0)) # Define manual heater identifiers heaterIdentifiers = (PSS_HTR_EN_1_GPIO, PSS_HTR_EN_2_GPIO,\ PSS_HTR_EN_3_GPIO, PSS_HTR_EN_4_GPIO) # Check if ShutAllBatteryHeaters is running if self.heaterShutDownLock.isLocked(): # Shut all battery heaters off print('Battery heaters must remain shut off') self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) for heater in heaterIdentifiers: SensorManager.gpio_output(heater, OFF) return # Check if payload is running if self.isPayloadAcquiringData(): # Shut all battery heaters off print('Payload is running... shutting off all battery heaters') self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) for heater in heaterIdentifiers: SensorManager.gpio_output(heater, OFF) return # Take control if required for i in range(0,len(tempValues)): if self.temp_threshold(tempValues[i], 'GT') and statusValues[i] == 0: print('Case 1: [Analog] Heaters are turned on') self.controlStatus = False SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, ON) return elif self.temp_threshold(tempValues[i], 'GT') and statusValues[i] == 1: print('Case 2: [Digital] Heaters are turned off') self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) SensorManager.gpio_output(heaterIdentifiers[i], OFF) return elif self.temp_threshold(tempValues[i], 'LT') and statusValues[i] == 0: print('Case 3: [Digital] Heaters are turned on') self.controlStatus = True SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, OFF) if self.is_battery_safe(): SensorManager.gpio_output(heaterIdentifiers[i], ON) return elif self.temp_threshold(tempValues[i], 'LT') and statusValues[i] == 1: print('Case 4: [Analog] Heaters are turned off') self.controlStatus = False SensorManager.gpio_output(PSS_HTR_MUX_SEL_GPIO, ON) return def temp_threshold(self, tempValue, sign): """ Returns boolean for crossed temperature threshold """ tempRef = self.thresholdVoltage thresholdPct = 0.1 if sign == 'GT': result = tempValue > tempRef*(1+thresholdPct) elif sign == 'LT': result = tempValue < tempRef*(1-thresholdPct) else: raise Exception("Invalid parameter: must be 'GT' or 'LT'") return result def is_battery_safe(self): """ Determines if heaters have enough power to heat batteries """ return True def isPayloadAcquiringData(self): """ Determines whether the payload experiment is running """ return self.payloadLock.isLocked() def areSensorsAcquiringData(self): """ Determines if there are sensor readings in progress """ return self.sensorReadingLock.isLocked()
def main(): lock = Lock(SENSOR_LOCK) try: lock.acquire() # Create sensor lists print("Creating sensor lists") tempSensorList = [TEMP_EPS_BRD, TEMP_CDH_BRD, TEMP_PAYLOAD_BRD] # TEMP_PAYLOAD_CHASSIS, TEMP_END_CAP batteryTempSensorList = [TEMP_BAT_1, TEMP_BAT_2, TEMP_BAT_3, TEMP_BAT_4] magSensorList= [MAG_0, MAG_1, MAG_2] adcSwitchCurrentList = [PAYLOAD_SWITCH_ADC_ID, RADIO_SWITCH_ADC_ID] powerSensorList = [POWER] panelTempSensorList = [PANEL0, PANEL1, PANEL2, PANEL3] masterList = list(set(tempSensorList) | set(magSensorList) | set(powerSensorList) | set(batteryTempSensorList) \ | set(adcSwitchCurrentList)) functionsDict = {} # Declare functions used for initialization print("Getting functions for initialization") functionsDict["init_temp"] = SensorManager.init_temp_sensor functionsDict["init_mag"] = SensorManager.init_magnetometer functionsDict["init_power"] = SensorManager.init_power_sensor # Declare functions used for reading print("Getting functions for reading") functionsDict["read_temp"] = SensorManager.read_temp_sensor functionsDict["read_mag"] = SensorManager.read_magnetometer functionsDict["read_power"] = SensorManager.read_power_sensor functionsDict["read_switch"] = SensorManager.read_switch_current # Declare functions used for stopping print("Getting functions for stopping") functionsDict["stop_temp"] = SensorManager.stop_temp_sensor functionsDict["stop_mag"] = SensorManager.stop_magnetometer functionsDict["stop_power"] = SensorManager.stop_power_sensor # Initialize sensors print("Initializing sensors") for sensor in masterList: if sensor in tempSensorList or sensor in batteryTempSensorList: functionsDict["init_temp"](sensor) elif sensor in magSensorList: functionsDict["init_mag"](sensor) elif sensor in powerSensorList: functionsDict["init_power"](sensor) #with open("/root/csdc3/src/sensors/temp_log.txt", "a") as f: for i in range(3): start = time() #sensorValueDict = {} # Get sensor values print("Getting sensor values") for sensor in masterList: if sensor in tempSensorList or sensor in batteryTempSensorList: result = functionsDict["read_temp"](sensor) elif sensor in magSensorList: result = functionsDict["read_mag"](sensor) elif sensor in powerSensorList: result = functionsDict["read_power"](sensor) elif sensor in adcSwitchCurrentList: result = functionsDict["read_switch"](sensor) #sensorValueDict[sensor] = result # Get time it took to complete operations readtime = time() - start #sensorValueDict["Time"] = readtime print("Getting completion time: {}".format(readtime)) #f.write(str(sensorValueDict) + '\n') # Stop sensors print("Stopping sensors") for sensor in masterList: if sensor in tempSensorList or sensor in batteryTempSensorList: functionsDict["stop_temp"](sensor) elif sensor in magSensorList: functionsDict["stop_mag"](sensor) elif sensor in powerSensorList: functionsDict["stop_power"](sensor) finally: lock.release() pass
class Payload(): PAYLOAD_MAX_TIME = 450 PAYLOAD_ACTUATE_TIME = 255.0 PAYLOAD_MAX_LOADCELL = 9999 PAYLOAD_SAMPLING_FREQ = 3 PAYLOAD_MIN_SPACE = 10440 PAYLOAD_MIN_VBAT = 3.0 MAX_ACTUATE_TEMP = 40. HEATER_ON_TIME = 3 def __init__(self, experiment, max_time=PAYLOAD_MIN_SPACE, \ max_loadcell=PAYLOAD_MAX_LOADCELL, sampling_freq=PAYLOAD_SAMPLING_FREQ, \ max_temp=MAX_ACTUATE_TEMP, heater_period=HEATER_ON_TIME, actuate_time=PAYLOAD_ACTUATE_TIME): self.experiment = experiment if self.experiment == 1: self.heater = PAYLOAD_HTR_B_GPIO self.temp_sensor = TEMP_PAYLOAD_B else: self.heater = PAYLOAD_HTR_A_GPIO self.temp_sensor = TEMP_PAYLOAD_A self.max_time = max_time self.max_loadcell = max_loadcell self.sampling_freq = sampling_freq self.heater_period = heater_period self.max_temp = max_temp self.actuate_time = actuate_time self.lock = Lock("/root/csdc3/src/utils/payloadLock.tmp") def check_initial_conditions(self): # Check battery voltage SensorManager.init_power_sensor(POWER) power = SensorManager.read_power_sensor(POWER) vbat = power[0] / 1000. # Check available memory free_space = utility.get_disk_usage('/') #print("Free space", free_space) if free_space >= self.PAYLOAD_MIN_SPACE and vbat >= self.PAYLOAD_MIN_VBAT: return True else: print("Experiment cancelled") print(free_space, vbat) insertDebugLog(NOTICE, "Cancelled. Free space: %d, vbat: %.2f" % \ (free_space, vbat, PAYLOAD, int(time.time()))) return False def init_sensors(self): SensorManager.init_adc(ADC) SensorManager.init_temp_sensor(self.temp_sensor) def start(self): insertDebugLog(NOTICE, "Starting. Runtime: %ds, Actuate time: %ds, Max strain: %d, Sampling Freq: %d." % \ (self.max_time, self.actuate_time, self.max_loadcell, \ self.heater_period), PAYLOAD, int(time.time())) print("Starting payload...") print("Runtime: %ds, Actuate time: %ds, Max strain: %d, Sampling period: %ds" % \ (self.max_time, self.actuate_time, self.max_loadcell, \ self.heater_period)) if not self.check_initial_conditions(): return False self.lock.acquire() self.set_power(True) self.init_sensors() start_time = time.time() elapsed = 0 while True: heater_temp = 0 if elapsed <= self.actuate_time or heater_temp < self.max_temp: self.set_heaters(self.experiment, True) time.sleep(self.heater_period) else: print("No longer turning heaters on") time.sleep(self.heater_period) self.set_heaters(self.experiment, False) self.set_heaters(self.experiment, False) elapsed = time.time() - start_time off_time = time.time() print("[" + str(round(elapsed, 3)) + " s] ", end='') strain, force, adc_temp = SensorManager.read_adc(self.experiment, ADC) heater_temp = SensorManager.read_temp_sensor(self.temp_sensor) print(strain, force, adc_temp, heater_temp) sleep_time = time.time() - off_time elapsed = time.time() - start_time time.sleep(abs(self.heater_period - sleep_time)) elapsed = time.time() - start_time strain, force, adc_temp = SensorManager.read_adc(self.experiment, ADC) heater_temp = SensorManager.read_temp_sensor(self.temp_sensor) print("[" + str(round(elapsed, 3)) + " s] ", end='') print(strain, force, adc_temp, heater_temp) if self.is_end_condition(strain, elapsed): break if self.experiment: exp = 'B' else: exp = 'A' insertPayloadLog(int(start_time), int(time.time()), exp) self.end() self.lock.release() return True def end(self): print("Payload ending...") insertDebugLog(NOTICE, "Ending", PAYLOAD, int(time.time())) SensorManager.stop_temp_sensor(self.temp_sensor) SensorManager.stop_adc_sensor(ADC) self.set_heaters(self.experiment, False) self.set_power(False) def set_heaters(self, experiment=0, state=False): if state == False: SensorManager.gpio_output(self.heater, OFF) else: SensorManager.gpio_output(self.heater, ON) return True def set_power(self, isOn=False): insertDebugLog(NOTICE, "Power to %d" % (isOn), PAYLOAD, int(time.time())) print("Setting power for payload: ", isOn) if isOn == False: SensorManager.gpio_output(PAYLOAD_EN_GPIO, OFF) SensorManager.gpio_output(OLD_PAYLOAD_EN_GPIO, OFF) else: SensorManager.gpio_output(PAYLOAD_EN_GPIO, ON) SensorManager.gpio_output(OLD_PAYLOAD_EN_GPIO, ON) SensorManager.gpio_output(SENSORS_EN_GPIO, ON) return True def is_end_condition(self, strain, elapsed): if strain >= self.max_loadcell or elapsed >= self.max_time: return True else: return False