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