Hi all, as the title suggests I have a small project I am unsure of how to achieve. Ill post what is required first and what its for last, in case anybody is interested. This could help others in a similar situation which is why I am asking publicly.
Using an Arduino nano as I have a few of them.
I need to output a set number of pulses (approx 40000) at a variable frequency (approx 100-2000Hz) set with a potentiometer, along with a pin that goes logic high. Small pause (200ms) then the exact number of pulses again, except at a higher non variable frequency (probably 10KHz) with the logic pin low. The second fixed frequency could be higher than 10KHz but it would have to start low and increase, then decrease again at the end.
That's about it, except a push button to start the cycle as it will be used a lot. As the title suggests I'm a complete noob to programming, I've been playing around with doing this using PWM but I don't think that is the correct way to achieve it.
I don't expect anybody to do the whole code for me, I certainly wouldn't complain if someone did, but maybe point me in the direction of how to go about it.
The project is that I have a friend that is doing a gear cutting job on his milling machine and needs an axis to slowly move along with a small fragile rotary cutter attached, take a small cut, move back then index manually to the next position...once the first depth is done the cutter is lowered and the process is repeated...over and over.
His mill had a full CNC controller in the past that has since died and I am helping him with that but that will take 6 months or so, and he would like this job done a lot sooner.
I have built CNC servo drives that will take step and direction so the idea is to plug a micro into one and jury rig it onto the milling machine for now. If the frequency is fairly low we could probably get away without acceleration and deceleration profiles as the servo drive will take up the slack, any more than 10KHz and I think bad things may happen.
The use for this for other people would be any single axis applications, camera slides for instance, possibly some simple process applications, that sort of stuff.
Thanks in advance.
Russell.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Actually in the first sentence of the third paragraph I mentioned outputting a set number of pulses, I did say approx 40000 in brackets meaning it may be slightly more or less. This will be directly translated into motion by a CNC servo drive, it has to be an exact number, with the same number in the reverse direction (logic pin low will sort that).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As I understand ,you want to replace one axis of the cnc mill with a dedicated micro. The mill has step and direction inputs similar to stepper motor drivers on the servos that drive the axis.
First you don't need PWM because the step and dir servos need short pulses and you need to count them to know where you are.
40000 is not the problem because you can use a word variable and go to 65535 or long variable and go to 4.2 billion. You will need to have a way to put the parameters in like a buttons and LCD?
The speed pot can go into an analog input to set the speed of the cut.
The ramping the speed for Acceleration and deceleration is trickier on the "return stroke" at 10khz
and you want to be at the start position ready to stroke.
"pin that goes logic high" is you direction pin and that is no problem.
Attached is an old program I made in 2015 for running step and dir on a dedicated axis that had 5 positions. You would press a button and it would go to the next position. Something for you to ponder. no accel/decel.
Finally I am wondering why you would limp when you could run? Have you looked at "grbl" which is a arduino CNC program that takes gcode and runs step and dir drivers. grbl runs on a 2560 arduino and there are programs to run on a PC that interface with it and you.
I play with stepper motors but don't use pwm... just change the step phases speed.
Probably doing it all wrong but work. I thought as a hobbyist pwm was more for dc brushed motors via a h-bridge
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Cheers Mike, I had a quick look at your code before leaving for work, and I'm pretty sure it would work almost as is, the value of 40000 was just an estimate but doesn't need to be adjusted a great deal if at all, the cutter simply has to pass past the job and clear it before heading back ready for the next cut. I figured the servo is 4000count per rev, and 10 revolutions gave 50mm of travel with a 5mm pitch ballscrew. If the frequency is low (sub 10Khz) it probably shouldn't need acceleration/deceleration.
I had tried the pwm object and it would have worked if I could have used a word for the cycles value, 255 in that field worked but the test motor only rotated about 1/8th of a turn.
I know of GRBL and that is/was the plan if it got too hard to quickly code something, I was just trying to keep it simple without the need for a PC near the mill to do a one off but insanely repetitive job. Thanks for the code, while not exactly what I had in mind it is close enough for me to get the job done.
Hi Stan, yes PWM isn't generally used for this but at the end of the day it is still just a stream of pulses, adjusting the frequency of which isn't really the norm but for step based motion control it is useful for kludging things together/testing motor drives.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Russell,
I think you realize that you can change the processor just by changing the #chip statement. I have no experience with nano's , but it should run the program just fine.
You really don't need all of the program. You have a good hold on the range of movement and can hard code those in.
The speed could be set using a pot like you mentioned before and two buttons could be up and down, the two positions. Or like you mention just put a wait in and then send it back.
Don't forget that GCB has a scale statement for ranging the pot to the wait xx us .
my_newvalue=scale(ReadAD10(An0),0,1023,10000,500)
the variable wait would replace the "wait 25 ms" with "wait my_newvalue us" .
the 25 ms was because I was going really slow
Also the pulseOut probably can be cut down to several us depending on your driver parameters for "step" pulse size. Some drivers want inverted pulses and that is available through PulseOutInv statement.
Hi Russell,
I wondered how you went with this project? I have a project that is more or less the same as yours. Doing code for the steppers is not much of an issue, it's the Acceleration and Deceleration that are more complicated.
I looked at Accelstepper and that looks like it would do what I want (and a lot more), but that's in C and while I have done a bit of C > Basic, I think that one is probably beyond my current abilities, or at least more time involved than I'm able to put into it. It's a pity, it would be a great addition to GCB. I don't know if there's any sort of Interface and Linker that could be used with GCB to allow C libraries like this. I imagine they could be linked in the backend, like OBJ files? Evan and Hugh would obviously know a lot more about how difficult that might be.
So how did you go with your project?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Actually I should have looked more than I did. The other night it was late and I only glanced at the files and saw the size of it and immediately thought it would be too big for me to tackle. Now that I've dug into the files a little, I realise a lot of that size was just comments and it doesn't look quite so bad, but I'd have to look into it further to see if it's something I might be able to tackle. Would GCB's Floats likely work at a similar speed to his cpp Floats?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Sleepwalker, I ended up retrofitting the machine with full CNC control, it seemed to be a good idea to have some handy features like the big red estop button, limit switches etc since we were cutting chunks out of steel bar.
This project would still be a useful one for single axis positioning jobs that don't need a great deal of extra features. I'll follow your progress, unfortunately lack of time and programming skills means I won't be of much assistance for a while.
Cheers.
Russell.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I only tried https://os.mbed.com/cookbook/4-Phase-Step-Motor and that was 4 phase.
Seemed I used an interrupt as they work best at 100 hz.
not real stepper motors like in my 3 d printer. rubbish really.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Stan those 28BYJ-48 steppers are fun to play around with, not a lot of power but the built in gearbox gives them a bit of torque, they are used commercially in air conditioners to move the fins about.
There are a lot of cheap stepper drives that will do the motor stuff for you and all you need to do is give them step/direction pulses.
There are usually two H bridges in a bipolar stepper drive, If you have a unipolar stepper like the one you mentioned you can get away with 4 power units, not really a h bridge as such, they just switch the coils to ground in a sequence to create rotation.
There is a lot of info on steppers and their control, interesting subject.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you use a timer0 interrupt every 20 ms then a counter in the isr to test when to drive the stepper through 1 phase, you could change the speed by changing the counter max before it resets. A sort of do every.
edit I got 2 3dprinters that run from a 328p surface mount. It controls 2 height steppers, heating nozzle temp and left/right and the bed move motor stepper. Good for a mega328
Last edit: stan cartwright 2022-05-07
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all, as the title suggests I have a small project I am unsure of how to achieve. Ill post what is required first and what its for last, in case anybody is interested. This could help others in a similar situation which is why I am asking publicly.
Using an Arduino nano as I have a few of them.
I need to output a set number of pulses (approx 40000) at a variable frequency (approx 100-2000Hz) set with a potentiometer, along with a pin that goes logic high. Small pause (200ms) then the exact number of pulses again, except at a higher non variable frequency (probably 10KHz) with the logic pin low. The second fixed frequency could be higher than 10KHz but it would have to start low and increase, then decrease again at the end.
That's about it, except a push button to start the cycle as it will be used a lot. As the title suggests I'm a complete noob to programming, I've been playing around with doing this using PWM but I don't think that is the correct way to achieve it.
I don't expect anybody to do the whole code for me, I certainly wouldn't complain if someone did, but maybe point me in the direction of how to go about it.
The project is that I have a friend that is doing a gear cutting job on his milling machine and needs an axis to slowly move along with a small fragile rotary cutter attached, take a small cut, move back then index manually to the next position...once the first depth is done the cutter is lowered and the process is repeated...over and over.
His mill had a full CNC controller in the past that has since died and I am helping him with that but that will take 6 months or so, and he would like this job done a lot sooner.
I have built CNC servo drives that will take step and direction so the idea is to plug a micro into one and jury rig it onto the milling machine for now. If the frequency is fairly low we could probably get away without acceleration and deceleration profiles as the servo drive will take up the slack, any more than 10KHz and I think bad things may happen.
The use for this for other people would be any single axis applications, camera slides for instance, possibly some simple process applications, that sort of stuff.
Thanks in advance.
Russell.
This is a PWM project.
See PWM Software Mode in the Help.
Have you looked at the Help?
Yup, didn't see where I could count any more than 255 pulses, I need 40000. I'll go and look again.
Thanks for the assistance.
@Russell. In the first post you wanted to output the pulse, now, count the pulses.
These are very different things.
Can you clarify?
Actually in the first sentence of the third paragraph I mentioned outputting a set number of pulses, I did say approx 40000 in brackets meaning it may be slightly more or less. This will be directly translated into motion by a CNC servo drive, it has to be an exact number, with the same number in the reverse direction (logic pin low will sort that).
As I understand ,you want to replace one axis of the cnc mill with a dedicated micro. The mill has step and direction inputs similar to stepper motor drivers on the servos that drive the axis.
First you don't need PWM because the step and dir servos need short pulses and you need to count them to know where you are.
40000 is not the problem because you can use a word variable and go to 65535 or long variable and go to 4.2 billion. You will need to have a way to put the parameters in like a buttons and LCD?
The speed pot can go into an analog input to set the speed of the cut.
The ramping the speed for Acceleration and deceleration is trickier on the "return stroke" at 10khz
and you want to be at the start position ready to stroke.
"pin that goes logic high" is you direction pin and that is no problem.
Attached is an old program I made in 2015 for running step and dir on a dedicated axis that had 5 positions. You would press a button and it would go to the next position. Something for you to ponder. no accel/decel.
Finally I am wondering why you would limp when you could run? Have you looked at "grbl" which is a arduino CNC program that takes gcode and runs step and dir drivers. grbl runs on a 2560 arduino and there are programs to run on a PC that interface with it and you.
GL
Mike w9ys
I play with stepper motors but don't use pwm... just change the step phases speed.
Probably doing it all wrong but work. I thought as a hobbyist pwm was more for dc brushed motors via a h-bridge
Cheers Mike, I had a quick look at your code before leaving for work, and I'm pretty sure it would work almost as is, the value of 40000 was just an estimate but doesn't need to be adjusted a great deal if at all, the cutter simply has to pass past the job and clear it before heading back ready for the next cut. I figured the servo is 4000count per rev, and 10 revolutions gave 50mm of travel with a 5mm pitch ballscrew. If the frequency is low (sub 10Khz) it probably shouldn't need acceleration/deceleration.
I had tried the pwm object and it would have worked if I could have used a word for the cycles value, 255 in that field worked but the test motor only rotated about 1/8th of a turn.
I know of GRBL and that is/was the plan if it got too hard to quickly code something, I was just trying to keep it simple without the need for a PC near the mill to do a one off but insanely repetitive job. Thanks for the code, while not exactly what I had in mind it is close enough for me to get the job done.
Hi Stan, yes PWM isn't generally used for this but at the end of the day it is still just a stream of pulses, adjusting the frequency of which isn't really the norm but for step based motion control it is useful for kludging things together/testing motor drives.
Russell,
I think you realize that you can change the processor just by changing the #chip statement. I have no experience with nano's , but it should run the program just fine.
You really don't need all of the program. You have a good hold on the range of movement and can hard code those in.
The speed could be set using a pot like you mentioned before and two buttons could be up and down, the two positions. Or like you mention just put a wait in and then send it back.
Don't forget that GCB has a scale statement for ranging the pot to the wait xx us .
the variable wait would replace the "wait 25 ms" with "wait my_newvalue us" .
the 25 ms was because I was going really slow
Also the pulseOut probably can be cut down to several us depending on your driver parameters for "step" pulse size. Some drivers want inverted pulses and that is available through PulseOutInv statement.
GL
Mike
Thanks again Mike, much appreciated.
I plan to have a crack at it on the weekend, will let you know how it goes.
Russell.
Hi Russell,
I wondered how you went with this project? I have a project that is more or less the same as yours. Doing code for the steppers is not much of an issue, it's the Acceleration and Deceleration that are more complicated.
I looked at Accelstepper and that looks like it would do what I want (and a lot more), but that's in C and while I have done a bit of C > Basic, I think that one is probably beyond my current abilities, or at least more time involved than I'm able to put into it. It's a pity, it would be a great addition to GCB. I don't know if there's any sort of Interface and Linker that could be used with GCB to allow C libraries like this. I imagine they could be linked in the backend, like OBJ files? Evan and Hugh would obviously know a lot more about how difficult that might be.
So how did you go with your project?
Does any have the source to the Accelstepper ? It cannot to be to hard to port.
Hi Evan,
Thanks for taking the time to post. Accelstepper is here http://www.airspayce.com/mikem/arduino/AccelStepper/files.html and then look under the 'Classes' and 'Files' tabs up the top of the main page.
Cheers
Sleeps
Thank you. To port you would need the .CPP files not the .H
Oops! Files are here sorry - http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.61.zip
Actually I should have looked more than I did. The other night it was late and I only glanced at the files and saw the size of it and immediately thought it would be too big for me to tackle. Now that I've dug into the files a little, I realise a lot of that size was just comments and it doesn't look quite so bad, but I'd have to look into it further to see if it's something I might be able to tackle. Would GCB's Floats likely work at a similar speed to his cpp Floats?
Good stuff.
The floats can be converted to factored integers. Worry about that when you port.
Hi Sleepwalker, I ended up retrofitting the machine with full CNC control, it seemed to be a good idea to have some handy features like the big red estop button, limit switches etc since we were cutting chunks out of steel bar.
This project would still be a useful one for single axis positioning jobs that don't need a great deal of extra features. I'll follow your progress, unfortunately lack of time and programming skills means I won't be of much assistance for a while.
Cheers.
Russell.
I only tried https://os.mbed.com/cookbook/4-Phase-Step-Motor and that was 4 phase.
Seemed I used an interrupt as they work best at 100 hz.
not real stepper motors like in my 3 d printer. rubbish really.
Well h-bridge is used for stepper motors.,it would seem.
Stepper motors just need a bit of studying cos they are just jumping between coils and magnets,
I got a printer for scrap and will strip it first.
Hi Stan those 28BYJ-48 steppers are fun to play around with, not a lot of power but the built in gearbox gives them a bit of torque, they are used commercially in air conditioners to move the fins about.
There are a lot of cheap stepper drives that will do the motor stuff for you and all you need to do is give them step/direction pulses.
There are usually two H bridges in a bipolar stepper drive, If you have a unipolar stepper like the one you mentioned you can get away with 4 power units, not really a h bridge as such, they just switch the coils to ground in a sequence to create rotation.
There is a lot of info on steppers and their control, interesting subject.
If you use a timer0 interrupt every 20 ms then a counter in the isr to test when to drive the stepper through 1 phase, you could change the speed by changing the counter max before it resets. A sort of do every.
edit I got 2 3dprinters that run from a 328p surface mount. It controls 2 height steppers, heating nozzle temp and left/right and the bed move motor stepper. Good for a mega328
Last edit: stan cartwright 2022-05-07