1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

PID Engine RPM Controller

Discussion in 'Embedded' started by Paul W., Jun 13, 2004.

  1. Paul W.

    Paul W. Guest

    Hi Guys,

    I'm building an engine speed controller using a 16 bit microcontroller
    for a gasoline engine. I have a 1.8 degree resolution stepper motor
    coupled to the throttle plate which moves the throttle angle to the
    target position.

    The objective is to regulate the speed of the engine to one fixed
    setpoint in terms of RPM. I'm measuring the engine speed with a
    magnetic pickup and calculating RPM in the microcontroller. My
    stepper motor driver simply works by setting a direction and number of
    steps which is generatred from the micro.

    I'm trying to set up a good PID control loop. I have implemented some
    code, but am a bit confused over some of the algorithm options. I'm
    considering using a velocity form (incremental). Basically, the
    engine speed is the feedback which is subtracted from a reference RPM
    and fed to the PID controller. The output of the controller would be
    throttle angle (I think?). The throttle angle then results in a
    certain engine RPM for a given load (highly non-linear of course).

    The problem I'm having is a conceptual one as to how to treat the
    stepper motor and the associated input to the "plant". I understand
    that a stepper motor is a "self integrating" device, but I don't have
    a good intuitive feel for what that means in real-life.

    Is the proper way to implement?:

    - Use an incremental algorithm to calculate my *change* in throttle
    angle for each sample period.
    - Take this delta, and translate it to stepper motor steps (and
    direction) based on the change in throttle angle.
    - Update this calculation each sample period

    The unknowns to me are:

    - Can I use a positional algorithm instead of incremental by taking
    the PID output and subtracting the last position to generate a delta
    and send that to the stepper? I have tried implemented the algorithm
    as positional and simply send the PID output (after translating to #
    of steps) and send that directly to the stepper - this seems to
    function but do I wind up with an extra, unwanted integration? In
    this scenario I interpreted the output of the PID as a delta position
    as opposed to absolute position, and to my surprise is seems to
    function reasonably well.
    - I am presently running the stepper motor at a fixed speed and always
    microstepping to 1/8 step. I could conceivably play with both the
    step speed and the step resolution in real time to create
    acceleration. Would this be of any benefit.
    - I've read about Three-Position Pulse Output type algorithms, which
    seems to be similiar to what I'm doing - how does this apply?
    - My PID sampling period is 30ms. With the current stepper speed, I
    would take upwards of 200ms for it to travel its entire stroke. This
    means my actuator would not be finished positioning when the next PID
    loop rolls around. Is this a problem?

    Thanks for any feedback,

    Paul W., Jun 13, 2004
    1. Advertisements

  2. Paul W.

    Tim Wescott Guest

    This means that the motor output is some form of an integral of it's
    input. For the stepper motor itself this is a bad analogy -- you've got
    two phases, four or six wires, you're microstepping, etc. An ideal DC
    motor is a better model -- you give it a voltage, it goes that speed,
    therefore its position is the integral of the drive.

    If you have some other process that's driving the stepper, and you just
    tell it the number of steps, then your stepper-driver combo is "self
    This should work depending on a number of things (if you want to be
    recognized as an engineer practice saying "it depends" in answer to
    every question).
    I assume that what you send to the stepper is a desired number of steps?
    The difference between outputting an angle vs. an angle delta is that
    you have that extra integration. So you need to ask if it's unwanted.
    If the motor needs that derivative term to be stable it probably is, but
    if you want a type-II loop it probably isn't.

    It would also be nice if you know what your throttle position is, for
    diagnostics if nothing else.
    Probably not.
    I'm not familiar with the term -- it sounds specific to one type of
    Yes, you'll have integrator windup in your PID. My article
    http://www.wescottdesign.com/articles/pidwophd.html touches on
    integrator windup and gives the most popular method of avoiding it, but
    doesn't go in depth. I suspect you'll find it useful anyway.
    Additional Comments:

    * Stepper motors -- blech.

    * A throttle position feedback would be a good idea.

    * You _really_ want some safety built-in, at least a solenoid that kicks
    the throttle to idle if your power cuts out, better yet a back-up rev
    limiter (you can get these on your ignition if it's not already there).
    Tim Wescott, Jun 13, 2004
    1. Advertisements

  3. Paul W.

    Joerg Guest

    Hi Paul,

    Why would a stepper be self integrating in this application? A stepper
    is an almost instant and pretty digital control when it comes to
    engines. A step and thus throttle change of 1.8 degrees happens in
    microseconds whereas the reaction of the engine takes a whole lot longer
    because of the flywheel and other delay causing elements. So the natural
    lowpass filter in the actuator section is going to be the engine. Well,
    maybe unless it was a souped up version from a Ferrari Testarossa :).

    One method to arrive at customary loop responses is to look at a good
    governor solution. Most stationary engines have governors, for example
    generators where the output voltage isn't supposed to stray more than 5%
    or so from spec. This doesn't mean they are all good though. I have just
    used a wood splitter that had a terrible governor which even caused the
    engine to bang once in a while.

    Regards, Joerg

    Joerg, Jun 13, 2004
  4. Yes, which could cause problems- if it can't be tuned properly at both
    extremes of load you'll likely have to add throttle position feedback
    or load feedforward to deal with the nonlinearity.
    Your controller inputs are SP and PV, your output is change in
    "throttle angle" (or whatever actually results from the stepper motor
    steps) per Ts.
    You don't really have a PID controller if the output is slew-rate
    limited, but it doesn't sound like this will happen much in normal
    I don't think it does. I've used things like this in controls such as
    heat/cool controls on extruders where some zones are run close to
    adiabatic (the heat from plasticizing is close to the heat loss, so
    you may need heating or cooling), but your setup sounds very
    symmetrical and I don't see much reason for that kind of thing unless
    you're trying to deal with problems (eg. "hunting") caused by
    mechanical backlash in the throttle linkage (and the best way to deal
    with that is to eliminate it).
    Probably not except at startup or sudden load increases you might get
    overshoot/undershoot on the RPM (but 0.2s is not really very long
    compared to the time constant of the plant, which is more important
    than the sample time). You'll probably have trouble calculating the
    2nd derivative of the RPM, BTW, without some filtering. You might try
    and see if just a PI controller will work well enough. Windup should
    not be a problem because you are using a velocity controller.
    Best regards,
    Spehro Pefhany
    Spehro Pefhany, Jun 13, 2004
  5. Paul W.

    Paul W Guest

    Yes, correct.

    Yes, I believe it does require the derivative term to maintain stability.

    When you say "extra integration", do you mean the integration in the PID
    loop plus the integration in the motor?

    I'm looking at a throttle position sensor to confirm this. Although I can
    know throttle position indirection by keeping track of all the deltas.
    Assuming the motor doesn't mis steps, then it would always be correct (and
    he hasn't missed any steps that I know of).

    Its where the output is either UP/DOWN/STOP for example, where UP might
    drive a fixed speed motor opening a valve.
    Do you mean that when the position output exceeds the number of steps that
    can be completed within one sample period is considered windup? In this
    case, the actuator is not really saturated on an absolute basis, only per
    sample period.

    Yes, I have overspeed/underspeed shut down which cuts the fuel.


    Paul W, Jun 14, 2004
  6. Paul W.

    Preppy Guest

    Yeah, I must implement some form of gain scheduling, or perhaps even a
    characterizer to linearize the system.

    Ts = Sample Period?

    When you say "or whatever actually results from the stepper motor
    steps", do you mean the case where the motor has reached its maximum
    number of steps per sample period?

    That's what I'm wondering, should my output be absolute throttle angle,
    or should it be *change* in throttle angle?

    Not sure I follow what you're saying. Are you saying that even though
    the motor is bandwidth limited, it likely doesn't matter compared to the
    response time of the engine?

    I could probably speed up the stepper motor so that it always completes
    the change in throttle within one PID period.
    I'm pretty sure I need derivative, it requires it for stability from my

    You mentioned 2nd derivative, I assume you mean that first derivative is
    that of the normal PID term, and the 2nd is because it is a velocity

    Can I implement a software low pass filter to supress high frequency


    Preppy, Jun 14, 2004
  7. Paul W.

    Preppy Guest

    Got it! Thanks.
    Is this true for both the positional and velocity algorithms?

    Do you mean the response time of the engine speed to changes in
    throttle? I don't really know, but is relatively slow in microcontroller
    terms - probably well over a second at least.
    Yes, as best I can.
    Instantly, upon application of higher electrical current (we are driving
    a generator).
    Tried that, but it seems to want D for stability.


    Preppy, Jun 14, 2004
  8. I've implemented valve motor control in a PID temperature controller, which
    would seem similar. I'm not sure I understand why you need to worry about
    throttle angle - it's just the output actuator within a closed loop. If
    you're measuring engine RPM, surely the throttle angle must do whatever it
    takes to maintain the measured variable (RPM)? What am I missing?

    Re delays in your actuator - most real-world processes are laggy, so I
    shouldn't worry too much unless your I term is inappropriately short.

    Steve at fivetrees, Jun 14, 2004
  9. I mean that stepper motor steps could be nonlinear wrt throttle angle.
    For example, if you have a spring return and a linkage so that it
    shuts off if the stepper motor loses power..
    The output of a velocity controller is the rate of change of the
    controller output, so, given fixed sample time, you can think of it as
    (proportional to) steps (maybe fractional, maybe + or -) per second
    and also per sample time.
    That's my guess.
    Think of the bandwidth of the controlled system (throttle to loaded
    engine shaft) first, and use a sample time that is appropriate to that
    (not too fast, not too slow).
    Correct- to get the I in PID you need the second derivative of the
    error term with the velocity algorithm.
    Certainly, given the usual caveats of digital filtering.
    Best regards,
    Spehro Pefhany
    Spehro Pefhany, Jun 14, 2004
  10. Ditto, that. I started a project to control huge steam and gas valves using
    pneumatics. The pneumatic cylinder was controlled by a pneumatic stepper
    valve. There were problems with the stepper valve shifting to a safe
    position when power was lost. Also, there were problems getting the
    initial position set properly. I wouldn't trust steppers valves for
    anything important.

    I bowed out. We did have position feedback for the pneumatic cylinder that
    was controlling the flow control spool, but when stuff happened, the only
    sure way I had to make sure the main flow control properly shut or open was
    to just give the stepper motor on the pneumatic valve a bunch of steps in
    one direction or the other depending on whether the valve was to fail shut
    or closed. Well what happens if our controller loses power? We can't make
    the valve fail in either direction because without power we cannot step the
    stepper motor. The pneumatic stepper valve would also fail to a neutral
    postion which would let the main flow control valve move at all.

    Peter Nachtwey
    Peter Nachtwey, Jun 14, 2004
  11. You do. Your system has two lags at least. The valve and the motor. This
    requires a PID for best response.

    Shame on those that suggest that just a PI would do.
    A second deriviative should be considered if there are many lags.
    Yes. The corner frequency should be much higher than the plant frequency
    so the filter has little affect on the response. I like to use the
    geometric mean between the same frequency and the plant frequency. I have
    used up to a four pole Butterworth filter.

    One more suggest. As Tim suggested, the integrator WILL wind up. To
    avoid this, do NOT ramp the set point up faster than what the stepper can
    respond. In fact, you may want to ramp the RPM as slow as possible. This
    way the the rate limited stepper will not be a factor.

    Peter Nachtwey
    Peter Nachtwey, Jun 14, 2004
  12. Paul W.

    Paul W Guest

    I'm using a spring to close the throttle all the way upon loss of power
    - its not a big issue in this application. Its certainly better than
    the conventional rotory actuator which is full of hysteresis and non-

    Paul W, Jun 14, 2004
  13. Paul W.

    Paul W Guest

    I've heard people say that with a velocity (incremental) algorithm integral
    wind up is not a big problem as it is inherently limiting. Is this true?

    Paul W, Jun 14, 2004
  14. Paul W.

    Tim Wescott Guest

    Well, the extra integration of the stepper-as-integrator.
    "Assuming". Heh heh. This is why I don't like steppers...

    Given that you know the throttle position you may want to take your PID
    output as a position command, then have a second loop that servos the
    throttle to that position.
    Read the article.

    Windup is what happens when _any_ output is saturated and your
    integrator keeps building up -- the consequence being that when you come
    out of saturation your integrator will be at a very wrong state. In a
    loop with enough lag this can induce large-scale, slow oscillations
    whose effects can range from embarrassment to actuator overheating to
    mechanical failure.
    Tim Wescott, Jun 14, 2004
  15. Paul W.

    Tim Wescott Guest

    Spehro, surely you meant 1st derivative?

    Even the simple derivative of the engine RPM will be noise, you'll want
    to use a "derivative" with a gain that levels off at some frequency:

    (1-d) (z-1)
    H_d(z) = -----------

    This has the effect of cascading a derivative and a low-pass, with a
    gain that levels off above the indicated pole, but with the correct
    derivative action below that.
    Tim Wescott, Jun 14, 2004
  16. Paul W.

    Paul W Guest

    I think he did mean 2nd derivative because with a velocity algorithm we
    are essentially taking the derivative of the positional PID form, which
    results in a second derivative term.

    Paul W, Jun 14, 2004
  17. It really is the second derivative, since the output is effectively
    fed into an integrator (the stepper, a 1/s thingie)..

    One (the simplest, I think) unfiltered difference equation form for
    the 2nd derivative is:

    is: e - 2* e + e
    n n-1 n-2

    There are better methods..

    Best regards,
    Spehro Pefhany
    Spehro Pefhany, Jun 14, 2004
  18. It won't likely be possible to tune it for this ideal "best response"
    (however that is defined) due to the large amount of nonlinearity. It
    will have to be detuned deliberately to keep it stable enough
    Looks to me like the two lags are about an order of magnitude
    different, especially for small signal response.. I also know for a
    fact that PI works rather well in very similar applications in
    production volume.

    Best regards,
    Spehro Pefhany
    Spehro Pefhany, Jun 14, 2004
  19. That's transient response to a load change, not "stability" (as in
    "not oscillating").

    Best regards,
    Spehro Pefhany
    Spehro Pefhany, Jun 14, 2004
  20. My memory finally kicked in. The way we did this was that the main PID loop
    provided an output actuator value (0-100%). We then used another (simpler,
    faster) loop to control the actuator, with the valve position as the
    measured variable and the motor as the actuator, and the output actuator
    value as the setpoint.

    With that proviso, my earlier comment/question still applies.

    Steve at fivetrees, Jun 14, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.