Dynomotion KFLOP’s KmotionDef.h’s entries pertaining to encoders (2015 to current)

double Position;

  • // encoder, ADC, Resolver, reading

double invDistPerCycle;

  • // for stepper distance for one complete cycle (4 full steps)
  • // for brushless 3-4 phase encoder counts for one complete cycle
  • // (saved as reciprocal for speed)

Inv Dist Per Cycle applies to Stepper, 3 Phase, and 4 Phase motors.  For a stepper motor, the distance per cycle defines the distance that the commanded destination should change by for a motor coil to be driven through a complete sinusoidal cycle.  Parameter should be entered as the inverse (reciprocal) of the distance per cycle.  Stepper motors are most often characterized by shaft angle change per “Full Step”.  A motor coil is driven through a complete cycle every four – “Full Steps”.  See the following examples:

Example #1 : A mechanism moves 0.001″ for each full step of a step motor  It is desired for commanded distance to be in inches.

Result: One Cycle = 4 full steps = 0.004″,  Thus InvDistPerCycle = 1.0/0.004 = 250.0 (cycles/inch).  Commanding a move of 1.00 will generate 250 sine waves, or the equivalent of 1000 full steps, or one inch of movement..

Example #2 : InvDistPerCycle is left at the default value of 1.0

Result: Move units are in cycles.  Commanding a move of 50 will generate 50 sine waves, or the equivalent of 200 full steps, or one revolution of a 200 Step or 1.8 degree motor.

For 3 Phase or 4 Phase motors, Inv Dist Per Cycle represents the inverse of the distance for one complete commutation cycle.

Example #1 : A 3 phase motor/encoder has a 4096 count per revolution encoder which is used for position feedback and for motor commutation.  InputGain0 is set to 1.0 so position measurement remains as encoder counts.  The motor design is  such that the commutation goes through 3 complete cycles each motor revolution.

Result: One Cycle = 4096 counts/3.0  Thus InvDistPerCycle = 3.0/4096 = 0.000732421875.

Note that it is important to use a high degree of precision to avoid commutation errors after moving to very large positions (or at constant velocity for a long period of time).  KMotion maintains Inv Dist Per Cycle (as well as position) as a double precision (64 bit) floating point number for this reason (more than 70 years at 1 MHz would be required to have 1 count of error)

int InputChan0,InputChan1;

  • // Encoder or ADC channels to use

float InputOffset1,InputGain1;

  • // or for ADC, or (InputGain0=-1 reverses encoder)

signed char last_enc;

  • // last fpga encoder reading


  • // address to read/write encoder noise rejection filter value (0..255)


  • // noise rejection filter default value (100MHz/3/7/2 = 2MHz)

#define ENC_NOISE_ERR_ADD 0x08

  • // encoder sudden change by 2 error address

#define ENC_NOISE_ERR_BIT0 4

  • // encoder sudden change by 2 error bit for encoder 0

#define ENC_NOISE_ERR_BIT1 5

  • // encoder sudden change by 2 error bit for encoder 1

#define ENC_NOISE_ERR_BIT2 6

  • // encoder sudden change by 2 error bit for encoder 2

#define ENC_NOISE_ERR_BIT3 7

  • // encoder sudden change by 2 error bit for encoder 3

void EnableAxis(int ch);

  • // enable the Axis at the current encoder position

void Zero(int ch);

  • // Zero the Encoder Position and Current Commanded Position


  • // any write clears all encoder errors

#define SNAP_ENC 8

  • // (4) 16 bit Encoders (bit15=error 7-0=data)

#define SNAP_DIFF_IN 16

  • // 16 bits (15-8= Diff inputs 7-0=encoder inputs)