def __init__(self, config, level): super().__init__() self._log = Logger('ioe', level) if config is None: raise ValueError('no configuration provided.') _config = config['ros'].get('potentiometer') self._pin = _config.get('pin') self._log.info('pin assignment: {:d};'.format(self._pin)) self._in_min = _config.get( 'in_min') # minimum analog value from IO Expander self._in_max = _config.get( 'in_max') # maximum analog value from IO Expander self._log.info('in range: {:d}-{:d}'.format(self._in_min, self._in_max)) self._out_min = _config.get('out_min') # minimum scaled output value self._out_max = _config.get('out_max') # maximum scaled output value self._log.info('out range: {:>7.4f}-{:>7.4f}'.format( self._out_min, self._out_max)) # configure board self._ioe = io.IOE(i2c_addr=0x18) self._ioe.set_adc_vref( 3.3 ) # input voltage of IO Expander, this is 3.3 on Breakout Garden # configure pin self._ioe.set_mode(self._pin, io.ADC) self._log.info('ready.')
def __init__(self, config, level): super().__init__() self._log = Logger('ioe', level) if config is None: raise ValueError('no configuration provided.') _config = config['ros'].get('ioe_potentiometer') # 0x18 for IO Expander, 0x0E for the potentiometer breakout # self._i2c_addr = 0x0E self._i2c_addr = _config.get('i2c_address') self._pin_red = _config.get('pin_red') self._pin_green = _config.get('pin_green') self._pin_blue = _config.get('pin_blue') self._log.info("pins: red: {}; green: {}; blue: {}".format( self._pin_red, self._pin_green, self._pin_blue)) self._pot_enc_a = 12 self._pot_enc_b = 3 self._pot_enc_c = 11 self._max_value = 3.3 # maximum voltage (3.3v supply) self._brightness = _config.get( 'brightness') # effectively max fraction of period LED will be on self._period = int( 255 / self._brightness ) # add a period large enough to get 0-255 steps at the desired brightness _in_min = _config.get( 'in_min') # minimum analog value from IO Expander _in_max = _config.get( 'in_max') # maximum analog value from IO Expander self.set_input_limits(_in_min, _in_max) _out_min = _config.get('out_min') # minimum scaled output value _out_max = _config.get('out_max') # maximum scaled output value self.set_output_limits(_out_min, _out_max) # now configure IO Expander self._ioe = io.IOE(i2c_addr=self._i2c_addr) self._ioe.set_mode(self._pot_enc_a, io.PIN_MODE_PP) self._ioe.set_mode(self._pot_enc_b, io.PIN_MODE_PP) self._ioe.set_mode(self._pot_enc_c, io.ADC) self._ioe.output(self._pot_enc_a, 1) self._ioe.output(self._pot_enc_b, 0) self._ioe.set_pwm_period(self._period) self._ioe.set_pwm_control( divider=2) # PWM as fast as we can to avoid LED flicker self._ioe.set_mode(self._pin_red, io.PWM, invert=True) self._ioe.set_mode(self._pin_green, io.PWM, invert=True) self._ioe.set_mode(self._pin_blue, io.PWM, invert=True) self._log.info("running LED with {} brightness steps.".format( int(self._period * self._brightness))) self._log.info("ready.")
def __init__(self, config, level): super().__init__() if config is None: raise ValueError('no configuration provided.') _config = config['ros'].get('io_expander') self._log = Logger('ioe', level) # infrared self._port_side_ir_pin = _config.get( 'port_side_ir_pin') # pin connected to port side infrared self._port_ir_pin = _config.get( 'port_ir_pin') # pin connected to port infrared self._center_ir_pin = _config.get( 'center_ir_pin') # pin connected to center infrared self._stbd_ir_pin = _config.get( 'stbd_ir_pin') # pin connected to starboard infrared self._stbd_side_ir_pin = _config.get( 'stbd_side_ir_pin') # pin connected to starboard side infrared self._log.info('infrared pin assignments:\t' \ + Fore.RED + ' port side={:d}; port={:d};'.format(self._port_side_ir_pin, self._port_ir_pin) \ + Fore.BLUE + ' center={:d};'.format(self._center_ir_pin) \ + Fore.GREEN + ' stbd={:d}; stbd side={:d}'.format(self._stbd_ir_pin, self._stbd_side_ir_pin)) # bumpers self._port_bmp_pin = _config.get( 'port_bmp_pin') # pin connected to port bumper self._center_bmp_pin = _config.get( 'center_bmp_pin') # pin connected to center bumper self._stbd_bmp_pin = _config.get( 'stbd_bmp_pin') # pin connected to starboard bumper self._log.info('bumper pin assignments:\t' \ + Fore.RED + ' port={:d};'.format(self._port_ir_pin) \ + Fore.BLUE + ' center={:d};'.format(self._center_ir_pin ) \ + Fore.GREEN + ' stbd={:d}'.format(self._stbd_ir_pin)) # configure board self._ioe = io.IOE(i2c_addr=0x18) self.board = self._ioe # TEMP self._ioe.set_adc_vref( 3.3 ) # input voltage of IO Expander, this is 3.3 on Breakout Garden # analog infrared sensors self._ioe.set_mode(self._port_side_ir_pin, io.ADC) self._ioe.set_mode(self._port_ir_pin, io.ADC) self._ioe.set_mode(self._center_ir_pin, io.ADC) self._ioe.set_mode(self._stbd_ir_pin, io.ADC) self._ioe.set_mode(self._stbd_side_ir_pin, io.ADC) # digital bumpers self._ioe.set_mode(self._port_bmp_pin, io.PIN_MODE_PU) self._ioe.set_mode(self._center_bmp_pin, io.PIN_MODE_PU) self._ioe.set_mode(self._stbd_bmp_pin, io.PIN_MODE_PU) self._log.info('ready.')
def __init__(self, i2c_addr=MICS6814_I2C_ADDR, interrupt_timeout=1.0, interrupt_pin=None, gpio=None): self._ioe = io.IOE(i2c_addr, interrupt_timeout, interrupt_pin, gpio) self._chip_id = self._ioe.get_chip_id() # TODO - Validate CHIP ID self.set_pwm_period(5100) self._ioe.set_pwm_control(divider=1) self.set_brightness(1.0) self._ioe.set_mode(MICS6814_LED_R, io.PWM) # P1.2 LED Red self._ioe.set_mode(MICS6814_LED_G, io.PWM) # P1.1 LED Green self._ioe.set_mode(MICS6814_LED_B, io.PWM) # P1.0 LED Blue self._ioe.set_mode(MICS6814_VREF, io.ADC) # P1.4 AIN5 - 2v8 self._ioe.set_mode(MICS6814_RED, io.ADC) # P0.5 AIN4 - Red self._ioe.set_mode(MICS6814_NH3, io.ADC) # P0.6 AIN3 - NH3 self._ioe.set_mode(MICS6814_OX, io.ADC) # P0.7 AIN2 - OX self._ioe.set_mode(MICS6814_HEATER_EN, io.OUT) # P1.5 Heater Enable self._ioe.output(MICS6814_HEATER_EN, io.LOW)
def test_cycles(): ioe = io.IOE() # ioe.set_mode(1, io.PWM) # ioe.set_mode(9, io.OUT) ioe.set_mode(4, io.OUT) # ioe.set_mode(14, io.ADC) # ioe.output(1, 1000) out_value = io.LOW while True: a = ioe.input(3) b = ioe.input(14) ioe.output(4, out_value) out_value = not out_value print("3: {a} 14: {b}".format(a=a, b=b)) time.sleep(1.0)
def __init__(self, ioe=None, i2c_addr=None, pin_r=1, pin_g=3, pin_b=5, invert=False, brightness=0.05): """RGB LED Device. IO Expander helper class to drive a single RGB LED connected to three PWM pins. Note: Will set the global PWM period and divider. :param ioe: Instance of ioexpander.IOE or None for automatic :param i2c_addr: I2C address when using automatic mode :param pin_r: Pin for the red LED :param pin_g: Pin for the green LED :param pin_b: Pin for the blue LED :param invert: Whether to invert the PWM signal (for common-cathode LEDs) :param brightness: LED brightness- translates to the maximum PWM duty cycle """ if ioe is None: ioe = ioexpander.IOE(i2c_addr=i2c_addr) self.ioe = ioe # Generate a period large enough to get 255 steps # at the desired brightness (maximum duty cycle) self.period = int(255.0 / brightness) self.pin_r = pin_r self.pin_g = pin_g self.pin_b = pin_b self.brightness = brightness ioe.set_pwm_period(self.period) ioe.set_pwm_control(divider=1) ioe.set_mode(self.pin_r, ioexpander.PWM, invert=invert) ioe.set_mode(self.pin_g, ioexpander.PWM, invert=invert) ioe.set_mode(self.pin_b, ioexpander.PWM, invert=invert)
import time from colorsys import hsv_to_rgb import ioexpander as io ioe = io.IOE(i2c_addr=0x18) chip_id = ioe.get_chip_id() print("Chip ID: {:04x}".format(chip_id)) ioe.set_pwm_period(255) ioe.set_mode(3, io.PWM) # P1.2 LED Red ioe.set_mode(7, io.PWM) # P1.1 LED Green ioe.set_mode(2, io.PWM) # P1.0 LED Blue ioe.set_mode(9, io.ADC) # P0.4 AIN5 - 2v8 ioe.set_mode(12, io.ADC) # P0.5 AIN4 - Red ioe.set_mode(11, io.ADC) # P0.6 AIN3 - NH3 ioe.set_mode(13, io.ADC) # P0.7 AIN2 - OX ioe.set_mode(1, io.OUT) # P1.5 Heater Enable ioe.output(1, io.LOW) last_heater = 0 while True: h = time.time() * 10.0 % 360 r, g, b = [int(c * 255.0) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] ioe.output(3, r) ioe.output(7, g) ioe.output(2, b)
""") PIN_RED = 1 PIN_GREEN = 7 PIN_BLUE = 2 POT_ENC_A = 12 POT_ENC_B = 3 POT_ENC_C = 11 BRIGHTNESS = 0.5 # Effectively the maximum fraction of the period that the LED will be on PERIOD = int( 255 / BRIGHTNESS ) # Add a period large enough to get 0-255 steps at the desired brightness ioe = io.IOE(i2c_addr=0x18, interrupt_pin=4) ioe.enable_interrupt_out(pin_swap=True) ioe.setup_rotary_encoder(1, POT_ENC_A, POT_ENC_B, pin_c=POT_ENC_C) ioe.set_pwm_period(PERIOD) ioe.set_pwm_control(divider=2) # PWM as fast as we can to avoid LED flicker ioe.set_mode(PIN_RED, io.PWM, invert=True) ioe.set_mode(PIN_GREEN, io.PWM, invert=True) ioe.set_mode(PIN_BLUE, io.PWM, invert=True) print("Running LED with {} brightness steps.".format(int(PERIOD * BRIGHTNESS))) count = 0
""") I2C_ADDR = 0x0E # 0x18 for IO Expander, 0x0E for the potentiometer breakout PIN_RED = 1 PIN_GREEN = 7 PIN_BLUE = 2 POT_ENC_A = 12 POT_ENC_B = 3 POT_ENC_C = 11 BRIGHTNESS = 0.5 # Effectively the maximum fraction of the period that the LED will be on PERIOD = int(255 / BRIGHTNESS) # Add a period large enough to get 0-255 steps at the desired brightness ioe = io.IOE(i2c_addr=I2C_ADDR) ioe.set_mode(POT_ENC_A, io.PIN_MODE_PP) ioe.set_mode(POT_ENC_B, io.PIN_MODE_PP) ioe.set_mode(POT_ENC_C, io.ADC) ioe.output(POT_ENC_A, 1) ioe.output(POT_ENC_B, 0) ioe.set_pwm_period(PERIOD) ioe.set_pwm_control(divider=2) # PWM as fast as we can to avoid LED flicker ioe.set_mode(PIN_RED, io.PWM, invert=True) ioe.set_mode(PIN_GREEN, io.PWM, invert=True) ioe.set_mode(PIN_BLUE, io.PWM, invert=True)
def __init__(self, config, level): super().__init__() if config is None: raise ValueError('no configuration provided.') _config = config['ros'].get('io_expander') self._log = Logger('ioe', level) # infrared self._port_side_ir_pin = _config.get( 'port_side_ir_pin') # pin connected to port side infrared self._port_ir_pin = _config.get( 'port_ir_pin') # pin connected to port infrared self._center_ir_pin = _config.get( 'center_ir_pin') # pin connected to center infrared self._stbd_ir_pin = _config.get( 'stbd_ir_pin') # pin connected to starboard infrared self._stbd_side_ir_pin = _config.get( 'stbd_side_ir_pin') # pin connected to starboard side infrared self._log.info('infrared pin assignments:\t' \ + Fore.RED + ' port side={:d}; port={:d};'.format(self._port_side_ir_pin, self._port_ir_pin) \ + Fore.BLUE + ' center={:d};'.format(self._center_ir_pin) \ + Fore.GREEN + ' stbd={:d}; stbd side={:d}'.format(self._stbd_ir_pin, self._stbd_side_ir_pin)) # moth/anti-moth self._port_moth_pin = _config.get( 'port_moth_pin') # pin connected to port moth sensor self._stbd_moth_pin = _config.get( 'stbd_moth_pin') # pin connected to starboard moth sensor self._log.info('moth pin assignments:\t' \ + Fore.RED + ' moth port={:d};'.format(self._port_moth_pin) \ + Fore.GREEN + ' moth stbd={:d};'.format(self._stbd_moth_pin)) # bumpers self._port_bmp_pin = _config.get( 'port_bmp_pin') # pin connected to port bumper self._cntr_bmp_pin = _config.get( 'center_bmp_pin') # pin connected to center bumper self._stbd_bmp_pin = _config.get( 'stbd_bmp_pin') # pin connected to starboard bumper self._log.info('bumper pin assignments:\t' \ + Fore.RED + ' port={:d};'.format(self._port_bmp_pin) \ + Fore.BLUE + ' center={:d};'.format(self._cntr_bmp_pin) \ + Fore.GREEN + ' stbd={:d}'.format(self._stbd_bmp_pin)) # debouncing "charge pumps" self._port_bmp_pump = 0 self._cntr_bmp_pump = 0 self._stbd_bmp_pump = 0 self._pump_limit = 10 # configure board try: import ioexpander as io self._ioe = io.IOE(i2c_addr=0x18) # self._ioe.set_i2c_addr(0x18) self._ioe.set_adc_vref( 3.3 ) # input voltage of IO Expander, this is 3.3 on Breakout Garden # analog infrared sensors self._ioe.set_mode(self._port_side_ir_pin, io.ADC) self._ioe.set_mode(self._port_ir_pin, io.ADC) self._ioe.set_mode(self._center_ir_pin, io.ADC) self._ioe.set_mode(self._stbd_ir_pin, io.ADC) self._ioe.set_mode(self._stbd_side_ir_pin, io.ADC) # moth sensors self._ioe.set_mode(self._port_moth_pin, io.ADC) self._ioe.set_mode(self._stbd_moth_pin, io.ADC) # digital bumper self._ioe.set_mode(self._port_bmp_pin, io.IN_PU) self._ioe.set_mode(self._cntr_bmp_pin, io.IN_PU) self._ioe.set_mode(self._stbd_bmp_pin, io.IN_PU) except ImportError: self._ioe = None self._log.error( "This script requires the pimoroni-ioexpander module\nInstall with: pip3 install --user pimoroni-ioexpander" ) self._log.info('ready.')
def __init__(self, config, level=Level.INFO): super().__init__() self._log = Logger("rot", level) if config is None: raise ValueError('null configuration argument.') _config = config['ros'].get('rotary_encoder') self._brightness = _config.get( 'brightness' ) # effectively the maximum fraction of the period that the LED will be on self._log.info("brightness:\t{:5.2f}".format(self._brightness)) self.increment = _config.get( 'increment') # the count change per rotary tick _i2c_address = _config.get('i2c_address') # self._log.info("I²C address:\t0x{:02x}".format(_i2c_address)) self._log.info( 'configured rotary encoder at I²C address: 0x{:02X}'.format( _i2c_address)) # _i2c_address = 0x0f # found via i2cdetect # self._log.info("default I²C address: 0x{:02X}".format(I2C_ADDR)) # self._log.info("assigned I²C address: 0x{:02X}".format(_i2c_address)) try: # _i2c_address = 0x16 _i2c_address = 0x0F # 0x18 for IO Expander, 0x0F for the encoder breakout # _i2c_address = 0x18 # 0x19 for IO Expander, 0x0F for the encoder breakout self._ioe = io.IOE(i2c_addr=_i2c_address, interrupt_pin=4) # swap the interrupt pin for the Rotary Encoder breakout self._ioe.enable_interrupt_out(pin_swap=True) # if I2C_ADDR != _i2c_address: # self._log.warning("force-setting I²C address of 0x{:02X}".format(_i2c_address)) # self._ioe.set_i2c_addr(_i2c_address) _POT_ENC_A = 12 _POT_ENC_B = 3 _POT_ENC_C = 11 self._ioe.setup_rotary_encoder( 1, _POT_ENC_A, _POT_ENC_B, pin_c=_POT_ENC_C) #, count_microsteps=False) self._period = int( 255 / self._brightness ) # add a period large enough to get 0-255 steps at the desired brightness self._log.info("running LED with {} brightness steps.".format( int(self._period * self._brightness))) self._ioe.set_pwm_period(self._period) self._ioe.set_pwm_control( divider=2) # PWM as fast as we can to avoid LED flicker self._channel = 1 self._pin_red = 1 self._pin_green = 7 self._pin_blue = 2 self._ioe.set_mode(self._pin_red, io.PWM, invert=True) self._ioe.set_mode(self._pin_green, io.PWM, invert=True) self._ioe.set_mode(self._pin_blue, io.PWM, invert=True) # reset IOE values on start self.reset() except Exception as e: self._log.error('error configuring rotary encoder: {}\n{}'.format( e, traceback.format_exc())) sys.exit(1) self._count = 0 self._log.info('ready.')
""") I2C_ADDR = 0x0F # 0x18 for IO Expander, 0x0F for the encoder breakout PIN_RED = 1 PIN_GREEN = 7 PIN_BLUE = 2 POT_ENC_A = 12 POT_ENC_B = 3 POT_ENC_C = 11 BRIGHTNESS = 0.5 # Effectively the maximum fraction of the period that the LED will be on PERIOD = int(255 / BRIGHTNESS) # Add a period large enough to get 0-255 steps at the desired brightness ioe = io.IOE(i2c_addr=I2C_ADDR, interrupt_pin=4) # Swap the interrupt pin for the Rotary Encoder breakout if I2C_ADDR == 0x0F: ioe.enable_interrupt_out(pin_swap=True) ioe.setup_rotary_encoder(1, POT_ENC_A, POT_ENC_B, pin_c=POT_ENC_C) ioe.set_pwm_period(PERIOD) ioe.set_pwm_control(divider=2) # PWM as fast as we can to avoid LED flicker ioe.set_mode(PIN_RED, io.PWM, invert=True) ioe.set_mode(PIN_GREEN, io.PWM, invert=True) ioe.set_mode(PIN_BLUE, io.PWM, invert=True) print("Running LED with {} brightness steps.".format(int(PERIOD * BRIGHTNESS)))