Question on PID for speed control of DC motor in treadmill application - Page 1
Forum section: electronics / projects

author: XaviPacheco
author_rank: Regular Contributor
timestamp: November 02, 2018, 02:22:31 pm
content: In treadmill applications with DC motor, you can't go from 0.5 mph to 1 mph instantly. The speed should be gradually increasing. If I want to design a PID for speed control, my setpoint should be a ramp instead of steps, right? Or, I can give the setpoint in steps and the P gain adjust the time response of the system? Which one is the right approach? I think that once the final setpoint is reached as ramp, the PID controller should compensate the output for changes in the load. My feedback is a speed sensor that will tell the controller the actual speed. If I said my setpoint is 0.5 mph, the PID doesn't care if gave the setpoint as a ramp or step. Am I right?

author: jbb
author_rank: Super Contributor
timestamp: November 02, 2018, 06:12:54 pm
content: Generally I would try to use fixed controller gains (P, I, optional D) and ramp the reference signal.

author: XaviPacheco
author_rank: Regular Contributor
timestamp: November 02, 2018, 06:40:26 pm
content: Quote from: jbb on November 02, 2018, 06:12:54 pmThat's what I think. As far as I know, for me to achieve the speed ramping, I shoud use a ramp as setpoint. But I've been told I'm wrong, that the PID should do the ramping itself by slowing the system time response with the P gain. So, I don't find a way to explain him that ramping the setpoint should not have to do with the PID purpose.

author: krisRaba
author_rank: Regular Contributor
timestamp: November 02, 2018, 10:15:19 pm
content: If you set PID for slow response to use it for ramping you will also get very poor response to load changes etc.In treadmills acceleration and deceleration is different so it's quite hard to find parameters that ideally fits both situations.It is easier to separate ramping algorithm and let fast PID just follow it as SP.

author: james_s
author_rank: Super Contributor
timestamp: November 02, 2018, 10:16:48 pm
content: Ramping the throttle signal is very easy once you have good closed loop control of the speed. All you need is what is essentially a very crude digital low pass filter.

author: XaviPacheco
author_rank: Regular Contributor
timestamp: November 02, 2018, 10:29:59 pm
content: So, the best way is to ramp the setpoint, and the PID is just going to track it. For example,1. The machine is stopped, i.e, SP is 0 mph (0% duty cycle)2. The user decides to go from 0 mph to 1 mph (10% duty cycle let's say)3. The ramping algorithm (different from PID) is going to increase duty cycle from 0 to 10 in increments of 1 every 0.5 sec.4. When the duty cycle is finally 10, then the PID comes to play and tries to keep the system in 1 mph (10 % duty cycle). It tries to compesate the system as fast as possible. So here the PID has fast response.5. At the same time, if the user decides to stop, I apply a deceleration algorithm. Again, different from PID.Am I right?

author: james_s
author_rank: Super Contributor
timestamp: November 02, 2018, 10:43:09 pm
content: Yes, that sounds pretty reasonable.The way I've implemented ramps like that is to have a routine run at a regular frequency that compares the current setting to the requested setting, if one is greater than the other, decrement it once per loop, if it's less than the other, increment it once per loop. If the values are equal, do nothing. By adjusting how much to increment/decrement at a time or how often that loop runs, you can adjust the rate of the ramp.

author: Yansi
author_rank: Super Contributor
timestamp: November 02, 2018, 11:12:23 pm
content: Every decent medium to high power motor control application there has to ALWAYS be a current regulating loop inside of a speed regulating loop. So you need two, typically PI controllers (D is of not much use here).The inner current loop needs to be very fast, working with the electrical time constant of L/R of the DC machine. The outer speed loop is the slower one, working with the whole mechanical system.Omitting the inner current loop will increase the risk (if not making it completely dangerous!) of blowing stuff up badly.Never! control duty cycle of any (large) electric machine directly! Even if it is small. Large dangerous currents may still flow through the circuit. Any suggestion to control duty cycle directly is mostly wrong assumption (at least troublesome!), so is switching between different PI regulators for a single control loop.Ramping the speed is to be done by ramping the set-point of the outer speed control loop.

author: rstofer
author_rank: Super Contributor
timestamp: November 03, 2018, 12:44:12 am
content: Ramping is seldom linear like the leading and falling edges of a trapazoid. Rather, it is like a falling 'S' - acceleration starts off low and increases to some maximum. When the ramp is approaching the end value, the acceleration falls such that the commanded speed is asymptotic to the desired speed.This 'S' type acceleration curve might not work out well if it is simply implemented as part of the inner PID.Google for 's shaped acceleration curve'We're trying to keep 'jerk', the derivative or rate of change of acceleration, low.https://en.wikipedia.org/wiki/Jerk_(physics)Read section on Physiological Effects starting at "To avoid passengers in transportation ..."

author: coppercone2
author_rank: Super Contributor
timestamp: November 03, 2018, 12:49:28 am
content: for these applications that have multiple PI controllers, could you theoretically use a higher order controller like Jerk, Snap, etc (whatever the opposite direction is), if you had time to mess with the algorithms rather then multiple control loops or are they some how performing a function not related to integrals/derivatives anymore?

author: XaviPacheco
author_rank: Regular Contributor
timestamp: November 03, 2018, 12:56:54 am
content: Wait.. what? Why multiple PI for this application? It's a 'simple' speed controller.Quote from: Yansi on November 02, 2018, 11:12:23 pmWhat do you mean exactly by "directly"?

author: coppercone2
author_rank: Super Contributor
timestamp: November 03, 2018, 01:04:23 am
content: it might have something to do with the momentum of the mechanical system, I am not sure.

author: krisRaba
author_rank: Regular Contributor
timestamp: November 03, 2018, 08:32:46 am
content: In my application I have current limiter. It is a shunt resistor + comparator + PWM_off input (OCref clear). I can adjust comparator threshold by analog output.I think it implements mentioned current control loop.One thing I am not sure now is the set point for that loop. At the moment I adjust the PWM duty in speed controller and current loop is only a limiter. It works OK as motor should not exceed given current limit but maybe it would be better to control current loop threshold by speed control loop?But I don't know what should control PWM duty then? Maybe it should be always 100% and limited by current loop? Hmmm...

author: jbb
author_rank: Super Contributor
timestamp: November 03, 2018, 09:51:21 am
content: Quote from: XaviPacheco on November 03, 2018, 12:56:54 amThe ‘classic’ DC motor control is as follows:- ‘fast’ inner current loop controls motor current. This loop sets PWM. Typically a PI controller is used here, because Derivative action is sensitive to noise.- ‘slow’ outer speed control loop sets the motor current referenceSo no user input (eg speed reference) is directly passed to the PWM unit.

author: krisRaba
author_rank: Regular Contributor
timestamp: November 03, 2018, 11:17:10 am
content: And one thing that is very important because of PID idea of operation. You MUST have reliable feedback!If your sensor is broken and you won't detect that, your PID will "see" huge error between SP and PV and will increase output value (PWM duty / current setpoint) to reduce error. Becasue it won't help (broken sensor gives wrong PV or 0) it will end up with maximum power applied to motor.In treadmill you probably use light-barrier sensor detecting slots on motor fan or somewhere on shaft. You can set timeout for incoming pulses or whatever. But take that into account for safety of your users.

author: rstofer
author_rank: Super Contributor
timestamp: November 03, 2018, 03:28:30 pm
content: Treadmill controls are available for about $130, ready to use (on something). Google has a lot of hits for 'treadmill speed controller' including a lot of DIY projects.There is nothing to be gained by re-inventing the wheel (or speed controller).

author: Siwastaja
author_rank: Super Contributor
timestamp: November 03, 2018, 03:37:48 pm
content: Quote from: XaviPacheco on November 03, 2018, 12:56:54 amWhat he's telling you is the same I'm always telling everyone: you need to control the motor current, to prevent blowing up the power semiconductors, and this needs to be quick. (Torque control and motor protection are side products; they wouldn't need to be that quick, but you are fine with one quickly-acting control.)One way to do it is a PI loop; measured signal is the measured motor winding (not input) current, and setpoint is your desired torque. A fast comparator terminating a PWM cycle is equally fine, although a bit less precise. Often, both are used. If you use a comparator, there's nothing fundamentally wrong to control the "duty cycledirectly" - it's just that this duty cycle is overridden by the current comparator at the PWM generator (timer module in MCU) level.

author: Yansi
author_rank: Super Contributor
timestamp: November 08, 2018, 12:58:05 pm
content: Look at it whatever you want, but the current through power stage should always be limited, if not directly controlled.With DC machines, torque is directly proportional to the armature current, rotational speed is proportional to armature voltage.The theory behind DC machines and their control is nothing complicated, but I understand general public and even average EE may be quite under-educated about these.So to put a bit of basic theory in here; please don't bash me for not trying to write another electrical machine textbook here. You can surely find one in your technical library yourself:A steady state voltage equation for armature isUa= Ra*Ia+ c*Phi*omega."a" being the suffixes for armature. Rais the DC resistance of the armature. Phi is the magnetic flux originated in the field windings or permanent magnets on the stator. Omega is the rotational speed (rad/s). "c" is a constant that is specific to the DC machine construction. Uaof course is the voltage across the armature terminals of the machine you connect your wires to.What a brisk mind might already see is that armature resistance is what causes the typical small motors to have low torque at low armature voltages. In fact, Radirectly affects how "stiff" the speed characteristic is, with increasing load torque.For further analysis, you need to add also the effect of inductance of the armature. It is especially important for a correct power stage design! The equation therefore is more likeUa= Ra*ia+ c*Phi*omega + La*dia/dt. Note that Lais the armature inductance, while ibbeing the instantaneous armature current.What comes important from this is that the switching frequency of the power stage needs to behigh enoughnot to cause the current (torque) to ripple excessively. This and exactly this is what excuse me all dumb EE hobbyists do wrong. (More on that later.) Excessive current swing will damage the power stage (high current ripple increases the effective current value seen in the circuit), damages the motor winding and damages also the mechanics of the system by torque ripple. This is also a major consideration when going over few kW of power. Then even the torque cogging caused by the motor itself may be of an issue. (Typical culprit: switched reluctance motors)With some basic math you should be able to derive that the maximum current ripple value peak-peak Ideltafor a specific switching frequency isIdelta= Vsrc/ (4*La*fpwm), where Vsrc is the supply voltage for that typical "buck configuration" power stage. Note that the maximum current ripple happens at exactly 50% duty cycle. Of course we have neglected the armature resistance, but it is not that important for this ballpark calculation. But what is important is if we say the current ripple Idelta shall be low, typically below 10% of the motor nominal current, we can solve for the minimum pwm frequency as such:fpwm>> fpwm(min)= Vsrc/ (0.4*La*In). For most typical motors it is a few kHz typically, 10-15 kHz is sufficient for most applications from few watts up to few tens of kW. Going excessively high with the switching frequency causes problems within the power stage itself (switching loss).The other equation I have not mentioned yet is the one for torque:M = c * Phi * Ia. I'd rather not come into much details why these two basic DC machine equations are exactly true or not for a real DC motor, but with these you can begin designing a lot stuff. Note that the "c" constant in theory should be the same as in the other equation, Phi is the same magnetic flux from either field winding or permanent magnets.The other thing I would like to appoint is that therated powerof any electrical machine is the output mechanical power on the shaft, not the maximum electrical input! The maximum electrical input is typically limited by the winding current.Pmech= M * omega. I have seen numerous times people to measure the power consumption of a motor with a wattmeter, comparing it to the nominal motor power (stamped on the nameplate) and considering this a check if a motor is not overloaded. Sure, it won't be, but you won't get the full output that way. Check for the inputcurrentinstead.Now what really ticks me off is when some under-educated creep argues that "you need low PWM frequency otherwise it does not have any torque at low speed". Above you have a mathematical proof, that such argument is bullshit. I have already explained what is the cause of the speed drop when load is increased. With the equations above a brisk mind can already tell how to compensate for it.Hint: Increase the armature voltage proportionally to armature current to get rid of thatRa*Iaterm in the first equation. In case of a typical "pwm regulator" you may build, you need to increase the duty cycle based on the motor current. Note that you can even overcompensate the term and obtain interesting torque-speed characteristic. This little trick is mostly used in dc motor wheel chair drives. Hence why the wheel chair with DC motors with high frequency PWM has no problems going over obstacles with high enough force. And yes, there isnonespeed feedback in most wheelchair drives. It is simply not needed. And the current is measured anyway, for obvious reasons of not blowing the controller into bits when someone mistakes the wheelchair for a bulldozer.What I would also like to point out at the end, regarding to the typical situation on image below taken from a typical machine textbook, is that the diode has to be rated for the same current as the mosfet is (in a typical application where the duty cycle is used from 0-100%, which inded is most of these applications). Also note that Ui is the c*Phi*omega term in the first equation.I hope this lengthy post may at least change the level of your confusion, if nothing else.Y.

author: XaviPacheco
author_rank: Regular Contributor
timestamp: November 08, 2018, 01:41:11 pm
content: Thanks for your lengthy and useful post. It has helped me understand some important concepts. For now, I'm very interested in determining the model and characteristics/parameters of my motor. I attach the nameplate of the motor I'm controlling via PWM.

author: Yansi
author_rank: Super Contributor
timestamp: November 08, 2018, 07:25:17 pm
content: Using the math above I have supplied, you should be now able to determine the nominal output torque, the torque and speed constant cspeedand ctorque. Just try it... nothing complicated here. You need to also measure Ra and La for completing the model and all unknowns into the equations.You should be able to even plot some approximate characteristic (speed vs. torque) for different armature voltages of that motor, even other ones.Other measurements might be to determine the rotational inertia (angular mass) of the motor or the whole system - but these are a bit more advanced concepts that help design the PI regulator cascade, the speed regulator respectively. But both can be tuned with a good degree of success just "by hand".In fact, if you are going to control speed of the system, usually you even need a third PI regulator loop just for the speed sensor itself. To be exact, it is not a PI loop, but in fact a PLL, google for "angle tracking observer". This concept is utilized for many speed sensor types like resolver, including the obligatory quadrature encoder.When trying to implement some control loops in the real world, you will quickly learn, that the digital control loop needs to be run at constant frequency that is typically in sync with the power stage switching frequency (optimally, both the same) and that to feed the speed regulator, you need to know the instantaneous speed exactly n-times (typicaly fpwmi.e. 15kHz times) a second.Which, if you have ever programed any kind of speed measurement you shall know that is an interesting requirement:Most hobbyist program a digital speed meter as a frequency counter. That is nice as an indicator-level only, but can't be used for speed control. A frequency counter need to count pulses per given time and at 15kHz (67us gating interval), you would not count much pulses at low speeds, if any and you would need an encoder with extreme numbers of pulses per revolution to obtain ever slightly good resolution at low speeds. Measuring the time in between pulses will not get you anywhere either, as that would not give you speed measurement at an exact interval - would depend on the speed.Now you probably guess there is a trick to it - that is the tracking observer. What you need to realize is that an encoder does not give you speed, but (angular) position. It should be no surprise that by taking a derivative of position in time will get you the speed. But in real world, making a derivative of any real world signal is very problematic due to noise pickup, you do the exactly opposite: integrate.It looks like this: By taking integral of an estimated speed, you get back the angle position (based on the estimated speed). Now if you take the estimated position and calculate an error against the real position from the encoder, by feeding it to a PI regulator you can close the loop and let the PI regulator deal with guesstimating the speed. That is what basic observer is. It may sound complicated, but it is not. In real applications, you just count pulses from the encoder indefinitely and trying to guess them by the PLL (PI+ integrator), and the speed comes like byproduct of the loop.Modelling an tuning this PLL loop is very easy, as it does not depend on outer world, it just defines the dynamics of the sensor element in the whole system.I know that this is a lot to cover in just two posts, I would strongly recommend looking for some appropriate literature here.Y.

author: MarkF
author_rank: Super Contributor
timestamp: November 08, 2018, 11:25:28 pm
content: I found this video quite interesting

author: XaviPacheco
author_rank: Regular Contributor
timestamp: November 09, 2018, 02:27:09 am
content: Well, yeah. It's kind of lot of information for me as student making one of his first real-world projects. Indeed, my project would be garbage considering some things you said that I didn't take into account. At the end of the day, I ended up making a PCB (attached) with a simple buck topology. The board works, i.e, I can control the motor speed with PWM. The board also measures speed using a reflective sensor (6 pulses per rev), and also measures motor current. It's okay the board works at open loop, but I haven't been able to build a special closed loop control system for this. In treadmills, I think the control doesn't have to be so precise. The DC motor I'm using can keep the torque under different loads, but that doesn't mean that I don't need a closed loop control. If the load is too much, the speed reduction will be noticeable. I know the project has errors as it's the first prototype.