4393 |
jacho |
1 |
#!/usr/bin/python |
|
|
2 |
|
|
|
3 |
# Python example of use pymlab with LIONCELL MLAB module |
|
|
4 |
|
|
|
5 |
import time |
|
|
6 |
import sys |
|
|
7 |
from pymlab import config |
|
|
8 |
|
|
|
9 |
import pandas as pd # data parsing library |
|
|
10 |
import struct |
|
|
11 |
|
|
|
12 |
filename = './guage.csv' |
|
|
13 |
|
|
|
14 |
df = pd.read_csv(filename,delimiter=',') |
|
|
15 |
|
|
|
16 |
def ReadFlash(ID, offset, name, typ, units, default): |
|
|
17 |
flash = guage.ReadFlashBlock(ID, offset // 32) |
|
|
18 |
print ID, '\t', offset, '\t', '"', '{:32s}'.format(name), '"', '\t', typ, '\t', |
|
|
19 |
# if (offset // 32) > 0 : |
|
|
20 |
offset = offset - 32 * (offset // 32) |
|
|
21 |
if typ == 'U1': |
|
|
22 |
print '{:10d}'.format(flash[offset]), |
|
|
23 |
if typ == 'I1': |
|
|
24 |
if flash[offset] > 127: |
|
|
25 |
ble = (256-flash[offset]) * (-1) |
|
|
26 |
else: |
|
|
27 |
ble = flash[offset] |
|
|
28 |
print '{:10d}'.format(ble), |
|
|
29 |
if typ == 'I2': |
|
|
30 |
ble = (flash[offset]<<8)+flash[offset+1] |
|
|
31 |
if ble > 32767: |
|
|
32 |
ble = (65536-ble) * (-1) |
|
|
33 |
print '{:10d}'.format(ble), |
|
|
34 |
if typ == 'U2': |
|
|
35 |
print '{:10d}'.format(abs((flash[offset]<<8)+flash[offset+1])), |
|
|
36 |
if typ == 'F4': |
|
|
37 |
print '{:10s}'.format(hex(0x1000000*flash[offset]+0x10000*flash[(offset+1)]+0x100*flash[(offset+2)]+flash[(offset+3)])), |
|
|
38 |
#print '{:10f}'.format(struct.unpack('@f', chr(flash[offset])+chr(flash[offset+1])+chr(flash[offset+2])+chr(flash[offset+3]))[0]), |
|
|
39 |
if typ == 'H1': |
|
|
40 |
print '{:10s}'.format(hex(flash[(offset)])), |
|
|
41 |
if typ == 'H2': |
|
|
42 |
print '{:10s}'.format(hex(0x100*flash[(offset)]+flash[(offset+1)])), |
|
|
43 |
if typ == 'H4': |
|
|
44 |
print '{:10s}'.format(hex(0x1000000*flash[offset]+0x10000*flash[(offset+1)]+0x100*flash[(offset+2)]+flash[(offset+3)])), |
|
|
45 |
if typ == 'S9': |
|
|
46 |
string = '' |
|
|
47 |
for n in range(9): |
|
|
48 |
string = string + chr(flash[offset+n+1]) |
|
|
49 |
print '{:12s}'.format(string+'\0'), |
|
|
50 |
if typ == 'S12': |
|
|
51 |
string = '' |
|
|
52 |
for n in range(9): |
|
|
53 |
string = string + chr(flash[offset+n+1]) |
|
|
54 |
print '{:12s}'.format(string+'...\0'), |
|
|
55 |
if typ == 'S5': |
|
|
56 |
string = '' |
|
|
57 |
for n in range(5): |
|
|
58 |
string = string + chr(flash[offset+n+1]) |
|
|
59 |
print '{:12s}'.format(string+'\0'), |
|
|
60 |
print '{:10s}'.format('['+units+']'), '\t', default, '{:10s}'.format('['+units+']') |
|
|
61 |
|
|
|
62 |
|
|
|
63 |
#### Sensor Configuration ########################################### |
|
|
64 |
cfg = config.Config( |
|
|
65 |
i2c = { |
|
|
66 |
"port": 0, # I2C bus number |
|
|
67 |
}, |
|
|
68 |
|
|
|
69 |
bus = [ |
|
|
70 |
{ |
|
|
71 |
"type": "i2chub", |
|
|
72 |
"address": 0x73, |
|
|
73 |
|
|
|
74 |
"children": [ |
|
|
75 |
{"name": "guage", "type": "lioncell", "channel": 7, }, |
|
|
76 |
], |
|
|
77 |
}, |
|
|
78 |
], |
|
|
79 |
) |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
cfg.initialize() |
|
|
83 |
guage = cfg.get_device("guage") |
|
|
84 |
|
|
|
85 |
print "Pack Configuration", hex(guage.PackConfiguration()) |
|
|
86 |
|
|
|
87 |
#flash = guage.ReadFlashBlock(112, 0) |
|
|
88 |
#print "112 - ", |
|
|
89 |
#print " ".join([hex(i) for i in flash]) |
|
|
90 |
|
|
|
91 |
#ReadFlash(2, 0, 'OT Chg', 'I2', '0.1 degC') |
|
|
92 |
#ReadFlash(112, 8, 'Authen Key3', 'H4', '-') |
|
|
93 |
|
|
|
94 |
for i in range(len(df)): |
|
|
95 |
ReadFlash(df['SubclassID'][i], df['Offset'][i], df['Name'][i], df['DataType'][i], df['Units'][i], df['DefaultValue'][i]) |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
while True: |
|
|
99 |
# Battery status readout |
|
|
100 |
print "NominalAvailableCapacity =", guage.NominalAvailableCapacity(), "mAh, FullAvailabeCapacity =", guage.FullAvailabeCapacity(), "mAh, AvailableEnergy =", guage.AvailableEnergy(), "* 10 mWh" |
|
|
101 |
print "Temp =", guage.getTemp(), "degC, RemainCapacity =", guage.getRemainingCapacity(), "mAh, cap =", guage.FullChargeCapacity(), "mAh, U =", guage.Voltage(), "mV, I =", guage.AverageCurrent(), "mA, charge =", guage.StateOfCharge(), "%" |
|
|
102 |
time.sleep(3) |
|
|
103 |
|
|
|
104 |
|