update calibration
This commit is contained in:
parent
47db59640b
commit
626bf43c41
3 changed files with 13 additions and 13 deletions
4
demo.py
4
demo.py
|
|
@ -3,8 +3,8 @@ import powermon as pm
|
|||
from time import sleep
|
||||
|
||||
sensors = [
|
||||
[0x40, 0x08B6], # pink/blue: Pi
|
||||
[0x44, 0x0B60] # pink/pink: Solar panel
|
||||
[0x40, 0.005, 0.00032, 0.696875], # pink/blue: Pi; 5 mOhm; 0.32 mA/bit; 0.696875 mult
|
||||
[0x44, 0.005, 0.00032, 0.91] # pink/pink: Solar panel; 5 mOhm; 0.32 mA/bit; 0.91 mult
|
||||
]
|
||||
|
||||
solarmon = pm.PowerMon(sensors)
|
||||
|
|
|
|||
17
inachip.py
17
inachip.py
|
|
@ -6,7 +6,6 @@ POWER_REG_ADDR = 0x03
|
|||
CURRENT_REG_ADDR = 0x04
|
||||
CAL_REG_ADDR = 0x05
|
||||
|
||||
CURRENT_LSB = 0.00032
|
||||
VOLTAGE_LSB = 0.00125
|
||||
SHUNT_LSB = 0.0000025
|
||||
|
||||
|
|
@ -16,14 +15,17 @@ class PowerData:
|
|||
current = 0.0
|
||||
|
||||
class INA226:
|
||||
def __init__(self, bus: smbus2.SMBus, addr: int, cal: int = 0):
|
||||
# I2C Address, Shunt Resistor, Current LSB, Calibration Multiplier
|
||||
def __init__(self, bus: smbus2.SMBus, addr: int, rshunt: int, clsb: float, cal: float = 1):
|
||||
self.addr = addr
|
||||
self.rshunt = rshunt,
|
||||
self.clsb = clsb,
|
||||
self.cal = cal
|
||||
self.bus = bus
|
||||
self.data = PowerData()
|
||||
|
||||
# Initial calibration
|
||||
self.calibrate(cal)
|
||||
self.calibrate()
|
||||
|
||||
def __readRegWord(self, reg: int) -> int:
|
||||
regWord = self.bus.read_word_data(self.addr, reg) & 0xFFFF
|
||||
|
|
@ -35,14 +37,15 @@ class INA226:
|
|||
self.bus.write_i2c_block_data(self.addr, reg, regBytes)
|
||||
|
||||
# Calibrate current sensor
|
||||
def calibrate(self, cal: int) -> None:
|
||||
self.__writeRegWord(CAL_REG_ADDR, cal)
|
||||
def calibrate(self) -> None:
|
||||
calReg = int((0.00512 / (self.clsb * self.rshunt)) * self.cal)
|
||||
self.__writeRegWord(CAL_REG_ADDR, calReg)
|
||||
|
||||
# Retrieve power data from sensor
|
||||
def getData(self) -> PowerData:
|
||||
self.data.voltage = float(self.__readRegWord(VOLTAGE_REG_ADDR)) * VOLTAGE_LSB
|
||||
self.data.power = float(self.__readRegWord(POWER_REG_ADDR)) * CURRENT_LSB * 25.0
|
||||
self.data.current = float(self.__readRegWord(CURRENT_REG_ADDR)) * CURRENT_LSB
|
||||
self.data.power = float(self.__readRegWord(POWER_REG_ADDR)) * self.clsb * 25.0
|
||||
self.data.current = float(self.__readRegWord(CURRENT_REG_ADDR)) * self.clsb
|
||||
if (self.data.current > 20.96):
|
||||
self.data.current = 0.0
|
||||
return self.data
|
||||
|
|
|
|||
|
|
@ -11,10 +11,7 @@ class PowerMon:
|
|||
try:
|
||||
ina = None
|
||||
for i in range(len(sensors)):
|
||||
if type(list[i]) is int:
|
||||
ina = INA226(self.bus, sensors[i])
|
||||
else:
|
||||
ina = INA226(self.bus, sensors[i][0], sensors[i][1])
|
||||
ina = INA226(self.bus, sensors[i][0], sensors[i][1], sensors[i][2], sensors[i][3])
|
||||
self.chipObjs.append(ina)
|
||||
except Exception as e:
|
||||
e.add_note("Initiating sensors failed")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue