CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/scripts/battery_fit.py
Views: 1798
1
#!/usr/bin/env python
2
'''
3
fit coefficients for battery percentate from resting voltage
4
5
See AP_Scripting/applets/BattEstimate.lua
6
'''
7
8
from argparse import ArgumentParser
9
parser = ArgumentParser(description=__doc__)
10
parser.add_argument("--no-graph", action='store_true', default=False, help='disable graph display')
11
parser.add_argument("--num-cells", type=int, default=0, help='cell count, zero for auto-detection')
12
parser.add_argument("--batidx", type=int, default=1, help='battery index')
13
parser.add_argument("--condition", default=None, help='match condition')
14
parser.add_argument("--final-pct", type=float, default=100.0, help='set final percentage in log')
15
parser.add_argument("--comparison", type=str, default=None, help='comparison coefficients')
16
parser.add_argument("log", metavar="LOG")
17
18
args = parser.parse_args()
19
20
import sys
21
import math
22
from pymavlink import mavutil
23
import numpy as np
24
import matplotlib.pyplot as pyplot
25
26
def constrain(value, minv, maxv):
27
"""Constrain a value to a range."""
28
return max(min(value,maxv),minv)
29
30
def SOC_model(cell_volt, c):
31
'''simple model of state of charge versus resting voltage.
32
With thanks to Roho for the form of the equation
33
https://electronics.stackexchange.com/questions/435837/calculate-battery-percentage-on-lipo-battery
34
'''
35
p0 = 80.0
36
p1 = c[2]
37
return constrain(c[0]*(1.0-1.0/math.pow(1+math.pow(cell_volt/c[1],p0),p1)),0,100)
38
39
def fit_batt(data):
40
'''fit a set of battery data to the SOC model'''
41
from scipy import optimize
42
43
def fit_error(p):
44
p = list(p)
45
ret = 0
46
for (voltR,pct) in data:
47
error = pct - SOC_model(voltR, p)
48
ret += abs(error)
49
50
ret /= len(data)
51
return ret
52
53
p = [123.0, 3.7, 0.165]
54
bounds = [(100.0, 10000.0), (3.0,3.9), (0.001, 0.4)]
55
56
(p,err,iterations,imode,smode) = optimize.fmin_slsqp(fit_error, p, bounds=bounds, iter=10000, full_output=True)
57
if imode != 0:
58
print("Fit failed: %s" % smode)
59
sys.exit(1)
60
return p
61
62
def ExtractDataLog(logfile):
63
'''find battery fit parameters from a log file'''
64
print("Processing log %s" % logfile)
65
mlog = mavutil.mavlink_connection(logfile)
66
67
Wh_total = 0.0
68
last_t = None
69
data = []
70
last_voltR = None
71
72
while True:
73
msg = mlog.recv_match(type=['BAT'], condition=args.condition)
74
if msg is None:
75
break
76
77
if msg.get_type() == 'BAT' and msg.Instance == args.batidx-1 and msg.VoltR > 1:
78
current = msg.Curr
79
voltR = msg.VoltR
80
if last_voltR is not None and voltR > last_voltR:
81
continue
82
last_voltR = voltR
83
power = current*voltR
84
t = msg.TimeUS*1.0e-6
85
86
if last_t is None:
87
last_t = t
88
continue
89
90
dt = t - last_t
91
if dt < 0.5:
92
# 2Hz data is plenty
93
continue
94
last_t = t
95
Wh_total += (power*dt)/3600.0
96
97
data.append((voltR,Wh_total))
98
99
if len(data) == 0:
100
print("No data found")
101
sys.exit(1)
102
103
# calculate total pack capacity based on final percentage
104
Wh_max = data[-1][1]/(args.final_pct*0.01)
105
106
fit_data = []
107
108
for i in range(len(data)):
109
(voltR,Wh) = data[i]
110
SOC = 100-100*Wh/Wh_max
111
fit_data.append((voltR, SOC))
112
113
print("Loaded %u samples" % len(data))
114
return fit_data
115
116
def ExtractDataCSV(logfile):
117
'''find battery fit parameters from a CSV file'''
118
print("Processing CSV %s" % logfile)
119
lines = open(logfile,'r').readlines()
120
fit_data = []
121
for line in lines:
122
line = line.strip()
123
if line.startswith("#"):
124
continue
125
v = line.split(',')
126
if len(v) != 2:
127
continue
128
if not v[0][0].isnumeric() or not v[1][0].isnumeric():
129
continue
130
fit_data.append((float(v[1]),float(v[0])))
131
return fit_data
132
133
def BattFit(fit_data, num_cells):
134
135
fit_data = [ (v/num_cells,p) for (v,p) in fit_data ]
136
137
c = fit_batt(fit_data)
138
print("Coefficients C1=%.4f C2=%.4f C3=%.4f" % (c[0], c[1], c[2]))
139
140
if args.no_graph:
141
return
142
143
fig, axs = pyplot.subplots()
144
145
np_volt = np.array([v for (v,p) in fit_data])
146
np_pct = np.array([p for (v,p) in fit_data])
147
axs.invert_xaxis()
148
axs.plot(np_volt, np_pct, label='SOC')
149
np_rem = np.zeros(0,dtype=float)
150
151
# pad down to 3.2V to make it easier to visualise for logs that don't go to a low voltage
152
low_volt = np_volt[-1]
153
while low_volt > 3.2:
154
low_volt -= 0.1
155
np_volt = np.append(np_volt, low_volt)
156
157
for i in range(np_volt.size):
158
voltR = np_volt[i]
159
np_rem = np.append(np_rem, SOC_model(voltR, c))
160
161
axs.plot(np_volt, np_rem, label='SOC Fit')
162
163
if args.comparison:
164
c2 = args.comparison.split(',')
165
c2 = [ float(x) for x in c2 ]
166
np_rem2 = np.zeros(0,dtype=float)
167
for i in range(np_volt.size):
168
voltR = np_volt[i]
169
np_rem2 = np.append(np_rem2, SOC_model(voltR, c2))
170
axs.plot(np_volt, np_rem2, label='SOC Fit2')
171
172
axs.legend(loc='upper left')
173
axs.set_title('Battery Fit')
174
175
pyplot.show()
176
177
def get_cell_count(data):
178
if args.num_cells != 0:
179
return args.num_cells
180
volts = [ v[0] for v in data ]
181
volts = sorted(volts)
182
num_cells = round(volts[-1]/4.2)
183
print("Max voltags %.1f num_cells %u" % (volts[-1], num_cells))
184
return num_cells
185
186
if args.log.upper().endswith(".CSV"):
187
fit_data = ExtractDataCSV(args.log)
188
else:
189
fit_data = ExtractDataLog(args.log)
190
191
num_cells = get_cell_count(fit_data)
192
BattFit(fit_data, num_cells)
193
194