def __init__(self, bus, adc_addr=0x48, motor_pin1=L293_1, motor_pin2=L293_2, motor_enable=STEREO_L293_ENABLE, dirmult=1, verbose=False): Thread.__init__(self) self.motor = Motor(pin1=motor_pin1, pin2=motor_pin2, enable=motor_enable) self.motor.set_speed(0) self.adc = ADS1015(bus, adc_addr) self.adc.write_config(MUX_AIN0 | PGA_4V | MODE_CONT | DATA_1600 | COMP_MODE_TRAD | COMP_POL_LOW | COMP_NON_LAT | COMP_QUE_DISABLE) self.dirmult = dirmult self.setPoint = None self.newSetPoint = False self.moving = False self.daemon = True self.verbose = verbose self.lastStopTime = time.time() self.start()
def __init__(self, config, clock, queue, message_factory, level): self._log = Logger("battery", level) if config is None: raise ValueError('no configuration provided.') _battery_config = config['ros'].get('battery') # configuration .................... self._enable_battery_messaging = _battery_config.get('enable_battery_messaging') self._enable_channel_a_messaging = _battery_config.get('enable_channel_a_messaging') self._enable_channel_b_messaging = _battery_config.get('enable_channel_b_messaging') _CHANNELS = ['in0/ref', 'in1/ref', 'in2/ref'] self._battery_channel = _CHANNELS[_battery_config.get('battery_channel')] self._five_volt_a_channel = _CHANNELS[_battery_config.get('five_volt_a_channel')] self._five_volt_b_channel = _CHANNELS[_battery_config.get('five_volt_b_channel')] self._raw_battery_threshold = _battery_config.get('raw_battery_threshold') self._five_volt_threshold = _battery_config.get('low_5v_threshold') _loop_delay_sec = _battery_config.get('loop_delay_sec') self._loop_delay_sec_div_10 = _loop_delay_sec / 10 self._log.info('battery check loop delay: {:>5.2f} sec'.format(_loop_delay_sec)) self._log.info('setting 5v regulator threshold to {:>5.2f}v'.format(self._five_volt_threshold)) self._log.info("channel A from '{}'; channel B from '{}'; raw battery threshold to {:>5.2f}v from '{}'".format(\ self._five_volt_a_channel, self._five_volt_b_channel, self._raw_battery_threshold, self._battery_channel)) self._clock = clock # configure ThunderBorg try: TB = ThunderBorg.ThunderBorg(Level.INFO) TB.Init() if not TB.foundChip: boards = ThunderBorg.ScanForThunderBorg() if len(boards) == 0: raise Exception('no thunderborg found, check you are attached.') else: raise Exception('no ThunderBorg at address {:02x}, but we did find boards:'.format(TB.i2cAddress)) self._tb = TB except Exception as e: self._tb = None self._log.error('unable to configure ThunderBorg: {}\n{}'.format(e, traceback.format_exc())) self._queue = queue self._queue.add_consumer(self) self._message_factory = message_factory self._battery_voltage = 0.0 self._regulator_a_voltage = 0.0 self._regulator_b_voltage = 0.0 try: self._ads1015 = ADS1015() self._ads1015.set_mode('single') self._ads1015.set_programmable_gain(2.048) self._ads1015.set_sample_rate(1600) self._reference = self._ads1015.get_reference_voltage() self._log.info('reference voltage: {:6.3f}v'.format(self._reference)) except Exception as e: raise RuntimeError('error configuring AD converter: {}'.format(traceback.format_exc())) self._count = 0 self._enabled = False self._closed = False self._log.info('ready.')
def __init__(self, level): self._log = Logger("ads1015", level) self._channel_0_voltage = 0.0 self._channel_1_voltage = 0.0 self._channel_2_voltage = 0.0 try: self._ads1015 = ADS1015() self._ads1015.set_mode('single') self._ads1015.set_programmable_gain(2.048) self._ads1015.set_sample_rate(1600) self._reference = self._ads1015.get_reference_voltage() self._log.info('reference voltage: {:6.3f}v'.format( self._reference)) except Exception as e: raise RuntimeError('error configuring AD converter: {}'.format( traceback.format_exc())) self._log.info('ready.')
def run(self): a0cal = 4.108 a1cal = 4.123 a2cal = 4.095 a3cal = 4.126 ads = ADS1015() ads.set_mode('single') ads.set_programmable_gain(4.096) while True: global breakIndicator if breakIndicator == True: return try: TimeStamp = time.time() a0 = ads.get_voltage('in0/gnd') a1 = ads.get_voltage('in1/gnd') a2 = ads.get_voltage('in2/gnd') a3 = ads.get_voltage('in3/gnd') battery1Voltage = round(a0 * a0cal, 2) battery2Voltage = round(a1 * a1cal, 2) voltage12v = round(a2 * a2cal, 2) voltage5v = round(a3 * a3cal, 2) OutgoingVoltages = ("volt#" + str(battery1Voltage) + ", " + str(battery2Voltage) + ", " + str(voltage12v) + ", " + str(voltage5v)) qVoltages.put(OutgoingVoltages) csvWrite(VoltageLog, (TimeStamp, battery1Voltage, battery2Voltage, voltage12v, voltage5v), "a") time.sleep(10) except Exception as Exc: print("Could not retrieve voltages.") time.sleep(0.1) return return
def main(): signal.signal(signal.SIGINT, signal_handler) print('ads_test :' + Fore.CYAN + Style.BRIGHT + ' INFO : starting test...' + Style.RESET_ALL) print('ads_test :' + Fore.YELLOW + Style.BRIGHT + ' INFO : Press Ctrl+C to exit.' + Style.RESET_ALL) CHANNEL_0 = 'in0/ref' CHANNEL_1 = 'in1/ref' CHANNEL_2 = 'in2/ref' ads1015 = ADS1015() ads1015.set_mode('single') ads1015.set_programmable_gain(2.048) ads1015.set_sample_rate(1600) reference = ads1015.get_reference_voltage() print('ads_test :' + Fore.CYAN + ' INFO : Reference voltage: {:6.3f}v'.format(reference) + Style.RESET_ALL) count = 0 while count < 10: count += 1 value_0 = ads1015.get_compensated_voltage(channel=CHANNEL_0, reference_voltage=reference) value_1 = ads1015.get_compensated_voltage(channel=CHANNEL_1, reference_voltage=reference) value_2 = ads1015.get_compensated_voltage(channel=CHANNEL_2, reference_voltage=reference) print('ads_test :' + Fore.CYAN + ' INFO : A0={:6.3f}v; A1={:6.3f}v; A2={:6.3f}v;'.format( value_0, value_1, value_2) + Style.RESET_ALL) time.sleep(0.25) print('ads_test :' + Fore.CYAN + Style.BRIGHT + ' INFO : test complete.' + Style.RESET_ALL)
def __init__(self, queue, channel, level): self._log = Logger("lrir", level) self._channel = CHANNELS[channel] self._queue = queue self._short_range_threshold_value_cm = 40.0 self._long_range_threshold_value_cm = 52.0 self._log.info( 'setting short range infrared sensor to {:>5.1f}cm, long range to {:>5.1f}cm' .format(self._short_range_threshold_value_cm, self._long_range_threshold_value_cm)) self._ads1015 = ADS1015() self._ads1015.set_mode('single') self._ads1015.set_programmable_gain(2.048) self._ads1015.set_sample_rate(1600) self._reference = self._ads1015.get_reference_voltage() self._log.info('reference voltage: {:6.3f}v'.format(self._reference)) self._enable_long_range_scan = True self._short_range_hits = 0 self._long_range_hits = 0 self._closed = False self._thread = None self._log.info('ready.')
#!/usr/bin/env python import time from ads1015 import ADS1015 CHANNELS = ['in0/ref', 'in1/ref', 'in2/ref'] print("""read-all.py - read all three inputs of the ADC Press Ctrl+C to exit! """) ads1015 = ADS1015() ads1015.set_mode('single') ads1015.set_programmable_gain(2.048) ads1015.set_sample_rate(1600) reference = ads1015.get_reference_voltage() print("Reference voltage: {:6.3f}v \n".format(reference)) try: while True: for channel in CHANNELS: value = ads1015.get_compensated_voltage( channel=channel, reference_voltage=reference) print("{}: {:6.3f}v".format(channel, value)) print("") time.sleep(0.5) except KeyboardInterrupt:
def main(logging, returnImage=False): try: ''' Try importing Configuration ''' from config import Configuration # for DHT Sensor DHT_PIN = Configuration['DHT-Pin'] # For Ads1015 CHANNEL = Configuration['CHANNEL'] # For Vaccum Pump GPIO.setup(Configuration["Vaccum-Pump-Pin"], GPIO.OUT) # set a port/pin as an output if DHT_PIN is not None and CHANNEL is not None: logging.info("Importing config file success") else: raise ValueError("Error with Config file..") except Exception as error: logging.error("Error occured while importing module...", exc_info=True) try: import ST7789 as ST7789 logging.info('Importing LCD module Success') # For Display # Create ST7789 LCD display class. disp = ST7789.ST7789( port=0, cs=ST7789.BG_SPI_CS_FRONT, # BG_SPI_CSB_BACK or BG_SPI_CS_FRONT dc=9, backlight=19, # 18 for back BG slot, 19 for front BG slot. spi_speed_hz=80 * 1000 * 1000) # Initialize display. WIDTH = disp.width HEIGHT = disp.height disp.begin() logging.info("Initialising Display Successful") except Exception as error: logging.error("Error occured while Initialising LCD module...", exc_info=True) try: import Adafruit_DHT logging.info("Importing module Adafruit_DHT Success") DHT_SENSOR = Adafruit_DHT.DHT22 humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) if humidity is not None and temperature is not None: logging.info("Initialising and reading from DHT Sensor Success") else: ValueError("Failed to Initialise and read data from DHT Sensor..") except Exception as error: logging.error("Error occured while importing modules...", exc_info=True) try: from ads1015 import ADS1015 logging.info("Importing module ads1015 for Vaccum Sensor Success..") ads1015 = ADS1015() ads1015.set_mode('single') ads1015.set_programmable_gain(Configuration['ADS-Gain']) ads1015.set_sample_rate(Configuration['Sample-Rate']) reference = ads1015.get_reference_voltage() value = ads1015.get_compensated_voltage(channel=CHANNEL, reference_voltage=reference) if value is not None: logging.info("Initialising ADS1015 For Vaccum Sensor Success") else: raise ValueError("Error Initialising ADS1015 for vaccum sensor") except Exception as error: logging.error("Error occured while importing modules...", exc_info=True) goOn = True startMotor = False while goOn: try: humidity, temperature = Adafruit_DHT.read_retry( DHT_SENSOR, DHT_PIN) if humidity is None or temperature is None: raise ValueError("Error reading values from DHT...") except: logging.error( "Failed to read data from temp sensor using dummy values", exc_info=True) humidity = 0.9 temperature = 30.92 try: negPressure = ads1015.get_compensated_voltage( channel=CHANNEL, reference_voltage=reference) if negPressure is None: raise ValueError('Failed ot read data from Vaccum Sensor') except: logging.error( "Failed to read data from vaccum sensor using dummy values", exc_info=True) negPressure = 1.5 goOn = False Readings = { "Temperature": temperature, "Humidity": humidity, "Neg Pressure": negPressure * Configuration["multiplier"] } logging.info("Temp={} Humidity={} Neg Pressure={}".format( temperature, humidity, negPressure)) if negPressure >= Configuration['PressureUpperBound']: startMotor = True GPIO.output(Configuration["Vaccum-Pump-Pin"], 1) # set port/pin value to 1/GPIO.HIGH/True if negPressure <= Configuration['PressureLowerBound']: startMotor = False GPIO.output(Configuration["Vaccum-Pump-Pin"], 0) # set port/pin value to 0/GPIO.LOW/False arrowRotation = 0 if startMotor: arrowRotation = (arrowRotation + 10) % 360 image = None try: image = produceImage(prevRotation=arrowRotation, Configuration=Configuration, Readings=Readings) image.save("Image.png") logging.info("Image Produced Successfully") if WIDTH is not None and HEIGHT is not None: image = image.resize((WIDTH, HEIGHT)) else: image = image.resize((480, 480)) disp.display(image) except: logging.error("Error producing or displaying image", exc_info=True) goOn = False time.sleep(1) if returnImage: return image
#!/usr/bin/python3 # # based on example at # https://github.com/pimoroni/ads1015-python/blob/master/examples/read-all.py # from ads1015 import ADS1015 # calibrations of resistive dividers measured with voltmeter a0cal = 4.108 a1cal = 4.123 a2cal = 4.095 a3cal = 4.126 ads = ADS1015() ads.set_mode('single') ads.set_programmable_gain(4.096) a0 = ads.get_voltage('in0/gnd') a1 = ads.get_voltage('in1/gnd') a2 = ads.get_voltage('in2/gnd') a3 = ads.get_voltage('in3/gnd') print('Battery 1: %.3fV (%.3f)' % ((a0 * a0cal), a0)) print('Battery 2: %.3fV (%.3f)' % ((a1 * a1cal), a1)) print(' 12 volt: %.3fV (%.3f)' % ((a2 * a2cal), a2)) print(' 5 volt: %.3fV (%.3f)' % ((a3 * a3cal), a3))