Controlling a Lathe with C++

This is a quick post – I’m intending on going into far more detail about this at a later stage – but recently, for fun, I’ve been working on automating some functions on a metalworking lathe. It’s been a really interesting project – I’ve been driving a fairly powerful stepper motor to give accuracy of positioning of tooling down to near-micron level, and measuring the position of the lathe’s spindle (via a rotary encoder) while it spins, at an accuracy required to start a cut at the same position over multiple passes, for cutting threads. I’ve been driving all this from a Raspberry Pi 3 Model B. It’s working beautifully, as you can see from the thread I cut below:

From a C++ perspective, this has required microsecond-level accuracy, and plenty of multi-threading to get suitable real-time performance – it’s interesting to have a real-world requirement for this kind of coding outside of my normal work, and incredibly rewarding to see the lathe chuck spinning in a blur but knowing that the code can determine exactly what its rotational position is at any moment.

Usage of mock objects and unit tests have allowed me to develop large chunks of the code without access to the hardware when I’m away from my workshop.

The code can be found at https://github.com/md81544/electronicLeadScrew

See the thread cutting in action at https://www.youtube.com/watch?v=VlhPiRscNeY

Leave a Reply

Your email address will not be published. Required fields are marked *