Replacing PID with an adaptive lookup table for PWM duty to current conversion - Page 1
Forum section: electronics / projects

author: Psi
author_rank: Super Contributor
timestamp: September 10, 2025, 05:47:56 am
content: I have 100hz 8bit PWM into a solenoid through a high-side driver which outputs analog current sense to MCU. The reading is converted to 8 bit value for RMS current for each duty cycle. So like 80/255 = 8.0 Amps.What I want is software based current control to set the duty cycle so i can set amps rather than duty.One way to do that would be through PID, which I have tried and am still playing with but it's either too slow or too jittery despite lots of fine tuning. Maybe there's a better suited PID for this kind of thing that I'm not aware of?The current data is a bit jittery due to software RMS calc's and stuff. It jumps around +/- 0.1A which the PID doesnt like.Anyway, I'm looking for alternatives to the PID approach. but open to better PID ideas.I can't simply calculate the curve for the duty that will cause X amps into an inductor of Y mH / Z ohms because the solenoid type isn't known exactly.So the main point of this thread is coming up with an idea to create and maintain some sort of lookup table out of past data points of duty vs amps and use that to determine what duty to use to get X amps in real-time.The current to duty relationship changes but only slowly just due to solenoid heating. So there should be enough time to build and maintain a table.I'm interested to hear ideas on how to code this sort of thing.It needs to priorities new samples over old samples (or replace them) There's no guarantee any particular duty % will get its own data point sample even if duty passes through that point. MCU might be busyPerhaps one way is to divide up the duty range into 10 exponential areas (since that's the curve), and try to get one data point within each area as they become available.Then have some sort of algorithm that turns this into a curve that can be interpolated to get duty for any current.Anyone have any ideas?I have 3KB of free RAM and 42KB of free FLASH, but CPU time is constrained a little due to only having a 8bit MCU that is busy doing other stuff. I do have the RAM to buffer an amps value for every single duty percentage but that seems excessive.

author: moffy
author_rank: Super Contributor
timestamp: September 10, 2025, 08:03:09 am
content: What you could use is an adaptive algorithm where one of the things you calculate are the parameters for the inductor R and L and based on that you can control the current via PWM. The R and L are constantly updated in a slow loop while the PWM current control is a fast loop. Maybe a type of Kalman filter, not sure.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: September 10, 2025, 08:23:17 am
content: Quote from: Psi on September 10, 2025, 05:47:56 amAre you trying to control the position of the solenoid?That's tough, as the solenoid acts in a regenerative manner and the moving armature has generator properties as a bonus

author: MariuszD
author_rank: Regular Contributor
timestamp: September 10, 2025, 08:59:31 am
content: Can you use a stepper motor driver or LED driver? These circuits regulate the current. Is chopper switching acceptable?Why are you interested in the RMS value?

author: Psi
author_rank: Super Contributor
timestamp: September 10, 2025, 11:55:23 am
content: Quote from: MariuszD on September 10, 2025, 08:59:31 amHas to be done in software as the hardware already exists, this is for a new feature to an existing product.Currently it has PWM duty control and I want to add current control as a user option.Quote from: MariuszD on September 10, 2025, 08:59:31 amNo reason other than that's what a DMM would show if you measured coil current. Do you think i should use avg or peak instead?Quote from: PCB.Wiz on September 10, 2025, 08:23:17 amno, just the force it applies, it really doesnt move much. Controlling the current should be fine for this in my application. Better than the pwm duty control it does now anyway.Quote from: moffy on September 10, 2025, 08:03:09 amWorth at try, thanks

author: IconicPCB
author_rank: Super Contributor
timestamp: September 10, 2025, 12:30:52 pm
content: Try "take back half " algorithm instead of PID... no integral wind up

author: langwadt
author_rank: Super Contributor
timestamp: September 10, 2025, 12:48:22 pm
content: why not regular chopper drive?