Spiral Subroutine
- Larry
- Offline
- Junior Member
Less
More
- Posts: 27
- Thank you received: 13
11 Oct 2016 04:36 - 11 Oct 2016 04:41 #81538
by Larry
Spiral Subroutine was created by Larry
The following code is an example of the enormous power of recursive programming. Inspired by the example program, “arcspiral.ngc”, which ships with linuxcnc, I decided to create my own version using two subroutines and my style of programming guide lines. While arcspiral.ngc uses over 1000 lines of code, my program uses just 42. In addition, my program creates two spirals instead of one. From inspection of the code, arcspiral.ngc was evidentially created by a drawing to g code program. This is okay, but when you are optimizing for speed or size, hand coding is often the better way to go. Employing these guide lines helps me make code that is readable even months after it was created. Although I am relatively new to this forum, I have been using this suite of programs since it was identified as EMC2.
Here are the guide lines I use:
1.) Parameters #1 - #30 are reserved only for internal subroutine use. In addition, in the rare event that I need a function instead of a subroutine, I reserve #31 for a function return value.
2.) Unlike other computer languages such as C, as of EMC2, the NGC interpreter supports neither forward references, or forward declarations. In simple terms, I put my subroutines at the top of my code ahead of any calls to them. In addition, if a subroutine makes calls to another subroutine, I always code the called subroutine ahead of the calling one.
3.) At the beginning of the subroutine I always place a comment naming the subroutine along with a list of arguments used by the call command.
4.) I use indentation between the opening and closing blocks of, “O”, codes. Although this is not required, the NGC code starts looking a lot like Python. One of the reasons Python is so popular is it's much easier to read. No offense to TCL/ WISH programmers, I also like that language.
5.) Regarding, “O”, codes, I always use a lower case o instead of upper case O. This makes it much less likely to mistake it for a zero.
In the program, the first subroutine is called helix. This code produces just one 365 degree revolution of the helix. The second subroutine called spiral, recursively calls helix to control the size of the shape. This is an example of program stacking. You hear these terms used in instances such as USB stack, or in the case of the Internet TCP/IP stack. These programs are designed fit one on top of another. This type of approach greatly simplifies the coding process as well as making this stack of programs easier to maintain. As a last example of recursion, lines 35 to 39 reposition the coordinate system to a point 2.5 x and 2.5 y units offset from true zero. The spiral is repeated again. I have seen so many coders go to extreme complexity to build repositioning directly into their code. All you have to do is create your shape to generate at the origin. If you want to move it all you have to do is use the codes g92 and g92.1. Finally, lines 40 to 42 reset the x and y offsets to zero and end the program.
If there is enough interest, I will finish coding this into a full fledged circular pocket subroutine that you can call from your own programs. Let me know what you think.
Here are the guide lines I use:
1.) Parameters #1 - #30 are reserved only for internal subroutine use. In addition, in the rare event that I need a function instead of a subroutine, I reserve #31 for a function return value.
2.) Unlike other computer languages such as C, as of EMC2, the NGC interpreter supports neither forward references, or forward declarations. In simple terms, I put my subroutines at the top of my code ahead of any calls to them. In addition, if a subroutine makes calls to another subroutine, I always code the called subroutine ahead of the calling one.
3.) At the beginning of the subroutine I always place a comment naming the subroutine along with a list of arguments used by the call command.
4.) I use indentation between the opening and closing blocks of, “O”, codes. Although this is not required, the NGC code starts looking a lot like Python. One of the reasons Python is so popular is it's much easier to read. No offense to TCL/ WISH programmers, I also like that language.
5.) Regarding, “O”, codes, I always use a lower case o instead of upper case O. This makes it much less likely to mistake it for a zero.
In the program, the first subroutine is called helix. This code produces just one 365 degree revolution of the helix. The second subroutine called spiral, recursively calls helix to control the size of the shape. This is an example of program stacking. You hear these terms used in instances such as USB stack, or in the case of the Internet TCP/IP stack. These programs are designed fit one on top of another. This type of approach greatly simplifies the coding process as well as making this stack of programs easier to maintain. As a last example of recursion, lines 35 to 39 reposition the coordinate system to a point 2.5 x and 2.5 y units offset from true zero. The spiral is repeated again. I have seen so many coders go to extreme complexity to build repositioning directly into their code. All you have to do is create your shape to generate at the origin. If you want to move it all you have to do is use the codes g92 and g92.1. Finally, lines 40 to 42 reset the x and y offsets to zero and end the program.
If there is enough interest, I will finish coding this into a full fledged circular pocket subroutine that you can call from your own programs. Let me know what you think.
Last edit: 11 Oct 2016 04:41 by Larry. Reason: MIsspelled word
The following user(s) said Thank You: jtc, ostrozub41@gmail.com
Please Log in or Create an account to join the conversation.
- ostrozub41@gmail.com
- Offline
- New Member
Less
More
- Posts: 3
- Thank you received: 0
09 Jun 2019 06:09 #136349
by ostrozub41@gmail.com
Replied by ostrozub41@gmail.com on topic Spiral Subroutine
Hey. I want to try your program.
But there is no beginning in it.
Variables # 1, # 2, # 3 are not described. You can add?
But there is no beginning in it.
Variables # 1, # 2, # 3 are not described. You can add?
Please Log in or Create an account to join the conversation.
- ostrozub41@gmail.com
- Offline
- New Member
Less
More
- Posts: 3
- Thank you received: 0
09 Jun 2019 06:11 #136350
by ostrozub41@gmail.com
Replied by ostrozub41@gmail.com on topic Spiral Subroutine
Hey. I want to try your program.
But there is no beginning in it.
Variables # 1, # 2, # 3 are not described. You can add?
forum.linuxcnc.org/40-subroutines-and-ng...al-subroutine#136349
But there is no beginning in it.
Variables # 1, # 2, # 3 are not described. You can add?
forum.linuxcnc.org/40-subroutines-and-ng...al-subroutine#136349
Please Log in or Create an account to join the conversation.
- pl7i92
- Offline
- Platinum Member
Less
More
- Posts: 1890
- Thank you received: 356
09 Jun 2019 09:11 #136362
by pl7i92
Replied by pl7i92 on topic Spiral Subroutine
there is a subroutine out in example where you can simply make your drillcomand G81-G83
by search and replace to a spiral mill drill
repl Gcode by o<spiraldrill>
X by #1 X
Y by #2 Y
by search and replace to a spiral mill drill
repl Gcode by o<spiraldrill>
X by #1 X
Y by #2 Y
Please Log in or Create an account to join the conversation.
- ostrozub41@gmail.com
- Offline
- New Member
Less
More
- Posts: 3
- Thank you received: 0
09 Jun 2019 09:24 #136363
by ostrozub41@gmail.com
Replied by ostrozub41@gmail.com on topic Spiral Subroutine
I have already figured out.
This program does not suit me.
Who can help write a program for 3-d spiral with variable pitch and variable radius.
the link is the fourth case
www.zwsoft.com/forum/thread-2980-1-1.html
This program does not suit me.
Who can help write a program for 3-d spiral with variable pitch and variable radius.
the link is the fourth case
www.zwsoft.com/forum/thread-2980-1-1.html
Please Log in or Create an account to join the conversation.
- Grotius
- Offline
- Platinum Member
Less
More
- Posts: 2232
- Thank you received: 1944
09 Jun 2019 21:16 #136402
by Grotius
Replied by Grotius on topic Spiral Subroutine
@Александр Дедков,
github.com/LinuxCNC/linuxcnc/blob/master/nc_files/spiral.ngc
If you look at this code for example :
Z stroke down one cycle = g1 z-.1 f24
X or Y stroke radius one cycle = g91 g1 @-.0025 ^4.5
This code is repeated 800 times, so quite lineair piece of code.
This will result in a lineair 3d spiral with pitch and radius.
You can expand this code to your needs. For example, you can adapt a sinus and you can make non lineair spiral cone in g-code.
You can also make connections to your gui screen for automated inputs. It's up to you !! Good luck !!
Greetings to Nick, Откуда: Gatchina, Saint-Petersburg distr., Russia
He is the developper of gcode tools for inkscape.
github.com/LinuxCNC/linuxcnc/blob/master/nc_files/spiral.ngc
If you look at this code for example :
Warning: Spoiler!
g20 g64 g90
s3400 m3
g0 z1
g0 x2 y0
g1 z-.1 f24
o100 repeat [800]
g91 g1 @-.0025 ^4.5
o100 endrepeat
g90 g0 z1
m2
Z stroke down one cycle = g1 z-.1 f24
X or Y stroke radius one cycle = g91 g1 @-.0025 ^4.5
This code is repeated 800 times, so quite lineair piece of code.
This will result in a lineair 3d spiral with pitch and radius.
You can expand this code to your needs. For example, you can adapt a sinus and you can make non lineair spiral cone in g-code.
You can also make connections to your gui screen for automated inputs. It's up to you !! Good luck !!
Greetings to Nick, Откуда: Gatchina, Saint-Petersburg distr., Russia
He is the developper of gcode tools for inkscape.
Please Log in or Create an account to join the conversation.
- Larry
- Offline
- Junior Member
Less
More
- Posts: 27
- Thank you received: 13
11 Jun 2019 23:01 - 11 Jun 2019 23:05 #136672
by Larry
Replied by Larry on topic Spiral Subroutine
First off I am honored that someone has shown interest in the post I made in 2016. I quote from the last paragraph of that post, “If there is enough interest, I will finish coding this into a full fledged circular pocket subroutine that you can call form your own programs. Let me know what you think.”. Finally, a year after this post I gave up hope that there was any interest in this sort of code.
Before I get too deep into this subject I feel I need to partially introduce myself. First I am a published author. I have published articles which have appeared in, “The Home Shop Machinist” as well as, “QST”, which is an amateur, (ham), radio / electronics magazine. Since this forum has a global reach I probably should explain that the former magazine is on book store magazine racks in the U.S. as well as Canada. The same is true for the latter as well, but at one time that publication also had reach into parts of Europe. I am currently composing an article for the, “Digital Machinist” magazine which is a sister publication of The Home Shop Machinist. This one could be a revolutionary bomb shell for LinuxCNC. I'm going to stop here because I will repeat most of this in the reply to my other forum post titled, “FreeCAD Gear Modeling and Design”, which appears in the CAD-CAM section of this forum. No I have not yet posted that reply, but it's coming. However, don't let that stop you form reading that post now as it's a call to action for my very infant YouTube channel. Do watch those videos and feel free to tell me what you think as a reply to that post.
Now for the code. Initially the code I submitted was solely an example of what recursive programming can do. To make that code of practical use, I have split it up into three separate files titled, spiralParent.ngc, spiral1.ngc, and helix.ngc. While the first file likely could go anywhere, and in actual practice would be replaced by a file you created that called the spiral subroutine, the latter two must be placed in the directory specified by your *.ini file. I could tell you where I placed mine, but on your system it would likely go elsewhere. I am currently running the latest and greatest version of LinuxCNC 2.7.14. In the accompanying documentation in section 5.5.6 titled, “Calling Files”, you are given explicit instructions as to where to store callable subroutine files on your system. Beware! To avoid a name collision with the file I posted at the beginning of this topic, I named the new file spiral1.ngc. When you install this file in its proper place on your system, you must change the name back to spiral.ngc, or the program will not run. All of these files were placed on my system and successfully run in back plot mode.
Well that's it. You now have a set of callable files on your system. As I said in the first post, this code will need further refinement. For instance we'll need to write a step down subroutine to recursively call spiral to affect the depth of the circular pocket you desire.
Before I get too deep into this subject I feel I need to partially introduce myself. First I am a published author. I have published articles which have appeared in, “The Home Shop Machinist” as well as, “QST”, which is an amateur, (ham), radio / electronics magazine. Since this forum has a global reach I probably should explain that the former magazine is on book store magazine racks in the U.S. as well as Canada. The same is true for the latter as well, but at one time that publication also had reach into parts of Europe. I am currently composing an article for the, “Digital Machinist” magazine which is a sister publication of The Home Shop Machinist. This one could be a revolutionary bomb shell for LinuxCNC. I'm going to stop here because I will repeat most of this in the reply to my other forum post titled, “FreeCAD Gear Modeling and Design”, which appears in the CAD-CAM section of this forum. No I have not yet posted that reply, but it's coming. However, don't let that stop you form reading that post now as it's a call to action for my very infant YouTube channel. Do watch those videos and feel free to tell me what you think as a reply to that post.
Now for the code. Initially the code I submitted was solely an example of what recursive programming can do. To make that code of practical use, I have split it up into three separate files titled, spiralParent.ngc, spiral1.ngc, and helix.ngc. While the first file likely could go anywhere, and in actual practice would be replaced by a file you created that called the spiral subroutine, the latter two must be placed in the directory specified by your *.ini file. I could tell you where I placed mine, but on your system it would likely go elsewhere. I am currently running the latest and greatest version of LinuxCNC 2.7.14. In the accompanying documentation in section 5.5.6 titled, “Calling Files”, you are given explicit instructions as to where to store callable subroutine files on your system. Beware! To avoid a name collision with the file I posted at the beginning of this topic, I named the new file spiral1.ngc. When you install this file in its proper place on your system, you must change the name back to spiral.ngc, or the program will not run. All of these files were placed on my system and successfully run in back plot mode.
Well that's it. You now have a set of callable files on your system. As I said in the first post, this code will need further refinement. For instance we'll need to write a step down subroutine to recursively call spiral to affect the depth of the circular pocket you desire.
Attachments:
Last edit: 11 Jun 2019 23:05 by Larry. Reason: Code attachments didn't seem to upload.
Please Log in or Create an account to join the conversation.
- Larry
- Offline
- Junior Member
Less
More
- Posts: 27
- Thank you received: 13
11 Jun 2019 23:11 #136674
by Larry
Replied by Larry on topic Spiral Subroutine
Thank you for your reply. I did a blanket reply to all those who replied to my post. However I'm not sure if the blanket reply would generate an email to all of you. Therefore, I am sending all of you this individual short note in the hope your email box will get notified.
Larry
Larry
The following user(s) said Thank You: tommylight
Please Log in or Create an account to join the conversation.
- andypugh
- Offline
- Moderator
Less
More
- Posts: 23537
- Thank you received: 4852
12 Jun 2019 15:35 #136750
by andypugh
Replied by andypugh on topic Spiral Subroutine
While we are at it, here is an example of somewhat elaborate G-code programming that computes and machines cam profiles:
bodgesoc.blogspot.com/2016/11/cams.html
bodgesoc.blogspot.com/2016/11/cams.html
The following user(s) said Thank You: Grotius
Please Log in or Create an account to join the conversation.
- Grotius
- Offline
- Platinum Member
Less
More
- Posts: 2232
- Thank you received: 1944
13 Jun 2019 18:32 - 13 Jun 2019 18:33 #136844
by Grotius
Replied by Grotius on topic Spiral Subroutine
Andy,
Much repect for your example.
bodgesoc.blogspot.com/2016/11/cams.html
You go to the finest calculation in your page, like a professor would do !
I never readed the “The Home Shop Machinist”, but i think you could do a article over there for sure.
Much repect for your example.
bodgesoc.blogspot.com/2016/11/cams.html
You go to the finest calculation in your page, like a professor would do !
I never readed the “The Home Shop Machinist”, but i think you could do a article over there for sure.
Last edit: 13 Jun 2019 18:33 by Grotius.
Please Log in or Create an account to join the conversation.
Time to create page: 0.145 seconds