From lathes to lasers

Programmers are in demand everywhere, even in manufacturing. The fact is that it takes a long time to make each part by hand, so automation is needed. And where there is automation, there are programs and algorithms. Today we will show you a direction in IT that we have not yet talked about: programming CNC machine tools.

CNC lathe that turns metal parts.

What is CNC machine tools

To understand what a CNC machine is, you must first understand what a conventional machine, such as a lathe, is. You have some kind of metal blank, such as a cylinder. You fix it on the machine. The fixing point begins to rotate (this place is called a spindle), the fixed workpiece rotates with it, and the turner, using a special cutter, can cut out a part of the desired size and shape from the workpiece. So far, everything is manual.

Now we take the same machine, but we make it so that the cutters move themselves in different planes. We hang all kinds of sensors – rotation speed, temperature and pressure. And we do so that the part is turned not by the turner, but by the machine itself.

To control such an automatic machine, you need some kind of control module – which will make the workpiece rotate, and the cutters move in the right directions. This block is called the CNC block – numerical control.

Each CNC unit is connected to all major parts of the machine so that they can be controlled or monitored. For example, in a lathe, the CNC will monitor:

  • the rotation speed of the workpiece,
  • the direction of rotation, the position of the cutters,
  • the temperature of the cutting edge,
  • the temperature of the part,
  • the force of pressing the cutter on the part,
  • the movements of the cutters and guides.
  • CNC blocks are needed in order to automate the operation of the machine. You program what should go where and how to rotate, and the machine does it.

What can be programmed in production You can program

everything that has a CNC unit – even a machine for woodworking, even a laser cutting machine, or even a manipulator with spot welding. The main thing is that the necessary parts of the production unit are equipped with drives and sensors.

The drive is what makes something move. For example, to make a robotic arm, you need 5–6 actuators that will move the joints of the robotic arm. You can say to the drive: “Bend at such and such degrees” or “Turn this way”, and it will set in motion what is attached to it.

A sensor is a piece that collects some kind of data. For example, rotation speed, temperature, pressure, bend angle. Thanks to the sensors, we can say: “unbend the drive such and such until you feel such and such pressure force with the pressure sensor.”

How programs for CNC written

There are two options: automatically create a program from a model of a part, or write it from scratch.

Most often, the first option is used – first they draw into the part in 3D (there is a special software for this), and then the program itself generates the necessary code for the machine to get the drawn part. The disadvantage of this approach is that the code may turn out to be sub-optimal: it will take too long to execute or there is a lot of waste in the process.

Three-dimensional model of the part, on the basis of which the code for the machine will be generated.

The second approach is to write the program manually from scratch. To do this, you need to perfectly know all the parameters of the machine and the possible states of each sensor. This is more difficult, but it gives you more control over how the part is made.

In practice, they usually do this: they draw a three-dimensional model, unload the code for the CNC based on it, and then modify it, if required.


The program generated a code for the machine, which can be corrected immediately if necessary.

What such programs are

written on The code for CNC machines is written in the G-code programming language. This is a relatively common standard for all CNC machines, but the parts, codes and sequences vary from manufacturer to manufacturer. Simply put, you cannot just transfer a program from a machine of one company and run it on a machine of another company – the commands may not match.

The G-code language is so called because in it almost all commands begin with the letter G, followed by numbers – commands for the machine. There is also the letter M – it is used to denote additional codes and O – for subroutines. But this division is conditional and can vary for each machine tool manufacturer.

What the program looks like for CNC

If we look at the code, we will see this:

N1 G17 G20 G34 G40
N2 T1 M16
N3 S8600 M2
N4 G54
N5 M8

The N-code is responsible for the line number – they can come in handy if we need to jump to some then a specific line or skip some of the commands. M are responsible for parts, for example, command N3 S8600 M2 means that you need to spin the working spindle (M2 is responsible for it) to a speed of 8600 rpm (command S8600).

So, team by team, the machine performs certain actions, and the result is the part we need.

The peculiarity of programming machine tools

Unlike a computer, where a new and empty memory area is allocated for each program and variable, in machine tools everything is different. The fact is that the program at the time of launch does not know in what position the cutters are located, whether the guides are fixed, and so on. If you just start the program without preparation, the CNC, for example, can move the cutter even more to the left, which is already in the leftmost position, and then the drive or the attachment of the cutter can break.

To prevent this from happening, before each launch, the zeroing and initialization commands are built into the program in order to return each element to its original position. This is better than just checking what is where – after zeroing, we will know for sure that all the elements of the machine are in the position we know and the program will be able to work with them correctly.

It is also important to understand that machines work with living material: metal, wood, acrylic, stone, etc. The material is imperfect, may have internal defects, may melt and crack. Cutters and spindles are also made of some kind of materials that have thresholds for heating, strength and speed. If you make a mistake in the computer code and cause a memory overflow, the computer will simply freeze. You reboot it, and that’s it. And at the machine, you can break the cutter or damage the spindle. And if this economy is worth, be healthy.

It turns out that this is the same programming and algorithms as in other languages?

Whether we program the server or the machines in the factory, algorithms are at the heart of everything: logic, variables, loops, subroutines, and condition checks. Therefore, if you know how algorithms are arranged and can program in any programming language, then it will be much easier to master programming for CNC.

Currency Rates

  • EUR  EUR: 0.85
  • RUB  RUB: 72.56