Arduino based Laser Control

A forum for the Arduino based Mach4/Darwin Laser Control Panel
Post Reply
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Arduino based Laser Control

Post by ArtF »

Hello All:

  This board is for questions, materials, code and suggestions involved with the Darwin/Mach4
Arduino based Laser Control panel. With Darwin now able to create laser engraving files and
with its requirement that an arduino laser controller be used, this board will contain my original
code and its evolution, along with ( hopefully) any users code suggestions and improvements.

Soon, I will start posting photo's, build suggestions and the code from the original laser panel
and we'll build from there.

What the hell is that you ask? :

    Darwin ( the printer port cnc driver for Mach4 ) has been released, and as of version 1.19 of it,
it can process grey scale images ( such as those made by Gearotic of Celtic knots) and from those
create mapping files so a fast laser system can engrave them with proper power levels. Darwin also creates
GCode files to match the power mapping files. But to do all this it needed more speed and power than a
printer port alone could offer. So a laser control panel was built with an eye to ease of use, creating and low cost
of owning one. The one I ended up with is made of wood, plexiglass and an inexpensive arduino. It connects to
a breakout board on 3 pins to provide the interface with Mach4 and Darwin. As such, its easy to make, inexpensive
and gives an amazing level of control over a DIY laser system while requiring no real knowledge of electronics and only basic
construction skills needed. In future many more cool effects could be done due to the way the power mapping is performed.
The following postings will detail the panel, provide videos and answer questions from those that would like to build
a panel or help the community improve on it, and perhaps we can all learn a lot about engraving techniques on
various materials at the same time. 


Art
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hello Everyone.

 So this thread is about a control panel for a laser. But more broadly its about ways to get signals
 from a printer port Mach4 to other devices for things youd like to experiment with. ITs probably best
 to go at this as a "This is the problem I had..and this is what I did" type of discussion as not everyone
 who reads this thread will be thinking of running a laser. An impact needle for engraving tombstones, or
 a unit to apply a known unit of glue to a particular spot, or even perhaps a UV diode to cure 3d liquids
 in various positions at various power may be your thing. All of these things have common features.
 
    My problem was simply to get laser power to a laser, but at the same time I had to control a very accurate
 tickle pulse, Perhaps software could have done it all, but it occured to me that these things called arduino's
 may be able to help and make the solution to my problem acceptable for other problems.
 
   I may as well admit now that this was my first arduino project, and my first arduino program. So dont expect
 any fancy programming skills to be shown. BUT, the fact I could code this arduino very quicky to do what its doing
 should clue you in that you could do such a project to use the same data for many other things. (Or just simply
 copy my code and build a laser panel..should you have a laser. :) )
 
  Here attached is a photo of an arduino leonardo. I use a Arduino Yun, which is basically a leonardo, but with
  a unix system attached running at 400mhz with wireless. I use that part only to allow me to program it wirelessly,
  you can use a normal leonardo if you dont mind having to plug in to usb to reprogram it. I like the fact I can just
  boot up a compiler and send updates to mine but if your not a programmer, than just usb is enough to program it from
  my files.
 
    A leonardo is about 20 bucks each. The total laser panel as I built mine, comprises
   
    1) Leonardo  : $21.00        (Yun: $79.00)
    2) 6 buttons : $2.00 total
    3) 1 led ring button for fire:  $2.00
    4) about 6 1k resistors          $1.00  ( 10k or even 100k resistors should work fine.)
    5) 4x20 LCD display              $6.00
    6) 5k pot for laser power control
    7) A box to put it in..
   
    I built my box from scrap wood, and used plexi on the front as a faceplate. So the whole things is worth about $30.00
    or so without wiress and around a hundred with. ( Although chinese Yun's on ebay are abotu 13.00 each.. dont know
    how they work..
   
      A leonardo is a 16mhz processor. Its pins are analogue, digital and in/out on pretty much all of them. You tell it
      the pins function as you program it. Some are limited to digital, others have PWM others are both, and some have interrupt
      capability.
     
   As I stated, this is my first Arduino project, so Im hopefull people will come along and help me make it better, over
   time perhaps Ill learn more arduino programming, but if a beginner can do it, so can you.
   
   Basic Theory of the device:
   
      OK, so I'm using a synrad laser. This laser is RF based and needs no water cooling or high voltage which is why Im
   using it. With modifications though this project will run a chineese laser or any laser. This laser , like most rf/co2 lasers
   needs a "tickle" pulse every 200us. You can damage the laser if you dont use this AND it has to be fairly accurate
   as if the pulse exceeds 3us, the laser will lase. It wants a 1us width. From 3us to 199us will give a power range of 0-100%
   of power output for that 200us period. High end Laser engravers like the epilog brand use these lasers using two main variables,
   they set power and frequency. SO you tell it for example to do 50%power at 4Khz. To understand this imagine you set 25% power
   at 2500hz, this means the laser should be given a 50us pulse every 400us. ( 1/4 power = 50us. and skipping the next 200us
   period gives you 2.5khz output. ). Its powerfull to combine these two variables. Instead of burning and charing plywood
   when you cut it, having the ability to use a set power at a lower frequency allows the spot to cool before being hit again
   thus reducing charing of material.
   
      Since all that was fairly new to me, I decided this panel I was building had to have a diagnostics mode where I could
   dial up a pulse count, power level, and set a frequency , and then, by pressing a flashing button I could shoot that
   exact amount of energy at that frequency to measure accurately laser's affects on various materials. This is all much
   too complex to do from a printer port accurately...so enter the arduino. Never having used one before, I had heard it should
   have enough power to do what I needed done. BUT , I really dont want to redo my breakout board on my robotic arm, so it
   was decided to build an arduino that simply hooked to 3 spots on the cnc's breakout baord. From those pins we'd make
   sure the tickle was accurate, and photos could be engraved as well as vectors..
   
&nbsp; <End of Part 1>
Attachments
Leonardo.jpg
Last edited by ArtF on Wed Oct 01, 2014 4:15 am, edited 1 time in total.
User avatar
tweakie
Old Timer
Posts: 169
Joined: Wed Dec 01, 2010 12:58 am

Re: Arduino based Laser Control

Post by tweakie »

Hi Art,

Excellent work.
Have a tremendous nice time on your holiday, please make a few notes whilst away so we can have Part II ... upon your return.&nbsp; ;D&nbsp; ;D

Tweakie.
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi Tweak:

&nbsp; I dont leave till later in the month.. so Ill be adding as much as I can till then. :)

Art
User avatar
Mooselake
Old Timer
Posts: 515
Joined: Sun Dec 26, 2010 12:21 pm
Location: Mooselake Manor

Re: Arduino based Laser Control

Post by Mooselake »

You're awfully busy for a retired guy :)

Maybe in your spare time you could figure out how to use Tweakie's jet engine to build a really high power laser.

Kirk
SkyMoBot
Old Timer
Posts: 52
Joined: Sat Oct 08, 2011 8:12 pm

Re: Arduino based Laser Control

Post by SkyMoBot »

Am following this thread with a great deal of interest as I have a Epilog Summit laser engraver with a burnt out control board.&nbsp; It uses the same Synrad laser tube that you all have.&nbsp; I converted it over to use Gecko's and Mach3 for the motion control.&nbsp; I have a modified version of Tweakie's control panel and I was just about to convert over to his version that uses the parallel port breakout board.

I tried getting an Arduino to output the tickle pulse and could never get the pulse down to under 4 msec,&nbsp; I do a lot of laser cutting and being able to change the pulse width and speed on the fly would be very very handy.

Since you are doing low level parallel port stuff, why not have Darwin output the tickle pulse?

Regardless, whatever you get working, I'm bound to try and replicate.&nbsp; I have a full time regular job and 4 kids so my free time is somewhat limited so I have to let you all do my experimentation.
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi:

&nbsp; Darwin is incapable of timing to the requirements of the laser. The Arduino though finds it trivial. It tickles with exactly 1us every 200us religiously, and each pulse is adjustable form 1 to 199us in 5000 discrete steps. Darwin uses only 100 steps of about 50 in that range. Darwin , as was mach3, was also limited in that it couldnt do pwm at 5khz, but had to use much lower.

&nbsp; So this panel has 3 modes, diags mode where you manually shoot what you wish.. CNC mode where it accepts pwm and fires on each step, and NGrave where speed is also taken into account to stop charing or burning in corners, all things Darwin on its own cannot do.

&nbsp; Now Im sure in the future Smoothsteppers and the like WILL do all that, but till then I needed it from a printer port,so the arduino was the obvious choice for tight control pulse to pulse..

Thx
Art
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi Guys:

&nbsp; Here is a video on how the panel operates.

https://www.youtube.com/watch?v=o6MbP672xzY


Art
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

What the arduino does internally::

Well, as the video in the last post shows the panel itself,before building one we just need
to know what its doing internally. Let me point out that the point here is to make a device that can take
power and fire commands from mach4 with a time dependency that allows a laser or impactor or whatever
to do its job. There's a lot of microprocessors available to do that kind of thing, TI's launchpad,
Arduino's, beagleboards..etc, and I woudlnt rule out building another panel with another micro at
some point. Hopefully by that point more a few of us will be getting commands from Darwin.

&nbsp; The software in the arduino isnt very complex, as my first arduino experiment you can imagine that
it wont be up to a knowledgable arduino programmers level. One of my hopes here is we attract such
people or we learn together how to do this better. Also worth noting is that while it was important
to me to have a diagnostics panel at the machine for testing and calibrating future engravings to the
material at hand, you may not want a diagnostics mode at all, and if so, just a simple arduino with
no box, buttons or LCD display would work fine, the program would simply require a bit of recoding
and you could hot glue the arduino into your cnc control box to permanently control your laser power
with no interaction. Point is.. there's lots of room for altering what we're doing here.

So.. that having been said, whatever controller we use,&nbsp; (a leonardo in this case) has the
primary job of supplying PWM to the laser. Synrads require ( as do many others) a 5khz base
tickle frequency at 1us. The leonardo has a complex timer chip in it with envelope outputs, this allows
us to program that chip to do 5khz pwn with 1us pulses as tickles.

&nbsp; To make any specific pulse longer than 1us ( up to the full 200us of the 5khz period..)
you simple change a register.&nbsp; In our case thats OCR1A , a register that sets the duty
cycle of the pwm. this is all really hardware so its very accurate and controllable.&nbsp;

So we then need to change it on the fly. SO I turned on an interrupt that the timer can generate
at the beginning of every period. In that interrupt call, we calculate based on current conditions
what length (duty cycle) this upcoming pulse should be. If we leave it at 1us, the laser will
not fire, but from 3.5us to 199us the laser will fire fully for that time frame. This then is what
we consider varying power, in reality the power is always at 100% but at varying lengths of time.

As the video shows,I didnt want the laser to fire all the time. SO a second interrupt is hooked up
to the StepFlag output of Darwin which gives a pulse whenever a step pulse is put out to any motor.
We have then, two interrupts going on so far, a timer interrupt that triggers at the start of each pwm
pulse, and a Step interrupt that happens whenever a step is taken. Now the diag mode has a fire button
which when pressed fires off a set number of pulses depending on what one selects for count.
So in NGrave mode, the interrupt on the step flag reloads the pulse count( usually set at one) and
internally presses the button. This makes it fire one pulse. As the CNC unit moves, each pulse
resets the counter to its setpoint and tells it to fire. The laser fires once and does nothing until the
next step pulse. Youd set higher pulse count numbers to cut materials and can use frequency and pulse count
settings on the panel to control charring or overburn in various materials.

&nbsp; So that leaves us with just needing a way to get the power of each shot. For that Darwin now has a clock
and a data output. When Darwin sees your in Laser mode, it will constantly send a stream of power
data, if the spindle is off it sends zero's.. if spindle is on it sends the last SWord.. ( 0-100%),
and if a photo is linked to Darwin, it sends the grey scale of the photo point in the spot
the axis is at currently..otherwise zero. The Data clock runs at full kernal speed, so 30khz on my system,
so to be sure the arduino doesnt miss it, it uses another interrupt. This means we have 3 interrupts
running constantly in the arduino to sync output to whatever Darwin is doing. This leaves most of the
complexity on the PC side in Darwin. If not doing a photo, Darwin calculates a TOF(Time of flight)
for your vector line, calculating a power level for each set of steps to maintain a level amount of power
throughout your motions.

&nbsp; So thats it really, not very complex, and from a programming standpoint not very hard to implement
&nbsp; so far. Ive cut many photo's and each is an interesting lesson in power vs speed vs output. :)
&nbsp;
&nbsp; Ill post the arduino code here later this week, Im happy to help as well if anyone has questions
&nbsp; about how this all works, or has ideas for other ways to make it all work better.
&nbsp;
&nbsp;
&nbsp; Art
SkyMoBot
Old Timer
Posts: 52
Joined: Sat Oct 08, 2011 8:12 pm

Re: Arduino based Laser Control

Post by SkyMoBot »

That makes a lot more sense to me now.&nbsp; &nbsp; I never did more than a set of digitalOn/DigitalOff and sleep for a few us to get the pulse.&nbsp; I never fooled around with the OCR1A register.

How about posting a snippet of code that does just the tickle so I can try it for myself while I wait for the rest of it?
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi Sky:

&nbsp;TO do that Im using a PWM library for the leonardo..but.. it was set to do only phase correct
PWM, in that mode if you change duty on the fly it creates a double pulse... so I modified the
library by changing the control bit to be FastPWM.. So if you use the library the following code
will allow one to control the PWM duty cycle on a pulse by pulse basis.

(I include the PWM library as the one I modified). Ive modified a coupdl fo the system files for
this project.. so just be aware when I post a library it may or may not match with the Arduino libraries
as released. &nbsp;


#define PulsePin 9

setup()
{
&nbsp; bool timeron = SetPinFrequency( PulsePin, 5000 ); //set pulsepin to 5Khz, modified for fast pwm mode
//this turns on the timer interrupt so that duty cycle can be set pulse by pulse..
&nbsp;TIMSK1 |= (1 << TOIE1); &nbsp;// enable timer overflow interrupt, we'll use this one for firing the laser if a fire is called for.
&nbsp; //this is the slow way of setting the pwm duty cycle..
&nbsp;pwmWrite( PulsePin, 16); //set for 1us for laser tickle , never go below this and tickle will be all good.
}

//this is a decalaration of an interrupt to be called at the start of every PWM period..

ISR(TIMER1_OVF_vect)
{
&nbsp; //I keep track of the PWM periods as a way of coordinating power in other code
&nbsp;cnt++;
&nbsp;Duty = FireLaser(); &nbsp; //find the next power level.. &nbsp; &nbsp;
&nbsp;OCR1A = Duty; &nbsp;&nbsp; &nbsp; //this is the fast way fo changing the upcoming duty cycle period.&nbsp; &nbsp; &nbsp;
}

Attachments
PWMlib.zip
(15.93 KiB) Downloaded 476 times
Last edited by ArtF on Mon Sep 08, 2014 3:00 am, edited 1 time in total.
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi All:

&nbsp;I dont normally draw schematics, so forgive the poor quality. Here is the wiring of the laser
panel, as you can see its pretty simple, just a few buttons, an led and a few wires. Not shown is the
5vdc and 0vdc wires connected to a breakout baord or external 5vdc power supply. You also need to hook
the negative of the 5vdc t0 the ground of the breakout board..

(Schematic Modified Oct 1/2014)

Art
Attachments
LaserPanel4.png
Last edited by ArtF on Wed Oct 01, 2014 8:37 am, edited 1 time in total.
Richard Cullin
Old Timer
Posts: 152
Joined: Sat Jun 02, 2012 5:45 am

Re: Arduino based Laser Control

Post by Richard Cullin »

do you think it might be "safer" to put 47k pull down resistors on your analog button reading pins ?&nbsp; . I'm assuming you have weak pullpowns turned on but a noise spike on the fire button could be nasty.
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi Richard:

&nbsp; Noise spikes typically have very little current capability behind them, but with 1k to ground as worst case
button press we get about 5 ma in normal operation, the pin will only source about 50 ma I think.. so even if the voltage
on a noise spike got to 50 volts, you'd still be at max about 50ma and even then for so small a period that actual power
dissipation would be too small to care about. Induction of enough power to hurt the arduino would be near impossible
on the button lines, so 1k is likely more than enough. Feel free though to use pretty much any resistance you like as
its truly not critical in this type of circuit. Its more about the ratio than the actual values.. I would worry about too high
a value making the circuit a bit more susceptible to noise.
(All just my opinion of course, I make no pretence at an engineering degree. :-) ). All that having been said, those are
the reason I picked those values, and since you ( or anyone) may know better than I, please use any values you might
calculate and let us know. Id worry on 47K though ( if used on all pins ) that youd then max out at around 200K for one of the
button presses and with that low a current flow noise could give wrong indications. ( 4.7K perhaps though)

&nbsp; &nbsp; The reason I state ratio's are more important than values is simply the code gets easier if all values are the same.
Button code such as this:

int16_t val = analogRead(Buttons); //read the jog buttons.
thisbut = (val >> 8);

&nbsp; So too high a value could in theory make the buttons a bit less responsive or error prone, and with a fire button as one
of the buttons in the loop I figured Id keep it as reliably set as I could in terms of returned analogue count.&nbsp; &nbsp;
&nbsp;

Art
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4415
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi All:

&nbsp; Here is the code for the Arduino Im currently using. With it comes a few pointers about why it is what it is. ( Again, please keep
in mind this is my first arduino project, so its messy , poorly written but works fine so far, hopefully together we can make it better
and more fireproof. Its the result of several recodes as I discovered limitations in the arduino's capabilities.

So lets discuss whats here.

&nbsp; Firstly, its just a few routines, BUT I did a change to the systems Winterrupts.c file that you will have to duplicate
for this to compile. Find the WInterrupts.c file and comment out the following lines

//ISR(INT0_vect) {
// if(intFunc[EXTERNAL_INT_0])
// intFunc[EXTERNAL_INT_0]();
//}

//ISR(INT1_vect) {
// if(intFunc[EXTERNAL_INT_1])
// intFunc[EXTERNAL_INT_1]();
//}
//ISR(INT6_vect) {
//&nbsp; &nbsp; if(intFunc[EXTERNAL_INT_4])
// intFunc[EXTERNAL_INT_4]();
//}

&nbsp; &nbsp; This was necessary because I found the interrupt time with AttachInterrupt(..) was too slow. The library was attempting
to figure out too much and wasted over 25us as a result, by recoding to direct calls and giving Attach() a dummy process in
its call while providing the actual interrupt routine I was able to get much better responce at higher speeds. So the interrupt
calls may look a bit weird to you.

attachInterrupt( 0, DUMMY, RISING ); //pulse external monitor

The above sets up the interrupt but the routine DUMMY isnt used, the routine
is directly set by the use of&nbsp; ISR(INT0_vect)&nbsp; further down in the code.

the program uses three interrupts, though really only 2 at once. The Int6 routine is used only for PWM
sensing and gives me a number of 0-100 as a current PWM setting at 50hz. It does this by
counting the 5Khz laser base and reporting how many "ticks" of that occur between PWM rise and fall
(thus at 50hz PWM you get a values of 0-100 which is a good slick way of getting pwm in this case. )

PWM though is used only in CNC mode which is the worst mode. In NGrave mode we user a power value
sent via serial by interrupt #1, this is a raw serial clock/data pulsing at kernal frequency. I use very noisy
servo's so to eliminate as much error as I could, I frame the power level in between hex f's, so the power is
sent as 0xfppf where pp is 8 bits of power. In addition the 8 power bits must be less than 100 else
the system will reject it as noise. As further noise reduction the same value is always sent twice and must
match, or its rejected as well.

&nbsp; This is not a great serial routine, we could probably do much better but I came up with it in frustration
at the level of noise I was getting from my servos.&nbsp; ( I shouldn't have put them in&nbsp; my breakout board
cabinet as they have their own AC/DC conversion which makes them very noisy.

&nbsp; SO the power once received is used as the next duty cycle to be used when a "stepflag" is seen, this is
on Interrupt 0.

if( (OpMode != DIAGS && SpindleOn))
{
FireCount = ReLoad;
FIRE = true;
}

&nbsp; Its code simple fires the laser if we're in NGrave or CNC mode and the spindle
is known to be running.

&nbsp; SO just keep in mind the code is pretty badly coded as a result of many many attempts
with various theories, text comments may be totally wrong from cut and pastes
during its development, but that as it stands it does engraving very well and with high
repetition rates. Changes to make it better as we have more testers will be great so
once anyone has a unit working, please let us know what changes you do and what their effect
is. I'm very willing to revise Darwin to comply with what any users feel would work better.
&nbsp; I suspect changes in theory that makes for good engraving will evolve to other hardware
like the smoothstepper in future, so ideas here are good for everyone.&nbsp;


Have fun
Art



Attachments
LaserControlCode.zip
(5.1 KiB) Downloaded 578 times
Post Reply