CNCzone.com-The Largest Machinist Community on the net!



Home Page Mark Forums Read Today's Posts My Replies Classifieds Reviews Photo Gallery Web Links Share Files Advertise With Us Ad List
Go Back   CNCzone.com-The Largest Machinist Community on the net! > Machine Controllers Software and Solutions > G-Code Programing


G-Code Programing Discuss G-code programing and problems here!


This forum is sponsored by:

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Ban this user!
Old 12-04-2009, 02:49 PM
 
Join Date: Nov 2006
Location: usa
Posts: 97
bobeson is on a distinguished road
Condition gcode execution is possible in Mach3

(cross-posted from the machsupport forum)

Hi Folks,
I'm new to gcode programming, having purchased my first CNC machine earlier this year (a Tormach PCNC1100). I have been a computer hardware engineer for many years, and have done a lot of low-level coding (assembly, microcode, RTL, FSM, etc).

When I started out my gcode programming learning curve, at first I was greatly disheartened to read that Mach3 does not support conditional code execution, since I had been planning extensive use of fixturing to get around the lack of a toolchanger on the Tormach, and conditionals make a lot of complex functionality easy to implement. Working around the lack of conditionals started to get annoying.

However, I realized that since subroutine label numbers can be the result of an expression evaluated at run-time, we can use expression math to effect conditional execution. As long as we can reduce our condition to a 1 or 0, we can multiply this condition flag by the intended subroutine label number to either execute the operation or else execute subroutine 0, which is coded to always return immediately and thus provides the "false" path to avoid execution of the other subroutine. The same method can be used to create an if-then-else clause if you use addition instead of multiplication.

For example, let's say my part discovery subroutine has finished probing my 16-part fixture, discovered which slots are populated and which are mpty, and now we've got a set of parameter flags that enable or disable code execution for that fixture slot. Let's say the part enable flag for the current part is stored in parameter #4. If the part is present, we want to first execute sub 21, and then call either sub 22 or sub 23 depending on parameter #5.

An example of the condition code execution technique I've been using:

Code:
 
#4 = 0 (this flag is actually set by the part probing subroutine, set it here for this example)
#5 = 1 (this flag chooses between execution of sub 22 or sub 23)
(here is an effective if-then conditional clause)
M98 P[ #4 * 21 ] ( call either "dummy" sub 0 or "real" subroutine 21 if the part is enabled )
M30
O0 (this the "false" subroutine which always returns immediately)
M99
O21
... (real code goes here)
(here is an effective if-then-else conditional clause)
M98 P[ #5 + 22 ] ( call sub 23 if flag #5 is set, or else call sub 22 if flag is not set)
M99
O22
... (real code goes here)
M99
O23
... (real code goes here)
M99
In order to use a wider assortment of conditions, we can use math to reduce an
arbitrary expression to a 0 or 1 value.
Say, for example, that we want to execute an additional facing subroutine
only if the probed part turns out to exceed our nominal raw stock allowance
of 1.25 inches. The actual height of the part as discovered by the probing
routine is stored in parameter #6.
Here is the gcode to effect this pseudo-code: if( #6 > 1.25 ) then { call sub P30 }.

Code:
#1 = [ 1.25 - #6 ] ( subtract part actual height from intended - if result is negative, then actual is > 1.25)
#2 = [ #1 + ABS[ #1 ]] ( adding the absolute value of a negative number to itself produces zero; a positive number yields non-zero)
#3 = [ 1 XOR #2 ] ( this serves two purposes: invert the logic sense of #2, and force it from zero/non-zero to zero/one )
M98 P[ #3 * 30 ]
This could also be written more compactly like so:
Code:
M98 P[ 30 * [ 1 XOR [ [1.25 - #6] + ABS [ 1.25 - #6 ] ] ] (same as above, but harder to read)
The same techniques can be used to code case statements, since a base label number plus an offset effectively becomes a jump table. You could also store a table of operation subroutine numbers to call for each of a number of different cases, and then use the switch expression as an index into the parameter table to find which subroutine number to call for that case value.

Here is the gcode to effect this pseudo-code: switch (#6); case(1): call sub P41; case(2): call sub P42; case(3): call sub P43;
Code:
M98 P[40 + #6]
Of course, I highly recommend being careful to make sure your expressions will only result in valid label numbers. In this case, we must make sure that #6 can only be 1,2 or 3. A default case statement could be constructed with more math and range checking, etc.

One thing to note is that Mach seems to choke on this kind of code when it comes to generating the toolpath display, so I get all kinds of bogus screen displays for the toolpath, but it then executes the actual machine operations just as I intended.

I've been using these techniques for a few weeks now with my 16-part fixture, which I can load with any number of parts and have the operations executed only for the parts that are loaded. With the data returned from my part probing routine, I can also setup exact fixture offsets for each discovered part, and optionally include a coordinate system rotation for that slot if the part is a little skewed by burrs or swarf, etc.

Perhaps the biggest benefit I've gotten from using conditional code execution has been when I setup a series of flags at the top of my part-program to enable or disable certain operations or part slots. For example, if I break a tool on the seventh part during the fourth operation (out of many ops) then I can fix the problem, and restart the code by disabling all operations except #4, and also disabling parts 1-6. Then operation #4 on parts 7-16 executes, and then I re-enable the remaining operations and all of the part slots, then run the program again to complete execution. This has saved me much heartache from trying to use the "run from here" function or else lots of cutting-and-pasting to execute code fragments in new files to complete a partial operation.
Reply With Quote

  #2  
Old 12-04-2009, 04:50 PM
Switcher's Avatar
Moderator
 
Join Date: Apr 2005
Location: Vectorink.com
Posts: 3,660
Switcher is on a distinguished road

bobeson,

Very cool ideas!

Is it possible to setup the conditional code, to read external "sub program" code on the same PC?

I've always worked with Siemens 840D control, that lets you use conditional code mixed in with the g-code.
__________________
Free DXF Files - Vectorink.com - myDXF.blogspot.com
Reply With Quote

  #3   Ban this user!
Old 12-05-2009, 01:20 PM
 
Join Date: Nov 2006
Location: usa
Posts: 97
bobeson is on a distinguished road

Originally Posted by Switcher View Post

Is it possible to setup the conditional code, to read external "sub program" code on the same PC?
Not that I'm aware of. It would be pretty handy though!
If you're willing to plunge into VisualBasic macros, you can certainly use a lot more functionality, possibly including file I/O, but I haven't started writing those yet, and don't know much about them. I'd rather stick to plain gcode wherever possible, which is why I was so happy to find this workaround for conditional execution. Having to install macros to run a program creates extra dependencies that I'd rather avoid if possible.
Reply With Quote

Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fanuc OM Program Execution Camber Fanuc 12 04-20-2011 05:44 AM
Purpose and execution of G39 ? MauserBob G-Code Programing 2 04-14-2008 09:48 AM
Radius problems when generating gcode from bobcad for mach3... ? scyan BobCad-Cam 7 12-14-2006 12:33 PM
Shoptask CNC Lathe and Mach3: Gcode delima KaptainKarst Shopmaster/Shoptask 20 08-05-2006 05:55 PM
Does Mach3 tool diameter override gcode setting? WarrenW Mach Software (ArtSoft software) 1 04-27-2006 08:09 AM




All times are GMT -5. The time now is 12:25 AM.





Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO
Template-Modifications by TMS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361