What's new
What's new

OT: Free CNC code

Boris

Titanium
Joined
Oct 4, 2005
Location
England
Had sometime yesterday during loading 2 VMCs(both had a 22 min cycle) to hack together some code for engraving sequential serial numbers on parts eg "12834" on the first part, "12835" on the next, "12836" on the next etc etc etc

Now we have'nt got any jobs that need this, so its give the code away time

Its in heidenhain,but converting it to fanuc or any other should'nt be too hard a job.
I've not included the sub-programs that do the engraving since we do use those and the boss gets upset if I give out our code
Anyway, the code is noted, feel free to use and abuse it

0 BEGIN PGM serial_test MM
1 BLK FORM 0.1 Z X-5 Y-3 Z-2
2 BLK FORM 0.2 X+20 Y+3 Z+0 ; defining a blank form for the graphics
3 TOOL CALL 7 Z S4000
4 L X+0 Y+0 Z+10 R0 FMAX M3
5 L Z+1 R0 FMAX
6 FN 0: Q1600 =+20
7 FN 0: Q1601 =+12
8 FN 0: Q1602 =+0.06 ; above 3 variables are to do with the engraving sub-programs
9 FN 0: Q1800 =+12834 ;first serial number to be engraved
10 CYCL DEF 10.0 ROTATION
11 CYCL DEF 10.1 ROT+180 ; just a rotation function I had to use, ignore
12 LBL "MO"
13 FN 0: Q1910 =+10000 ;DIVISOR
14 FN 0: Q1900 =+Q1800 ;temperary storage for serial number
15 LBL "LOOP"
16 Q1901 = INT ( Q1900 / Q1910 ) ;divide the serial number by the divisor
17 Q1901 = Q1901 % 10 ; and use a modulo 10 function to extract the first number
18 FN 9: IF +Q1901 EQU +0 GOTO LBL "ZERO"
19 FN 9: IF +Q1901 EQU +1 GOTO LBL "ONE"
20 FN 9: IF +Q1901 EQU +2 GOTO LBL "TWO"
21 FN 9: IF +Q1901 EQU +3 GOTO LBL "THREE"
22 FN 9: IF +Q1901 EQU +4 GOTO LBL "FOUR"
23 FN 9: IF +Q1901 EQU +5 GOTO LBL "FIVE"
24 FN 9: IF +Q1901 EQU +6 GOTO LBL "SIX"
25 FN 9: IF +Q1901 EQU +7 GOTO LBL "SEVEN"
26 FN 9: IF +Q1901 EQU +8 GOTO LBL "EIGHT"
27 FN 9: IF +Q1901 EQU +9 GOTO LBL "NINE" ;The above 10 lines look at the result and decide which label to jump to
28 LBL "ZERO" ;this engraves the number zero on the part
29 CALL PGM zero ; the sub-program for zero
30 FN 9: IF +101 EQU +101 GOTO LBL "END" ; a line because heidenhain code does not contain a simple 'goto label' command along the lines of M99 P234
31 LBL "ONE" ; and this bit does the number one
32 CALL PGM one
33 FN 9: IF +101 EQU +101 GOTO LBL "END"
34 LBL "TWO" ; and so on
35 CALL PGM two
36 FN 9: IF +101 EQU +101 GOTO LBL "END"
37 LBL "THREE"
38 CALL PGM three
39 FN 9: IF +101 EQU +101 GOTO LBL "END"
40 LBL "FOUR"
41 CALL PGM four
42 FN 9: IF +101 EQU +101 GOTO LBL "END"
43 LBL "FIVE"
44 CALL PGM five
45 FN 9: IF +101 EQU +101 GOTO LBL "END"
46 LBL "SIX"
47 CALL PGM six
48 FN 9: IF +101 EQU +101 GOTO LBL "END"
49 LBL "SEVEN"
50 CALL PGM seven
51 FN 9: IF +101 EQU +101 GOTO LBL "END"
52 LBL "EIGHT"
53 CALL PGM eight
54 FN 9: IF +101 EQU +101 GOTO LBL "END"
55 LBL "NINE"
56 CALL PGM nine
57 LBL "END"
58 Q1900 = Q1900 - Q1901 * Q1910 ; strip the first figure from the serial number via subtracting the result times the dividing figure, this was the reason for having a temperary storage of it at line 14
59 Q1910 = Q1910 / 10 ; divide the divisor by 10
60 CYCL DEF 7.0 DATUM SHIFT
61 CYCL DEF 7.1 IX-3.5 ; a shift over to the next engraving position
62 FN 11: IF +Q1910 GT +0.2 GOTO LBL "LOOP" if the divisor is still above 1 go around the function again to extract and engrave the next figure
63 CYCL DEF 7.0 DATUM SHIFT
64 CYCL DEF 7.1 #1 ; engraving finished, reset the datum shift
65 L Z+25 R0 FMAX M9 M5
66 FN 1: Q1800 =+Q1800 + +1 ; add one to the serial number
67 STOP ; the remainder of the code is so I could test the function out on the machine
68 LBL 0
69 CALL LBL "MO" REP1000 ;
70 L X+50 Y+0 Z+0 R0 FMAX M91
71 END PGM serial_test MM

In tests the program would engrave "00001" for a serial number of 1 or "00101" for a serial number of 101 or "12834" for the program listed above
By changing the divisor to a larger or smaller multiple of 10, you can make engrave larger of smaller serial numbers eg you have a 4 figure serial number to engrave, then use a divisor of 1000.

Any questions, just post away and i'll try and help (just no PMs ta)

Boris
 
I've got some comments for you.

On line 16 you convert the number to a interger and line 17 you find the modulo of 10 but if the number is less than 10 it is the same number. If the number is greater than ten you have a problem and your macro will place the wrong number (ie 512834 would come out as 12834). I would put in a error check to make sure the interger is less than 10.

In line 58 I would have wrote Q1900 = FRAC(1900/1910) but either way.

Really suprised you didn't use tables, unless you wanted to dumb it down for the fanuk users. Read and write to a .tab file and you can call the serial number anytime without the operator inputing the wrong number.
 








 
Back
Top