Exemplo n.º 1
0
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")
Exemplo n.º 2
0
def f(i):
	l = Lock("/home/justin/Desktop/lock.tmp")
	l.acquire()
	try:
		print('Thread', i, 'has lock')
		sleep(2)
	finally:
		l.release()
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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