From 0f020562ee46e4428a84263241ab3567fb84d5e3 Mon Sep 17 00:00:00 2001 From: Nikolay Khabarov <2xl@mail.ru> Date: Tue, 13 Jun 2017 02:17:30 +0300 Subject: [PATCH] ads111x driver --- cnc/sensors/__init__.py | 0 cnc/sensors/ads111x.py | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 cnc/sensors/__init__.py create mode 100644 cnc/sensors/ads111x.py diff --git a/cnc/sensors/__init__.py b/cnc/sensors/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cnc/sensors/ads111x.py b/cnc/sensors/ads111x.py new file mode 100644 index 0000000..c13cfd8 --- /dev/null +++ b/cnc/sensors/ads111x.py @@ -0,0 +1,70 @@ +import os +import fcntl +import struct +import time +import atexit + +ADS111x_ADDRESS = 0x48 +I2C_SLAVE = 0x0703 + +# Initialize i2c interface and register it for closing on exit. +__i2c_dev = os.open("/dev/i2c-1", os.O_SYNC | os.O_RDWR) + + +def __close(): + os.close(__i2c_dev) + +if __i2c_dev < 0: + raise ImportError("i2c device not found") +else: + if fcntl.ioctl(__i2c_dev, I2C_SLAVE, ADS111x_ADDRESS) < 0: + __close() + raise ImportError("Failed to set up i2c address") + else: + atexit.register(__close) + + +def measure(channel): + """ + Measure voltage on chip input. + :param channel: chip channel to use. + :return: Voltage in Volts, None if no device connected. + """ + global __i2c_dev + if channel < 0 or channel > 3: + raise ValueError("Wrong channel") + if __i2c_dev < 0: + return None + # configure + data = struct.pack(">BH", + 0x01, # config register + # single shot mode, +-4.096V, AINN = GND + ((0b100 | channel) << 12) | 0x8380 + ) + os.write(__i2c_dev, data) + # wait for conversion + while True: + os.write(__i2c_dev, struct.pack("B", 0x01)) + if struct.unpack(">H", os.read(__i2c_dev, 2))[0] & 0x8000 != 0: + break + time.sleep(0.0001) + # read result + os.write(__i2c_dev, struct.pack("B", 0x00)) # conversion register + v = struct.unpack(">h", os.read(__i2c_dev, 2))[0] + return v / 8000.0 # / 32768.0 * 4.096 according to specified range + + +# check if ads111x is connected +try: + measure(0) +except OSError: + raise ImportError("ads111x is not connected") + + +# for test purpose +if __name__ == "__main__": + while True: + for i in range(0, 4): + print(str(i), measure(i)) + print("-----------------------------") + time.sleep(0.5)