ELS Electronic Lead Screw Gearbox Musings
Encoder
2000 rpm lathe spindle
1024 line rotary encoder, 4096 edges per rotation
33.3 rotations per second, 136,533 events per second
Motor
200 steps per rotation stepper motor driving gearbox to leadscrew and feed system
8 microsteps per step (common choice)
1600 microsteps per rotation
Processing
So how hard is it to manage this control?
Pi Pico controller, 133 MHz, 974 clock cycles per encoder edge (one core)
Teensy 4.1, 600 MHz, 4,394 clock cycles per encoder edge
If we use a simple encoder edge driven state machine approach using fractional ratios less than one we can get exact fractional gear ratios and generate output step pulses with very little code. This would allow electronic gearing up to 4096/1600 or 1:2.56, which, in combination with the gearbox on most lathes is more than enough. If higher ratios are needed then a higher resolution encoder can be chosen or a lower microstep value can be used.
The Algorithm
Find the encoder edges, either with interrupt on pin change or by polling or by using the quadrature input hardware (which probably adds more complexity than required here). In the case of the Pico a separate core can be dedicated to the realtime control algorithm so continuous polling is easy.
When an encoder edge occurs the fractional numerator of the ratio is added to an accumulator. If the accumulated sum is greater than the denominator the denominator is subtracted from the accumulator and an output motor step pulse is generated. This is essentially a one dimensional version of the Bresenham algorithm.
Embellishments for directional control will be needed, and debouncing if the encoder edges are not clean. I use a lookup table to decode the quadrature and it gets directional information and filters out bounces, but there are many ways to handle this.
There is no need to worry about acceleration and deceleration as this is a gearbox following the spindle, the physics of which will control the acceleration and deceleration. The simplest approach is to allow changing the fractional ratio (electronic gearshifting) only when the spindle is stopped, but there are ways of synchronously adjusting the fraction while running if that is desired. Adjusting the ratio too quickly (while in motion) might cause issues with some motor controllers. Slowly adjusting the gear ratio during feed adjustment while moving is not a problem. During threading there should be no need to adjust ratios while the spindle is moving.