def channel_update(self, ch, sample): if sample.value: self.property_set('channel%d_output' %(ch+1), Sample(0, True)) digihw.gpio_set_value(ch, 1) else: self.property_set('channel%d_output' %(ch+1), Sample(0, False)) digihw.gpio_set_value(ch, 0)
def channel_update(self, ch, sample): if sample.value: self.property_set(ch, Sample(0, True)) digihw.gpio_set_value(self.channel_array[ch], 1) else: self.property_set(ch, Sample(0, False)) digihw.gpio_set_value(self.channel_array[ch], 0)
def set_gpio(self,gpio,sample): self.setting_gpio = True #Update the channel and the module GPIO if sample.value==True: self.property_set(gpio,Sample(digitime.time(), True)) digihw.gpio_set_value(self.gpios_ind[gpio], 1) else: self.property_set(gpio,Sample(digitime.time(), False)) digihw.gpio_set_value(self.gpios_ind[gpio], 0) self.setting_gpio = False
def set_gpio(self, gpio, sample): self.setting_gpio = True #Update the channel and the module GPIO if sample.value == True: self.property_set(gpio, Sample(digitime.time(), True)) digihw.gpio_set_value(self.gpios_ind[gpio], 1) else: self.property_set(gpio, Sample(digitime.time(), False)) digihw.gpio_set_value(self.gpios_ind[gpio], 0) self.setting_gpio = False
def run(self): cm = self.__core.get_service("channel_manager") cp = cm.channel_publisher_get() cdb = cm.channel_database_get() #threshold = SettingsBase.get_setting(self, 'acc_threshold') sample_rate_ms = SettingsBase.get_setting(self, 'sample_rate_ms') #self._tracer.info("Threshold set to %.3fG" %threshold) if digihw.NB_GPIO != len(channel_type): self._tracer.info('Invalid number of channels') while 1: if self.__stopevent.isSet(): self.__stopevent.clear() break # acceleration_value = self.update_accelerometer_info() # self.property_set("acceleration", Sample(0, acceleration_value)) for index, ch_type in enumerate(channel_type): if ch_type == 'in': current_status = bool(digihw.gpio_get_value(index)) digihw.gpio_set_input(index) self.property_set('channel%d_input' %(index+1), \ Sample(0, current_status)) elif ch_type == 'out': status = self.property_get('channel%d_output' %(index+1)).value digihw.gpio_set_value(index, status) try: self._tracer.info("Reading GPS data.....") gps_data = digihw.gps_location() #Retrieve information from gps_location name = ['latitude', 'longitude', 'altitude', 'current_time'] for index in range(len(name)): self.property_set(name[index], Sample(0, value = gps_data[index])) except: import traceback traceback.print_exc() self._tracer.info("Unable to get a GPS signal.") self._tracer.info("Please place the GPS antenna in another place.") status = digihw.ignition_sense() self.property_set("ignition_status", Sample(0, status)) digitime.sleep(sample_rate_ms / 1000)
def turn_on_calibration_series2(self): """ Turn on calibration mode for the channel when using a XBee Series 2 Radio (ZNet 2.5, ZB). On all supported products, the channel must be changed to either TenV or CurrentLoop mode first. Since Dia requires all the AIO channels to be defined to a mode during configuration, we can assume here that the channel has been configured to either TenV or CurrentLoop already, and so we do NOT need to set a channel mode here. Then based on the product, to have the unit read the known "calibrated" values of .6V and 1V respectively, you must do: On X3-based products, the GPIO pin 0 must be set to 0. On nds-based products, the device uses 2 GPIO pins on the XBee radio for switching to and from the calibrated values. Thus, we need to issue 2 XBee DDO commands that will tell the XBee radio to swap its reads for that channel. Channel 0: DDO P2 -> 4 Channel 1: DDO D4 -> 4 """ # X3-based product calibration # Set GPIO 0 = 0 if get_platform_name() == 'digix3': digihw.gpio_set_value(0, 0) # nds-based product calibration # Issue the special XBee DDO commands to swap inputs to known values elif get_platform_name() == 'digiconnect': if self.__channel == 0: self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 4, apply = True) elif self.__channel == 1: self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 4, apply = True)
def turn_on_calibration_series2(self): """ Turn on calibration mode for the channel when using a XBee Series 2 Radio (ZNet 2.5, ZB). On all supported products, the channel must be changed to either TenV or CurrentLoop mode first. Since DIA requires all the AIO channels to be defined to a mode during configuration, we can assume here that the channel has been configured to either TenV or CurrentLoop already, and so we do NOT need to set a channel mode here. Then based on the product, to have the unit read the known "calibrated" values of .6V and 1V respectively, you must do: On X3-based products, the GPIO pin 0 must be set to 0. On nds-based products, the device uses 2 GPIO pins on the XBee radio for switching to and from the calibrated values. Thus, we need to issue 2 XBee DDO commands that will tell the XBee radio to swap its reads for that channel. Channel 0: DDO P2 -> 4 Channel 1: DDO D4 -> 4 """ # X3-based product calibration # Set GPIO 0 = 0 if get_platform_name() == 'digix3': digihw.gpio_set_value(0, 0) # nds-based product calibration # Issue the special XBee DDO commands to swap inputs to known values elif get_platform_name() == 'digiconnect': if self.__channel == 0: self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 4, apply = True) elif self.__channel == 1: self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 4, apply = True)
def turn_off_calibration_series2(self): """ Turn off calibration mode for the channel when using a XBee Series 2 Radio (ZNet 2.5, ZB). This function reverses the process of what the function 'turn_off_calibration_series2' did, and sets us back into normal operating mode. To do this: On X3-based products, the GPIO pin 0 must be set to 1. On nds-based products, the device uses 2 GPIO pins on the XBee radio for switching to and from the calibrated values. Thus, we need to issue 2 XBee DDO commands that will tell the XBee radio to swap its reads back for that channel. Channel 0: DDO P2 -> 5 Channel 1: DDO D4 -> 5 """ # X3-based product calibration # # Set GPIO 0 = 1 if get_platform_name() == 'digix3': digihw.gpio_set_value(0, 1) # nds-based product calibration # # Issue the special XBee DDO commands to swap inputs to known values elif get_platform_name() == 'digiconnect': if self.__channel == 0: self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 5, apply = True) elif self.__channel == 1: self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 5, apply = True)
def __calibrate(self): """__calibrate() Calibrate analog inputs. Calculates scale and offset.""" xbee_dd_ddo_value = self.__xbee_manager.xbee_device_ddo_get_param(None, 'DD', use_cache=True) module_id, product_id = parse_dd(xbee_dd_ddo_value) # XBee series 1 uses one calibration voltage on AN2 if module_id == MOD_XB_802154: # Enable calibration voltage on channel 1 self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 4, apply=True) digitime.sleep(0.010) # Read calibration sample result = self.__xbee_manager.xbee_device_ddo_get_param(None, 'IS') sample = parse_is(result)["AD1"] self.__tracer.debug("Calibration sample is %d", sample) # Return channel to operating mode self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 5, apply=True) digitime.sleep(0.010) if sample == 0: raise ValueError, "Calibration error: bad sample" # Calulate linear scale and offset. # These apply to all analog channels. self.__scale = 1.25 / sample self.__offset = 0 # XBee series 2 uses two calibration voltages on AN1 and AN2 elif module_id == MOD_XB_ZNET25 or module_id == MOD_XB_ZB or module_id == MOD_XB_S2C_ZB: # Enable calibration voltages on channels 0 and 1 if get_platform_name() == 'digix3': self.__xbee_manager.xbee_device_ddo_set_param(None, self.LOCAL_AIO_CONTROL_LINES[0], 2, apply=True) self.__xbee_manager.xbee_device_ddo_set_param(None, self.LOCAL_AIO_CONTROL_LINES[1], 2, apply=True) digihw.gpio_set_value(0, 0) elif get_platform_name() == 'digiconnect': self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 4, apply=True) self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 4, apply=True) digitime.sleep(0.010) # Read calibration samples result = self.__xbee_manager.xbee_device_ddo_get_param(None, 'IS') data = parse_is(result) sample = [ data["AD0"], data["AD1"] ] self.__tracer.debug("Calibration samples are %d, %d", sample[0], sample[1]) # Return channels to operating mode if get_platform_name() == 'digix3': digihw.gpio_set_value(0, 1) elif get_platform_name() == 'digiconnect': self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 5, apply=True) self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 5, apply=True) digitime.sleep(0.010) for io_pin in range(2): mode = SettingsBase.get_setting(self, 'channel%d_mode' % (io_pin+1) ) mode = self.LOCAL_AIO_MODE_MAP[mode.lower()] if mode == self.LOCAL_AIO_MODE_CURRENTLOOP: self.__xbee_manager.xbee_device_ddo_set_param(None, self.LOCAL_AIO_CONTROL_LINES[io_pin], 2, apply=True) elif mode == self.LOCAL_AIO_MODE_TENV: self.__xbee_manager.xbee_device_ddo_set_param(None, self.LOCAL_AIO_CONTROL_LINES[io_pin], 2, apply=True) if sample[0] == sample[1]: raise ValueError, "Calibration error: equal samples" self.__sample1 = sample[1] self.__sample2 = sample[0] scale1 = self.LOCAL_AIO_CALIBRATION_06 / float(sample[1]) scale2 = self.LOCAL_AIO_CALIBRATION_10 / float(sample[0]) self.__scale = (scale1 + scale2) / 2.0 if self.__OLD_HARDWARE == True: self.__offset = (self.__sample1 * self.__scale - self.LOCAL_AIO_CALIBRATION_06) * 2.4 else: self.__offset = 0.0 else: raise ValueError, "XBee does not support analog inputs" self.__calibration_time = digitime.real_clock() self.__tracer.debug("Scale is %f, offset is %f", self.__scale, self.__offset)
def initialize_gpios(self): for gpio in self.output_gpios: digihw.gpio_set_value(gpio,0) for gpio in self.input_gpios: digihw.gpio_set_input(gpio)
def initialize_gpios(self): for gpio in self.output_gpios: digihw.gpio_set_value(gpio, 0) for gpio in self.input_gpios: digihw.gpio_set_input(gpio)
def __calibrate(self): """__calibrate() Calibrate analog inputs. Calculates scale and offset.""" xbee_dd_ddo_value = self.__xbee_manager.xbee_device_ddo_get_param( None, 'DD', use_cache=True) module_id, product_id = parse_dd(xbee_dd_ddo_value) # XBee series 1 uses one calibration voltage on AN2 if module_id == MOD_XB_802154: # Enable calibration voltage on channel 1 self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 4, apply=True) digitime.sleep(0.010) # Read calibration sample result = self.__xbee_manager.xbee_device_ddo_get_param(None, 'IS') sample = parse_is(result)["AD1"] self.__tracer.debug("Calibration sample is %d", sample) # Return channel to operating mode self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 5, apply=True) digitime.sleep(0.010) if sample == 0: raise ValueError, "Calibration error: bad sample" # Calulate linear scale and offset. # These apply to all analog channels. self.__scale = 1.25 / sample self.__offset = 0 # XBee series 2 uses two calibration voltages on AN1 and AN2 elif module_id == MOD_XB_ZNET25 or module_id == MOD_XB_ZB or module_id == MOD_XB_S2C_ZB: # Enable calibration voltages on channels 0 and 1 if get_platform_name() == 'digix3': self.__xbee_manager.xbee_device_ddo_set_param( None, self.LOCAL_AIO_CONTROL_LINES[0], 2, apply=True) self.__xbee_manager.xbee_device_ddo_set_param( None, self.LOCAL_AIO_CONTROL_LINES[1], 2, apply=True) digihw.gpio_set_value(0, 0) elif get_platform_name() == 'digiconnect': self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 4, apply=True) self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 4, apply=True) digitime.sleep(0.010) # Read calibration samples result = self.__xbee_manager.xbee_device_ddo_get_param(None, 'IS') data = parse_is(result) sample = [data["AD0"], data["AD1"]] self.__tracer.debug("Calibration samples are %d, %d", sample[0], sample[1]) # Return channels to operating mode if get_platform_name() == 'digix3': digihw.gpio_set_value(0, 1) elif get_platform_name() == 'digiconnect': self.__xbee_manager.xbee_device_ddo_set_param(None, 'P2', 5, apply=True) self.__xbee_manager.xbee_device_ddo_set_param(None, 'D4', 5, apply=True) digitime.sleep(0.010) for io_pin in range(2): mode = SettingsBase.get_setting( self, 'channel%d_mode' % (io_pin + 1)) mode = self.LOCAL_AIO_MODE_MAP[mode.lower()] if mode == self.LOCAL_AIO_MODE_CURRENTLOOP: self.__xbee_manager.xbee_device_ddo_set_param( None, self.LOCAL_AIO_CONTROL_LINES[io_pin], 2, apply=True) elif mode == self.LOCAL_AIO_MODE_TENV: self.__xbee_manager.xbee_device_ddo_set_param( None, self.LOCAL_AIO_CONTROL_LINES[io_pin], 2, apply=True) if sample[0] == sample[1]: raise ValueError, "Calibration error: equal samples" self.__sample1 = sample[1] self.__sample2 = sample[0] scale1 = self.LOCAL_AIO_CALIBRATION_06 / float(sample[1]) scale2 = self.LOCAL_AIO_CALIBRATION_10 / float(sample[0]) self.__scale = (scale1 + scale2) / 2.0 if self.__OLD_HARDWARE == True: self.__offset = (self.__sample1 * self.__scale - self.LOCAL_AIO_CALIBRATION_06) * 2.4 else: self.__offset = 0.0 else: raise ValueError, "XBee does not support analog inputs" self.__calibration_time = digitime.real_clock() self.__tracer.debug("Scale is %f, offset is %f", self.__scale, self.__offset)