As a bit of a diversion from my second build, I’ve put together a joystick that allows jogging with analog control of the feed rate.
I decided to take a different approach from the approaches I’ve seen, which operate through the Raspberry Pi or a separate microcontroller like @arminth’s solution (https://www.v1engineering.com/forum/topic/manual-joystick-control-fro-mpcnc-2/).
This one has a very dumb joystick, with modifications made to Marlin to read the analog values and inject g-code into itself. Here is a schematic of the joystick. The enable pin is active-low with a pullup on the digial pin, so if it’s disconnected it will treat it as disabled and not lose its mind trying to read floating analog voltages. Right now I have this connected on AUX-2, using A5, A10, D44, and A12.
I’ll publish the code, but I’m not sure whether to try for a fork and pull request, or to just publish a patch file. There is not that much code and it’s a pretty non-invasive modification. One way or another I’ll post it once I get it organized for publication.
I started with one of the super cheap x/y thumb joysticks, but the inconsistency of the ADC readings was making me nervous, so I bought this one for $17: https://www.amazon.com/gp/product/B07CVGPMDP/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1
The overall strategy is mostly straightforward. Read the analog values, by mimicking the method used for temperature ADC reading. Then in the idle loop, check to see if the enable pin is active (low) and if the joystick is outside the dead zone. If the movement queue has more than 4 commands, then abort, so we fill the queue with only about 1/4th of a second worth of movement.
If all the checks pass, then calculate a feed rate (as a fraction of the maximum feed rate) based on how far the joystick is pushed. Then for that feed rate, calculate the distance that would take about 1/20th of a second to traverse. Then temporarily enter relative mode and inject a g-code command to move that distance at that feed rate.
As a result, the joystick should generate a constant flow of about 20 commands per second with roughly a 1/4 second lag between the joystick position and the speed response. Since a small movement of the joystick can provide slow movement, a bit of lag should be alright since you can jog quickly and fine-tune at low speed, so you don’t have to stop on a dime.