# PID loop frequency

Started by October 5, 2018
```I'm working on a digital PID motion controller. The target/goal is to
move at (very) constant velocity. Position feedback is from a linear
encoder with quadrature output. The input to the controller is a linear
ramp (i.e. constant velocity) and the PID changes the motor output to
minimize the error. The system works, but I'm looking to improve its
performance at low velocities.

At low velocities there are only a few pulses from the encoder per servo
cycle. In the error signal I can clearly see this quantization. If I
lower the frequency of the servo cycle; the controller performance
increases (i.e. smaller velocity variations).

Two questions:
- Is there a method to determine optimal servo frequency?
- Would it be beneficial to separate the controller in two control
loops; an inner velocity loop and an outer position loop. Perhaps with
both running at different frequencies?

Any pointers to good documentation are appreciated.

Thanks,
Fitzgerald
```
```On Saturday, October 6, 2018 at 12:07:48 AM UTC+10, Fitzgerald wrote:
> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.
>
> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).
>
> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?
>
> Any pointers to good documentation are appreciated.

If the target is to move at constant velocity, surely the control loop should be aimed at doing that.

We don't know what kind of motor you are using, but a microstepped stepper motor would be getting in-phase and quadrature sinusoidal components to do what you want, and the control loop becomes phase-locked loop, with motor drives taken from a divided-down voltage-controlled oscillator, and the steps from the position sensor as the phase signal you are trying to lock to.

The best phase-locked loops do use product detectors to extract the phase, but most realisations lock the voltage-controlled oscillator to the zero-crossings in the frequency signal to which the loop is being locked, so quantisation is a well-known probelm.

Floyd M. Gardner's "Phaselock Techniques" - my copy is ISBN 0-471-04294-3 but it's only the second edition and there's been a third one since

<https://www.wiley.com/en-au/Phaselock+Techniques%2C+3rd+Edition-p-9780471430636>

goes into loop control strategies at some length.

--
Bill Sloman, Sydney

```
```On 10/5/18 10:07 AM, Fitzgerald wrote:
> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.
>
> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).
>
> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?
>
> Any pointers to good documentation are appreciated.
>
> Thanks,
> Fitzgerald

You might look at the PWM absolute position encoders from US Digital.
That makes the position update rate constant, which is a help.

NB the output is the duty cycle, not the ON time, so you have to divide
by the period.  The duty cycle is very stable with temperature, but the
frequency drifts a bit.

Cheers

Phil Hobbs

--
Dr Philip C D Hobbs
Principal Consultant
ElectroOptical Innovations LLC / Hobbs ElectroOptics
Optics, Electro-optics, Photonics, Analog Electronics
Briarcliff Manor NY 10510

http://electrooptical.net
http://hobbs-eo.com

```
```On 10/05/2018 10:07 AM, Fitzgerald wrote:
> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.
>
> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).
>
> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?
>
> Any pointers to good documentation are appreciated.
>
> Thanks,
> Fitzgerald

I did something similar for just a "dumb" brushed DC motor controller
PID a while back, a cascade loop worked pretty well for me. Since your
goal is a constant velocity I wonder why you need a position loop? if
it's just that the shaft turn at a constant angular velocity of some RPM
then what its precise position is at any time doesn't seem like a
relevant state variable.

I believe I used a fast sampling inner loop, PI, to achieve coarse
regulation of motor torque sensed from a current shunt in series with
the motor. And a slower outer loop, PID, for velocity, sensed indirectly
from motor back EMF generated by the PWM drive switching waveform.

I found a little "D" coefficient in the outer loop goes a long way, at
least for my little motor.

All DC motors and servos are finally current/torque driven for whatever
control variable it is you want to regulate, angular position, velocity,
acceleration etc. so I think it's a good idea to have torque be the
primary state variable of interest in the process.

I recall a problem I encountered with my "dumb" motor with just a single
PID loop that only "watched" velocity is it that a single set of
coefficients which achieved good control when it's at speed are too
aggressive trying to bring it up out of stall. "Oh shit 0 RPM? 10 amps,
right now!!" except the PSU size I needed to use for the project size
not capable of that.

```
```On 06/10/2018 00:07, Fitzgerald wrote:
> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.
>
> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).
>
> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?
>
> Any pointers to good documentation are appreciated.
>
> Thanks,
> Fitzgerald

Maybe do reciprocal counting - measure the time between transitions of
the encoder, and from this time difference generate a velocity estimate
(and run the PID) on every edge from the encoder, with variable timesteps.

```
```fredag den 5. oktober 2018 kl. 16.07.48 UTC+2 skrev Fitzgerald:
> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.
>
> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).
>
> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?
>
> Any pointers to good documentation are appreciated.
>
> Thanks,
> Fitzgerald

afaik an inner velocity loop and a slower outer position loop is how it is
commonly done for CNC machines and such
```
```On Fri, 05 Oct 2018 16:07:41 +0200, Fitzgerald wrote:

> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.
>
> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).
>
> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?
>We do this in LinuxCNC.  Many of the encoder counters use a velocity
estimation scheme, that uses time between encoder counts at low velocity.
So, every time an encoder count arrives, it is time-stamped.  When the
servo loop checks position, it also samples the time stamp and the
current number in the time stamp counter.  Thus, it can determine how
long ago the last encoder state transition came in.  This greatly reduces
the effect of "jumpy" velocity if you just compute velocity from
difference in position between last two encoder samplings.

You can look at :
https://github.com/LinuxCNC/linuxcnc/blob/master/src/hal/components/
encoder.c

which is the software encoder component to see how it was implemented.  I
did a hardware implementation that has a 16-bit free-running counter
for the time stamps, and then the rest of the interpretation is done in a
software driver.

Jon
```
```A question regarding terms: what is a "linear encoder with quatrature
output" that produces "pulses"?

Hul

Fitzgerald <john.smith@mailinator.com> wrote:
> I'm working on a digital PID motion controller. The target/goal is to
> move at (very) constant velocity. Position feedback is from a linear
> encoder with quadrature output. The input to the controller is a linear
> ramp (i.e. constant velocity) and the PID changes the motor output to
> minimize the error. The system works, but I'm looking to improve its
> performance at low velocities.

> At low velocities there are only a few pulses from the encoder per servo
> cycle. In the error signal I can clearly see this quantization. If I
> lower the frequency of the servo cycle; the controller performance
> increases (i.e. smaller velocity variations).

> Two questions:
> - Is there a method to determine optimal servo frequency?
> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?

> Any pointers to good documentation are appreciated.

> Thanks,
> Fitzgerald
```
```Fitzgerald <john.smith@mailinator.com> writes:
> I'm working on a digital PID motion controller.

Isn't everyone?  ;-)

> - Is there a method to determine optimal servo frequency?

In my case, the pid loops ran at the PWM frequency.

> - Would it be beneficial to separate the controller in two control
> loops; an inner velocity loop and an outer position loop. Perhaps with
> both running at different frequencies?

I ended up with three PID loops:

* the innermost one managed the current through each phase of the
motor.  I.e. this is a torque loop.

* the next one is the RPM loop

* the outermost one is the position loop.

For high RPM, you can drive the RPM loop directly.  For low RPM, you can
calculate the desired position vs time and drive the position loop
instead.  It will lag a bit according to the holding torque, but follow
the desired RPM.

I can usually keep the motors within a few counts of the desired
position, at 4000 counts/rot.
```
```On Fri, 05 Oct 2018 19:52:00 +0000, Hul Tytus wrote:

> A question regarding terms: what is a "linear encoder with quatrature
> output" that produces "pulses"?
There are things called linear encoders which produce a pair of square
wave signals that are 90 degrees out of phase with each other.  The phase
relationship allows you to determine direction of motion as well as
distance.  The signals are identical to rotary quadrature encoders.
The 90 degree phase relationship is the definition of quadrature.

The individual signals consist of pulses.

Jon
```