Hi! This is really useful. Thanks!
We have made your code a Python class, and changed the A to D conversions from single shot to continuous, which makes everything faster (a read gives the last complete conversion the chip did when it's converting in a continuous internal loop).
I haven't done a pull request for this, as a pull from all of our robot code would not be very useful.
###########################################################################################
#
# The LTC 2990 Quad I2C Voltage, Current and Temperature Monitor is used to
# measure the output from the Hall effect sensors that determine if a foot is touching a
# surface or not.
#
#
# LTC 2990 Quad I2C Voltage, Current and Temperature Monitor
# Retrieves LTC2990 register and performs some basic operations.
# Specs: http://www.linear.com/product/LTC2990
# Source: https://github.com/rfrht/ltc2990
#
# Copyright (C) 2015 Rodrigo A B Freire
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
#--------------------------------------------------------
#
# Updated to Python3 and made into a class by
#
# Adrian Bowyer
# RepRap Ltd
# https://reprapltd.com
#
# 26 May 2022
#
class AToD:
def __init__(self):
self.bus = smbus.SMBus(1) # 512-MB RPi the bus is 1. Otherwise, bus is 0.
# Pro tip: Ensure that ADR0 and ADR1 are grounded. Do not let them
# open. Otherwise, the i2c address will randomly change.
self.address = 0x4c # I2C chip address
# self.mode = 0x5f # Register 0x01 mode select - single aquisition
self.mode = 0x1f # Register 0x01 mode select - repeated aquisition
err = ""
try:
if self.bus.read_byte_data(self.address, 0x01) != self.mode: # If current IC mode != program mode
self.bus.write_byte_data(self.address, 0x01, self.mode) # Initializes the IC and set mode
self.bus.write_byte_data(self.address, 0x02, 0x00) # Trigger a initial data collection
time.sleep(1) # Wait a sec, just for init
except (IOError, err):
print(err)
# Check for a specific bit value
def GetBit(self, number, bit):
return (number >> bit) & 1
# 2 bytes to Chip temperature
def GetTemperature(self, msb, lsb):
msb = format(msb, '08b')
msb = msb[3:]
lsb = format(lsb, '08b')
temp = msb + lsb
temp = int(temp, 2)/16
return temp
# 2 bytes to voltage
def GetVoltage(self, msb, lsb):
msb = format(msb, '08b')
msb = msb[1:]
lsb = format(lsb, '08b')
signal = self.GetBit(int(msb, 2),6)
#print "positive:0 negative:1 %s" %signal
volt = msb[1:] + lsb
volt = int(volt, 2) * 0.00030518
return volt
# Return everything the chip knows as a printable string
def GetAllValues(self):
if self.mode is 0x5f:
self.bus.write_byte_data(self.address, 0x02, 0x00) # Trigger a data collection
time.sleep(0.1)
r0 = self.bus.read_byte_data(self.address, 0x00) # Status
r1 = self.bus.read_byte_data(self.address, 0x01) # Control - mode select
r4 = self.bus.read_byte_data(self.address, 0x04) # Temp. Int. MSB
r5 = self.bus.read_byte_data(self.address, 0x05) # Temp. Int. LSB
r6 = self.bus.read_byte_data(self.address, 0x06) # V1, V1 - V2 or TR1 MSB
r7 = self.bus.read_byte_data(self.address, 0x07) # V1, V1 - V2 or TR1 LSB
r8 = self.bus.read_byte_data(self.address, 0x08) # V2, V1 - V2 or TR1 MSB
r9 = self.bus.read_byte_data(self.address, 0x09) # V2, V1 - V2 or TR1 LSB
ra = self.bus.read_byte_data(self.address, 0x0a) # V3, V3 - V4 or TR2 MSB
rb = self.bus.read_byte_data(self.address, 0x0b) # V3, V3 - V4 or TR2 LSB
rc = self.bus.read_byte_data(self.address, 0x0c) # V4, V3 - V4 or TR2 MSB
rd = self.bus.read_byte_data(self.address, 0x0d) # V4, V3 - V4 or TR2 LSB
re = self.bus.read_byte_data(self.address, 0x0e) # Vcc MSB
rf = self.bus.read_byte_data(self.address, 0x0f) # Vcc LSB
result = "Status register: " + hex(r0) + "\n"
result += "Control register: " + hex(r1) + "\n"
result += "Int. Temp. : " + str(self.GetTemperature(r4,r5)) + " Celsius\n"
result += "Voltage V0 : " + str(self.GetVoltage(r6,r7)) + " V\n"
result += "Voltage V1 : " + str(self.GetVoltage(r8,r9)) + " V\n"
result += "Voltage V2 : " + str(self.GetVoltage(ra,rb)) + " V\n"
result += "Voltage V3 : " + str(self.GetVoltage(rc,rd)) + " V\n"
result += "Supply: " + str(self.GetVoltage(re,rf) + 2.5) + " V\n"
# If you want to use TR, use the temperature(msb,lsb) function to get the
# value. I.e., if you have set the mode TR1 & TR2 (mode 0x5d),
# Comment the print "Voltage" lines and uncomment these ones:
# TR1
# print "Temperature TR1: %s Celsius" %temperature(r6,r7)
# TR2
# print "Temperature TR2: %s Celsius" %temperature(ra,rb)
return result
#
# Voltage on one of the four channels.
#
def Voltage(self, channel):
msb = channel*2 + 0x06
lsb = msb + 1
if self.mode is 0x5f:
self.bus.write_byte_data(self.address, 0x02, 0x00) # Trigger a data collection
time.sleep(0.1) # 100 ms is horribly long...
msb = self.bus.read_byte_data(self.address, msb)
lsb = self.bus.read_byte_data(self.address, lsb)
return self.GetVoltage(msb, lsb)