logo elektroda
logo elektroda
X
logo elektroda

CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation

nuclear 1323 13
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
📢 Listen (AI):
  • Hi

    I would like to present another project of mine, this time it is a CNC controller.


    CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation .

    CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation .

    There are already a bunch of different types of CNC machine tool controllers on the market, but as usual I wanted to make something of my own.

    Key design considerations:
    - Location of all time-critical operations in the execution module.
    - Connection of the module with a PC using an Ethernet interface.
    - Concurrent operation of 4 axes, expandable to 5.
    - Possibility of future addition of encoder operation on axes.
    - Inverter control with speed regulation.
    - PC application running in Windows environment.

    I decided to use the STM32H725 microcontroller - powerful, has lots of counters and at an affordable price.
    As standard, I used FreeRTOS and the libraries I had already used in previous projects. The most important thing for me was to generate control pulses while maximising the use of hardware resources in order to achieve the greatest possible timing stability.
    To do this, I used a serial connection of counters, the master is used to generate a variable frequency signal which corresponds to the travel speed, this signal then drives a set of counters - one for each axis, plus one for synchronisation.
    In this way, I achieved an easy division between direction and speed of movement. This involves setting the ARR and PSC registers for each axis counter and the synchronising counter, respectively, for a given section of movement. The result is a specific number of pulses for each axis.
    With the master counter, on the other hand, we can freely control the speed of travel of this section. By using the preload mode for the counters, we are able to load the parameters of the next section before the current section ends - we load the new values in the interval between the last pulse and the end of the section.
    As a result, the episodes are executed without any timing disturbances.
    The module receives simple commands from the PC, i.e. only segments and arcs, all already in machine coordinates. The arcs are then subdivided into short segments, and together with the straight segments are converted to register settings and buffered.
    Each segment also has a pre-calculated start, end and maximum speed.

    I decided to write the PC software in C# .net Windows Forms, in the Visual Studio environment. The choice was based on the simplicity of writing an extensive application with a GUI and, at the same time, not having to perform time-critical operations.
    The main view of this application is to parse the G-Code and process it into simple commands sent to an external module.
    The processing consists of several stages, the first one being the conversion of the G-Code from a string to an array of commands, at which stage the correctness of the syntax is checked, e.g. whether all commands are supported, variables defined and loops and conditional expressions properly closed.
    The next stage is the processing of commands into a simple form - mathematical expressions are calculated, loops and subroutines are executed. At this stage, the maximum speed for each simple command (segments and arcs) is also calculated. The calculated fragments are placed in a fifo buffer.
    The final stage is to scan the fifo buffer to calculate the allowed speed when passing between segments, taking into account the angle between the segments, the distance to the end of the buffer and the allowed accelerations. Once the start and end speeds have been calculated
    the segments are already packed and sent to the control module.

    The programme supports most of the standard G-Code expressions, and I have also added new G2.1 and G3.1 codes - these realise arcs in any plane parallel to the Z-axis - useful, for example, when milling decors.
    The second additional functionality is the automatic measurement of surface irregularities - the specified area is probed and the measured grid of corrections is sent to the execution module. These corrections are then taken into account for all movements in this area. The function was developed for
    The function was developed for the precise milling of PCBs, but can also be used for engraving.

    The controller works stably, I have been using it for some time in my milling machine and am satisfied with it. Using the MONO library, the application also runs on Linux with limited functionality - only the 3D visualisation does not work.

    What I would do differently:
    Basically probably just minor tweaks to the PCB, the circuit from the limiters could be improved and maybe consider completely hardware handling of the emergency stop and hard limit, bypassing the uC but that's mind blowing.

    What else I would like to do:
    First of all as I often write code by hand I would like to improve the code editor, add syntax highlighting, keyboard shortcuts etc. The second thing to improve is the 3D visualisation - so far it's quite clunky.
    I'm also considering another approach, i.e. replacing my PC application with some sort of LinuxCNC plug-in, while for now I don't yet know if this is possible and how to go about it but maybe I'll look into it at some point.

    The program was created with a specific PCB in mind, but there's nothing to stop it running on other hardware, e.g. an EVM, as the PCB only has buffers and isolators on the inputs and outputs, so you can leave them out for experiments. You can also try to use another uC, it is important first of all that it has enough counters - 1 master + 1 slave 32bit + Number of axes x 1slave + 2 (speed control and HAL). It is also highly recommended that it has an FPU, as a large part of the calculations, especially speed and conversion of arcs into segments, is performed on float variables.

    Code and PCB design in KiCAD format: https://github.com/r-gal/CNC
    PC control program: https://github.com/r-gal/CNC_app


    CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation .
    CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation .
    CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation .
    CNC controller on STM32H725 with FreeRTOS, Ethernet and support for 4-5 axes - project presentation .

    Cool? Ranking DIY
    About Author
    nuclear
    Level 16  
    Offline 
    nuclear wrote 188 posts with rating 127, helped 18 times. Live in city Siepraw. Been with us since 2003 year.
  • ADVERTISEMENT
  • #2 21602896
    tesla97
    Level 18  
    What is the maximum step frequency?
  • ADVERTISEMENT
  • #3 21602939
    TechEkspert
    Editor
    Very impressive! You can also show on film how the machine works.
    I associate the project with multi-axis robot control and connected motion.

    Looking at the amateur machines that used LPT, rarely RS-232, then USB, I see the use of Ethernet as a very good idea and this is how controllers should connect to PCs nowadays. Unless we have a different kind of environment and Modbus/Profibus etc is native.
  • #4 21602981
    nuclear
    Level 16  
    tesla97 wrote:
    What is the maximum step frequency?
    .
    With the current implementation, it is theoretically 75kHz. In practice this is no longer a useful range as the granularity of the speed control increases to absurd values - in the range above 20khz only values of 75,50,37,30,25,21 kHz can be set. For controlling stepper motors, in my opinion, this is sufficient.
    If there is such a need, it would be possible to get some more in slave counters, but at the same time it would be necessary to move the program to ram memory to make it execute faster.
  • ADVERTISEMENT
  • #6 21603613
    nuclear
    Level 16  
    As it's a hobby project I don't put a price on the time spent on it, if I were to do it commercially I would have to sell a few pieces at a price like the one in the link. As for the price of materials, I think it is possible to close around 200zl, whether making a board from scratch or using e.g. Nucleo and making a board with buffers.
    This reminds me that I am planning to add a cable manipulator.
  • #7 21603623
    Jacekser
    Level 26  
    The colleague is programmatically implementing the trajectory for the G-code himself, as I understand it?
    nuclear wrote:
    I am also considering another approach, i.e. replacing my PC application with some LinuxCNC plug-in,...
    .
    How about MACH, as implemented by CS-LAB? There is a (MACH3) SDK available for it.
    Anyway - congratulations on the project!
  • #8 21604124
    nuclear
    Level 16  
    TechEkspert wrote:
    it is also possible to show on film how the machine works.


    Some chips:


    .

    Close-up of a PCB with etched tracks on a dark workbench. .
    The PCBs also come out quite sensibly:
    Close-up of two PCB boards with etched tracks on a workshop table. .

    Jacekser wrote:
    A colleague is programmatically implementing the trajectory for the G-code himself, as I understand it?
    .

    I'm not sure I understand the question correctly - after all, the G-code already defines the trajectory, I just process it into straight segments and determine the velocities,


    Jacekser wrote:
    And maybe MACH, as implemented by CS-LAB? There is a (MACH3) SDK available for it.
    Anyway - congratulations on the project
    .

    I rather prefer to stay with free solutions, which is why I didn't even look at MACH too closely.
  • #9 21604171
    TechEkspert
    Editor
    What is the spindle drive?
    Is that gold button in the background an emergency stop?
  • #10 21604204
    nuclear
    Level 16  
    TechEkspert wrote:
    What is the spindle drive?
    Is that gold button in the background an emergency stop?


    The spindle is a Vevor 1.5kW with water cooling. The gold button is the tool height sensor, the emergency stop is the red one on the right on the housing.
  • ADVERTISEMENT
  • #11 21604451
    Jado_one
    Level 22  
    Hello,
    A beautiful project! :-) Well, and most importantly - comprehensive, because both the processor and the application.
    In-house projects always have the quality of meeting all our needs, and ready-made ones always lack something or work differently than we would like.
    I wonder how long it took to create it?
    Was it more difficult to write a PC application or write code for the STM?
    Do you plan to add a local small display with at least current position parameters, etc...?
    The project interested me, as I will soon be doing something similar (also on STM32H7xx - beautiful processors by the way ;-) ) , only that a controller for a CNC lathe (maybe that's even saying too much - I guess you could call it more of an electronic guitar for a lathe). Not too much to use off-the-shelf, as you need synchronisation with the spindle rotation to cut the threads. Especially as I am running notoriously under Linux ;-) .
    I'll have a bit of a peek at how you solved the power supply for the processor - as I haven't yet made my own board on the STM32H7, and there you can do the power supply in several ways.
    Any further plans to expand the controller? A manual control (position referrer I think it's called) would certainly be useful.
    A touch probe?
    Anyway, congratulations on a successful implementation :-) .
  • #12 21604465
    TechEkspert
    Editor
    Is the communication over TCP or UDP?
  • #13 21604777
    nuclear
    Level 16  
    Jado_one wrote:
    .
    I wonder how long it took to create it?


    It's hard to say exactly how long it took, I started writing the code about a year ago, but I did it on the fly because I had other projects to take care of, it's not the first version either, I abandoned the idea twice and started basically from scratch.

    Jado_one wrote:
    .
    Was it more difficult to write a PC application or write code for the STM?
    .

    I don't have a clear answer to this question either, both parts had their more difficult parts. In the STM code, it took me a long time to tune the whole set of counters so that it worked exactly, i.e. so that everything matched every pulse, so that all the axis counters starting at the same moment at the end of a segment were 0, and this at any combination of the number of pulses per segment. I also had a few approaches to the issue of converting arcs into segments, and finally settled on a version in which I generate an arc in the XY plane and then treat it with a rotation matrix, which allowed me to handle arcs in any plane.
    The PC application, on the other hand, meant that I had to retrain in this environment, which is not natural for me - I prefer to work "lower". Here, there was a lot of work due to the whole GUI, some design work was also required to handle the tool thickness compensation - calculating a new trajectory at the transitions between successive segments, some geometry had to be processed here.

    Jado_one wrote:
    .
    Are you planning to add a local small display with at least current position parameters, etc...?
    .
    There's an idea, the board even has a connector prepared for an alphanumeric lcd display, but it's not the most important thing for me at the moment. Initially I considered the option of working without a PC for specific applications but I have moved away from this idea indefinitely.

    Jado_one wrote:
    .
    The project interested me, as I will soon be doing something similar (also on STM32H7xx - beautiful processors by the way ) , only that a controller for a CNC lathe (maybe that's even too much to say - I guess you could call it more of an electronic guitar for a lathe). Not too much to use an off-the-shelf one, as you need synchronisation with the spindle rotation to cut the threads. Especially as I operate notoriously under Linux
    I'll have a little peek at how you solved the power supply for the processor - as I haven't yet made my own board on STM32H7, and there you can do the power supply in several ways.


    All in all, a controller for a milling machine or a lathe is one thing in the overall design, maybe some G-code commands would need to be implemented. Synchronisation with spindle rotation is not a problem in theory - the spindle is treated as a rotary axis and controlled like any other axis. For example, if you give the G1 X100 A360 command, it moves the knife by 100mm while rotating the spindle by 360 degrees. My controller can do this and I think most of the ones on the market should too.
    The STM32H7xx are actually cool, the capabilities are mass and the price is affordable - I migrated from STM32F4xx as STM32H7xx became cheaper. I made the power supply as simple as possible - I didn't use the on-board inverter but used the on-board LDOs which is the default power supply.

    Jado_one wrote:
    .
    Any further plans for expansion of the controller? A manual control (position transmitter I think it's called) would certainly be useful..
    Touch probe?
    .
    A manual position finder is now probably #1 in the queue to do because I miss it. I'm not planning a probe for now, I'm mainly doing woodworking and here positioning "on paper" is completely sufficient. I have a tool length measurement and sampling of conductive surfaces with the tool itself (e.g. for PCB milling) and this is enough for me for now.

    TechEkspert wrote:
    Communication is over TCP or UDP?
    .
    I had a dilemma as to what to use, in the end I decided on UDP with software transmission control. Each command has a sequence number and if necessary it is retransmitted - ie if we don't get an acknowledgement. A bit like TCP only we acknowledge individual commands rather than bytes. TCP would also work just as well overall.
  • #14 21616976
    nuclear
    Level 16  
    Hey, as if anyone by chance is interested in testing my driver and happens to have a NUCLEO board, I've uploaded a version prepared for the NUCLEO-F439ZI platform to github, the project is in STM32CubeIDE format - using this tool, just download the repo, compile and upload. It probably can't get any simpler ;) .
    https://github.com/r-gal/CNC_Nucleo_stm32f439
📢 Listen (AI):

Topic summary

A CNC controller project based on the STM32H725 microcontroller was presented, featuring FreeRTOS, Ethernet connectivity, and support for 4 to 5 axes with potential future encoder integration and inverter speed control. The design prioritizes locating all time-critical operations within the execution module and uses Ethernet for PC communication, running a Windows-based application. The maximum step frequency is theoretically 75 kHz, practically limited to around 20 kHz for effective speed granularity. The project includes custom PCB designs and uses a Vevor 1.5 kW water-cooled spindle. The G-code trajectory is processed into straight segments with velocity calculations, avoiding proprietary solutions like MACH in favor of free software. Development took about a year with iterative rewrites, balancing challenges between STM32 firmware and PC application development. Communication protocol specifics (TCP/UDP) were queried but not definitively answered. A version for the NUCLEO-F439ZI board is available on GitHub for testing. The project aims to provide a comprehensive, customizable CNC controller alternative to commercial products, with an estimated material cost around 200 PLN.
Summary generated by the language model.
ADVERTISEMENT