Pseudocode and BMS

Elmo

Member
Joined
Feb 19, 2017
Messages
189
No, it's not something out of Fifty Shades of Ghastly, a long time ago when I learned to code I was taught to write pseudocode first. I'll refer to Wiki..

https://en.wikipedia.org/wiki/Pseudocode


Pseudocode[1] is an informal high-level description of the operating principle of a computer program or other algorithm.
It uses the structural conventions of a normal programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are essential for machine understanding of the algorithm, such as variable declarations, system-specific code and some subroutines. The programming language is augmented with natural language description details, where convenient, or with compact mathematical notation. The purpose of using pseudocode is that it is easier for people to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications that are documenting various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place.
No standard for pseudocode syntax exists, as a program in pseudocode is not an executable program. Pseudocode resembles, but should not be confused with skeleton programs which can be compiled without errors. Flowcharts, drakon-charts and Unified Modeling Language (UML) charts can be thought of as a graphical alternative to pseudocode, but are more spacious on paper. Languages such as HAGGIS bridge the gap between pseudocode and code written on programming languages. Its main use is to introduce students to high level languages through use of this hybrid language.

I'm interested in what the or perhaps a pseudocode for a BMS would look like, it's been my experience that once once you have the pseudocode for an operation nailed down nicely writing the actual code usually is fairly straightforward. If you know where you are going when you start it's a lot easier to get there.

I've been doing some experiments with flying capacitor balancing and have come to realize that, logically enough (insert smack head smiley here) the charge and discharge times can be different depending on the internal resistance of the cells or parallel packs you are trying to discharge or charge. I think that with a proper capacitor a nice balancing system can be set up that is almost 100% energy efficient. My next step is to eliminate as much stray resistance from the circuit as I can, I may parallel the relays to lessen resistance, we shall see.

My pseudocode extremely rough outline for balancing with a flying capacitor is.. Every so often turn off all the balancing and after a pause for things to settle check the actual voltages across the cells, take the highest cell, siphon energy from it into a capacitor until the voltage curve starts to peak, turn off both legs of that cell, then turn on both legs of the lowest cell and discharge the capacitor into it until the voltage curve starts to bottom out, then it's turn off the lowest cell and back to the charge cell again, turn on both legs until capacitor voltage nearly peaks then turn them off and turn on both legs of the low cell until near bottom. Do this until you have a new low or high cell in the periodic scan and then do it to those cells and so on..

You can and indeed have to expand and add all kinds of other things, actually reading the cell voltages, sophisticated noise filtering, making absolutely positively darn sure that the wrong relays don't get turned on at the wrong time, talking to the overall charge controller overlord part of the program which of course needs its own pseudocode that would necessarily interface the PC of the balancing function as they are known in the Arduino world. Read the system timer and call subs you need for measurement and actions as the situation requires. BMS is actually a pretty easy microcontroller chore compared to say MPPT which turns into quite the juggling act quickly. At least you don't have to write your own ADC code in assembler, taking care to add nops so that all decision trees come out the same number of cycles so the speed is always the same. It was a day of wooden computers and iron men.

What would you like to see in a pseudocode outline for a BMS?
 
A capacitive balancer, aka a charge pump, note that these are normally only used at very low currents (less than 50mA on average when time constants are added in)

However as it is a BMS, what occurs in such an arrangement when all cells are equal, but at an over voltage or under voltage condition? (A limitation of some active balancers with no other features)

As for how to match your charge and discharge thresholds, a comparator should be all you need, when capacitor voltage >= 99% of battery voltage, it signals to your controller that it is ready to switch, this will equally give you an almost direct way to measure cell ESR for back end monitoring, by measuring the time between starting and the comparator switching, (subject to the tolerance of your components)

As your dealing with relays that will generally burn through some power, I would have a strong hysteresis on your system, "If cell is greater than Over voltage limit, balance immediately, otherwise balance only while charging and cell is greater than 0.02V to another cell. OR, If at undervoltage limit, and other cells greater than 0.1 above, charge from them",
 
Good points, especially about the hysteresis, basically all it will be is a software comparator, hysteresis is but another line of code, asymmetrical hysteresis may be desirable.

I hadn't considered conserving relay power, that's excellent. A reasonably wide hysteresis will help with power conservation. Relays would of course be configured to be normally in the off state.

As for what to do in an all over or all under situation that is up to the BMS overlord function that's calling the shots, balancing is but a peon, a humble subfunction if you will.
 
Just a thought, but wouldn't it be better to use a couple mosfets instead of a relay? They can handle quite a bit of amperage, especially if paired/line. And, they wouldn't use no where near as much power to run.
 
If I felt confident I could design such a MOSFET circuit without letting out a great deal of magic smoke in the process that's the terminology I would have used. I've followed enough discussions about MOSFETS and switching controllers to know that stuff is rather beyond my pay grade.

Elmo's comfort zone is a bit closer to stone axe technology, Elmo loves relays.

But that's a detail which isn't really relevant to a discussion about pseudocode. If I was going to resort to MOSFET I'm more comfortable with an inductor based flyback type of booster. There is a really nice MOSFET switch that will do 30A at 60V driven straight off a 3.3V logic line.

Getting the pseudocode right is the key to good programming, I kind of do it in my head when I write spaghetti code making one function work at a time but it's not the same as sitting down and hammering out the logical basis for your program before you ever write a line of Perl or Java or Forth or Logo or C- - or whatever.
 
Yeah, I agree.

I never was a pseudo kinda programmer. I thought of all the logic and steps involved in my head for a few days (usually when I'm about to go to sleep, since that's when my brain isn't distracted), and when it's time to write, I can usually just plop it all down with little rewrites.

Or, at least, that's how I used to do it. I've been away from coding for quite some time and it's all a bit rusty now :p

I enjoy reading others info, tho. It helps me get back on track with stuff. Thanks for the thread and comments that are passing through. :)
 
I was looking at these mosfets for full pack shutdown: https://www.digikey.ca/product-detail/en/stmicroelectronics/STW120NF10/497-5166-5-ND/1117519
process in building BMS/monitoring will provide temp, voltage of each 1s pack. Will shutdown pack if temps get too high, or voltage to high/low. I will have remote web shutdown of discharge/charge circuit as well.

After I can get a basic system up and running, I'll probably look at combining bq76940 with ESP32for better accuracy of battery voltage, but probably still plan on keep the separate discharge circuit since mine will do .75-1A discharge. http://www.ti.com/product/bq76940.
 
Back
Top