We all know that programming can be complicated. So let me explain to you how it all works. This article explains the real meaning of Modal and non modal G codes.
Modal means that once a command is issued it stays in the control.
How Can you Actually Use This?
If you issue a G0 or G00 command the machine is in rapid and you do not need to re-state it.
Rapid means all motors are flat out, like a teenager in a Ferrari.
Every move from then on will be a rapid move unless you tell it otherwise. The G code that changes it must be in the same group. For example G0 G1 G2 and G3 are all in the same group a bit like The Beatles used to be.
The other day I was talking to a “young person” who hadn’t even heard of the Beatles. I mean fuckin hell, am I really really old or are they doomed to be forgotten?
By the way my definition of a young person seems to get older every day.
Imagine being called a “young person”. You hear politicians on the telly saying “oh yes I was talking to one of those young people the other day”.
God I hate the fuckin telly, well apart from Love Island, Naked Attraction, Embarrassing Bodies, Goggle-box, Coronation Street, Emmerdale Farm, East Enders, and Strictly Come Dancing.
To be really honest I watch everything but in my defence I never stop complaining and throwing Pot Noodles at the TV.
My TV’s are covered in dents and scratched from various foreign bodies being thrown at them. Obviously I have a TV in every room and toilet. They all still work even when the noodles trickle inside.
I have five, toilets not TV’s. My mother said “you must use loads of water, not to mention lavatory paper”
I said “mummy darling don’t you understand that you can’t shit or a piss in five toilets at the same fuckin time”
Obviously I didn’t say toilet in front of my mother it’s always lavatory.
Now I know a lot of you CNC Programmers out there are big fans of the show but I got to say it.
Spoiler I mean how in the fuck can you spoil something that’s already a mind numbing piece of crap!!
When I hear the music to that show it gets me really enthusiastic about emptying the cat trays that are heaving with piss and shit. (I know you are not supposed to leave them for over a week but in my defence I am getting into a routine now)
“Come on Dave the swearing gets worse, the Americans won’t like it”
I admit this is a rant too far and I wouldn’t blame you for going to a different website.
This guy writes serious stuff about G Codes unfortunately for you, almost every post and video is full of mistakes and often blatantly wrong.
Anyway you deserve it for not sticking with me through difficult times.
I shouldn’t say this but there are so many mistakes in these articles I don’t know how the fuck he gets away with it. Well that’s the Internet folks.
His new series of training videos is called “Learn The Five Quickest Ways to get Instantly Dismissed as a CNC Machinist”
It is pretty obvious that these would have to replace one another because if you had them on the same line they would contradict one another.
You can’t go around a clockwise circle and in a straight line at the same time.
G0 X0 Y0 (Rapid to X0 Y0) Y50.
(Control says “ok I get it, rapid again, no need to repeat yourself, I heard you the first time”)
Modal and non modal G codes. How do they actually Work?
There are not many non modal commands G53 and G4
Can’t think of anymore right now.
G4 is a dwell if this were modal it would cause many problems
G0 X50. Y50. (Rapid move) G4 X5. (Five second dwell) X0 (This would be a rapid command not another dwell because it remembers the G0)
It remembers the G0 but conveniently forgets the G4.
My third wife was a bit like that she said I slapped her in the face but conveniently forgot that this was only after she kicked me in the bollocks and chopped off my left ear with a potato peeler.
The machine will do the G4 dwell and then completely forget it. If you really want another dwell you’d have to repeat the G4. Otherwise it’s like being told it’s your turn to feed the dog, the command is ignored.
Yes folks you can use X or P for a dwell command. I use X cos I love the feeling when the operator comes back to me n says “Dave you got this wrong”.
First of all I ignore him because my name is David and I don’t answer to Dave.
When he finally gets through to me, I love that feeling of power and superiority when I explain to him it works with P and X.
If you use a non modal command you have to repeat it for every line you want to use it on.
G53 X0 Y0;
“Wake Up State” Modal and non modal G codes.
What’s This all About?
These are the G codes that are active when you turn on the control. Wake Up State, not to be confused with getting out of bed with a steaming hangover after a night on the piss.
This wake up state cannot be guaranteed as you can change this state by parameters.
There is also a parameter which controls what happens when you press the reset button. So for example pressing reset may cancel a canned cycle. (Or not).
Your modal G codes will change when……
You press reset.
When you first turn on your machine (Wake Up State)
You read an M30 at the end of a program.
These are not actually called modal but the same applies once you start the spindle (M3) you do not need to write it again until you want to stop or reverse it. M4 will start the spindle in reverse CCW. M5 will stop spindle.
Anyway if you must write a load of bollocks at the beginning of your program please learn what each G Code means and why it’s there. Oh and don’t blame me when your fingers wear out.
It’s just not good enough to say “the bloke on nights always puts that in”
And That’s Not All with Modal and non modal G codes
If you are running short on memory these are things to look at. Some CAD systems will output loads of unnecessary shit but you need to sort your post processor to stop this. It’s quite easy to fix. Loads of leading and trailing zeros to get rid of.
X0.000 same as:
X050.000 same as:
Feed-rates are modal too so you only need to state them once and then again when you wish to change them.
It is best to state all of the modal information you require at the start of each section of code. Just after the tool change is the best place.
Try to program each tool as a section, as if it were a program on it’s own.
Each tool should have all the necessary information to run on its own. Nothing should be assumed at the start of each tool. Oh and always take into account Modal and non modal G codes.
But There is a Catch
Be careful when using MDI with Modal and non modal G codes as you do not know what G codes are active. All controls will have a screen displaying active G codes.
There are loads of them on this screen, don’t worry if you don’t know what they all are (even Fanuc don’t really know, they just make them up).
How Can you Actually Use This?
If for example in a section of program you have only one feed rate then that is all you will need to alter. If it’s repeated in several places (which your pesky CAD system may do). Then you will need to remember to change each one.
I very much doubt that you enjoyed reading this article but their are loads more
Loads of articles on CNC Programming, enjoy yourself:
Now I don’t want to appear sexist here but sometimes my wife will be awake at night worrying about whether or not she left a gas ring turned on.
Now me I worry about different things I mean how can you compare a gas explosion to a collision on a CNC Machine.
(I really should clean that cooker)
Anyway my point is if you worry about G91 and if you left one active in a program and it causes a collision.
Help is at hand. Now this maybe another piece of useless shit and you might wish you had stayed on Facebook, reading your mates post, ruminating on the benefits of vegan cheese and how you can’t tell the difference. (I have tried and it’s really nice once you get the thought of eating semtex out of your brain).
Don’t set me off about vegans. Just answer one question, if they don’t like meat why do they have to keep banging on “I’ve just eaten this burger and you would think it was real beef”
Macro System Variables, I often get random emails from people asking me all sorts of questions.
I got one only the other day it began “Hi David, your articles are shit”
I don’t answer every email I get, but I try to answer when I think there is an appropriate response or I feel I can help.
Macro System Variables
Anyway someone called Neil from America (I think) asked me if I would create a tool setting macro. This involves using Macro System Variables
There is a really annoying thing on a Fanuc control (well actually there are quite a few). This one really pisses me off because I find myself making excuses for the control. I mean like I designed the fuckin thing.
For one, I am nowhere near clever enough and don’t even work for Fanuc. (I am available Mr. Fanuc, sorry I dissed your control)
To set a tool length it’s a really convoluted procedure. You first zero out the Z on the REL display.
You then bring down a tool and get the control to record the Z position by pressing Z then the soft key INP C.
Here’s a couple of videos showing you how.
Use INP C to Enter Offset
Once the Z has been set you can measure as many tools as you want. Just bring each tool down to the setting block and press Z then INP C.
Here comes the problem…..
If you turn turn off the machine or, for some reason, you have to reference it again you will lose your REL Z position. You will now have to set it again.
Oh and if you don’t notice you’ll be in deep shit. (Your tool length will be wrong!!!)
There are ways around this by using a work offset, but to be honest it’s all a pain in the fuckin arse.
Oh and one other thing. Make sure you write the offset to the correct tool because you can write it to any tool.
Check the Z zero position each time before setting tools.
A good guide is to slap a good old steel rule or a tape measure against the tool for a rough check.
Macro System Variables to the Rescue
For years I have used a work around on Fanuc Controls. First of all you need to establish where your setting block is. In my case it’s a table probe.
Oh and you need macro (it is an option so if you don’t have it look away now.
To do this you need to note down your machine position in Z when you touch your block or setting probe.
This will later be stored in a variable to be used to calculate the tool length.
Try to set your block in a known position so that each time the Z figure is the same. You may even be able to fix it to the machines table. Don’t weld it, the boss won’t like it.
Read on to see how this variable can be written to automatically with a calibration programme.
The Programme… OK Let’s Do This
Bring the spindle nose down to touch your setting block. this is the value you will write into #102 (Mine says -500.877)
Call your tool to be measured into the spindle in MDI (T06 M06;)
Bring the tool down to the setting probe.
Run the programme below.
O9001(Tool Measurement Macro) 1. #100=#4120 (Grab Tool Number); This will cause the machine to store the current tool number in #100
2. #101=#5023 (Store Machine Z Position); This will cause the machine to store the current Z position in #101
3. #102=-500.877 (Setting Block Z); This is the figure you recorded from the position of the setting block.
4. #150=#102-#101 (Calculate Tool Length); Now we can calculate the tool length by taking the known position from the current position.
This will give us a minus figure, we will reverse this in the code below.
5. #150=-#150 (Reverse Z Figure);
6. G90 G10 L10 P#100 R#150 This puts the tool length into the correct offset.
7. G28 G91 Z0 (Return Z To Zero Return); 8. G90;
More Explanation (Macro System Variables)
System variables know shit.
What I mean by this is that system variables contain information about the system. Some are read only and some you can write to, like the tool offsets for instance.
You can ask the system loads of stuff like.
What speed do you have stored?
What’s the tool in the spindle?
What position are you in?
What modal G codes do you have.
How old is my auntie Joan?
Do I have a fat arse?
These are all stored in special Macro System Variables except the last two.
You can read them and sometimes you can write to them. It’s not like that bloke at work who thinks he knows every fuckin thing. Sometimes he’ll listen, but most of the time he has to tell you.
No, no macro system variables follow special rules.
Anyway (The Explanation)
O9001(Tool Measurement Macro)
1. #100=#4120 (Grab Tool Number);
2. #101=#5023 (Store Machine Z Position);
3. #102=-500.877 (Setting Block Z);
4. #150=#102-#101 (Calculate Tool Length);
5. #150=-#150 (Reverse Z Figure);
6. G90 G10 L10 P#100 R#150
7. G28 G91 Z0 (Return Z To Zero Return); 8. G90;
The first line 1. looks into system variable #4120 which contains the number of the current tool in the spindle.
You ask what this is and then put it in #100. Obviously you can’t write to this variable.
The next line 2. asks where the machine is in Z (Machine Position) #5023. Again you can’t write to this but you can store it in #101.
Line 3. stores the value that you measured early in #102. This is where your measuring block is from zero return.
In line 4. you take these values away from one another to give you the tool length
This ends up as a minus figure so we need to reverse it. We do this on line 5.
5. #150=-#150 (Reverse Z Figure);
6. G90 G10 L10 P#100 R#150
Line 6. is a standard G10 because we have L10 this means it writes to tool lengths. P is the tool number and R is the tool length.
I always stick a G90 at the front of this G10 in case some twat leaves the machine in G91 in which case it will add to the existing tool length. (Oh dear total fuckin dog shit mayhem)
So You Want To Use Macro Like An Adult?
Lots of programmers use macro in a very complex and confusing way but I think that is the way they want it so you think “wow he must be a clever bastard.
In my opinion a good macro should have a really simple interface.
I’ll show you mine.
Yep that’s all it is
M200 is aliased to my programme O9001 meaning if you run M200 it goes into my programme 9001 and returns.
Don’t forget you can run M200 in MDI too.
Read this article if you don’t know how to alias a macro to an M code or a G code.
More Macro System Variables
Now what we could also do is when we bring the spindle nose down to set the original Z figure, we could make the machine store this in #500 instead of my #102
500 series variables stay in the control even when it’s switched off. These variables are the cockroaches of macro programming (they survive anything).
In the event of a nuclear war the survivors would be as follows:
500 system variables
My mother in law.
The code for this is really simple. Just one line in fact,
All you need do is bring your spindle down to the setting block. Then run M300, which I have aliased to programme 9002
Or if you like just run programme 9002
O9002 (Calibrate Table Block);
#500=#5023 (Z Machine Position);
Word Of Caution
Don’t use M99 at the end of your Macro unless you are calling it with an M CodeAlias. Otherwise it will be stuck in a never ending loop.
So Now It’s Simple
Bring the spindle down to setting block and run M300.
This sets #500. If the block is in the same place then this is not needed.
Call the tool you want to measure to the spindle in MDI otherwise it won’t be registered in #4120
Bring the tool down to the block and run M200.
Or simply type M200 into MDI and run it.
This will then store the tool length in the correct offset.
G96 however means meters per minute. This is a surface speed. G96 S200 M3
Your machine would start up at a surface speed of 200 meters a minute. Now your RPM would depend on where on the diameter the tool was positioned.
If the tool was positioned at a 100mm diameter it would be as if the tool were able to run around this diameter at a speed 200 meters a minute.
It’s a bit like being on a running machine if you ran at 200 meters a minute and placed various diameters under your feet the large ones would turn at slow rpm and the small ones would turn at high rpm. (Just like the hamsters above)
That’s why on a manual lathe it is hard to face a large diameter without changing speed half way.
You know when you face a part on a CNC Lathe and you hear that change in pitch? It’s the spindle increasing in RPM as it gets closer to the center of the part.
Well I bought my hamster loads of different wheels to play on just like the one above.
My hamster suffered with depression on account of being stuck in a cage all day and not having a girlfriend oh and he had a lot of credit card debts too.
These wheels varied in diameter from about 6 inches to a massive 2 foot one. They kept him happy all night. He was so tired he slept all day.
Harold could only run so fast but I noticed when he was on the small 6 inch wheel it absolutely whizzed around. Now on the big two foot diameter one it took him ages just to get it to spin around once.
Harold Had G96
A CNC machine in G96 will give a lovely finish because the surface speed always remains the same.
So even though Harold ran at 200 metres a minute (this is fuckin lightening speed for a hamster)
The wheels ran at different RPM depending on what diameter they were.
Harold Was a Clever Bastard
Oh by the way Harold had a tail (unlike other hamsters) and a maths qualification.
He knew that if he multiplied the diameter of the wheel by .00312 it would give him the circumference of whatever wheel he was running on in meters.
200 mm wheel (.00312 x 200 = .6864)
All he now needed to do was divide this answer into the speed he was running at and he would know how many RPM his wheel was revolving at.
If he was running at 200 meters a minute not only would he be fuckin knackered but the wheel would be running at 291 rpm
200 / .6864 = 291
Basic Turning Manual Machining
Using a manual machine you have to compromise. At the outside your speed is too fast and when you get to the centre you are too slow.
On a CNC lathe we would normally program in mm per revolution as well because the speed is changing all the time so we need our feed to be locked into the speed.
With a machining centre our cutter is always revolving at the same speed so the feed can be constant in mm per minute.
Someone out there will be thinking “what happens in G96 when you get to the centre of the part”. Well the spindle will be flat out!
See how surface speeds are translated to speeds in RPM. There are many converters online that you can use for this and I do recommend their use. It will also mean you don’t have to watch my tedious video.
When I train people at the CNC Training Centre my emphasis is on understanding not memorising. I usually start by saying “please don’t remember all the things I am telling you”.
In the early days training students in Basic Turning I remember them saying to me the next day that they had G codes floating around in their head from the lessons the day before.
What I really mean is that the most important thing is to understand what the machine can do and the concepts of programming and Basic Turning.
You could say “I know there is a G code that makes the machine run in RPM” so all you need is a list of G codes.
If you can be bothered to work through the simple maths above. It will help you to fully understand how G96 is works.