Electronic Lead Screw Mod

I'm not a very good c/c++ programmer so you may not want me as a teacher.

it is ugly because it is the minimal working code, not the best code or the easiest to read code. It has tons of hanging half finished ideas sprinkled throughout.

If you can design an interface that you'd never want to change then you are a better designer than me. this is not easy for me. I wanted to display the angle releative to the start. I wanted to add a turn counter for winding coils. I wanted to add a mode that made the cut and did an auto rapid back to the starting position. I will re-use it to drive my hobbing controller. Simple is good. websocket control is $0 hardware, you already have a phone or tablet. The bigger win is the ease of refactoring the controller code. Doing all of that in hardware is yuck. if you could abstract it all away then fine, but a MCU doesn't have the resources for that.

There are 93 forks, Kent VanderVelden for example added a nextion touch screen. It looks like he added some capability for limit switches. You may get some ideas from a fork/PR or ask someone who's forked it for help. I don't have any experience with Clough's code and it seems he uses TI's proprietary IDE which I also have no experience with.

There are not updates to clough's code in 2 years, i'd consider it dead. You might as well hard fork it if you want to reuse it.

Here is a very simple view of how clough's ISR works



The most niave implementation is you need to insert a calculation deciding if the move calculated in step 2 exceeds the stop and if so tell the controller it is either done or in error. This is likely best done with 2 stops, the first is where you start and the 2nd is where you want to stop. Since the direction could go wonky you want to be sure you stay between these. You can implement these stops as simple integer counters relative to a "start position counter". Be aware that you might get overflows if you use 32bit values since when threading you may leave the spindle running for a while. It looks like clough switched to Uint64 because of this10/2020.

The problem with this niave approach is that it doesn't account for acceleration/deceleration and you likely don't have any spindle control. The practical thing is that if you use a closed loop stepper it tends to compensate for this naivete quite nicely (at the expense of accuracy). I'm honestly not really sure of the best way to do this to account for acceleration without spindle control. You'd need a motion controller that could estimate the current distance from the stop and calculate the acceleration curve (based on your settings (based on your hardware)) and as it gets into the range where it would need to decelerate it somehow balances that with the need to keep up with the spindle (which you have no control over). This is tough because you must decelerate and maybe mess your thread up or overshoot. Again, in practice this error doesn't seem all that big and in my opnion isn't worth messing with, just use a closed loop stepper which can compensate. If you use an open loop stepper you may loose steps when it tries to stop on a dime and then you could slowly lose position and not be able to repeat theading passes.

Before you begin you need to figure out how to tell the controller to stop. Are you using a hardware stop or a software stop. If software how do you tell the controller where the stop is? Are you going to have a 10 digit keypad to enter a value? can you change the units? How do you account for thread direction vs spindle direction? this is hairy, when left hand threading with the tool in front you need to move the carriage left if the spindle is moving counter clockwise. if the tool is in the back and you run the spindle in reverse (clockwise) you need to move the carriage right. swap all that if you are cutting a right hand thread. How do you enter "stop mode" vs "slave mode"? How do you exit it? How do you tell all this to the controller since it wasn't designed for this? Do you try to implement double button presses, or long presses? What about the display?
For the most part, I totally agree with you. Thinking out the user interface IN ADVANCE, is essential. This includes all the permutations, as you have illustrated. Got burned part way in. Had to redesign things to clean it all up. Added a state machine which forced me to think about the transitions (entry and exit of the state). Before that it was spaghetti code. Afterwards, it has been much easier to add capabilities.

The UI (and by inference, the state machine) is by far the tougher part of the problem. Motion control is roughly 75 LOC, the state machine is 220 LOC, the UI is 820 LOC. Advanced functions tend to need additional data input.
 
Keep in mind, the Clough UI is a simple 7 segment LED array with momentary switches and LED light array. So you end up with really basic UI like up/down/mode buttons and basic display feedback. You could switch it out to something fancy like a LCD, but I haven't seen the need for it and it would require a fair bit of code. It sounds like the OP idea is to use the SET button to mark places for the start/end stops. Simple enough in theory. And if you leave everything engaged all the time and count output pulses, it should be reasonably clean.

I don't have time to work on it, or I'd offer. I also don't have a spare setup and my control box for the lathe is not easy to get to.

As for "dead project" meh, it's not like it really needs much added to it. It does what he designed it to do. I suspect if you contact him with a pull request he'd consider adding it. The project was about replacing change gears, and it does that very well indeed. He seems to jump between projects a lot, partly at least for his channel content. I can understand that as I tend to do it too. Spare time for me is limited so I tend to focus on one thing at a time and jump around a bit.
So, to clarify, it is dead because there are 12 PR's and I'd bet none of them are going to be merged. But, perhaps a better label is "done" because you are right. It works as designed and bloating it with features isn't likely something clough can't keep up with based on the # of issues and support problems.

Here's how I us my ELS.

1. position the carriage, i have a hand wheel on the leadscrew to fine tune position.
2. turn the ELS on, this turns on the stepper and establishes a reference position
3. measure the part and set the feed per revolution
4. enter a travel distance value.

if it is just a bit of material i'll just tell it which way to drive the carriage. It drives the carriage the travel distance. The accuracy depends on the leadscrew backlash. You can remove the backlash just like you would operating manually. Once I have the initial position I can just drive the carriage back and forth until the material is removed.

If it is a lot of material i use "bounce mode" where I can set the speed to cut and the speed and when it gets to the end of the cut it "bounces" back to return to where I started using a faster speed. Then I just rerun the bounce and advance the X axis as needed.

Once the "feature" is done I either jog to a new position or turn off the ELS disengaging the half nut as needed and repeat step 1.

So, entering the feed per rev and cutting distance and clicking a "start" button (left or right) are the main things I do in the UI. I find that I change the cutting distance the most. For threading it is a bit more stuff that can be done, for instance figuring out the depth per pass and offsetting each pass to repicate feeding in a compound (i don't use a compound most often since my crappy lathe isn't all that rigid). There is no timing issues because i'm not trying to time button presses to hit a number. The controller feeds a distance (slaved to the spindle) and stops.

I have a DRO for the Z but haven't bothered to install it since I effectively can use the ELS to get to the right positions as long as I take out the backlash. Once the DRO is on there it will save me a bit of hassle but not that much.

Back to the topic at hand, trying to press a button and jog back and forth and having to hand spin the spindle to set a stop manually would be a big pain in the ass in my opinion. The controller knows the position so why not use that to your advantage?
 
I'm not a very good c/c++ programmer so you may not want me as a teacher.
By definition, I am the worst possible C++ programmer, since I have never even produced a single line of C++ code, so you are way ahead of me.
it is ugly because it is the minimal working code, not the best code or the easiest to read code. It has tons of hanging half finished ideas sprinkled throughout.
No, I meant, "Why do you say creating a hardware user interface is begging for pain?" To my eye, a hardware interface is ideal for this sort of thing. A purely hardware interface worked very well for over 150 years. Duplicating that with a digital panel is quite straightforward.
If you can design an interface that you'd never want to change then you are a better designer than me.
It is not the interface so much as the application. Changing the user interface for a screwdriver, for example, doesn't yield much of an advantage, and in fact may be a really, really bad idea. The interface for a specifically manual lathe - decidedly not a CNC lathe - should be minimalistic and limited by design intent.

That is emphatically not to say creating a more automated machine even up to and including a full blown CNC conversion is a fool's errand. Quite to the contrary, however far one wishes to go in that direction is purely a judgement call based upon one's own desires and available funds. My desire is for a capable, safe, manual lathe, not a programmable one. The ability to thread or turn to a shoulder is a very desirable feature for an otherwise perfectly manual lathe. A single button can readily be employed to enable all the necessary features with no need for any enhancement, as far as I can tell.

this is not easy for me. I wanted to display the angle releative to the start. I wanted to add a turn counter for winding coils.
I designed a coil winder a couple of years ago. I have not had the time or the money to actually build it, but I really do not want the function to be part of my lathe. I have an extreme distaste for "all in one" devices. I won't get into the long list of reasons why, but suffice to say I don't want to use my lathe for winding coils. If you want to add that functionality to your lathe, then go for it. My needs are far more modest.

I wanted to add a mode that made the cut and did an auto rapid back to the starting position. I will re-use it to drive my hobbing controller.
Again, that is a great idea if you want it for your own setup. I don't want it for mine. It involves more hardware and software than I am willing or even able to consider at this point, for a purpose for which I have no need.

Simple is good.
Yes. A WiFi system is not simple. It is fraught with issues. I would never, EVER even begin to suggest a highly dangerous, potentially fatal machine should have any sort of WiFi interface. I detest WiFi in general, but it is just too flaky for any industrial use.

websocket control is $0 hardware, you already have a phone or tablet.
First of all, that is an assumption. It happens to be true in my case, but it is also true I hate the phone with a seathing passion. It is horrible to the Nth degree. If I move forward with selling the thermostat design I showed you, I suppose I will have to implement a phone based UI, but I do so only under extreme protest.

In addition, it isn't $0. I would have to add a WiFi interface to the ELS board. No thanks.

On an aside, my phone cannot reliably contact any other device on my home network. Why is a puzzle I have not had a chance to address, but the bottom line is I would be spending lots of time unable to work with my lathe just so I could implement a feature I hate in the first place and which provides me no advantage whatsoever.

The bigger win is the ease of refactoring the controller code. Doing all of that in hardware is yuck. if you could abstract it all away then fine, but a MCU doesn't have the resources for that.
Again, to what end? Just because something can be done in no way implies it is of any advantage to do so.

The most niave implementation is you need to insert a calculation deciding if the move calculated in step 2 exceeds the stop and if so tell the controller it is either done or in error.
I m not quite sure what you mean. Simply compare the value of the "desired" lead screw position to the stop point. If the "desired" position is beyond the shoulder, simply update the "desired" position to the stop point and stop sending pulses. I am not sure what you mean by "tell the controller". Theree should not be any error. Any error as far as the software is concerned at this point could only be due to a programming error. Of course, it could be due to one or more missed encoder pulses, but there is no simple way to know that.

This is likely best done with 2 stops, the first is where you start and the 2nd is where you want to stop.
In this context, what do you mean by "start" and "stop"? All that needs to be known or managed is the position of the lead screw when the mode is set and the modulus of the rotation of the spindle, provided the lead screw is in sync.

Since the direction could go wonky you want to be sure you stay between these.
I don't follow you, at all. How can the direction "go wonky". Stay between what?

You can implement these stops as simple integer counters relative to a "start position counter". Be aware that you might get overflows if you use 32bit values since when threading you may leave the spindle running for a while.
That is irrelevant. When the lead screw is stopped, the number of revolutions of the spindle is immaterial. Only the small angle (between 0 and 360 degrees) is of any concern. When it si moving, only the differential between the previous position of the lead screw and the new position is required. A 16 bit counter is much more than enough for the spindle position.

It looks like clough switched to Uint64 because of this10/2020.
I didn't see this. In Encoder.h, it si a Uint32. Where do you see it as 64 bit? It certainly is not necessary.
The problem with this niave approach is that it doesn't account for acceleration/deceleration and you likely don't have any spindle control.
It doesn't really need to. As long as the code can keep up with the spindle position, and all outputs to the motor are implemented during a single encoder step, the system will keep up as well as it possibly can. The error should typically be less than 1 encoder step, or perhaps 5 arc-minutes. That is far more than accurate enough for a hobby shop, or indeed even the vast majority of industrial shops.
The practical thing is that if you use a closed loop stepper it tends to compensate for this naivete quite nicely (at the expense of accuracy).
I am using a servo. Its precision is roughly 11 arc-minutes, or 2.7 arc-minutes after being geared down. Its repeatability is not quite that good, but it is good enough in practical terms. UNC or even UNF (or metric fine) threads are just not all that accurate. One might not wish to cut threads for a micrometer or a high precision optical measuring device, but then such things should not be attempted on an ordinary lathe in the first place.
I'm honestly not really sure of the best way to do this to account for acceleration without spindle control. You'd need a motion controller that could estimate the current distance from the stop and calculate the acceleration curve (based on your settings (based on your hardware)) and as it gets into the range where it would need to decelerate it somehow balances that with the need to keep up with the spindle (which you have no control over).
It doesn't really matter how fast or slow the spindle is turning, per se. All that matters is the lead screw stops turning when the lead screw counter reaches 0x800,000. Whether the encoder pulses are coming in at over 200 Khz or at one pulse per minute, the lead screw needs to stop between the same two pulses. It is the inertia of the lead screw that can be a problem, not really the spindle. Clearly, it is true the accuracy of the stop point is of some concern. How much depends on the situation. Typically, a matter of a few thouandths is not an issue.
This is tough because you must decelerate and maybe mess your thread up or overshoot. Again, in practice this error doesn't seem all that big and in my opnion isn't worth messing with, just use a closed loop stepper which can compensate. If you use an open loop stepper you may loose steps when it tries to stop on a dime and then you could slowly lose position and not be able to repeat theading passes.
Correct.
Before you begin you need to figure out how to tell the controller to stop. Are you using a hardware stop or a software stop. If software how do you tell the controller where the stop is? Are you going to have a 10 digit keypad to enter a value?
'Simple. I have outlined this before. With the ELS engaged and the cross slide retracted, wind the spindle until it is close to the point where the shoulder will be. Stop the spindle and wind the chuck by hand until the DRO, dial indicator, or whatever says the tool is where it needs to be. Press the <Set> button three times. Entering a value requires knowing some value, and it is simply unnecessary. All either the machinist or the controller needs to know i s the carriage is where it needs to stop.

can you change the units?
No particular units are necessary. The position 0 is 0 whether it is in inches, millimeters, parsecs, whatever. Again, don't introduce unnecessary complexity. Of course, the ELS must be able to handle positions on either side of the zero reference, so zero should be represented as 0x8000,0000.

How do you account for thread direction vs spindle direction?
By the LED indicators on the controller. James has already taken care of this. One thing, I want to blink the direction LED when the lead screw is stopped to remind the machinist which way the carriage will move when the <Set> key is pressed again.

this is hairy, when left hand threading with the tool in front you need to move the carriage left if the spindle is moving counter clockwise. if the tool is in the back and you run the spindle in reverse (clockwise) you need to move the carriage right. swap all that if you are cutting a right hand thread.
Yes, of course. So what? This is always the case. No one who does not understand this should be operating a lathe.
How do you enter "stop mode" vs "slave mode"?
I propose pressing the <Set> button three times in quick succession to enter Shoulder Mode. I have gone over this all before.
How do you exit it?
Again, three presses of the <Set> button.
How do you tell all this to the controller since it wasn't designed for this?
I am not sure what you mean by that. It has several buttons, including the one labeled <Set>. This button is not used for anything at this point.

Do you try to implement double button presses, or long presses?
I was thinking triple presses. A single press can start and stop the lead screw, irrespective of the mode. Two quick presses does nothing. Three enters and exits Shoulder Stop.

What about the display?
When the lead screw is stopped, the word "STOP" should be in the right hand display. The left hand should still show RPM. When in Shoulder Stop mode, the text "Shoulder", or some such, should flash on the entire display every few seconds. When stopped (regardless of mode), the direction the carriage will move WRT the spindle when the <Set> key is pressed should flash on the LEDs. In standard mode, this will be the same direction the carriage was moving when the machinist pressed <Set> the last time. In Shoulder Stop mode, the direction will switch.
 
Keep in mind, the Clough UI is a simple 7 segment LED array with momentary switches and LED light array. So you end up with really basic UI like up/down/mode buttons and basic display feedback. You could switch it out to something fancy like a LCD, but I haven't seen the need for it and it would require a fair bit of code.
Code, cost, complexity, possible errors, etc. That is not what I want. 'Not at all.
It sounds like the OP idea is to use the SET button to mark places for the start/end stops. Simple enough in theory. And if you leave everything engaged all the time and count output pulses, it should be reasonably clean.
Exactly. The <Set> button is currently unused. A single button and the existing display is more than enough to implement the ability to thread and turn to a shoulder.
I don't have time to work on it, or I'd offer. I also don't have a spare setup and my control box for the lathe is not easy to get to.
I understand. I have already done a little bit of coding, but not much. C++ gives me vertigo.
As for "dead project" meh, it's not like it really needs much added to it. It does what he designed it to do. I suspect if you contact him with a pull request he'd consider adding it.
I have already done so, and he is quite interested.
 
My effort at quoting failed. I agree with OP on Clough42's encoder. When I cloned his repo in May 2022, it clearly shows (in Encoder.h) the position and the MaxCount are Uint32, not 64 bits. So maybe he has a private repo for the cool kids, ie, patrons? But the public repo shows uint32... Have no idea why it wasn't implemented as a signed integer...

I changed mine to int64_t.
 
edit: sorry wasn't clear, this post is mostly in response to lesrhorer.

I wouldn't expect James to implement any of these kinds of features. He has been very clear since the inception of the project that the ELS is designed to do one thing: replace the gearbox with a stepper driver. He specifically avoided any additional bloat/features like feeding to a stop etc, and he has been clear that he has no intention of adding any "CNC-lite" functions like this. And to that end I think he's right - the ELS does one thing and does it extremely well.

I'm the most amateur of all programmers - it has taken me nearly a year, but I've extended the Clough42 ELS to include a second display/button set which allows feeding to a stop, spindle angle display, jogging and other functions. But it hasn't been easy for me - again, an amateur, and the code is just not well set up for this kind of expansion. I'd love to help with what you want, but honestly it's not realistically possible with the existing display in my view. Plus I simply have no time whatsoever. But I really encourage you to dig into the code - c++ is very learnable. This is something you can do if you really want to.

Here is my incomplete repo - there are some bugs and problems with this specific version and I'll be updating it soon.

 
By definition, I am the worst possible C++ programmer, since I have never even produced a single line of C++ code, so you are way ahead of me.

No, I meant, "Why do you say creating a hardware user interface is begging for pain?" To my eye, a hardware interface is ideal for this sort of thing. A purely hardware interface worked very well for over 150 years. Duplicating that with a digital panel is quite straightforward.

It is not the interface so much as the application. Changing the user interface for a screwdriver, for example, doesn't yield much of an advantage, and in fact may be a really, really bad idea. The interface for a specifically manual lathe - decidedly not a CNC lathe - should be minimalistic and limited by design intent.

That is emphatically not to say creating a more automated machine even up to and including a full blown CNC conversion is a fool's errand. Quite to the contrary, however far one wishes to go in that direction is purely a judgement call based upon one's own desires and available funds. My desire is for a capable, safe, manual lathe, not a programmable one. The ability to thread or turn to a shoulder is a very desirable feature for an otherwise perfectly manual lathe. A single button can readily be employed to enable all the necessary features with no need for any enhancement, as far as I can tell.


I designed a coil winder a couple of years ago. I have not had the time or the money to actually build it, but I really do not want the function to be part of my lathe. I have an extreme distaste for "all in one" devices. I won't get into the long list of reasons why, but suffice to say I don't want to use my lathe for winding coils. If you want to add that functionality to your lathe, then go for it. My needs are far more modest.


Again, that is a great idea if you want it for your own setup. I don't want it for mine. It involves more hardware and software than I am willing or even able to consider at this point, for a purpose for which I have no need.


Yes. A WiFi system is not simple. It is fraught with issues. I would never, EVER even begin to suggest a highly dangerous, potentially fatal machine should have any sort of WiFi interface. I detest WiFi in general, but it is just too flaky for any industrial use.


First of all, that is an assumption. It happens to be true in my case, but it is also true I hate the phone with a seathing passion. It is horrible to the Nth degree. If I move forward with selling the thermostat design I showed you, I suppose I will have to implement a phone based UI, but I do so only under extreme protest.

In addition, it isn't $0. I would have to add a WiFi interface to the ELS board. No thanks.

On an aside, my phone cannot reliably contact any other device on my home network. Why is a puzzle I have not had a chance to address, but the bottom line is I would be spending lots of time unable to work with my lathe just so I could implement a feature I hate in the first place and which provides me no advantage whatsoever.


Again, to what end? Just because something can be done in no way implies it is of any advantage to do so.


I m not quite sure what you mean. Simply compare the value of the "desired" lead screw position to the stop point. If the "desired" position is beyond the shoulder, simply update the "desired" position to the stop point and stop sending pulses. I am not sure what you mean by "tell the controller". Theree should not be any error. Any error as far as the software is concerned at this point could only be due to a programming error. Of course, it could be due to one or more missed encoder pulses, but there is no simple way to know that.


In this context, what do you mean by "start" and "stop"? All that needs to be known or managed is the position of the lead screw when the mode is set and the modulus of the rotation of the spindle, provided the lead screw is in sync.


I don't follow you, at all. How can the direction "go wonky". Stay between what?


That is irrelevant. When the lead screw is stopped, the number of revolutions of the spindle is immaterial. Only the small angle (between 0 and 360 degrees) is of any concern. When it si moving, only the differential between the previous position of the lead screw and the new position is required. A 16 bit counter is much more than enough for the spindle position.


I didn't see this. In Encoder.h, it si a Uint32. Where do you see it as 64 bit? It certainly is not necessary.

It doesn't really need to. As long as the code can keep up with the spindle position, and all outputs to the motor are implemented during a single encoder step, the system will keep up as well as it possibly can. The error should typically be less than 1 encoder step, or perhaps 5 arc-minutes. That is far more than accurate enough for a hobby shop, or indeed even the vast majority of industrial shops.

I am using a servo. Its precision is roughly 11 arc-minutes, or 2.7 arc-minutes after being geared down. Its repeatability is not quite that good, but it is good enough in practical terms. UNC or even UNF (or metric fine) threads are just not all that accurate. One might not wish to cut threads for a micrometer or a high precision optical measuring device, but then such things should not be attempted on an ordinary lathe in the first place.

It doesn't really matter how fast or slow the spindle is turning, per se. All that matters is the lead screw stops turning when the lead screw counter reaches 0x800,000. Whether the encoder pulses are coming in at over 200 Khz or at one pulse per minute, the lead screw needs to stop between the same two pulses. It is the inertia of the lead screw that can be a problem, not really the spindle. Clearly, it is true the accuracy of the stop point is of some concern. How much depends on the situation. Typically, a matter of a few thouandths is not an issue.

Correct.

'Simple. I have outlined this before. With the ELS engaged and the cross slide retracted, wind the spindle until it is close to the point where the shoulder will be. Stop the spindle and wind the chuck by hand until the DRO, dial indicator, or whatever says the tool is where it needs to be. Press the <Set> button three times. Entering a value requires knowing some value, and it is simply unnecessary. All either the machinist or the controller needs to know i s the carriage is where it needs to stop.


No particular units are necessary. The position 0 is 0 whether it is in inches, millimeters, parsecs, whatever. Again, don't introduce unnecessary complexity. Of course, the ELS must be able to handle positions on either side of the zero reference, so zero should be represented as 0x8000,0000.


By the LED indicators on the controller. James has already taken care of this. One thing, I want to blink the direction LED when the lead screw is stopped to remind the machinist which way the carriage will move when the <Set> key is pressed again.


Yes, of course. So what? This is always the case. No one who does not understand this should be operating a lathe.

I propose pressing the <Set> button three times in quick succession to enter Shoulder Mode. I have gone over this all before.

Again, three presses of the <Set> button.

I am not sure what you mean by that. It has several buttons, including the one labeled <Set>. This button is not used for anything at this point.


I was thinking triple presses. A single press can start and stop the lead screw, irrespective of the mode. Two quick presses does nothing. Three enters and exits Shoulder Stop.


When the lead screw is stopped, the word "STOP" should be in the right hand display. The left hand should still show RPM. When in Shoulder Stop mode, the text "Shoulder", or some such, should flash on the entire display every few seconds. When stopped (regardless of mode), the direction the carriage will move WRT the spindle when the <Set> key is pressed should flash on the LEDs. In standard mode, this will be the same direction the carriage was moving when the machinist pressed <Set> the last time. In Shoulder Stop mode, the direction will switch.

I think you are underestimating the effort involved here and I don't think anyone is going to try to write this for you the way you want. Not sure what else to say but good luck.
 
Theree should not be any error. Any error as far as the software is concerned at this point could only be due to a programming error. Of course, it could be due to one or more missed encoder pulses, but there is no simple way to know that.
also, this is all about errors. You can't do this with no errors except for a few combinations of leadscrew pitch, encoder resolution, and thread pitch.

If you decide to try to write this into Clought42's code you should familiarize yourself with this: https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
 
Back
Top