Control a CNC Machine Using Java or Groovy
Feel like doing your own woodworking, or other, similar work? Take a look at how you can give commands to your own computer numeric control machine.
Join the DZone community and get the full member experience.
Join For Freelet’s discuss some topics ranging from work and occupations to machine tools, java/groovy automation, and estimating the future of humanity.
you will find in this article information about personal, computer-aided manufacturing in the in-home workshop, moving from the creator’s idea to the finished item. let’s consider the following approach for g-code instruction from the jvm and groovy/java to the nc machine tool to automate features that do not exist in an out - of -the- box system.
i hope this article will be useful for the programmers who work with jvm, for those interested in iot, for those who are familiar with diy work, for those who have a 3d printer or who are planning to buy one, and for those who made prototypes of any system and run the software at the raspberry pi/beagleboard black level. and what we cover, you could retry using open source software and open hardware.
this post was written based on my conference report from the central and eastern european software engineering conference in russia 2016 and my personal experience with hardware and software.
introduction
good morning, ladies and gentlemen. let me start by saying thanks to all of you for waking up this early in the weekend day and coming to the conference here. i'm happy to see you in the meeting room! we don't have much time for the report, so there won't be a question-answer section. but feel free to come and ask questions after the report.
your opinion on the report is very important to me. i would like to hold a meetup where we can use, in practice, the theory that i will describe today — operating a computer numeric control (cnc) machine. that will be open/free, and everyone in moscow at that time will be able to join up. i hope i will find a place to meet up soon and will publish the information at the meetup.com. those who are interested in it could write an email to me and i will send you the event notification. i will surely publish results of this meetup for the habrahabr readers, and you could be involved in it, too.
here is a report: "how to prevent the nc machine tool in your home workshop from becoming the cartoon characters 'two twins from the box.'" who has watched the cartoon and remembers the plot?
meeting room voice: «everyone has (watched it) »
great, so you are more or less familiar with the problem i will talk about. at the end of the report, we'll try to control the machine using java and groovy.
one more question for you. who is using nc machine tools, or works as an engineer, a designer, or technologist at a manufacturer? fortunately, i don't hear a response. it makes the things easier for me and you will not troll me on this point. those who use these technologies every day in production could find the price of the tools, the depth of subject immersion, and my style of explanation hilarious.
nc machine tools
what do we know about work? the occupation of the man and his work always were settled/organized in two ways:
either you control workers, or you get to work yourself and sweat it out together. now new, more easy-to-use tools were developed, which brings a third possibility.
they created technologies that make the work easier, life more pleasant, and give people the opportunity to be occupied with more intellectual things while letting the machines do the hard work.
there is an old cartoon, «vovka in the far away kingdom». there was a boy in this story who wanted his wishes to come true, but he didn’t want to make something himself and he didn’t want to learn anything.
in the far away kingdom, he has found two helpers, twins from the box. they were diligent and
made literally everything for him, but not the way he wanted
.
here is another cartoon "wings, feet, and tails."
a pheasant called an ostrich to the place filled with tasty things. when the ostrich started to run, the pheasant stopped him with the phrase “it's better to lose a day, then to fly in five minutes.” when you have to do routine tasks, you should think whether it is more effective to lose a day for its automation.
now we’ll talk about technologies of 1970s. these are machine tools controlled by stored programs. previously, there were punch cards; currently, flash drives are being used. the automatic machines could be of different sizes and kinematic diagrams and equipped with different tools, all of which carry out material processing at the plant.
things are simpler in the in-home workshop. basically, those machine tools are either aliexpress products or products of russian import merchants, which rebrand the celestial empire's products. you can get whatever you want in the assembled condition, or you can assemble the machine yourself from the parts.
if you're not worried about time and money, you have no experience, or any other similar reason, it will be easier for you to buy the ready-to-work machine. also, there are several projects: shapeoko, mechmate cnc router, zenbot cnc etc.
from the software point of view, there is not much difference between nc machine tool and the 3d printer or plotter. the operating tool differs, but a lot of 3d printers are controlled the same way as big industrial machines with the same instruction language.
there was a moment in the cartoon when the ostrich was running around, but he could not fly up. the pheasant keeps telling that wings are the best, but the ostrich insists that legs are. in the end, a lizard appears and says that most important thing is the tail. its tail saved her life in the beginning of the cartoon.
while working with nc machine tool, please be mindful of the danger for your life and safety and o bserve the safety procedures! an nc machine tool includes acutting tool, solid shifting, and rotating elements.
the contact with the frame of the electric drive is dangerous, especially if the insulation is damaged. if there's a fire in that area, cut the power before extinguishing anything.
from the model to g-code
almost every nc machine tool is operated with g-code. this is a text command and data format that is a worldwide standard now. but some of its implementations for machines of different manufacturers have extensions that could make it impossible to port control software to the other machine.
this example is really easy, but as a matter of fact, g-code is rarely handwritten. usually, any kind of model is made with the control software created at its base.
the path could be made in the drawing program, inkskape for example, and gcodetools turns it into a g-code program for a machine tool. this approach is good for designers.
those who used to work with blender can convert the 3d object to the 2.5d object using the open source blendercam.
industrial cad systems, with their prices over a gazillion dollars, are not accessible for a hobbyist. but the open source software freecad can be used.
it cannot be compared in ease and functionality with expensive cad/cam software suites, but it is sufficient for simple tasks. heekscad fork is established much better in its cam part and allows creating the control software on the base of the model/drawing.
numerical control
what exactly controls the motion axis of the machine tool, mill, or other tools?
the dedicated dsp controller of the nc machine tool comes with a usb port for a flash drive and a machine. these are either accessory boards for the single-board computer (raspberry pi, beagleboard, etc.) to control electric drive and spindle, or an ordinary pc with an lpt port or pci/pcie accessory board to interface with the machine electronics.
the hero of the day is a pc. you can control the machine and interpret the g-code using the open source software linuxcnc.
the software is based on the linux kernel real-time. its fork, machinekit, (for an arm ready-to-use iso image) is used for the raspberry pi, beagleboard black single-board computers. there is a machinekit build for the single-board c.h.i.p. at the price of $9.
linuxcnc is a constructor, and you can build almost any configuration on its base. this software controls both hobby-use machines and huge industrial nc machine tools. it is used not only for upgrading industrial nc machine tools, but also for the newly developed machine tool.
part of its components work in a real-time process, while less critical parts work in the environment with unscheduled delays. it is possible to extend the internal logic of the response to events in the linuxcnc using ld diagrams, and even shell scripts or command line programs.
control the machine using java and groovy
instead of its gui component, we’ll try to control the nc machine tool through the external api using software technologies more usable to us. those who are interested in this idea could find my poc apache camel component and jvm library on github.
there is a set of solutions for iot based on java — frequently, they are servers for iot gateways. now it is possible to integrate linuxcnc control of the machine into the complex cloud-based automation scripts with internet access. it is no trouble to build the working solution using thousands of ready-made components in this constructor with eclipse kura or apache camel. you can integrate computer vision libraries and add matching components or camera/network image capture tools.
let’s start with a little java example. we’ll use the api to send commands to control the machine. the library sends telnet commands to the linuxcncrsh process from the linuxcnc/machinekit.
//maven dependency com.github.igor-suhorukov:camel-gcode:0.1
import com.github.igorsuhorukov.gcode.gcodeclient;
public class gcode {
public static void main(string[] args) throws exception {
string hostname = "beaglebone.local";
int port = 5007;
try (gcodeclient gcodeclient = new gcodeclient(hostname, port)) {
system.out.println(gcodeclient.login("emc", "jvm", "1.0"));
gcodeclient.sendcommand("set mode manual");
gcodeclient.sendcommand("set estop off");
gcodeclient.sendcommand("set machine on");
for (int axis = 0; axis < 4; axis++)
gcodeclient.sendcommand("set home " + axis);
system.out.println(gcodeclient.sendcommand("get abs_act_pos"));
gcodeclient.sendcommand("set mode mdi");
gcodeclient.sendcommand("set mdi g0 x3 y4");
gcodeclient.sendcommand("set mdi g0 x5 y2");
}
}
}
this example moves the gantry/portal at the idle full speed first to the (3; 4) position, then to the (5; 2) position.
the next example is a groovy program based on the apache camel framework and com.github.igor-suhorukov:camel-gcode component for sending commands.
@grab('com.github.igor-suhorukov:camel-gcode:0.1')
@grab('org.apache.camel:camel-groovy:2.18.0')
@grab('org.apache.camel:camel-core:2.18.0')
@grab('org.apache.camel:camel-jetty:2.18.0')
@grab('org.slf4j:slf4j-simple:1.6.6')
import org.apache.camel.builder.routebuilder
import org.apache.camel.impl.defaultcamelcontext
import com.github.igorsuhorukov.smreed.dropship.mavenclassloader
def camelcontext = new defaultcamelcontext()
camelcontext.setname("i'll be back")
camelcontext.addroutes(new routebuilder() {
def void configure() {
from('jetty:http://0.0.0.0:9090/moveto').routeid('camelcnc')
.process{ it.in.body =
("set mdi g0 x${math.round(math.random()*10)} y${math.round(math.random()*10)} z1") }
.to('gcode:?host=beaglebone.local&port=5007&autohomeaxiscount=4')
}
})
addshutdownhook{ camelcontext.stop() }
camelcontext.start()
def hawtio = mavenclassloader.usingcentralrepo()
.formavencoordinates('io.hawt:hawtio-app:2.0.0').loadclass('io.hawt.app.app')
thread.currentthread().setcontextclassloader(hawtio.getclassloader())
hawtio.main('--port','10090')
to run it, use: java -jar groovy-grape-aether-2.4.5.4.jar camelcnc.groovy
in this example, the groovy application links the jetty and g-code components, builds a route to the groovy dsl camel, and loads and runs the web console for hawtio monitoring. at each http request at host:9090/moveto, the new command to move at the random positions along the x and y axis is created and sent to the linuxcnc and machine to be complete. then, the generated command returns to the client as an http response. the web console listens at the :10090/hawtio address.
the apache camel , groovy, and hawtio magic will help you to make complex things quite easily.
the perfect f uture ...
we can try to predict the future. we can distinguish a trend: all things that could be automated when it makes economic sense to do are being automated. recently, there has been news about foxconn in china: 40,000 machines at the manufacturer caused a mass layoff.
i’m not nostradamus, but automation might destroy workplaces in times to come. it is also quite clear that demand for automation will increase with time. and people who are familiar with these technologies will be still more sought-after at the labor market, but only up to a point… and individual production at the nc machine tool level will be in every workshop.
what will be the future like?
will the robots enslave or destroy humanity?
or people will still control robots.
it depends on us and our skills and decisions today!
conclusion
a lot of things that we make with our hands and routine tasks usually could be automated. in this case, we should start with the design of the model. the next step is to turn the model into the control program for the machine. these commands will be executed by some kind of nc machine, and all elements in the production process that are reasonable to automate should be automated. the computer vision technologies and feedback sensors should be used, too. surely it is unacceptable to run between a computer and a machine with a flash drive — the commands will be transmitted through the network. let the machine make all the work . but to prevent the tool from becoming the twins from the box, you have to learn the technology and have a good handle in the process details. or you’ll have to share financial resources with those who understand these technologies and get a different result from the one you wanted.
Opinions expressed by DZone contributors are their own.
Comments