def __init__(self, min_volts=3, max_volts=4):
     RingBuffer.__init__(self, 10)
     self.MIN_VOLTS = min_volts
     self.MAX_VOLTS = max_volts
     max_volts_diff = self.max_volts - self.min_volts
     i2c = busio.I2C(board.SCL, board.SDA)
     ina219 = adafruit_ina219.INA219(i2c)
Beispiel #2
0
 def __init__(self, voltagesensor_config):
     super().__init__(voltagesensor_config)
     self.property_bus = "i2c"
     self.i2cbus = busio.I2C(board.SCL, board.SDA)
     self.ina219 = adafruit_ina219.INA219(self.i2cbus)
     self.voltage_value = 0
     self.current_value = 0
Beispiel #3
0
 def __init__(self):
     threading.Thread.__init__(self)
     i2c_bus = busio.I2C(SCL, SDA)
     self.ina219 = adafruit_ina219.INA219(i2c_bus)
     self.exitFlag = 1
     self.retFlag = 0
     self.totalvoltage = 0.0
     self.totalcurrent = 0.0
     self.totalpower = 0.0
     self.counter = 0
    def start(self):
        """
        Setup the INA219 sensor driver.

        :return:
        """

        # Ensure a bus object exists and is ready.
        self.ensure_bus()

        # Initialize the hardware driver.
        try:

            # MicroPython
            if platform_info.vendor in [
                    platform_info.MICROPYTHON.Vanilla,
                    platform_info.MICROPYTHON.Pycom
            ]:
                from ina219 import INA219
                # TODO: Make "shunt_ohms" configurable. The Adafruit breakout board uses a resistor value of 0.1 Ohms.
                self.driver = INA219(shunt_ohms=0.1,
                                     i2c=self.bus.adapter,
                                     address=self.address)
                # TODO: Optionally invoke "self.driver.configure()".
                self.driver.configure()

            # Adafruit CircuitPython
            elif platform_info.vendor == platform_info.MICROPYTHON.RaspberryPi:

                # TODO: https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/python-circuitpython
                # TODO: optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage

                import adafruit_ina219
                #self.adc_resolution = adafruit_ina219.ADCResolution
                #self.bus_voltage_range = adafruit_ina219.BusVoltageRange
                self.driver = adafruit_ina219.INA219(i2c_bus=self.bus.adapter,
                                                     addr=self.address)

            else:
                raise NotImplementedError(
                    'INA219 driver not implemented on this platform')

            return True

        except Exception as ex:
            log.exc(ex, 'INA219 hardware driver failed')
            return False
Beispiel #5
0
import board
import busio
import adafruit_ina219
import subprocess

try:
    i2c = busio.I2C(board.SCL, board.SDA)
    ina219 = adafruit_ina219.INA219(i2c, 0x41)

    current_voltage = ina219.bus_voltage
    print(f"Current voltage = {current_voltage}")

    if 5.5 < current_voltage < 7:
        print("current voltage under 7. Shutting down to protect the pi")
        subprocess.check_output(['sudo', 'shutdown', '-h', 'now'])
except Exception as e:
    print(e)
spi.max_speed_hz = 244000  # sets master freq at 244 kHz, must be (150:300) kHz for RWA
spi.mode = 0b00  # sets clock polarity to 0, sets clock phase to 0

# ENABLE GPIO INITIALIZATION
GPIO.setmode(GPIO.BCM)

GPIO.setup(25, GPIO.OUT)
GPIO.output(25, True)  # sets ENABLE to high - +3.3V

GPIO.setup(21, GPIO.OUT)
GPIO.output(21, True)  # sets NSS to high - +3.3V

# INA219 INITIALIZATION
i2c = busio.I2C(board.SCL, board.SDA)
ina219 = adafruit_ina219.INA219(i2c)

# FUNCTIONS --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

# CRC FUNCTION
crcTable = [
    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108,
    0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210,
    0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b,
    0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401,
    0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee,
    0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6,
    0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d,
    0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
    0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5,
    0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc,
Beispiel #7
0
 def __init__(self, i2c=None):
     if i2c is None:
         i2c = board.I2C()
     self._ina219 = adafruit_ina219.INA219(i2c)
Beispiel #8
0
 def __init__(self):
     self._ina219 = adafruit_ina219.INA219(shared.I2C_BUS)
Beispiel #9
0
    def __init__(self):
        self.imu = False
        self.baro = False
        self.currentSense = False
        self.gps = False

        # I2C connection:
        self.i2c = busio.I2C(board.SCL, board.SDA)
        try:
            self.imu = adafruit_lsm9ds1.LSM9DS1_I2C(self.i2c)
        except:
            print("could not connect to LSM9DS1")

        try:
            self.currentSense = adafruit_ina219.INA219(self.i2c, addr=0x45)
        except:
            print("could not connect to INA219")

        try:
            self.baro = adafruit_mpl3115a2.MPL3115A2(self.i2c)
            # Calibrate altimeter
            self.baro.sealevel_pressure = 102250
        except:
            print("could not connect to MPL3115A2")

        #self.RX = board.D15 #RX
        #self.TX = board.D14 #TX

        # Create a serial connection for the GPS connection using default speed and
        # a slightly higher timeout (GPS modules typically update once a second).
        try:
            #self.uart = busio.UART(self.TX, self.RX, baudrate=9600, timeout=3000)
            self.uart = serial.Serial("/dev/ttyS0",
                                      baudrate=9600,
                                      timeout=3000)
            self.gps = adafruit_gps.GPS(self.uart, debug=False)
            # Initialize the GPS module by changing what data it sends and at what rate.
            # These are NMEA extensions for PMTK_314_SET_NMEA_OUTPUT and
            # PMTK_220_SET_NMEA_UPDATERATE but you can send anything from here to adjust
            # the GPS module behavior:
            #   https://cdn-shop.adafruit.com/datasheets/PMTK_A11.pdf

            # Turn on the basic GGA and RMC info (what you typically want)
            self.gps.send_command(
                b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
            # Turn on just minimum info (RMC only, location):
            #gps.send_command(b'PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
            # Turn off everything:
            #gps.send_command(b'PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
            # Tuen on everything (not all of it is parsed!)
            #gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0')

            # Set update rate to once a second (1hz) which is what you typically want.
            self.gps.send_command(b'PMTK220,900')
            # Or decrease to once every two seconds by doubling the millisecond value.
            # Be sure to also increase your UART timeout above!
            #gps.send_command(b'PMTK220,2000')
            # You can also speed up the rate, but don't go too fast or else you can lose
            # data during parsing.  This would be twice a second (2hz, 500ms delay):
            # gps.send_command(b'PMTK220,500')
            print(self.gps)
        except:
            print("could not connect to GPS")

        self.data = {
            'accel': [0, 0, 0],
            'gyro': [0, 0, 0],
            'mag': [0, 0, 0],
            'imu_temp': 0,
            'temp': 0,
            'pressure': 0,
            'alt': 0,
            'gps': 'no_fix',
            'bus_voltage': 0,
            'shunt_voltage': 0,
            'current': 0,
            'dt': 0
        }

        self.accel_offset = [0, 0, 0]
        self.gyro_offset = [0, 0, 0]

        super(Sensors, self).__init__()
Beispiel #10
0
 def __init__(self, address=0x40):
     i2c = busio.I2C(board.SCL, board.SDA)
     try:
         self.sensor = adafruit_ina219.INA219(i2c, address)
     except:
         self.sensor = None
Beispiel #11
0
#

import board
import digitalio
import time

import busio
import adafruit_ina219

i2c_bus = busio.I2C(board.SCL, board.SDA)

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

# Create library object on our I2C port
ina219 = adafruit_ina219.INA219(i2c_bus, 0x41)

while True:
    led.value = True
    time.sleep(1)

    print("Bus Voltage:     {} V".format(ina219.bus_voltage))
    print("Shunt Voltage:   {} mV".format(ina219.shunt_voltage / 1000))
    print("Load Voltage:    {} V".format(ina219.bus_voltage +
                                         ina219.shunt_voltage))
    print("Current:         {} mA".format(ina219.current))
    print("")

    led.value = False
    time.sleep(4)