Dual Closed Loop Control with KFLOP

The Dynomotion KFLOP controller can control 8 motor axis (either as 4 dual/slaved or 8 independent), complete with 8 dedicated hardware encoder channels (while any generic input could be used in theory for encoding,  I understand the general I/O speed is much less than the dedicated lanes).

Placing rotary encoders on stepper or BLDC/BDC/AC motors can feed information on the motor position back to KFLOP, which will then apply corrective action to the drivers to accurately place the motors exactly where they were commanded to be (See Dynomotion – Closed Loop Stepper Control)

Unfortunately, this does nothing to compensate for pitch error in the screws, lash, or other purely linear mechanical elements. The solution to these is to add linear encoders in addition to the rotary; measuring the actual position of the linear elements and sending that feedback to KFLOP.

With a stepper system, one might be able to utilize linear encoders alone, and achieve good results. The stepper motor design itself imparts built in feedback: when the motor is commanded to assume a position, the drive pulse forces the rotor to assume the commanded angle via the two phases. When the motor overcomes the initial frictional opposing forces, the torque is naturally reduced. A stepper motor can be though of as a spring and mass system. The rotor is the mass, and as torque is applied, the magnetic force acts like the spring, storing energy and releasing it (this is also the reason for mid-band resonance in a stepper system). If the load attempts to move the motor out of that position, the increased magnetic field will “push” it back to the commanded position; this force does vary depending on where the motor is (full step and half step are stronger than microstep positions), as the magnetic torque follows a sine pattern.

Having said all that, the “bandwidth” (ability to react in short time frames) with linear encoders alone is said to be rather low/slow.  Enter the Dual Loop setup: using the rotary encoders for velocity control of the motors, and the “outer loop” (linear encoders) for motion/positional control (see Dynomotion – Dual Closed Loop).

Multiplexing Encoders

Unfortunately, encoders #0-4 occupy the same JP connector as that used to connect to a KSTEP (which then uses those pins for multiplexing KSTEP I/O). This is perfectly acceptable for the majority of stepper driven systems (being open loop) and the JP5 connector can be utilized for single loop motor or linear encoders (using the #4-7 encoder channels).

Fortunately, KFLOP can be programmed to multiplex the encoder inputs over to alternative connectors. If the multiplex bit is asserted, the dedicated inputs shift from their normal pin assignments on JP5 and JP7 (respectively) to the first 8 inputs on JP4 and JP6, respectively (4/2012 firmware changesMux’d Encoders Discussion).

The first 8 Inputs on JP4 are Encoders 0-3 A/B signals (when mux’ed). The first 8 Inputs on JP6 are Encoders 4-7 A/B signals (when mux’ed).

Reference MUX program:

 // Mux encoder inputs from KFLOP JP7 & JP5 to JP4 and JP6


Mux = 0                                                              Mux = 1
Signal       I/O Bit  Pin                             I/O Bit    Pin
ENC0A        0                 JP7 – 7                       16             JP4 – 5
ENC0B        1                  JP7 – 8                       17             JP4 – 6
ENC1A         2                 JP7 – 9                       18             JP4 – 7
ENC1B         3                 JP7 – 10                     19             JP4 – 10
ENC2A        4                 JP7 – 11                      20             JP4 – 11
ENC2B        5                 JP7 – 12                      21             JP4 – 12
ENC3A        6                 JP7 – 13                      22            JP4 – 13
ENC3B        7                 JP7 – 14                      23            JP4 – 14

ENC4A        36               JP5 – 1                        26            JP6 – 5
ENC4B        37               JP5 – 2                        27            JP6 – 6
ENC5A        38               JP5 – 3                        28           JP6 – 7
ENC5B        39               JP5 – 4                        29           JP6 – 10
ENC6A        40               JP5 – 5                        30           JP6 – 11
ENC6B        41              JP5 – 6                          31           JP6 – 12
ENC7A        42               JP5 – 7                         32          JP6 – 13
ENC7B        43               JP5 – 8                         33          JP6 – 14

Note that JP6 and JP4 are 3.3V only. Driving them above 3.8V will not do good things to KFLOP’s integrated circuits (JP5 is 5V tolerant by design).


KMOTION Dual Loop Programming

KMotion (the C program loader) must be programmed to join the two servo control loops together, forming an “inner” and “outer” loop. Dynomotion has an example program to do this:

#include "KMotionDef.h"

// Creates dual feeback loops for cases such as rotary motor encoder feedback
// with linear scale encoder feedback.  Two KFLOP Servo Axes are required,
// one for each loop.  Output of the outer loop is applied as a velocity
// to the inner loop 

    for (;;)  // loop forever
        ch0->Dest += ch2->Output;  // move ch0 at speed of ch2 output

This uses two KFLOP Axes channels per set, allowing 4 combined axis. The “FOR” loop can be added to the KFLOP initialization C program as well.

Examples of the settings for KFLOP axes:

Set Inner Loop Axis as:
Input Mode = Encoder
Input Channel = Rotary Encoder
Output Mode = DAC Servo

Set Outer Loop Axis as:
Input Mode = Encoder
Input Channel = Linear Encoder
Output Mode = No Output

Each loop can be tuned separately:

– Tune the inner loop axis for optimal performance based on the rotary encoders

– Tune the outer positional loop for optimal performance based on the linear encoders.

The outer (positional) loop is a higher level control, so it’s output should drive the inner (velocity) loop. example: ch2->Dest +=ch3->Output;


Multiplexing Encoders vs. Input Loss

Multiplexing the encoders to JP4 and JP6 means the previously utilized general I/O connections can only move to JP5 and other connectors (JP7 will still use the I/O pins for multiplexing KSTEP’s inputs). This results in a net loss of available inputs that may be non-trivial to regain.

The Velocity Loop in External Drives

A simpler solution is to fit servo drives that accept encoder feedback and close the velocity/torque loop in the drives, leaving KFLOP to handle the linear encoder inputs, which forms the position loop. The drives will then make certain the motors respond to where they should, and the KFLOP will correct for positional errors via the linear scales. Less control by KFLOP, easier wiring and installation.

Pros and Cons

If the level of mechanical compliance/accuracy of the system is already acceptable, adding the (possibly) significant cost and complexity of closed loop control may not be warranted. Conversely, if the mechanicals are so sloppy a strong wind or quake can shift positions, additional feedback will not make for quality performance.

Closing the control loop around linear scales includes more “bad” mechanical things inside, making the loop more difficult to control. Lash for example, crossing a axis point (think the four quadrants of a circle) will cause considerable havoc as the servo system chases the lash around for that split moment the affected axis is unloaded. Always best to remove lash rather than attempt to correct for it. In addition to Backlash, there is Stiction (static (sticking) vs dynamic friction), and Compliance (elastic modulus). Poor machine design can render expensive zero backlash ballscrews useless. As examples:

  • Inherent design (Box Ways, Dovetail Ways) have relatively large amounts of stiction, which is offset by the proportionally greater load bearing surface area, compared to linear guides.
  • Ballscrew classes can have significant lead error (According to Thompson Ballscrews), a Transport Class 7 rolled Ballscrew can have a permissible variation of 900um in 2500um (0.035 inches in 98), and up to 0.18mm of lash (0.007″).
  • Thermal expansion. On a large machine, the growth of the ballscrew can be significant. (11.7 ppm/° Celsius). As an exaggerated example, a 1219mm long (48 inch) ballscrew, during a 50 degree Celcius rise in temperature, will “grow” 0.73152mm (.0288 inches).
  • Rigidity (stiffness, opposite of compliance) of the unit: floppy drive belts, worn or “sloppy” gearboxes, torsion and bend loads in the structure, flexible couplers

As an example of such from Tom Kerekes of Dynomotion (on a post from CNCZone):


 …So for example say the linear encoder has a small error that the servo wants to correct. This will usually cause the servo to ramp up the motor torque. Eventually the motor will have enough torque to break stiction and begin accelerating. But the linear scales may not yet indicate any thing has moved because of the system’s backlash/compliance. So the Servo will continue to ramp up torque further. Eventually when the backlash/compliance is taken out the Axes will move and the Linear scales will report the motion. But at that point the motor may be at such a velocity that it is difficult to avoid a significant overshoot. Most systems like this can be made to work and be stable but only with very low feedback gains (torque is ramped up very very gradually) resulting in poor dynamic performance (errors are not connected quickly and therefore are allowed to grow to larger values).