Browse code

minor revision

Gandolf authored on 07/06/2021 21:04:25
Showing 1 changed files
... ...
@@ -70,8 +70,9 @@ class ina260:
70 70
 
71 71
         if self.debugMode:
72 72
             data = self.getInfo()
73
+            print(self)
73 74
             print("manufacturer ID: %s %s\n"\
74
-                  "configuration register: %s %s\n" % data)
75
+                  "INA260 configuration register: %s %s\n" % data)
75 76
     ## end def
76 77
 
77 78
     def getInfo(self):
Browse code

minor revision

Gandolf authored on 07/03/2021 01:42:15
Showing 1 changed files
... ...
@@ -57,16 +57,24 @@ class ina260:
57 57
     # register.
58 58
     def __init__(self, sAddr=DEFAULT_BUS_ADDRESS,
59 59
                        sbus=DEFAULT_BUS_NUMBER,
60
-                       config=DEFAULT_CONFIG):
60
+                       config=DEFAULT_CONFIG,
61
+                       debug=False):
61 62
         # Instantiate a smbus object.
62 63
         self.sensorAddr = sAddr
63 64
         self.bus = smbus.SMBus(sbus)
65
+        self.debugMode = debug
66
+
64 67
         # Initialize INA260 sensor.  
65 68
         initData = [(config >> 8), (config & 0x00FF)]
66 69
         self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
70
+
71
+        if self.debugMode:
72
+            data = self.getInfo()
73
+            print("manufacturer ID: %s %s\n"\
74
+                  "configuration register: %s %s\n" % data)
67 75
     ## end def
68 76
 
69
-    def status(self):
77
+    def getInfo(self):
70 78
         # Read manufacture identification data.
71 79
         mfcid = self.bus.read_i2c_block_data(self.sensorAddr, ID_REG, 2)
72 80
         mfcidB1 = format(mfcid[0], "08b")
... ...
@@ -91,7 +99,7 @@ class ina260:
91 99
         # Get the current data from the sensor.
92 100
         # INA260 returns the data in two bytes formatted as follows
93 101
         #        -------------------------------------------------
94
-        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
102
+        #    bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
95 103
         #        -------------------------------------------------
96 104
         # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
97 105
         #        -------------------------------------------------
... ...
@@ -100,6 +108,12 @@ class ina260:
100 108
         # The current is returned in d15-d0, a two's complement,
101 109
         # 16 bit number.  This means that d15 is the sign bit.        
102 110
         data=self.bus.read_i2c_block_data(self.sensorAddr, CUR_REG, 2)
111
+
112
+        if self.debugMode:
113
+            dataB1 = format(data[0], "08b")
114
+            dataB2 = format(data[1], "08b")
115
+            print("current register: %s %s" % (dataB1, dataB2))
116
+
103 117
         # Format into a 16 bit word.
104 118
         bdata = data[0] << 8 | data[1]
105 119
         # Convert from two's complement to integer.
... ...
@@ -126,7 +140,7 @@ class ina260:
126 140
         # Get the voltage data from the sensor.
127 141
         # INA260 returns the data in two bytes formatted as follows
128 142
         #        -------------------------------------------------
129
-        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
143
+        #    bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
130 144
         #        -------------------------------------------------
131 145
         # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
132 146
         #        -------------------------------------------------
... ...
@@ -134,6 +148,12 @@ class ina260:
134 148
         #        -------------------------------------------------
135 149
         # The voltage is returned in d15-d0 as an unsigned integer.
136 150
         data=self.bus.read_i2c_block_data(self.sensorAddr, VOLT_REG, 2)
151
+
152
+        if self.debugMode:
153
+            dataB1 = format(data[0], "08b")
154
+            dataB2 = format(data[1], "08b")
155
+            print("voltage register: %s %s" % (dataB1, dataB2))
156
+
137 157
         # Convert data to volts.
138 158
         volts = (data[0] << 8 | data[1]) * 0.00125 # LSB is 1.25 mV
139 159
         return volts
... ...
@@ -143,7 +163,7 @@ class ina260:
143 163
         # Get the wattage data from the sensor.
144 164
         # INA260 returns the data in two bytes formatted as follows
145 165
         #        -------------------------------------------------
146
-        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
166
+        #    bit | 7   |  6  |  5  |  4  |  3  |  2  |  1  |  0  |
147 167
         #        -------------------------------------------------
148 168
         # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
149 169
         #        -------------------------------------------------
... ...
@@ -151,6 +171,12 @@ class ina260:
151 171
         #        -------------------------------------------------
152 172
         # The wattage is returned in d15-d0 as an unsigned integer.
153 173
         data=self.bus.read_i2c_block_data(self.sensorAddr, PWR_REG, 2)
174
+
175
+        if self.debugMode:
176
+            dataB1 = format(data[0], "08b")
177
+            dataB2 = format(data[1], "08b")
178
+            print("power register: %s %s" % (dataB1, dataB2))
179
+
154 180
         # Convert data to milliWatts. 
155 181
         mW = (data[0] << 8 | data[1]) * 10.0  # LSB is 10.0 mW
156 182
         return mW
... ...
@@ -159,15 +185,10 @@ class ina260:
159 185
 
160 186
 def test():
161 187
     # Initialize the smbus and INA260 sensor.
162
-    pwr1 = ina260(0x40, 1)
163
-    # Read the INA260 configuration register and manufacturer's ID.
164
-    data = pwr1.status()
165
-    print("manufacturer ID: %s %s\nconfiguration register: %s %s\n" % data)
188
+    pwr1 = ina260(0x40, 1, debug=True)
166 189
     # Print out sensor values.
167 190
     while True:
168
-        print("current register: %s %s" % pwr1.getCurrentReg())
169 191
         print("%6.2f mA" % pwr1.getCurrent())
170
-        print("volt register: %s %s" % pwr1.getVoltageReg())
171 192
         print("%6.2f V" % pwr1.getVoltage())
172 193
         print("%6.2f mW\n" % pwr1.getPower())
173 194
         time.sleep(2)
Browse code

added class methods for configuring sensor; added exception catching for json formatting code

Gandolf authored on 06/20/2021 00:40:59
Showing 1 changed files
... ...
@@ -38,32 +38,40 @@ CUR_REG = 0x1
38 38
 VOLT_REG = 0x2
39 39
 PWR_REG = 0x3
40 40
 
41
+# Define default sm bus address.
42
+DEFAULT_BUS_ADDRESS = 0x40
43
+DEFAULT_BUS_NUMBER = 1
44
+
45
+# Define the default sensor configuration.  See the INA260 data sheet
46
+# for meaning of each bit.  The following bytes are written to the
47
+# configuration register
48
+#     byte 1: 11100000
49
+#     byte 2: 00100111
50
+DEFAULT_CONFIG = 0xE027
51
+
41 52
 class ina260:
42 53
     # Initialize the INA260 sensor at the supplied address (default
43 54
     # address is 0x40), and supplied bus (default is 1).  Creates
44 55
     # a new SMBus object for each instance of this class.  Writes
45 56
     # configuration data (two bytes) to the INA260 configuration
46 57
     # register.
47
-    def __init__(self, sAddr=0x40, sbus=1):
48
-        # Instantiate a smbus object
58
+    def __init__(self, sAddr=DEFAULT_BUS_ADDRESS,
59
+                       sbus=DEFAULT_BUS_NUMBER,
60
+                       config=DEFAULT_CONFIG):
61
+        # Instantiate a smbus object.
49 62
         self.sensorAddr = sAddr
50 63
         self.bus = smbus.SMBus(sbus)
51
-        # Initialize INA260 sensor.  See the data sheet for meaning of
52
-        # each bit.  The following bytes are written to the configuration
53
-        # register
54
-        #     byte 1: 11100000
55
-        #     byte 2: 00100111
56
-        initData = [0b11100000, 0b00100111]
57
-        #initData = [0x60, 0x27]
64
+        # Initialize INA260 sensor.  
65
+        initData = [(config >> 8), (config & 0x00FF)]
58 66
         self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
59 67
     ## end def
60 68
 
61 69
     def status(self):
62
-        # Read configuration data
70
+        # Read manufacture identification data.
63 71
         mfcid = self.bus.read_i2c_block_data(self.sensorAddr, ID_REG, 2)
64 72
         mfcidB1 = format(mfcid[0], "08b")
65 73
         mfcidB2 = format(mfcid[1], "08b")
66
-        # Read configuration data
74
+        # Read configuration data.
67 75
         config = self.bus.read_i2c_block_data(self.sensorAddr, CONFIG_REG, 2)
68 76
         configB1 = format(config[0], "08b")
69 77
         configB2 = format(config[1], "08b")
... ...
@@ -97,9 +105,9 @@ class ina260:
97 105
         # Convert from two's complement to integer.
98 106
         # If d15 is 1, the the number is a negative two's complement
99 107
         # number.  The absolute value is 2^16 - 1 minus the value
100
-        # of d14-d0 taken as a positive number.
108
+        # of d15-d0 taken as a positive number.
101 109
         if bdata > 0x7FFF:
102
-            bdata = -(0xFFFF - bdata) # 0xFFFF is 2^16 - 1
110
+            bdata = -(0xFFFF - bdata) # 0xFFFF equals 2^16 - 1
103 111
         # Convert integer data to mAmps.
104 112
         mAmps = bdata * 1.25  # LSB is 1.25 mA
105 113
         return mAmps
... ...
@@ -154,14 +162,14 @@ def test():
154 162
     pwr1 = ina260(0x40, 1)
155 163
     # Read the INA260 configuration register and manufacturer's ID.
156 164
     data = pwr1.status()
157
-    print "manufacturer ID: %s %s\nconfiguration register: %s %s\n" % data
165
+    print("manufacturer ID: %s %s\nconfiguration register: %s %s\n" % data)
158 166
     # Print out sensor values.
159 167
     while True:
160
-        print "current register: %s %s" % pwr1.getCurrentReg()
161
-        print "%6.2f mA" % pwr1.getCurrent()
162
-        print "volt register: %s %s" % pwr1.getVoltageReg()
163
-        print "%6.2f V" % pwr1.getVoltage()
164
-        print "%6.2f mW\n" % pwr1.getPower()
168
+        print("current register: %s %s" % pwr1.getCurrentReg())
169
+        print("%6.2f mA" % pwr1.getCurrent())
170
+        print("volt register: %s %s" % pwr1.getVoltageReg())
171
+        print("%6.2f V" % pwr1.getVoltage())
172
+        print("%6.2f mW\n" % pwr1.getPower())
165 173
         time.sleep(2)
166 174
 ## end def
167 175
 
Browse code

modify charts

Gandolf authored on 06/07/2021 20:50:02
Showing 1 changed files
1 1
old mode 100644
2 2
new mode 100755
... ...
@@ -51,9 +51,10 @@ class ina260:
51 51
         # Initialize INA260 sensor.  See the data sheet for meaning of
52 52
         # each bit.  The following bytes are written to the configuration
53 53
         # register
54
-        #     byte 1: 01100000
54
+        #     byte 1: 11100000
55 55
         #     byte 2: 00100111
56
-        initData = [0x60, 0x27]
56
+        initData = [0b11100000, 0b00100111]
57
+        #initData = [0x60, 0x27]
57 58
         self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
58 59
     ## end def
59 60
 
... ...
@@ -69,6 +70,15 @@ class ina260:
69 70
         return (mfcidB1, mfcidB2, configB1, configB2)
70 71
     ## end def
71 72
 
73
+    def getCurrentReg(self):
74
+        # Read current register and return raw binary data for test and
75
+        # debug.
76
+        data = self.bus.read_i2c_block_data(self.sensorAddr, CUR_REG, 2)
77
+        dataB1 = format(data[0], "08b")
78
+        dataB2 = format(data[1], "08b")
79
+        return (dataB1, dataB2)
80
+    ## end def
81
+
72 82
     def getCurrent(self):
73 83
         # Get the current data from the sensor.
74 84
         # INA260 returns the data in two bytes formatted as follows
... ...
@@ -86,15 +96,24 @@ class ina260:
86 96
         bdata = data[0] << 8 | data[1]
87 97
         # Convert from two's complement to integer.
88 98
         # If d15 is 1, the the number is a negative two's complement
89
-        # number.  The absolute value is 2^15 - 1 minus the value
99
+        # number.  The absolute value is 2^16 - 1 minus the value
90 100
         # of d14-d0 taken as a positive number.
91 101
         if bdata > 0x7FFF:
92
-            bdata = -(0xFFFF - bdata) # 0xFFFF is 2^15 - 1
102
+            bdata = -(0xFFFF - bdata) # 0xFFFF is 2^16 - 1
93 103
         # Convert integer data to mAmps.
94 104
         mAmps = bdata * 1.25  # LSB is 1.25 mA
95 105
         return mAmps
96 106
     ## end def
97 107
 
108
+    def getVoltageReg(self):
109
+        # Read voltage register and return raw binary data for test
110
+        # and debug.
111
+        data = self.bus.read_i2c_block_data(self.sensorAddr, VOLT_REG, 2)
112
+        dataB1 = format(data[0], "08b")
113
+        dataB2 = format(data[1], "08b")
114
+        return (dataB1, dataB2)
115
+    ## end def
116
+
98 117
     def getVoltage(self):
99 118
         # Get the voltage data from the sensor.
100 119
         # INA260 returns the data in two bytes formatted as follows
... ...
@@ -138,7 +157,9 @@ def test():
138 157
     print "manufacturer ID: %s %s\nconfiguration register: %s %s\n" % data
139 158
     # Print out sensor values.
140 159
     while True:
160
+        print "current register: %s %s" % pwr1.getCurrentReg()
141 161
         print "%6.2f mA" % pwr1.getCurrent()
162
+        print "volt register: %s %s" % pwr1.getVoltageReg()
142 163
         print "%6.2f V" % pwr1.getVoltage()
143 164
         print "%6.2f mW\n" % pwr1.getPower()
144 165
         time.sleep(2)
Browse code

Initial release

Gandolf authored on 05/29/2021 20:13:39
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,149 @@
1
+#!/usr/bin/python
2
+#
3
+# Module: ina260.py
4
+#
5
+# Description: This module acts as an interface between the INA260 sensor
6
+# and downstream applications that use the data.  Class methods get
7
+# current, voltage, and power data from the INA260 sensor.  It acts as a
8
+# library module that can be imported into and called from other Python
9
+# programs.
10
+#
11
+# Copyright 2021 Jeff Owrey
12
+#    This program is free software: you can redistribute it and/or modify
13
+#    it under the terms of the GNU General Public License as published by
14
+#    the Free Software Foundation, either version 3 of the License, or
15
+#    (at your option) any later version.
16
+#
17
+#    This program is distributed in the hope that it will be useful,
18
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+#    GNU General Public License for more details.
21
+#
22
+#    You should have received a copy of the GNU General Public License
23
+#    along with this program.  If not, see http://www.gnu.org/license.
24
+#
25
+# Revision History
26
+#   * v10 released 01 June 2021 by J L Owrey; first release
27
+#
28
+#2345678901234567890123456789012345678901234567890123456789012345678901234567890
29
+
30
+# Import the I2C interface library
31
+import smbus
32
+import time
33
+
34
+# Define Device Registers
35
+CONFIG_REG = 0x0
36
+ID_REG = 0xFE
37
+CUR_REG = 0x1
38
+VOLT_REG = 0x2
39
+PWR_REG = 0x3
40
+
41
+class ina260:
42
+    # Initialize the INA260 sensor at the supplied address (default
43
+    # address is 0x40), and supplied bus (default is 1).  Creates
44
+    # a new SMBus object for each instance of this class.  Writes
45
+    # configuration data (two bytes) to the INA260 configuration
46
+    # register.
47
+    def __init__(self, sAddr=0x40, sbus=1):
48
+        # Instantiate a smbus object
49
+        self.sensorAddr = sAddr
50
+        self.bus = smbus.SMBus(sbus)
51
+        # Initialize INA260 sensor.  See the data sheet for meaning of
52
+        # each bit.  The following bytes are written to the configuration
53
+        # register
54
+        #     byte 1: 01100000
55
+        #     byte 2: 00100111
56
+        initData = [0x60, 0x27]
57
+        self.bus.write_i2c_block_data(self.sensorAddr, CONFIG_REG, initData)
58
+    ## end def
59
+
60
+    def status(self):
61
+        # Read configuration data
62
+        mfcid = self.bus.read_i2c_block_data(self.sensorAddr, ID_REG, 2)
63
+        mfcidB1 = format(mfcid[0], "08b")
64
+        mfcidB2 = format(mfcid[1], "08b")
65
+        # Read configuration data
66
+        config = self.bus.read_i2c_block_data(self.sensorAddr, CONFIG_REG, 2)
67
+        configB1 = format(config[0], "08b")
68
+        configB2 = format(config[1], "08b")
69
+        return (mfcidB1, mfcidB2, configB1, configB2)
70
+    ## end def
71
+
72
+    def getCurrent(self):
73
+        # Get the current data from the sensor.
74
+        # INA260 returns the data in two bytes formatted as follows
75
+        #        -------------------------------------------------
76
+        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
77
+        #        -------------------------------------------------
78
+        # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
79
+        #        -------------------------------------------------
80
+        # byte 2 | d7  | d6  | d5  | d4  | d3  |  d2 | d1  | d0  |
81
+        #        -------------------------------------------------
82
+        # The current is returned in d15-d0, a two's complement,
83
+        # 16 bit number.  This means that d15 is the sign bit.        
84
+        data=self.bus.read_i2c_block_data(self.sensorAddr, CUR_REG, 2)
85
+        # Format into a 16 bit word.
86
+        bdata = data[0] << 8 | data[1]
87
+        # Convert from two's complement to integer.
88
+        # If d15 is 1, the the number is a negative two's complement
89
+        # number.  The absolute value is 2^15 - 1 minus the value
90
+        # of d14-d0 taken as a positive number.
91
+        if bdata > 0x7FFF:
92
+            bdata = -(0xFFFF - bdata) # 0xFFFF is 2^15 - 1
93
+        # Convert integer data to mAmps.
94
+        mAmps = bdata * 1.25  # LSB is 1.25 mA
95
+        return mAmps
96
+    ## end def
97
+
98
+    def getVoltage(self):
99
+        # Get the voltage data from the sensor.
100
+        # INA260 returns the data in two bytes formatted as follows
101
+        #        -------------------------------------------------
102
+        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
103
+        #        -------------------------------------------------
104
+        # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
105
+        #        -------------------------------------------------
106
+        # byte 2 | d7  | d6  | d5  | d4  | d3  |  d2 | d1  | d0  |
107
+        #        -------------------------------------------------
108
+        # The voltage is returned in d15-d0 as an unsigned integer.
109
+        data=self.bus.read_i2c_block_data(self.sensorAddr, VOLT_REG, 2)
110
+        # Convert data to volts.
111
+        volts = (data[0] << 8 | data[1]) * 0.00125 # LSB is 1.25 mV
112
+        return volts
113
+    ## end def
114
+
115
+    def getPower(self):
116
+        # Get the wattage data from the sensor.
117
+        # INA260 returns the data in two bytes formatted as follows
118
+        #        -------------------------------------------------
119
+        #    bit | b7  | b6  | b5  | b4  | b3  | b2  | b1  | b0  |
120
+        #        -------------------------------------------------
121
+        # byte 1 | d15 | d14 | d13 | d12 | d11 | d10 | d9  | d8  |
122
+        #        -------------------------------------------------
123
+        # byte 2 | d7  | d6  | d5  | d4  | d3  |  d2 | d1  | d0  |
124
+        #        -------------------------------------------------
125
+        # The wattage is returned in d15-d0 as an unsigned integer.
126
+        data=self.bus.read_i2c_block_data(self.sensorAddr, PWR_REG, 2)
127
+        # Convert data to milliWatts. 
128
+        mW = (data[0] << 8 | data[1]) * 10.0  # LSB is 10.0 mW
129
+        return mW
130
+   ## end def
131
+## end class
132
+
133
+def test():
134
+    # Initialize the smbus and INA260 sensor.
135
+    pwr1 = ina260(0x40, 1)
136
+    # Read the INA260 configuration register and manufacturer's ID.
137
+    data = pwr1.status()
138
+    print "manufacturer ID: %s %s\nconfiguration register: %s %s\n" % data
139
+    # Print out sensor values.
140
+    while True:
141
+        print "%6.2f mA" % pwr1.getCurrent()
142
+        print "%6.2f V" % pwr1.getVoltage()
143
+        print "%6.2f mW\n" % pwr1.getPower()
144
+        time.sleep(2)
145
+## end def
146
+
147
+if __name__ == '__main__':
148
+    test()
149
+