# ELS Project - spindle position read-out



## Wheat.Millington (Nov 27, 2021)

I'm a complete beginner machinist - I've wanted to learn home machining for some time, so finally bought myself an import lathe. Because it doesnt have a quick change gearbox I decided before buying it I wanted to do Clough42's electronic lead screw. I've done the board, encoder and display, but waiting on my bank balance to recover before getting a stepper/controller/power supply. So in the mean time I dug into the code and set up an option to show me the absolute spindle position. I don't know how much utility this really has, but it was a fun little project.

One thing I haven't figured out is a spindle brake, which would increase the usefulness of this set up. However for basic jobs, as shown, it's enough on my lathe to stick it in low gear to hold the spindle steady-ish.


----------



## GunsOfNavarone (Nov 27, 2021)

I have a g0602 with Clough's ELS. When you say "spindle brake", are you referring to the resistor braking of VFD or a mechanical brake?


----------



## brino (Nov 27, 2021)

Interesting and complicated first project.

Welcome to the group!

-brino


----------



## Wheat.Millington (Nov 27, 2021)

GunsOfNavarone said:


> I have a g0602 with Clough's ELS. When you say "spindle brake", are you referring to the resistor braking of VFD or a mechanical brake?


My lathe doesn't have a VFD, it uses a mechanical gearbox. There is no braking system on my lathe at all, but it takes pretty reasonable pressure to move the spindle by hand in the lowest gear (90rpm). I'd like to implement some kind of mechanical brake directly on the spindle at some stage.


----------



## RJSakowski (Nov 27, 2021)

The spindle position would be useful for CNC type functions.  You have already committed to the Clough42 system but another approach  would be to know the position of the spindle relative to the carriage position.  As I understand it, Clough's approach is to keep track of the encoder counts from the beginning and sends appropriate pulses to the stepper motor to keep in sync.  This requires a fair amount processing power. to handle all the calculations in real time.

Aside from that, I'm not sure what advantage would be gained by knowing the spindle position.


----------



## Wheat.Millington (Nov 27, 2021)

RJSakowski said:


> The spindle position would be useful for CNC type functions.  You have already committed to the Clough42 system but another approach  would be to know the position of the spindle relative to the carriage position.  As I understand it, Clough's approach is to keep track of the encoder counts from the beginning and sends appropriate pulses to the stepper motor to keep in sync.  This requires a fair amount processing power. to handle all the calculations in real time.
> 
> Aside from that, I'm not sure what advantage would be gained by knowing the spindle position.


It's more of an experiement/familiarisation project than anything particularly useful, but like I showed you can do thinks like index markings without additional hardware. I'm really not sure what else it would be useful for. Maybe nothing, to be honest. Fun though.

I should mention that I've already decided to add an additional Clough42 board to my set up for a separate control box, and I'll add some additional features. For example I'd like to add soft stops, jogging, maybe the ability to automatically thread (advance, pause, retract, repeat). I'm a beginner machinist and the most amateur of coders, so it's fun for me to learn this stuff. I've never had the opportunity to do any coding that had real-world (physical) results so this is fun to me.

Addition of a second Clough42 board would also give me access to a a second encoder input and stepper output, so may consider another axis of control, not too sure.


----------



## jeffkash (Dec 2, 2021)

Buddy and I are doing something similar. He for his Grizzly Gunsmith lathe and me for my LMS 8.5x20. We have added a 2nd Clough board and are controlling the x axis. Intending on automating turning, threading, and tapering. Added a 7" Nextion display for the HMI.


----------



## xr650rRider (Dec 2, 2021)

I can see the advantage.  Doing exactly what you show, making index marks on parts, indexing a rifle barrel, using a drill motor in toolpost and marking/drilling around a part.  Good idea.

You sharing code?


----------



## jeffkash (Dec 2, 2021)

xr650rRider said:


> I can see the advantage.  Doing exactly what you show, making index marks on parts, indexing a rifle barrel, using a drill motor in toolpost and marking/drilling around a part.  Good idea.
> 
> You sharing code?


Our focus is in automating functions, so spindle position is incidental to what we are doing. Our code is very much a work in progress and we were not planning on making it available as we don't want to be responsible for supporting it. We are retired programmers and know what support entails. That said, at some point we will describe what we did and how we did it.


----------



## xr650rRider (Dec 2, 2021)

I was actually addressing the original poster, I'm only interested in the spindle position readout.


----------



## Wheat.Millington (Dec 2, 2021)

I'm more than happy to share my code, just don't scrutinise it too closely because my coding skills are... not great. I'll update my github when I get home and share it then.


----------



## Wheat.Millington (Dec 2, 2021)

jeffkash said:


> Buddy and I are doing something similar. He for his Grizzly Gunsmith lathe and me for my LMS 8.5x20. We have added a 2nd Clough board and are controlling the x axis. Intending on automating turning, threading, and tapering. Added a 7" Nextion display for the HMI.


Very interested to hear how you get on with this. What are you doing for interface? Using another LED&KEY board, or something more sophisticated?


----------



## jeffkash (Dec 2, 2021)

Wheat.Millington said:


> Very interested to hear how you get on with this. What are you doing for interface? Using another LED&KEY board, or something more sophisticated?


Using two Clough42 interface boards and a 7" Nextion touchscreen LCD display. No LED boards. It is based on Clough42's code as modified by Kent VenderV
	

		
			
		

		
	



	

		
			
		

		
	
elden. We added the 2nd Clough42 interface for the X axis stepper. The display uses a serial port. The photo shows a version of the main screen.


----------



## Wheat.Millington (Dec 2, 2021)

That's awesome, quite a bit further than I was planning to go, but great inspiration.


----------



## GunsOfNavarone (Dec 4, 2021)

I have thought about moving forward and doing X and Z, but I would never want to lose the ability to run manually. E.G. I'd like CNC but still have manual control. I'm assuming Clough's code was never meant to be converted to CNC control?


----------



## jeffkash (Dec 4, 2021)

GunsOfNavarone said:


> I have thought about moving forward and doing X and Z, but I would never want to lose the ability to run manually. E.G. I'd like CNC but still have manual control. I'm assuming Clough's code was never meant to be converted to CNC control?


No, Clough's code was not meant to support CNC, in fact he doesn't support X or automated operations. Manual Z just means not engaging the leadscrew. Manual X is just disabling the x stepper or servo. We are not planning on full CNC but with motors on both Z and X it would be easy to add a CNC controller for G Code.


----------



## GunsOfNavarone (Dec 4, 2021)

Understood, but if you did add the Z/X to your platform, would you lose manual control?


----------



## jeffkash (Dec 4, 2021)

GunsOfNavarone said:


> Understood, but if you did add the Z/X to your platform, would you lose manual control?


No, the handwheels are still present. Manual Z just means not engaging the leadscrew. Manual X is just disabling the x stepper or servo. We made provisions for individually disabling the motors for manual operation. So either manual or automated is readily available.


----------



## Wheat.Millington (Dec 8, 2021)

Wheat.Millington said:


> I'm more than happy to share my code, just don't scrutinise it too closely because my coding skills are... not great. I'll update my github when I get home and share it then.


Regarding this, I had a hard drive die and I think it took my source code with it :'(

I'm going to have to re-write it because I had some improvements in mind... ugh.


----------



## Wheat.Millington (Dec 9, 2021)

OK I have rewritten the code and uploaded it to Github, now merged to the master branch here:








						GitHub - WheatMillington/electronic-leadscrew: Lathe electronic leadscrew controller
					

Lathe electronic leadscrew controller. Contribute to WheatMillington/electronic-leadscrew development by creating an account on GitHub.




					github.com
				




This compiled just fine on the latest version of CCS.


----------



## Just for fun (Dec 9, 2021)

That's pretty cool.  I looked at your info just for kicks and grins.  It wasn't exactly Greek but pretty dam near it.  Years ago, I used to play around on an Apple IIe writing some programs.  It was just for the fun of it and well girls and motorcycles took presidents and computer programing didn't only take the back burner, but it completely fell off the stove.

Welcome to the site!  I'm pretty new here also and I don't even have any equipment yet.  Although it is on the way, currently on a ship somewhere between Tiawan and the USA.

Looking forward to seeing your setup up and running!

Tim


----------



## xr650rRider (Dec 10, 2021)

Wheat.Millington said:


> OK I have rewritten the code and uploaded it to Github, now merged to the master branch here:
> 
> 
> 
> ...



Good job, code works. I'm still using CSS 9.3 and made changes in my source.  I might not ever use it but nice to know it's there if I need to.  One thing was I had to change back to Encoder_Max_Count 0x00ffffff or my servo got very jumpy as I was turning spindle.  

 Original  

#define _ENCODER_MAX_COUNT 0x00ffffffYour code
   #define _ENCODER_MAX_COUNT 0xffffffff


----------



## Wheat.Millington (Dec 11, 2021)

xr650rRider said:


> Good job, code works. I'm still using CSS 9.3 and made changes in my source.  I might not ever use it but nice to know it's there if I need to.  One thing was I had to change back to Encoder_Max_Count 0x00ffffff or my servo got very jumpy as I was turning spindle.
> 
> Original
> 
> ...



Thanks - I wondered why James' original code used a value lower than a full 32 bit uint. Honestly I don't understand why this causes an issue, but the only reason I extended it to 0xfffffff was so I didn't have to manage under/overflows lol. No problem, I'll revert that to its original value and introduce some under-overflow management to the spindle position.


----------



## Wheat.Millington (Dec 11, 2021)

Just for fun said:


> That's pretty cool.  I looked at your info just for kicks and grins.  It wasn't exactly Greek but pretty dam near it.  Years ago, I used to play around on an Apple IIe writing some programs.  It was just for the fun of it and well girls and motorcycles took presidents and computer programing didn't only take the back burner, but it completely fell off the stove.
> 
> Welcome to the site!  I'm pretty new here also and I don't even have any equipment yet.  Although it is on the way, currently on a ship somewhere between Tiawan and the USA.
> 
> ...


Cheers, I'm definitely not a coder by any stretch but I have a pretty big advantage - my brother is a software engineer so I've got help whenever I need it. I learned BASIC when I was 7 or 8, and still use it to this day writing macros in excel for my work. That's the extent of my programming experience.


----------



## xr650rRider (Dec 11, 2021)

Wheat.Millington said:


> Thanks - I wondered why James' original code used a value lower than a full 32 bit uint. Honestly I don't understand why this causes an issue, but the only reason I extended it to 0xfffffff was so I didn't have to manage under/overflows lol. No problem, I'll revert that to its original value and introduce some under-overflow management to the spindle position.



I don't see why it would cause an issue either but changing it back to original, definitely calmed the servo down to where it was.   There must be something overflowing but I don't see it as I was running lathe for an hour and cutting an external thread.  I pushed the set button and it displayed position but then had a value of 567.2 (might not be exact number but definitely bigger than 360) and would not update any longer turning spindle until I powered down and reset.  RPM was still working as normal.


----------



## Wheat.Millington (Dec 11, 2021)

Appreciate the help, I'll keep chipping away at it and try to squash those little bugs.


----------



## Wheat.Millington (Dec 13, 2021)

I have updated my code with better overflow/underflow handing and cleaner, smarter arithmetic. I won't have time to properly test it until next weekend, but feel free to download it off my github and give it a go.


----------



## xr650rRider (Dec 13, 2021)

I downloaded and it definitely looks like it fixed the overflowing issue.  I ran the lathe for 30 minutes or more and position still worked.  It does start dropping a few degrees the longer it runs.  For instance, you start out with spindle at 0 degrees and after running at 1000 rpm for a while, same spot might be 357 degrees, then later, 354 degrees.  For intended purpose of making index marks on a part, not a big deal.  I haven't dug too deep into code but I would have thought there would have been a fairly repeatable position coming in from the encoder.  The encoder can't really lose position, unless the belt slips.   

Another useful feature might be to power up and initialize spindle position as 0 degrees on startup.  Mine will read 0 after code flash, but reads 225.2 on power up any other time.  Maybe even place spindle where you want to be 0 degrees, then pressing "Set" and going into position mode, sets it to 0.


----------



## Wheat.Millington (Dec 13, 2021)

Loving the feedback mate, really helpful. I've got a busy household with 2 kids under 5, and a garage attached to the house, so my time in front of the lathe is limited. Having someone else doing the testing is working well for me!

I suspect I know the source of the creeping - the encoder has (in my case) 8,192 positions, counting 0 to 8,191. I don't ahve my code in front of me, but I suspect I've got it set up to wrap around at 8,192 instead of 8,191, so it's probably out by 1/8192 per rotation (which adds up if you spin the lathe).

Regarding the zeroing, my plan is still to incorporate a second clough42 board with a second breakout display, because I want to include some new functionality. I planned on having a zero button on that second display. However, it would be straight forward enough to have it reset to zero when each time Position is selected.

Interesting bug with it initialising to 225.2, no idea off the top of my head why this would be. I'll check it out.

Thanks!


----------



## xr650rRider (Dec 13, 2021)

I love someone adding value added features.  Beats having a 100 tooth saw blade and metal pointer hanging off back of lathe for indexing.  My encoder resolution = 4096.


----------



## Wheat.Millington (Dec 14, 2021)

Fixed the issue of the creeping error when the lathe spins, and fixed the bug of it starting at the wrong value.

edit: I just realised that my logic for avoiding under/overflow makes no sense, which happens when I do my coding last thing at night :/ It works at the moment but there's scope for errors, just so you know. Important to note that I don't see any reason why this function could affect any functionality outside of the spindle position counter, so there's no safety issue with this.


----------



## xr650rRider (Dec 14, 2021)

I made the pCount=0 change and that initialized spindle to 0 position on startup.  I commented out the code below out and had no problems.  Ran lathe for > 1-1/2 hrs.  Position was still right on, no over-runs.  One thing I have noticed, is between 360 and 1 degree, it doesn't display a decimal but only 1-9, until you hit 1.0 or 360.0 going reverse.

//   // Manage overflow - as count approaches maximum uint32 value, deduct 0x000fffff
//   if ( pcurrent > 0x0fffffff ) {
//       pcurrent -= 0x000fffff;
//       pprevious -= 0x000fffff;
//   }
//
//    // Manage underflow - as count approaches zero, add 0x000fffff
//    if ( pcurrent < 0x0000ffff ) {
//        pcurrent += 0x000fffff;
//        pprevious += 0x000fffff;
//    }


----------



## Wheat.Millington (Dec 20, 2021)

I've made a complete change to the logic to calculate the position and it's now much simpler and handles under/overflow without a problem (hopefully). Latest version of my code is on my github. 

I also took the opportunity to add 0. in front of numbers below 1deg.


----------



## RJSakowski (Dec 20, 2021)

Wheat.Millington said:


> I've made a complete change to the logic to calculate the position and it's now much simpler and handles under/overflow without a problem (hopefully). Latest version of my code is on my github.
> 
> I also took the opportunity to add 0. in front of numbers below 1deg.



It appears that you are counting the total pulses number of pulses, correcting for under/overflow to determine spindle position?  The Omron E6B2-C encoders have a Z phase output which occurs at an origin position.  Can you not use that output to determine spindle position by resetting a counter and counting CE/CCW pulses?  The counter would be reset with each pass of the origin  so the total count would never exceed the 4K/rev.


----------



## Wheat.Millington (Dec 20, 2021)

The original Clough42 code uses pulse counts to calculate stepper motor pulses for a given feed. The way he has implemented this gave no consideration to what I wanted to achieve. As such, James' code doesn't use the index pulse to reset the pulse count register, rather his code resets at an arbitrarily large integer. This in itself isn't a problem, you can easily change one flag to have the counter reset of the index pulse. However, because this was not anticipated in James' code, it won't play nice because of his implementation of under/overflow management for stepper control.

It wouldn't be that big a deal to rewrite his code, but given my much lower expertise than James, I'd rather leave his reliable code alone and work around it in the way I have.


----------



## xr650rRider (Dec 21, 2021)

I've been running with your changes for last few days, seems to work well.


----------



## Wheat.Millington (Dec 21, 2021)

Can't express enough how grateful I am to have someone testing this out for me, thanks!


----------



## xr650rRider (Dec 30, 2021)

I've used it several times in last couple of days.  I had a radial muzzle brake that doesn't have ports on bottom 120 degrees.  Put an electronic level on bottom of recoil lug and rotated until it read 180 (it was upside down), with backlash taken up out of gear train, powered ELS down then back up and now position is 0 with lug straight down.  Screw on brake and then simple matter to use position function to figure exact number of degrees brake needs to rotate to be clocked correctly.

Then I made a couple reamer stops and easy to mark the 40 tpi locks while they are still in lathe.

Sure is nice that the function is there and nothing hanging off of lathe.


----------

