{"id":726,"date":"2019-08-30T08:21:05","date_gmt":"2019-08-30T07:21:05","guid":{"rendered":"https:\/\/www.martyndavis.com\/?p=726"},"modified":"2019-09-09T14:56:38","modified_gmt":"2019-09-09T13:56:38","slug":"controlling-a-lathe-with-c","status":"publish","type":"post","link":"https:\/\/www.martyndavis.com\/?p=726","title":{"rendered":"Controlling a Lathe with C++"},"content":{"rendered":"\n<p>This is a quick post &#8211; I&#8217;m intending on going into far more detail about this at a later stage &#8211; but recently, for fun, I&#8217;ve been working on automating some functions on a metalworking lathe.  It&#8217;s been a really interesting project &#8211; I&#8217;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&#8217;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&#8217;ve been driving all this from a Raspberry Pi 3 Model B. It&#8217;s working beautifully, as you can see from the thread I cut below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"1131\" src=\"https:\/\/www.martyndavis.com\/wp-content\/uploads\/2019\/08\/threads.jpg\" alt=\"\" class=\"wp-image-731\"\/><\/figure>\n\n\n\n<p>From a C++ perspective, this has required microsecond-level accuracy, and plenty of multi-threading to get suitable real-time performance &#8211; it&#8217;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.<\/p>\n\n\n\n<p>Usage of mock objects and unit tests have allowed me to develop large chunks of the code without access to the hardware when I&#8217;m away from my workshop.<\/p>\n\n\n\n<p>The code can be found at <a href=\"https:\/\/github.com\/md81544\/electronicLeadScrew\">https:\/\/github.com\/md81544\/electronicLeadScrew<\/a><\/p>\n\n\n\n<p>See the thread cutting in action at <a href=\"https:\/\/www.youtube.com\/watch?v=VlhPiRscNeY\">https:\/\/www.youtube.com\/watch?v=VlhPiRscNeY<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick post &#8211; I&#8217;m intending on going into far more detail about this at a later stage &#8211; but recently, for fun, I&#8217;ve been working on automating some functions on a metalworking lathe. It&#8217;s been a really interesting project &#8211; I&#8217;ve been driving a fairly powerful stepper motor to give accuracy of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.martyndavis.com\/?p=726\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Controlling a Lathe with C++&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/726"}],"collection":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=726"}],"version-history":[{"count":7,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":736,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=\/wp\/v2\/posts\/726\/revisions\/736"}],"wp:attachment":[{"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.martyndavis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}