Machine Learning G-Code Optimization †
Abstract
:1. Introduction
2. Methodology
2.1. Experimental Model Selection
2.2. Slicing Software
2.3. Coding
2.4. Data Preprocessing
2.4.1. Library Imports
- import numpy as np
- import pandas as pd
- from sklearn.cluster import KMeans
2.4.2. Data Definition
2.4.3. DataFrame Creation
- df = pd.DataFrame(data)
2.5. K-Means Model Training
2.5.1. K-Means Algorithm Initialization
- kmeans = KMeans(n_clusters = 5)
2.5.2. Model Fitting
- kmeans.fit(df)
2.5.3. Assigning Cluster Labels
- df[‘cluster’] = kmeans.labels_
2.6. Optimal Parameter Selection
2.6.1. Identifying the Most Frequent Cluster
- optimal_cluster = df[‘cluster’].value_counts().idxmax()
2.6.2. Calculating Average Parameters for the Optimal Cluster
- df[df[‘cluster’] == optimal_cluster] filters the DataFrame to select only the rows belonging to the most frequent cluster (optimal_cluster).
- drop([‘cluster’], axis = 1) removes the ‘cluster’ column from the filtered rows as it is no longer needed for the average parameter calculation.
- mean() computes the mean of each column in the filtered DataFrame, resulting in a pandas Series with average values for each parameter.
- to_dict() converts this Series into a dictionary where the keys are parameter names and the values are the computed averages. The result is stored in the variable optimized_params.
- Additionally, the printing parameters were set to an infill speed of 80 mm/s and an infill density of 10%.
- infill_speed = 80
- infill_density = 10
2.7. G-Code Optimization
2.7.1. Import the Regular Expression Module
- import re
2.7.2. Define the Function
- def optimize_gcode(input_file_path, output_file_path, params, infill_speed, infill_density):
2.7.3. Read the Input File
2.7.4. Initialize the List of Optimized Lines
2.7.5. Process Each Line to Apply Optimizations
2.7.6. Update Layer Height Comment
2.7.7. Adjust Print Speed
2.7.8. Adjust First Layer Speed
2.7.9. Adjust Perimeter Speeds
2.7.10. Adjust Infill Speed
2.7.11. Modify Extruder and Bed Temperatures
- if ‘M104’ in line or ‘M109’ in line:
2.7.12. Adjust Material Flow Rate
2.7.13. Update Infill Density
2.7.14. Write to Output File
2.8. Deployment of Optimization Function
- input_file_path = r”file_path”
- output_file_path = r”file_path”
- optimize_gcode(input_file_path, output_file_path, optimized_params, infill_speed, infill_density)
- input_file_path: the location of the G-code file to be optimized.
- output_file_path: the destination path for the optimized G-code file.
- optimized_params: a dictionary of parameters including layer height, print speed, first layer speed, perimeter speed, extruder temperature, bed temperature, and material flow rate.
- infill_speed: the speed of infill printing, in millimeters per second.
- infill_density: the percentage of infill density to be applied.
3. Results and Discussion
Results
4. Conclusions
Author Contributions
Funding
Institutional Review Board Statement
Informed Consent Statement
Data Availability Statement
Conflicts of Interest
References
- Daskalogrigorakis, G.; Kirakosian, S.; Marinakis, A.; Nikolidakis, V.; Pateraki, I.; Antoniadis, A.; Mania, K. G-Code Machina: A Serious Game for G-code and CNC Machine Operation Training. In Proceedings of the 2021 IEEE Global Engineering Education Conference (EDUCON), Vienna, Austria, 21–23 April 2021. [Google Scholar] [CrossRef]
- Bae, H.C. G-code 3D Method and Apparatus for Managing 3D Printing Using G-code. 2019.
- Negi, P.K.; Ram, M.; Yadav, O.P. CNC Machine Programming Codes (G-Codes and M-codes). In Basics of CNC Programming, 1st ed.; Taylor & Francis: New Dehli, India, 2018. [Google Scholar] [CrossRef]
- John, P.; Komma, V.R.; Bhore, S.P. Development of MATLAB Code for Tool Path Data Extraction from the G Code of the Fused Filament Fabrication (FFF) Parts. Eng. Res. Express 2023, 5, 025018. [Google Scholar] [CrossRef]
- Pascual, A.; Ortega, N.; Plaza, S.; Holgado, I.; Arrizubieta, J.I. A RE Methodology to Achieve Accurate Polygon Models and NURBS Surfaces by Applying Different Data Processing Techniques. Metals 2020, 10, 1508. [Google Scholar] [CrossRef]
- Husiev, O.; Nikiforova, T. Research of the Converting Stages for the Volume Model of the Product into the Control Code for a 3D Printer in the Context of Automated Construction of 3D Printing Technology. Ukr. J. Constr. Archit. 2022, 2312, 250822. [Google Scholar] [CrossRef]
- Eva, S.-C.; Sover, A.; Ermolai, V. The Impact of the G-code Flavour Selection in FFF. In Proceedings of the 4th International Conference. Business Meets Technology, Ansbach, Germany, 7–9 July 2022. [Google Scholar] [CrossRef]
- Xu, H.; Wu, J.; Qin, Y.; Lin, X.; Song, H.; Zhu, W. Method for Converting Numerically Controlled G-Codes. Patent WO2007078025A1, 12 July 2007. [Google Scholar]
- Aciu, R.-M.; Ciocarlie, H. G-code Optimization Algorithm and Its Application on Printed Circuit Board Drilling. In Proceedings of the 2014 IEEE 9th IEEE International Symposium on Applied Computational Intelligence and Informatics (SACI), Timisoara, Romania, 15–17 May 2014. [Google Scholar] [CrossRef]
- Beckwith, C.; Naicker, H.S.; Mehta, S.; Udupa, V.R.; Nim, N.T.; Gadre, V.; Pearce, H.; Mac, G.; Gupta, N. Needle in a Haystack: Detecting Subtle Malicious Edits to Additive Manufacturing G-Code Files. IEEE Embed. Syst. Lett. 2022, 14, 111–114. [Google Scholar] [CrossRef]
- Li, X.; Tan, H. K-Means Algorithm Based on Initial Cluster Center Optimization. In Cyber Security Intelligence and Analytics; Lecture Notes in Computer Science; Springer: Cham, Switzerland, 2020; Volume 12337, pp. 477–485. [Google Scholar] [CrossRef]
- You, Y.; Liu, Z.; Liu, Y.; Huang, Y.; Huang, Q. K-Means Module Division Method of FDM 3D Printer-Based Function–Behavior–Structure Mapping. Appl. Sci. 2023, 13, 7453. [Google Scholar] [CrossRef]
- Mathur, M.B. K-means Optimizer: An Efficient Optimization Algorithm for Predicting the Uncertain Material Parameters in Real Structures. In Proceedings of the 5th International Conference on Numerical Modelling in Engineering, Ghent, Belgium, 23–24 August 2022; Lecture Notes in Electrical Engineering. Springer: Singapore, 2023; Volume 833, pp. 95–106. [Google Scholar] [CrossRef]
- Qin, X.; Li, J.; Hu, W.; Yang, J. Machine Learning K-Means Clustering Algorithm for Interpolative Separable Density Fitting to Accelerate Hybrid Functional Calculations with Numerical Atomic Orbitals. J. Phys. Chem. A 2020, 124, 7397–7407. [Google Scholar] [CrossRef] [PubMed]
- Vinué, G.; Simó, A.; Alemany, S. The K-means Algorithm for 3D Shapes with an Application to Apparel Design. Adv. Data Anal. Classif. 2016, 10, 297–312. [Google Scholar] [CrossRef]
- Fitri, M.; Wardhani, A.R.; Purnomowati, W.; Vitianingsih, A.V.; Maukar, A.L.; Puspitarini, E.W. Potential Customer Analysis Using K-means with Elbow Method. JIKO (J. Inform. Dan Komput.) 2023, 7, 911. [Google Scholar] [CrossRef]
- Géron, A. Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, 2nd ed.; O’Reilly Media: Sebastopol, CA, USA, 2019. [Google Scholar]
- 3DBenchy. 3DBenchy Brochure. 2015. Available online: http://www.3dbenchy.com/wp-content/uploads/2015/04/3DBenchy_Broschure_3DBenchy.com_.pdf (accessed on 31 July 2024).
- Wang, Y.; Tang, M.; Zhou, Z.; Wang, C. Study on the temperature field distribution of the interface and process parameter optimization for material extrusion of PLA. Polym. Eng. Sci. 2024, 64, 718–732. [Google Scholar] [CrossRef]
- Artillery. (n.d.). Manual de la Impresora 3D Sidewinder X1, Hornet V4. Manuals Plus. Available online: https://manuals.plus/es/artillery-hornet/sidewinder-x1-3d-printer-v4-manual (accessed on 31 July 2024).
Cura | Prusa Slicer | Ideamaker | |
---|---|---|---|
Developer | Ultimaker | Alessandro Ranellucci | Raise 3D |
Algorithm | Arachne Engine | Slic3r | Pathfinder |
Principle | Voronoi Diagram | Planar Intersection, tessellación | Algorithms A star Dijkstra |
Disadvantage | Unnecessary non-extrusion movements and longer printing time. | False retraction movements and other non-essential movements. | Low trajectory optimization |
Parameter | Measures | ||||
---|---|---|---|---|---|
Layer height (mm) | 0.2 | 0.2 | 0.3 | 0.1 | 0.3 |
Print speed (mm/s) | 50 | 60 | 45 | 55 | 65 |
First layer speed (mm/s) | 20 | 30 | 25 | 15 | 35 |
Perimeter speed (mm/s) | 40 | 50 | 45 | 35 | 55 |
Extruder Temp (°C) | 200 | 210 | 195 | 205 | 220 |
Bed Temp (°C) | 60 | 65 | 55 | 60 | 70 |
Flow rate (%) | 100 | 95 | 105 | 90 | 110 |
Time | Material | Surface Finish | Accuracy | ||
---|---|---|---|---|---|
CURA | Original | 48 m 14 s | 3724 mm | 15 μm | 99.41% |
Optimized | 39 m 33 s | 3538 mm | 14.8 μm | 99.39% | |
% variation | −18% | −5% | −1% | −0.02% | |
PRUSASLICER | Original | 1 h 22 m 21 s | 4297 mm | 16.5 μm | 99.4% |
Optimized | 51 m 56 s | 4082 mm | 16.3 μm | 99.38% | |
% variation | −36.90% | −5% | −1% | −0.01% | |
IDEAMAKER | Original | 52 m 48 s | 3908 mm | 18 μm | 99.13% |
Optimized | 43 m 12 s | 3712 mm | 17.7 μm | 99.12% | |
% variation | −18.18% | −5.02% | −1.5% | −0.01% |
Layer Height (mm) | Extruder Temp (°C) | Bed Temp (°C) | Print Speed (mm/s) | Flow Rate (%) |
---|---|---|---|---|
0.1 | 195 | 55 | 45 | 90 |
0.15 | 200 | 60 | 50 | 95 |
0.2 | 205 | 65 | 55 | 100 |
0.3 | 210 | 70 | 60 | 105 |
Experiment | Layer Height (mm) | Extruder Temp (°C) | Bed Temp (°C) | Print Speed (mm/s) | Flow Rate (%) | Fabrication Time Variation (%) | Material Consumption Variation (%) | Accuracy Variation (%) |
---|---|---|---|---|---|---|---|---|
1 | 0.1 | 195 | 55 | 45 | 90 | −12.5 | −2.5 | −0.5 |
2 | 0.1 | 200 | 60 | 50 | 95 | −15 | −1.5 | −1 |
3 | 0.1 | 205 | 65 | 55 | 100 | −17.75 | −3 | −0.75 |
4 | 0.1 | 210 | 70 | 60 | 105 | −20.5 | −4.25 | −1.5 |
5 | 0.15 | 195 | 60 | 55 | 105 | −19 | −3.75 | −1.25 |
6 | 0.15 | 200 | 55 | 60 | 100 | −21.5 | −2.25 | −1.75 |
7 | 0.15 | 205 | 70 | 45 | 95 | −23.25 | −4.5 | −0.85 |
8 | 0.15 | 210 | 65 | 50 | 90 | −22 | −3.5 | −1 |
9 | 0.2 | 195 | 65 | 60 | 95 | −24 | −5 | −0.01 |
10 | 0.2 | 200 | 70 | 55 | 90 | −21 | −3 | −1.1 |
11 | 0.2 | 205 | 55 | 50 | 105 | −19.5 | −2.5 | −1.4 |
12 | 0.2 | 210 | 60 | 45 | 100 | −16.75 | −2.75 | −1.2 |
13 | 0.3 | 195 | 70 | 50 | 100 | −14 | −1.75 | −1.3 |
14 | 0.3 | 200 | 65 | 45 | 105 | −10 | −1.25 | −1.5 |
15 | 0.3 | 205 | 60 | 60 | 90 | −7.5 | 0.5 | −1.75 |
16 | 0.3 | 210 | 55 | 55 | 95 | −5 | 1.25 | −1.9 |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2024 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Lasluisa-Naranjo, H.; Rivas-Lalaleo, D.; Vaquero-López, J.; Cruz-Moposita, C. Machine Learning G-Code Optimization. Eng. Proc. 2024, 77, 32. https://doi.org/10.3390/engproc2024077032
Lasluisa-Naranjo H, Rivas-Lalaleo D, Vaquero-López J, Cruz-Moposita C. Machine Learning G-Code Optimization. Engineering Proceedings. 2024; 77(1):32. https://doi.org/10.3390/engproc2024077032
Chicago/Turabian StyleLasluisa-Naranjo, Héctor, David Rivas-Lalaleo, Joaquín Vaquero-López, and Christian Cruz-Moposita. 2024. "Machine Learning G-Code Optimization" Engineering Proceedings 77, no. 1: 32. https://doi.org/10.3390/engproc2024077032
APA StyleLasluisa-Naranjo, H., Rivas-Lalaleo, D., Vaquero-López, J., & Cruz-Moposita, C. (2024). Machine Learning G-Code Optimization. Engineering Proceedings, 77(1), 32. https://doi.org/10.3390/engproc2024077032