Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Wednesday, 9 September 2015

Sniff and the Laser Engraver

I guess a couple of things at Scratch2015AMS led me to this... they first was TurtleStitch which lets you control an embroidery machine using Snap. That's pretty cool. The second thing was more of an accident. Steve Holmes had brought some pretty nice laser beam breakers, which I thought might work pretty well in Sniff, so I started trying to find where he got them.

I still haven't found them, but I did find some almost affordable laser engravers. These look a LOT of fun, but these ones are a little too expensive for a first step, take up a bit more room than I have on my desk, and some of those lasers are a little more powerful than is safe without serious precautions.

A bit more searching turned up one of these for under £50 (eBay UK seller, free delivery within a few days):

The working area is limited to about 4cmX4cm and the laser is much less powerful less powerful which means you can do less with them but actually is a win in for me as I can try it out more easily.

So a few days later it arrived. It's a pretty amazing but of construction, that looks like someone comercialised a really cool DIY project. The mechanical transport is made from a couple of CD drives, and the electronic control is an Arduino - not a custom AVR board based on Arduino but an actually Arduino Pro Mini, connected to a couple of driver boards for the steppers and lasers. The rest of the hardware is laser cut Acrylic sheet. While the parts may be recycled, the actually assembly is really nice, and the over all design is excellent.

So plugging it in we hit a few snags. The supplied software is Windows only. I'm not planning to use it, but we should at least get it working before we throw everything out and write our own code. Unfortunately the Arduino Pro Mini doesn't include USB support, so you need a USB->Serial converter. That's OK as they supply one, but its based on the Prolient Chipset which doesn't work in Windows 10. It works fine on Mac/Linux but Prolient don't support it any more, so we have Windows only software, and everything except Windows hardware!

Fortuanatly a few months ago I bought a few USB-Serial converters - they cost about £3 on eBay. You need a 5V one. With that fixed, we're up and running. The supplied software isn't great, but it gets the job done, and I was able to burn some images onto some scraps of leather. I've mainly been using leather as we had a bag of leather scraps that were ideal (again from eBay for not much money). Old credit cards work great too!

The other upgrade I made was to get an A4 sheet of translucent green perspex. The laser is about 200mW Red. Compared to "real" laser based CNC machines that's pretty wimpy,  but for comparison your average laser pointer is about 5mW. Safety glasses are provided with machine, but they're uncomfortable, and they're so dark that you can't actually see anything except the laser. Glasses are good practise, and are essential if you're going to be staring at the beam, but it was impractical to wear them just because I was in the room with the machine, and in fact would be a serious hazard if I was walking round in them. Adding the Perspex as a guard around the machine mean that the machine was safe and non-distracting for anyone in the room (inverse square law!) unless they were actively inspecting it.

My next upgrade is going to be some green LED's to light the work, so its easier to see how the design is progressing.

With everything working we can now ditch the provided software and do something more interesting... CNC machines are programmed using G-Code. This is a simple set of text based commands. Our machine is running a program called GRBL, which is an arduino implementation of this. While G-Code supports a number of advanced commands we just need a few:

  • G00 - move the tool as fast as possible
  • G01- move the tool at a specified feed rate
  • M03 - turn on the laser
  • M04 - turn off the laser
The Move commands take X,Y, and Z parameters, though for now we only use the X and Y.

We've wrapped all of this up in a "gcode"  device which behaves rather like a display, so we can draw stuff on the Window device, and see what it looks like, then just replace window with gcode, check that everything is the right size and then burn the pattern.

If you check back through the blog posts you've find we wrote some turtle code, so lets take the simple spiral example from that:

make display gcode device
make displayX number
make displayY number
make displayColor number

make direction number
make distance number
make penDown boolean

when move
.change displayX by distance* cos of direction
.change displayY by distance  * sin of direction
.if penDown 
..tell display to "draw"
..tell display to "move"

when start
.set displayColor to 200
.set displayX to displayX/2
.set displayY to displayY/2
.tell display to "move"
.ask "ready to burn?" and wait
.if not answer ="yes"
..stop script
.set direction to 0
.set distance to 0.1
.repeat 300
..broadcast move and wait
..change direction by 9.5
..set distance to 1.01 * distance
..set penDown to not penDown

The turtle code is handled in the "move" script which moves the turtle and either moves or draws depending on if the pen is up or down. To draw a spiral we turn the turtle a little each step, and increase the size of the step.

The main script starts by moving the laser head to the centre of the bed, and asks if we're ready - this is pretty good practise if we're about to start firing lasers that can serisouly hurt you! Then it simply draws the spiral.

displayColor is used to control the speed of the tool, in mm/minute. I've used 200 as a good baseline. Moving faster means that less power hits each spot, so there isn't as much burning. This produces lighter marks, but also thinner marks. As it also has the advantage of getting the job one quicker, we generally want this to be bigger if possible. However the correct value is very dependant on your machine, and the material your marking.

When we run this nothing happens... That's because the gcode device has a number of modes. The default mode (or mode 3) outputs the gcode to a file, and doesn't include any commnds to turn the laser on! In that repect its pretty safe. It's best to run your code in this mode first and check that it works before you start messing round with stuff that can actually injure you. The gcode device has the engravers size built into it, (39mmx39mm), so running in this mode checks that you're not trying to crash the engraver head into the side of the machine. More expensive machines have mechanisms to detect this, but our little machine doesn't so we're dependant on our software to not do anything silly. If you have access to a larger machine just edit lib/Hosted/Xgcode.c to increase the limits.

Once we're happy with our code we can change the device to mode 2:
make display gcode device 2
Now this will try to talk to the real hardware and start moving the head around. If it doesn't find the device automatically when you run setup, then you might need to tell it explicitly using something like:

Now you should start seeing the machine come to life and do its thing. However the laser is STILL TURNED OFF. We're now checking that the laser is moving over the right area of the material we're engraving - we don't want the laser to start engraving the table!! (I put a couple of sheets of white cardboard under the machine just in case - white materials reflect the light and don't burn as well as darker ones).

Once you're happy its time to burn! Set the mode to 0, make sure that you're wearing the safety glasses or have some other filter in place, and the run the code!

And here it is. This photo also shows evidence of where I got things a little wrong. The laser has a focusing lens, and its important that you focus the beam correctly. If not, then at first nothing will happen, which will encourage you to reduce the feed rate, so the laser moves more slowly - this will cause the material to burn, but over a much to large area. We want the focus as tight as possible so we can burn the smallest details.

Alternatively you can run the code in mode 1, which outputs to a file with the laser enabled. You can then take this file and download it into your machine using some other program (there's a Sniff program called Sender which can do this).

With our spiral burnt,  it was pretty trivial to get some of the other old Sniff programs burning leather! I've used Hilbert curves, and last weeks Spirograph example:

Overall I'm pretty impressed with the little gadget, and totally hooked on the whole idea of CNC. The code should work with more or less any laser cutter or engraver, with only minor tweaks. Now I'm seriously looking and CNC milling machines...

No comments:

Post a Comment