четверг, 17 ноября 2016 г.

CNC machine control by using Java/Groovy

Let’s sequentially discuss topics from work and occupation, machine tools, java/groovy automation to the estimating the future of humanity.
You will find in this article information about personal computer-aided manufacturing in the in-home workshop, the way from the creator’s idea to the end/finished item. Let’s consider the following approach for the G-code instruction sending from the JVM and groovy/java to the NC machine tool to automatize features that do not exist in out-of-the-box system.
Image title
I hope this article will be useful for the programmers, who work with JVM, for those who is interested in the IoT, those who are familiar to DIY, those who have 3D printer or who is planning to buy it, those who made prototype of any system and run the software at the Raspberry PI/Beagleboard Black. Things I will tell about you could retry using the open source software and open hardware.
This post is written based on my conference report from Central & 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 to wake up this early in the weekend day and come to the conference\here. I'm happy to see you in the meeting room! We have not much time for the report, so there won't be question-answer section. But feel free to come and ask questions after the report. Also, there is an e-mail address at the last slide and I'll be glad to answer your questions this way too
Your opinion on the report is very important to me. I would like to hold a meetup, where we can use in practice theory I will describe today, operate the NC machine tool. That will be open/free event and all who will be in Moscow at that time will be able to join up. I hope I will find a place to meetup any time soon and publish the information at the meetup.com. Those who are interested in it could write an e-mail to me and I will send you the event notification. The note to article: I will surely publish results of this meetup for the habrahabr readers, and you could be involved in it.
Here is a report "How to prevent the NC machine tool in the in-home workshop become cartoon characters "two twins from the box"". Who has watched the cartoon and remembers the plot?

Meeting room voice: «Everyone have (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 questions for you. Who are using NC machine tools, work as an engineer, designer or technologist at the manufacture? Fortunately I hear negative responses. 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. 
Image title

Either you control workers, or you get to work yourself and you make it with the others, sweat it out together.

Now new, more easy to use tools were developed by the inventors and engineers unlike the depicted situation. 
Image title

Technologies that make the work easier, life more pleasant and give people the opportunity to be occupied with more intellectual things and let 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. 
Image title

In the Far Away Kingdom he has found two helpers, twins from the box. They were diligent and made literally everything instead of him, but not the way he waned

Here is another cartoon "Wings, Feet and Tails"
Image title

The Chir pheasant was calling the Ostrich to the place where there were a lot of tasty things. When the Ostrich has started to run the Chir pheasant has stopped him with an “It's better to lose a day, then to fly in five minutes” phrase. 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 1970’s. These are machine tools controlled by the stored programs. Previously there were punched cards; currently the flash drives are being used. The automatic machines could be of different size, kinematic diagrams and equipped with different tools, that carries out materials processing at the manufacture/plant. 
Image title

Things are simpler in the in-home workshop. Basically those machine tools are either AliExpress products, or products of the Russian import merchants, that 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 spare parts. 
Image title

If you don't want to spare your time and money, you have no experience, access to the machines for production of the portal frames; 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.

What do you need the NC machine tool in your in-home workshop and what can you do with it? You can perform woodwork, machine ornamental panels for redecoration. Or you have a creative idea, and you could not find readymade details for it. You can also make a detail for some kind of consumer electronics. For example, spare part for the tea-kettle or the steamer from nonferrous metal if there spare parts are out of stock or there are not available in the service centers. You can do spare parts for the other NC machine, the same thing is for 3D printer, but you are not limited to plastics as a material of construction for the parts.

From the software point of view there is no much difference between NC machine tool, 3D printer or plotter. The operating tool differs but a lot of 3D printers are controlled the same way as the 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 Chir pheasant keeps telling that wings are the best and the Ostrich insists that legs are. In the end the Lizard appears and tells that most important thing is the tail. Its tail has saved her life in the beginning of the cartoon. 
Image title

While working with NC machine tool please be mindful of the danger for your life and safety and observe the safety procedures! NC machine tool includes cutting tool, solid shifting and rotating elements. 
Image title

The contact with the frame of the electric drive is dangerous, especially if the insulation is damaged. Do not even try to extinguish with water the powered electric driver before cutting the power.

From the model to G-code


Almost every NC machine tool is operated with the 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.
Image title

This example is really easy, but as a matter of fact the G-code is rare to be handwritten. Usually the any kind of model is made and on its base the control software is being created.

The path could be made in the drawing program, Inkskape for example, and in GCodeTools opensource software turns it into a g-code program for a machine tool. This approach is good for designers. 
Image title

Those who get used to work with Blender, can convert the 3D object to the 2.5D object using the opensource BlenderCAM.
Image title

Industrial CAD systems with their prices over gazillion dollars are not accessible for a hobby-use. But the opensource software FreeCAD can be used. 
Image title

It is 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 it allows creating the control software on the base of the model/drawing.
Image title

Numerical control


What exactly controls motion axis of machine-tool, mill or other tool of the machine?

Image title

Dedicated DSP controller of the NC machine tool with a USB port for a flash drive and a machine. These are either accessory boards for the single-board computer (Raspberry PI, BeagleBoard) to control electric drive and spindle, or ordinary PC with LPT port or PCI/PCIe accessory board to interface with the machine electronics.

The hero of the day is a PC. Because you can control the machine and interpret g-code using the open source software LinuxCNC. 


The software based on the Linux kernel real-time. Its fork Machinekit (for 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 works in a real-time process, less critical part works in the environment with unscheduled delays. It is possible to extend 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 usual for us. Those who are interested in this idea could find my PoC Apache Camel component and JVM library on the github.

There is 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 the Internet access. It is no trouble to build the working solution using thousands of ready-made components in this constructor with Eclipse Kura, 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 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 Apache Camel framework and com.github.igor-suhorukov:camel-gcode component for sending commands for the machine. 

CamelCNC.groovy
 
To run it use command: java -jar groovy-grape-aether-2.4.5.4.jar CamelCNC.groovy
Image title
Image title

In this example the Groovy application links jetty and gcode components, build a rout to the groovy DSL camel, and load and run 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 started to the LinuxCNC and machine to be complete. Then the generated command returns to the client as an http response. Web Console listens at the :10090/hawtio address.

The Apache Camel, Groovy and HawtIo magic will help you to make quite complex things easy.

The perfect future...


We can try to predict the future. 

We can distinguish a trend: all things that could be automatized when it makes economic sense to do, they are automatizing. Recently there has been news about Foxconn in China: 40000 machines at manufacture cause the mass layoff. Most of the machine details they produce themselves as well as driver electronics.

I’m not the Nostradamus, but it is borne in on that automatization destroys workplaces in times to come. It is also quite clear that demand for automatization of production 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 will come in every workshop.
What will be the future like?
Will the robots enslave or destroy humanity?
Image title

Or people will still control robots.
Image title

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 automatized. 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 the NC machine and all elements in the production process that are reasonable to automatize should be automatized. The computer vision technologies and feedback sensors should be used too. Surely it is unacceptable to run between computer and machine with a flash drive, the commands shall be transmitted through the network. 

Let the machine make all the work. But to prevent NC machine tool become twins from the box you have to learn technologies and have a good handle in process details. Or you’ll have to share financial resources with those who understand these technologies and have a different result from the one you have been needed. 



Читать на русском.

суббота, 5 ноября 2016 г.

java.net.URL as datasource based on Apache Camel, commons-vfs and maven repository

What we know about java.net.URL?
  1. It is one of the mature JVM runtime class (@since JDK1.0).
  2. URL used in lot of programs.
  3. file, jar, http is the most frequently used protocols.
  4. Any existing JVM application can use new URL protocol as datasource without recompile and reassembly.

Is it possible to use java.net.URL with CIFS/SMB, SCP, HDFS or Maven repository protocols?

Whats about capture still image from webcam by use only one expression new URL('camel:/webcam:spycam?resolution=HD720').openStream()?

My answer is "YES, you can do it"!!! Just use special Groovy assembly groovy-grape-aether-2.4.5.4.jar. By default it contains initialization of UniversalURLStreamHandlerFactory handler.


Another approach is to use com.github.igor-suhorukov:mvn-classloader:1.6 maven dependency in your JVM project and register handler before first use of URL SUPERPOWER ;-)
Just register handler in your program: java.net.URL.setURLStreamHandlerFactory(new com.github.igorsuhorukov.url.handler.UniversalURLStreamHandlerFactory());

Save image from webcam


To capture capture image from webcam and save it to file 'snap.png' just write script webcam_to_file.groovy

com.github.igorsuhorukov.codehaus.plexus.util.IOUtil.copy(new URL('camel:/webcam:spycam?resolution=HD720').openStream(), new FileOutputStream('snap.png'))

and run this example with command
java -jar groovy-grape-aether-2.4.5.4.jar webcam_to_file.groovy


View image from webcam


Groovy help us create Swing UI with it cool SwingBuilder

webcam_to_screen.groovy

import groovy.swing.SwingBuilder
import javax.imageio.ImageIO
import javax.swing.*

def swing = new SwingBuilder()
swing.edt {
    frame(title: 'Webcam protocol', defaultCloseOperation: JFrame.EXIT_ON_CLOSE, pack: true, show: true) {
        vbox {
            swing.panel() {
                def webcamStream = new URL('camel:/webcam:spycam?resolution=HD720').openStream()
                label(new JLabel(new ImageIcon(ImageIO.read(webcamStream))))
            }
        }
    }
}



java -jar groovy-grape-aether-2.4.5.4.jar webcam_to_screen.groovy

You can see snapshot image from Swing UI in the beginning of this article.

 

Groovy scripts from remote sources


from maven repository:
java -jar groovy-grape-aether-2.4.5.4.jar mvn:/groupId:artifactId[:extension[:classifier]]:version[?custom_repository_URL]


from another host by ssh:
 java -jar groovy-grape-aether-2.4.5.4.jar vfs:/sftp://myusername:mypassword@somehost/pub/downloads/my_script.groovy

 

Protocols supported by library


UniversalURLStreamHandlerFactory hide all complexity of loadable URL protocol handlers. It fetch each implementation from maven repository and place it in local .m2 cache by using eclipse aether library.

I try to support following protocols for java.net.URL
  • mvn: — allow to use artifact from maven repository with syntax groupId:artifactId[:extension[:classifier]]:version[?custom_repository_URL]
  • vfs: — 16 protocols from apache commons-vfs library
  • camel: try to use 189 components from Apache Camel. It get content from Apache Camel component as PollingConsumer under the hood.

Links on github:


This publication is based on my original Russian article on habrahabr.

понедельник, 19 декабря 2011 г.

OpenCL 1.1: Atomic operations on floating point values

Atomic operations on floating point values in OpenCL standard is not exist (except atomic_xchg), but we can implement it using atomic_cmpxchg:
inline void AtomicAdd(volatile __global float *source, const float operand) {
    union {
        unsigned int intVal;
        float floatVal;
    } newVal;
    union {
        unsigned int intVal;
        float floatVal;
    } prevVal;
    do {
        prevVal.floatVal = *source;
        newVal.floatVal = prevVal.floatVal + operand;
    } while (atomic_cmpxchg((volatile __global unsigned int *)source, prevVal.intVal, newVal.intVal) != prevVal.intVal);
}
 We also can implement AtomicAddLocal function by using  "volatile __local" instead of "volatile __global".

Other operations:
AtomicMul(): newVal.floatVal = prevVal.floatVal * operand;
AtomicMad(source,operand1,operand2): newVal.floatVal = mad(operand1,operand2,prevVal.floatVal);
AtomicDiv(): newVal.floatVal = prevVal.floatVal / operand;

              ...

пятница, 2 декабря 2011 г.

OpenCL kernel debugging for java host code

Introduction

Java is powerful platform for rapid software development with open source frameworks(spring, apache camel, apache hadoop, jclouds), different data sources (http, soap, rest, smtp, scp, jms, CORBA, relational/NoSQL databases etc) and a huge community of developers. OpenCL is portable language and API for heterogeneous parallel computation on GPU and many core CPU. It's great to join these cross-platform technologies together!

The following information is about debugging OpenCL kernel invocation from java program on Ubuntu Linux for AMD implementation.

Hardware: x86 SSE3 capable CPU
OS environment: Ubuntu 11.04 + AMD Accelerated Parallel Processing
Software: IntelliJ Idea or any other java IDE, GNU Project Debugger(gdb), Data Display Debugger (ddd)
Java libraries: javacl ...

First you need to install the latest stable AMD Accelerated Parallel Processing, create a java project with javaCL binding library dependency, write openCL kernels and invoke it from java code ;) You can try debug demos from http://code.google.com/p/javacl/wiki/SamplesAndDemos to quick start

JavaCL is an excellent OOP java library for OpenCL with clear documentation. JavaCL is available as maven2 artifacts: http://code.google.com/p/nativelibs4java/wiki/Maven

So let me use openCL code form http://stackoverflow.com/a/5495666

Debug OpenCL kernel on CPU


Attention! AMD OpenCL CPU implementation(APP SDK 2.4) don't skip writing out of texture bound in contrast with GPU implementation, but pixel reading are emulated correctly. So please check pixel coordinates before writing in each  write_imagef function

For debugging purposes we need to run kernel on CPU (instead of GPU)

List<CLPlatform.DeviceFeature> features =
      Arrays.asList(CLPlatform.DeviceFeature.CPU);
for (CLPlatform platform : JavaCL.listPlatforms()) {
            List<CLDevice> devices = Arrays.asList(platform.listAllDevices(true));
            device=CLPlatform.getBestDevice(features, devices);
}

Compile OpenCL kernel with -g  and -O0 options:

    private static KernelItem createKernel(String kernelResource, CLContext context) throws IOException, CLBuildException {
        String kernelSrc = IOUtils.toString(Wavelet.class.getResourceAsStream(kernelResource));
        if(kernelSrc==null){
            throw new IllegalArgumentException("Resource "+kernelResource+" not found in classpath");
        }
        CLProgram program = context.createProgram(kernelSrc);
        program.addBuildOption("-g");
        program.addBuildOption("-O0");
        CLKernel[] kernels = program.createKernels();
        if (kernels.length == 0){
            throw new RuntimeException("No kernels found in the source code "+kernelResource
                    +" ! (please mark a function with __kernel)");
        } else {
            if (kernels.length > 1)
                throw new RuntimeException("Too many kernels found in the source code ! Expected one kernel function");
        }
        return new KernelItem(program,kernels[0]);
    }
With latest version of javaCL use JAVACL_DEBUG=1 (or the equivalent Java property javacl.debug=true) to achieve compiling kernel with debug information without optimization.  Thanks Oliver for quick support!

Use this code snippet to print on console command for debugging the current java process (for latest javaCL just read program log in debug mode)

String mxBeanName = ManagementFactory.getRuntimeMXBean().getName();
String pid = mxBeanName.substring(0, mxBeanName.indexOf("@"));
System.out.println("sudo gdb --tui --pid=" + pid);
System.out.println("sudo ddd --debugger \"gdb --pid=" + pid + "\"");
When our kernel code compiled and available to run by javacl host code we can freeze a java program in breakpoint:



Command "echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope" let you to start debugger as unprivileged user without "sudo".
Open terminal and attach to running JVM using command like:
sudo ddd --debugger "gdb --pid=14276"


You can list all available for ddd openCL kernel with debugging symbols:
info funct __OpenCL


After that you need execute command:
list __OpenCL_bicubicUpscale_kernel
to display source of "bicubicUpscale" kernel.
Now you can set breakpoint visually in source code.


Don't forget to continue an execution of your program with gdb command: continue
We can see a variable value in ddd, we can use step command and other powerful debugging techincs when the java program invoke "bicubicUpscale"


If you like console gdb use command: sudo gdb --tui --pid=14276

 

Debug OpenCL kernel on GPU


Also you can watch internal OpenCL kernel data on GPU. To achieve it you need add in kernel preprocessor directive
#pragma OPENCL EXTENSION cl_amd_printf : enable
and use printf functionality inside kernel

Conclusion


Do you think that it's too complicated to create OpenCL programms on linux?))) I am still waiting for free and easy to use development tools for linux...

If AMD will release fast APU fusion x86 processors with Graphics Core Next (GCN), Radeon HD 7000 GPU, implements zero-copy for APU on openCL Linux drivers and improved development tools for Linux and java developers, we can develop powerful cross-platform application for hardware architecture of future.

This is my own opinion)

Links



Trademarks

The names and trademarks of companies, products and services are the property of their respective owners.