ZHCABD1 October   2021 BQ21061 , BQ21062 , BQ24179 , BQ25150 , BQ25155 , BQ25157 , BQ25672 , BQ25790 , BQ25792 , BQ25798 , BQ25882 , BQ25883 , BQ25887 , BQ25895 , BQ25896 , BQ25898 , BQ25898D

 

  1.   使用智能电池充电器估计充电状态
  2.   商标
  3. 1引言
  4. 2电池特征
  5. 3生成查找表
  6. 4BQ25155 寄存器配置
  7. 5最佳用例
  8. 6Python 查找表发生器
  9. 7MSP430 代码片段

Python 查找表发生器


#Command Line Arguments: [TI format gauge output csv file], [Polynomial order for regression]
#Output: Plots VBAT vs SOC reported by TI gauge and creates a polynomial regression of the specified order.
#        This regression is plotted on the same graph as the data and is mapped to a 101-pt hexadecimal lookup table given in the "lookup_table.txt" file.import matplotlib.pyplot as plt
import numpy as np
import csv
import sys
vbat_arr = []
num_reads = 0
read_arr = []
soc_arr = []

poly4x = []
poly4y = []
poly3y = []
poly3x = []

bin_vals = []

#reads in the TI Gauge csv file and puts the data into the corresponding list.
#Adjust this section if not using TI gauge
with open (sys.argv[1]) as csv_file1: 
    csv_reader = csv.reader(csv_file1, dialect='excel',delimiter=',')
    line_count = 0
    for row in csv_reader: 
        if (line_count > 8): #To get rid of the labels and other unnecessary stuff
            vbat_arr.append(float(row[6]))
            soc_arr.append(int(row[16]))
            num_reads += 1
        line_count += 1
        

#create a polynomial regression of the order specified in cmd line
polyfunc = np.polyfit(soc_arr, vbat_arr, int(sys.argv[len(sys.argv)-1]))
poly4 = np.poly1d(polyfunc)
#This for loop creates an x and y list from the regression such that it can be plotted later.
#It also calculates the hex values for the battery voltages needed to create the lookup table.
for i in range (0, 101): 
    poly4y.append(poly4(i))
    poly4x.append(i)
    vbat_16 = int(round(((poly4(i)/1000)*(2**16))/6)) #Vbat formula found in datasheet
    bin_vals.append(hex(vbat_16)) 

#This for loop outputs the lookup table to the file called "lookup_table.txt"
with open ("lookup_table.txt","w+") as outfile: 
    for i in range(0, 101): 
        outfile.write(str(bin_vals[i])[0] + str(bin_vals[i])[1] + str(bin_vals[i])[2].upper() + str(bin_vals[i])[3].upper() + str(bin_vals[i])[4].upper() + str(bin_vals[i])[5].upper() + ",\n") #Ensures that hex letters are all uppercase
outfile.close()

#The rest of this is for plotting the data collected and the calculated regression
plt.plot(soc_arr, vbat_arr, 'r', label='Battery Data')
plt.yticks([3000, 3200, 3400, 3600, 3800, 4000, 4200, 4400])
plt.plot(poly4x, poly4y,'b',label='Regression')

plt.xlabel('SOC (%)')
plt.ylabel('Battery Voltage (mV)')
plt.gca().invert_xaxis() #Reverses x axis so that 100% is shown as the leftmost value
plt.title('Battery Voltage vs SOC') 
plt.legend()
plt.grid(b=True, which='major', axis='both')

plt.show()