def apply_settings(self): SettingsBase.merge_settings(self) accepted, rejected, not_found = SettingsBase.verify_settings(self) if len(rejected) or len(not_found): # there were problems with settings, terminate early: self.__tracer.error("Settings rejected/not found: %s %s", rejected, not_found) return (accepted, rejected, not_found) # Walk each setting, and verify the physical channel type # is the same type the user specified in their config. for setting in accepted.copy(): if setting[0:7] == "channel": try: channel = setting[7] operation = setting[9:] type = digihw.get_channel_type(int(channel) - 1) if type == self.IO_TYPE_ANALOG: if operation != "mode": raise ValueError, "Channel mode is not correct" elif type == self.IO_TYPE_DIGITAL: if operation != "dir" and operation != "source": raise ValueError, "Channel mode is not correct" else: raise ValueError, "Channel mode is not correct" except Exception, e: self.__tracer.error("Unable to parse settings: %s", e) rejected[setting] = accepted[setting] del accepted[setting]
def sample(self, channel): """sample(channel) Returns digital sample data for specified channel""" if channel >= len(control_lines): raise ValueError, "Unrecognized channel" if digihw.get_channel_type(channel) != Digital: raise ValueError, "Not a digital channel" result = self.XBeeCommandGet("is") return parseIS(result)["DIO%d" % channel]
def configure(self, channel, mode): """configure(channel, mode) - Define channel usage channel - Channel number to be configured. mode - One of (CurrentLoop, TenV) """ if channel >= len(input_lines): raise ValueError, "Unrecognized channel" if digihw.get_channel_type(channel) != Analog: raise ValueError, "Not an analog input channel" if mode == CurrentLoop or mode == TenV: digihw.configure_channel(channel, mode) self.XBeeCommandSet(input_lines[channel], 2) else: raise ValueError, "Unrecognized mode" self.channels[channel] = mode
def configure(self, channel, mode, highlow=0): """ configure(channel, mode, highlow) - Define channel usage channel - Channel number to be configured. mode - One of (Input, Output) highlow - If in Output mode, this specifies whether the signal should be driven high or low. """ if channel >= len(control_lines): raise ValueError, "Unrecognized channel" if digihw.get_channel_type(channel) != Digital: raise ValueError, "Not a digital channel" # XBee pin is always a digital input self.XBeeCommandSet(control_lines[channel], 3) if mode == Output and highlow == 0: digihw.configure_channel(channel, Mode_OutputLow) else: digihw.configure_channel(channel, Mode_Input)
def raw_sample(self, channel): """raw_sample(channel) => A/D reading Returns raw unscaled A/D convertor sample data""" if channel >= len(input_lines): raise ValueError, "Unrecognized channel" if digihw.get_channel_type(channel) != Analog: raise ValueError, "Not an analog input channel" # Calibrate every calInterval seconds now = time.clock() if debug: print "time is %f, calTime is %f" % (now, calTime) if now >= calTime + calInterval or now < calTime: self.calibrate() result = self.XBeeCommandGet("is") val = float(parseIS(result)["AI%d" % channel]) val = scale * val val1 = int(round(val)) return val1
def __init__(self, name, core_services): self.__name = name self.__core = core_services ## Local State Variables: self.__xbee_aio_driver = None self.__xbee_dio_driver = None self.__analog_channels = [] self.__digital_channels = [] from core.tracing import get_tracer self.__tracer = get_tracer(name) ## Settings Table Definition: settings_list = [ Setting(name = 'power', type = Boolean, required = False, default_value = Boolean("On", STYLE_ONOFF), verify_function = self.__verify_power_setting), Setting(name = 'sample_rate_ms', type = int, required = False, default_value = 60000, verify_function = lambda x: x >= 100 and x <= 6000000), Setting(name = 'calibration_rate_ms', type = int, required = False, default_value = 900000, verify_function = lambda x: x >= 0), ] ## Channel Properties Definition: property_list = [] # Dynamically create the rest of our settings based on what the device supports. for ch in range(0, self.IO_MAX_CHANNELS): # Attempt to get the channel type. # If we try to go beyond the amount of supported channels, # we will get a ValueError exception. If we do, stop our loop. try: type = digihw.get_channel_type(ch) except ValueError: break if type == self.IO_TYPE_ANALOG: self.__tracer.info("Detected IO channel %d is " + "an Analog Channel", ch + 1) self.__analog_channels.append(ch) settings_list.append(Setting( name = 'channel%d_mode' % (ch + 1), type=str, required=False, verify_function = _verify_aio_channel_mode, default_value = self.LOCAL_AIO_MODE_TENV)) elif type == self.IO_TYPE_DIGITAL: self.__tracer.info("Detected IO channel %d is" + "a Digital Channel", ch + 1) self.__digital_channels.append(ch) settings_list.append(Setting( name = 'channel%d_dir' % (ch + 1), type = str, required=False, default_value='in')) settings_list.append(Setting( name = 'channel%d_source' % (ch + 1), type = str, required = False, default_value='')) # Finally, on some platforms, the AIO and DIO support is done by # the XBee radio inside the unit. # # If this is the case, we will need to also have the # 'xbee_device_manager' setting provided to us as well. if get_platform_name() == 'digix3': settings_list.append(Setting(name='xbee_device_manager', type = str, required = True)) self.__xbee_aio_driver = self.__xbee_dio_driver = \ XBeeLocalIO(self, core_services, self.__analog_channels, self.__digital_channels) elif get_platform_name() == 'digiconnect': settings_list.append(Setting(name='xbee_device_manager', type = str, required = True)) self.__xbee_aio_driver = self.__xbee_dio_driver = \ XBeeLocalIO(self, core_services, self.__analog_channels, self.__digital_channels) ## Initialize the DeviceBase interface: DeviceBase.__init__(self, self.__name, self.__core, settings_list, property_list)
def calibrate(self): """calibrate() Calibrate analog inputs. Calculates scale and offset.""" global calTime, scale, offset calTime = time.clock() # XBee series 1 uses one calibration voltage on AN2 if self.device_type == XBeeSeries1: if digihw.get_channel_type(1) != Analog: raise ValueError, "No analog input channels" # Configure channel 1 as analog input self.XBeeCommandSet(input_lines[1], 2) # Enable calibration voltage on channel 1 self.XBeeCommandSet("d4", 4) time.sleep(0.010) # Read calibration sample result = self.XBeeCommandGet("is") sample = parseIS(result)["AI1"] if debug: print "calibration sample is %d" % sample # Return channel to operating mode self.XBeeCommandSet("d4", 5) time.sleep(0.010) if sample == 0: raise ValueError, "Calibration error: bad sample" # Calulate linear scale and offset. # These apply to all analog channels. scale = 1.25 / sample offset = 0 # XBee series 2 uses two calibration voltages on AN1 and AN2 elif self.device_type == XBeeSeries2 or self.device_type == XBeeZB: if digihw.get_channel_type(0) != Analog or digihw.get_channel_type( 1) != Analog: raise ValueError, "No analog input channels" # Configure channels 0 and 1 as analog inputs self.XBeeCommandSet(input_lines[0], 2) self.XBeeCommandSet(input_lines[1], 2) # Enable calibration voltages on channels 0 and 1 self.XBeeCommandSet("p2", 4) self.XBeeCommandSet("d4", 4) time.sleep(0.010) # Read calibration samples result = self.XBeeCommandGet("is") data = parseIS(result) sample = [data["AI0"], data["AI1"]] if debug: print "calibration samples are %d, %d" % (sample[0], sample[1]) # Return channels to operating mode self.XBeeCommandSet("p2", 5) self.XBeeCommandSet("d4", 5) time.sleep(0.010) if sample[0] == sample[1]: raise ValueError, "Calibration error: equal samples" scale1 = 511.5 / float(sample[1]) scale2 = 853.333 / float(sample[0]) scale = (scale1 + scale2) / 2.0 # Wasn't sure how to figure this out... offset = 0 else: raise ValueError, "XBee does not support analog inputs" if debug: print "scale is %f, offset is %f" % (scale, offset)
def __init__(self, name, core_services): self.__name = name self.__core = core_services ## Local State Variables: self.__xbee_aio_driver = None self.__xbee_dio_driver = None self.__analog_channels = [] self.__digital_channels = [] from core.tracing import get_tracer self.__tracer = get_tracer(name) ## Settings Table Definition: settings_list = [ Setting(name = 'power', type = Boolean, required = False, default_value = Boolean("On", STYLE_ONOFF), verify_function = self.__verify_power_setting), Setting(name = 'sample_rate_ms', type = int, required = False, default_value = 60000, verify_function = lambda x: x >= 100 and x <= 6000000), Setting(name = 'calibration_rate_ms', type = int, required = False, default_value = 900000, verify_function = lambda x: x >= 0), ] ## Channel Properties Definition: property_list = [] # Dynamically create the rest of our settings based on what the device supports. for ch in range(0, self.IO_MAX_CHANNELS): # Attempt to get the channel type. # If we try to go beyond the amount of supported channels, # we will get a ValueError exception. If we do, stop our loop. try: type = digihw.get_channel_type(ch) except ValueError: # we'll hit this once we go past valid count since we support 10 # channels, but likely device like X4H only has 4 # traceback.print_exc() break except: traceback.print_exc() if type == self.IO_TYPE_ANALOG: self.__tracer.info("Detected IO channel %d is Analog", ch + 1) self.__analog_channels.append(ch) settings_list.append(Setting( name = 'channel%d_mode' % (ch + 1), type=str, required=False, verify_function = _verify_aio_channel_mode, default_value = self.LOCAL_AIO_MODE_TENV)) elif type == self.IO_TYPE_DIGITAL: self.__tracer.info("Detected IO channel %d is Digital", ch + 1) self.__digital_channels.append(ch) settings_list.append(Setting( name = 'channel%d_dir' % (ch + 1), type = str, required=False, default_value='in')) settings_list.append(Setting( name = 'channel%d_source' % (ch + 1), type = str, required = False, default_value='')) else: raise "Unknown IO hardware type channel %d, is %d" % \ ((ch + 1), type) # Finally, on some platforms, the AIO and DIO support is done by # the XBee radio inside the unit. # # If this is the case, we will need to also have the # 'xbee_device_manager' setting provided to us as well. if get_platform_name() == 'digix3': settings_list.append(Setting(name='xbee_device_manager', type = str, required = True)) self.__xbee_aio_driver = self.__xbee_dio_driver = \ XBeeLocalIO(self, core_services, self.__analog_channels, self.__digital_channels) elif get_platform_name() == 'digiconnect': settings_list.append(Setting(name='xbee_device_manager', type = str, required = True)) self.__xbee_aio_driver = self.__xbee_dio_driver = \ XBeeLocalIO(self, core_services, self.__analog_channels, self.__digital_channels) ## Initialize the DeviceBase interface: DeviceBase.__init__(self, self.__name, self.__core, settings_list, property_list)