titusmc
100 W
Hi ES,
This is my first post on these forums. This July I did my first E-Bike build with a 2003 Trek 4500, an eBay 48v front hub motor kit, and recycled laptop 18650 cells. The bike has been great (commuted about 300 miles on it so far).
I figured I would share with the community a little Python code I wrote to group cells into (roughly) capacity balanced groups. This type of problem is similar to the balanced partition problem in dynamic programming, and one approximate solution is the "greedy algorithm" which I have implemented here. It basically works by sorting the cells in descending order of capacity in mAh, then iterating through the list and putting the next cell into the parallel group with the lowest total capacity.
The code provided simply generates a list of random cell capacities between 2000 and 2500 mAh for a given pack configuration (mine is 13S7P). Replace the list with actual measured capacities, and this should help you create a well-balanced pack.
My goal here is not to teach Python, so please take it or leave it.
This is my first post on these forums. This July I did my first E-Bike build with a 2003 Trek 4500, an eBay 48v front hub motor kit, and recycled laptop 18650 cells. The bike has been great (commuted about 300 miles on it so far).
I figured I would share with the community a little Python code I wrote to group cells into (roughly) capacity balanced groups. This type of problem is similar to the balanced partition problem in dynamic programming, and one approximate solution is the "greedy algorithm" which I have implemented here. It basically works by sorting the cells in descending order of capacity in mAh, then iterating through the list and putting the next cell into the parallel group with the lowest total capacity.
The code provided simply generates a list of random cell capacities between 2000 and 2500 mAh for a given pack configuration (mine is 13S7P). Replace the list with actual measured capacities, and this should help you create a well-balanced pack.
My goal here is not to teach Python, so please take it or leave it.
Code:
# Python 18650 Pack Balancer
import random
cells = []
s = 13
p = 7
ncells = s*p
# generate random cell capacities between 2000 and 2500 mAh
for i in range(0,ncells):
cells.append(random.randint(2000,2500))
# sort these cell capacities in descending order
cells.sort(reverse = True)
# make cell list iterable
cells = iter(cells)
pack = [] # create pack as an empty list of lists with length s
for i in range(0,s):
pack.append([])
for i in range(0,ncells-s+1):
if (i == 0): # first time through, don't need to calculate sums - they are all zero
for group in pack:
group.append(cells.next())
else:
# calculate group sums
groupsums = []
for group in pack:
groupsums.append(sum(group))
# find the index of the lowest group sum
lowestsumindex = groupsums.index(min(groupsums))
# add the next cell to the group with the lowest sum
pack[lowestsumindex].append(cells.next())
print pack
groupsums = []
for group in pack:
groupsums.append(sum(group))
print groupsums