0

I've been studying and trying to implement a PI feedback controller for a BLDC.

Right now, just to unit test the code for the PI controller, I'm driving two phases with a PWM signal on a MOSFET inverter and trying to get a prescribed current out by watching the voltage that develops on a sense resistor.

Here is the what I have the architecture:

1) A PWM has a register value and starts outputting pulses

2) The windings ramp up with current flow

3) A voltage is developed across a 0.015R sense resistor

4) That voltage is gained up by 5x through an OpAmp

5) That voltage is fed to and ADC that samples at the exact center of each pulse.

6) The ADC readings are collected and averaged as fast as the pulses occur for a period of 20ms

7) The PID controller that I've included then takes action and that's where I'm stuck.

enter image description here

Ultimately, what I really want, is a value to stick in the PWM on-time register...I want a 16bit value. But, what I'm getting in are 12 bit ADC values.

So, I said to myself, "no problem...I'm not getting current readings in, but I know what those values really are, so I'll just convert them to current!"

So, I did this:

$$ I_m = \dfrac{ADC}{R_{sense} * 5 * \dfrac{4096}{3.3}}$$

So, that's OK. Then I go through the PI control, where we do:

$$ I_{err} = I_{set point} - I_m $$

Then, do what you do in a PI controller...Add integral gains to their past values, scale proportional gains by a scalar, and then add them up:

$$ I_{feedback} = P * I_{err} + I * \int{I_{err}}{dt} $$

This is the part where me and my buddy looked at each other and didn't know what to say...Here we're holding current feedback in our hands, measured in Amps (and a rather large value, I might add, thanks to the high gains that I've read PI controllers like to see)....But, what I need to do is put this into a PWM counter-compare register...That's a 16bit counter, not a...current register, whatever that would be!

Consider this case...I ask for 100mA and I observe 200mA...I calculate my error of -100mA, so I need to cool this thing off...I intuitively know that the answer here is to reduce the PWM duty cycle from whatever it is now, to a lesser value.

If I go through the whole algorithm, because of the negative error, that minus-sign is going to propogate all the way to my $$I_{feedback}$$....So, I leave the feedback with a negative value. What can I do with this? How should I apply that to a PWM register, which is the real control output?

testname123
  • 475
  • 1
  • 5
  • 18
  • This is where tuning the PI gains comes in. In the example you gave, if the error is negative just make the duty cycle lower by some amount and see how this responds. Depending on the performance you see and your requirements you can then tune the gains to accomplish this. – jramsay42 Jan 29 '18 at 22:27
  • If you are running without changing phases, you are testing without rotation, simulating a locked rotor or stall condition and your loop is compensating only for winding inductance. Your actual loop will have to handle the inertia of the rotor and the load, which normally has a much longer time constant. As jramsay42 says, start out with small increments. Your biggest torque load may occur during start-up if you try to accelerate up to speed quickly. – John Birckhead Jan 29 '18 at 22:37
  • @jramsay42 when you say to lower the duty cycle if the value is negative, I'm with you... But how do you know what to do to accomplish this? Does this call for another "feedback current to pulse width" transfer function? – testname123 Jan 29 '18 at 23:14
  • @testname123: See if my answer to a basic question on [PI contro](https://electronics.stackexchange.com/questions/346730/understanding-the-flow-of-a-pi-controller/346759#346759) helps your understanding in any way. It's very simple but it may give you a different way of looking at the problem. – Transistor Jan 29 '18 at 23:19
  • @testname123 You could do that, but I think just experimentation combined with some intuition about the effects of the P and I gains can get you pretty far. So say you have your -100mA of current error, start arbitrarily with a too-small P gain that may reduce the duty cycle by a few percent. This probably won't have much effect, slowly increase the gain till you start to see some overshoot and such then start playing with the I gain. – jramsay42 Jan 30 '18 at 00:25
  • **Have you considered any specs for this design?** Step Overshoot, settling time, slew rate, Control inputs, for a,v, mass, RPM/V anything? – Tony Stewart EE75 Jan 30 '18 at 02:15
  • @Transistor Thanks...The part about limiting the output (dVelocity/dTime 10kph when Velocity < 70kph)when the errors are huge was a big missing piece, I think. It's at least stable now. – testname123 Jan 30 '18 at 16:34
  • @TonyStewart.EEsince'75 This is a chicken/egg problem that I'm having. The real motor I want to control is big and expensive, so I'm doing all my testing on a smaller motor with much different characteristics. My issues is that I have no confidence that this controller is working at all, so I don't want to hook up the expensive motor to it...I want to do testing on the inexpensive stand-in motor. But I have no specs on what you're talking about with the stand-in motor, since I'm only planning on making sure the low-level PI controller is actually working in the first place. – testname123 Jan 30 '18 at 16:36
  • @jramsay42 Yes, that's what I ended up doing...I have reworked and stepped through the code slowly and incrementally enough to make sure that the values are being passed along and manipulated as I expect and as you all have pointed out. Now, I'm confident that the controller is in fact trying to control what I'm measuring, although it's doing a bad job of it. The mechanism is working, thanks to Transistors mention of limiting the bandwidth of the output as well as your suggestion about tuning. It seems the guitar is assembled and now I need to tune the strings, so to speak. – testname123 Jan 30 '18 at 16:40
  • Just beware that if you don't grasp how to tune PID parameters based on Action forces and Reaction forces ( inertia etc) Servo Phase margin or don't have a Simulation tool, It is not scaleable. Like going from an ant to an elephant changes everything. – Tony Stewart EE75 Jan 30 '18 at 16:49
  • @TonyStewart.EEsince'75 the one thing I've learned from this project is that there's much more that I have less of a grasp on than I ever thought possible. I'm not even sure how to model those things in a controller system. Maybe we can talk offline about that sometime in a different forum, since this may not be the place for it. I'd love to learn more about this, though. – testname123 Jan 30 '18 at 16:54
  • You will need Control System Theory for PID Controller tuning before trial and error. But all the answers are on the web, if you have the right question and can understand it. https://robotics.stackexchange.com/questions/167/what-are-good-strategies-for-tuning-pid-loops But I find the best overshoot , best seek time from my HDD servo experience and have controlled ramps in addition to optimal feedback. So when controling accel, use I for feedback and when using position control, use position feedback – Tony Stewart EE75 Jan 30 '18 at 17:14
  • @testname123: I'm glad you found the PI link useful. Have a look for a tutorial on PID "anti-windup". This prevents the I term growing beyond some preset limit as would happen if something was preventing the system reaching the setpoint. e.g., A room heating controller working when all the windows are open. The problem occurs when the windows are closed that the integral term has gone huge, the temperature will overshoot and the I term will take ages to come back into range. – Transistor Jan 31 '18 at 21:19

0 Answers0