Control your CNC automated spatial measurements with Python? Yes, you can!

Author: Luka Banović

The first thing that pops into people’s minds when mentioning the words “CNC” is a family of automated cutting or engraving tools. If we raise above the level of subtractive manufacturing, we can look at a CNC machine solely as a system that enables repeatable and automated movement in a defined space by following a pre-defined trajectory; very similar to an industrial robot hand. Looking at it from this perspective, with a little creativity one can execute fully automated spatially controlled operations.

A good example that fits this category is a beam profiling system – an optical measurement system for beam characterization with a transmitter on one side and receiver on the other. An important factor in such systems is alignment between the transmitter and receiver. Evaluating performance a set of discrete points in space can be an extremely valuable step in the development process. What if we can integrate that system with a CNC and run well-defined patterns repeatedly to collect data? What if we could easily generate custom motion patterns and automatically log all measurements in the database?

Creating a mock-up of such a system is a half-day job for most engineers. However making it fully-automated and ready to make it a robust tool used on a daily basis is a whole different story.

PlanetCNC, our CNC control technology provider, added support for real-time communication between their CNC control software and external Python scripts. This allows the user to execute any Python script in real-time during g-code execution.

How have we done it?

With a beam scanning system, we’ve mounted a transmitter to a fixed point on one end and mounted a receiver onto the moving CNC head. We then created a network of python scripts that automatically navigate the whole process:

  • MySQL database
  • PlanetCNC control software
  • Python scripts:
    1. Generate pattern script: the Python script for scanning pattern g-code generation
    2. Master script: the main script navigating the intermodular communication and triggering
    3. Trigger script: the script emitting a signal to master script when the machine has reached that line in g-code

Here we are assuming a standalone external measurement system that receives a run command and responds with done once the measurement is successfully completed. It can either be triggered directly from Master script or can listen to the database and run the measurement as soon as new location entry is detected.

Process diagram

WEBINAR: Hands-on experience with LR1110 LoRaWAN transceiver with GNSS and WiFi functionality

Semtech LR1110 LoRaWAN transceiver with GNSS and WiFi scanning evaluation has shown promising results. To best share our hands-on experience at the first instance we are organizing this session to explore the capabilities of the solution and share our initial experience. 

Step 1: Generate the scanning pattern

The Step 1 process is indicated on the process diagram above in green.

The output of this step is a series of G-CODE lines that are at the end of this step fed into PlanetCNC control software. There are three ways one can generate g-code:

  1. Select a pre-defined pattern and specify spatial parameters (scanning area, mesh size etc.). Here a set of (x,y) points is generated in the background
  2. Import a set of (x,y) points from a .csv file
  3. Pull locations from database

The pattern_generation.py script does the following tasks:

  • allows the user to create a set of points through any of the methods mentioned above
  • directly translates the set of (x,y) locations into G-CODE that PlanetCNC operates with
  • feeds the G-CODE lines into the PlanetCNC

For the last step to succeed, the PlanetCNC software must be running. Therefore a neat way of doing it would be to run the script from the PlanetCNC software directly. Luckily, the PlanetCNC software allows configurable buttons, meaning a button is simply added to the menu that executes the pattern_generation.py script when pressed.

Let’s demonstrate the process here. Assume we want to generate a spiral scanning pattern with 50mm mesh size and 1000mm scanning area.

  1. Run the pattern generation script from PlanetCNC

2. Choose the scanning pattern from the selection

3. Configure the chosen pattern with the above-mentioned parameters (50mm mesh size, 1000mm scanning area)

4. Press Generate! and see the pattern generated in the working area section in PlanetCNC.

On the right-hand side, you should see G-CODE console beginning with the following lines.

(PYTHR,C:\demo\master_script.py)    
G0 X0 Y0                            
(PY,C:\demo\trigger_script.py)      
G01 X50 Y0                          
(PY,C:\demo\trigger_script.py)      
G01 X50 Y50                         
(PY,C:\demo\trigger_script.py)      
G01 X50 Y100                        
...

The scanning procedure will now be broken down.

Step 2: Run the g-code and collect data automatically

The user presses the Play button to run the g-code. When a g-code reaches the line where it should execute an external Python script, PlanetCNC allows two ways of script execution:

  • with (PY,<PATH-TO-PYTHON-SCRIPT>) command, a python script is executed and PlanetCNC waits for it to finish until it proceeds to the next line of g-code

  • with (PYTHR,<PATH-TO-PYTHON-SCRIPT>) command, a script is run on a separate thread and PlanetCNC ignores it from then on and proceeds with execution of g-code independently

In pattern_generation.py the placeholders for PY/PYTHR commands have been positioned in the right place and linked to the respective scripts that need to run at that particular line.

The Master Script is executed in a separate thread immediately as the g-code is run (with PYTHR). It is a status monitor script that navigates both the PlanetCNC control and measurement backends throughout the whole scan. All checks and background processes are also handled in the Master script (such as database communication, error handling, constant position logging etc.).

Once the machine reaches the first defined point, a Trigger Script is executed (with PY, therefore the system will wait until the script has finished). The trigger script sends a signal to the Master Script that the point has been reached. When the master script receives the signal it:

  • pauses the CNC machine with a pause command that is interpreted by PlanetCNC software
  • logs current position in the database with a timestamp

This part is marked red in the process diagram.

Once a new location entry is detected in the database, a beam measurement system is triggered (either it monitors the database itself, or it is triggered from the Master script). Once the measurement at this point is complete, the measured data are logged into the database and associated with this particular location (<font color=blue>blue</font> loop in the process diagram). The Master Script at this point receives an “OK” signal saying All data successfully dumped in the database. Continue!. The Master Script then triggers the g-code to continue with execution. This process repeats as the Trigger script is automatically run at the next point (<font color=magenta>magenta</font> loop in the process diagram). The CNC control system is completely time-independent from the measurement system. It waits for the measurement to finish and continues upon receiving the signal.

Describing all steps in relation to the respective g-code line:

(PYTHR,C:\demo\master_script.py)    ; Master script is ran in a separate thread navigating the whole procedure
G0 X0 Y0                            ; start at the origin
(PY,C:\demo\trigger_script.py)      ; trigger_script.py is executed. G-CODE halts at this line until the trigger_script.py sends the 'continue' signal
G01 X50 Y0                          ; after successful measurement at 0,0 the machine moves to the first measurement point
(PY,C:\demo\trigger_script.py)      ; trigger_script.py is executed
G01 X50 Y50                         ; second measurement point
(PY,C:\demo\trigger_script.py)      ; trigger_script.py is executed
G01 X50 Y100                         ; third measurement point
...

Result and it’s significance

With collecting measurements at each point we can create a 2D (or 3D) map of the measured beam parameter (light intensity, for example).

What we’ve achieved is an automated advanced beam characterization process which is highly valuable for extensive lab testing and collecting large amounts of data. Such a test rig can be relevant both during the development phase before we launch our product to the market, as well as afterwards when the product is already out there in the field.

It is very valuable to have an option unlocked to be able to run an automated series of tests that are easy to reproduce. With self-driven testing, we can spot sporadic and irregular defects that happen on unexpected occasions. In the figure below (with a much coarser mesh), an example of curious behaviour is clearly visible.

Conclusion

This example was a brief depiction of what is possible with advanced CNC solutions we build. A setup like this is completely standalone and unrelated to the operation taking place. We used beam characterization as an example which we might equally replace with any other process that is space-dependent and/or has to undergo a repeated operation at multiple defined locations (PCB testing/programming, material properties measurement, etc.).

What gives it such a high degree of freedom is that it is driven by a well-integrated set of external scripts written in Python, which virtually eliminates all limitations on what can be done. Adding advanced data processing, automatic measurement report generation, integration with other sensors, web-based operation, or almost any other module you can think of, is easily achievable in this setup.

PlanetCNC having available the support for Python integration has undoubtedly unlocked several possibilities that we shall explore in the future and happily integrate with our custom CNC solutions.

About the author

Luka Banović

Luka Banović

Is fascinated by the comprehensive experience that product development offers. He makes sure to maximize the value of our work every step of the way. He ensures complete functionality and usability of our solutions and always works hard to find a quick and efficient way of delivering our solutions to the customers.

Share this post

Share on facebook
Share on linkedin
Share on twitter
Share on google
Share on print
Share on email

Stay Updated!

Subscribe to receive News on interesting projects we’re involved in and open hardware projects we create.