Arduino based Laser Control

A forum for the Arduino based Mach4/Darwin Laser Control Panel
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
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.

  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,  (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.

  To make any specific pulse longer than 1us ( up to the full 200us of the 5khz period..)
you simple change a register.  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. 

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.

  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.

  So thats it really, not very complex, and from a programming standpoint not very hard to implement
  so far. Ive cut many photo's and each is an interesting lesson in power vs speed vs output. :)
 
  Ill post the arduino code here later this week, Im happy to help as well if anyone has questions
  about how this all works, or has ideas for other ways to make it all work better.
 
 
  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.    I never did more than a set of digitalOn/DigitalOff and sleep for a few us to get the pulse.  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: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi Sky:

 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.  


#define PulsePin 9

setup()
{
  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 579 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: 4592
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: 4592
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: 4592
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 676 times
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Hi:

&nbsp; I should add one more thing.. the labels for the button names are wrong in the schematic, I just relaised
I used an old napkin sketch and cant find my final layout paper, so Ill have to trace the code to see what
button is actually which in their respective groups.

Art
Richard Cullin
Old Timer
Posts: 152
Joined: Sat Jun 02, 2012 5:45 am

Re: Arduino based Laser Control

Post by Richard Cullin »

art
my concern is that the analog input pins are essentially floating until a button is pressed . floating pins are very subject to noise
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Richard:

&nbsp; In this case I turned on the internal pullup resistor with a digitalwrite to that analogue pin, it keeps it pulled high until a button is pressed, that seems to make it resistive enough to noise .. or at least on the buttons I haven't seen any..

Art
Richard Cullin
Old Timer
Posts: 152
Joined: Sat Jun 02, 2012 5:45 am

Re: Arduino based Laser Control

Post by Richard Cullin »

art
I'm mainly thinking of others making this , where the buttons may have longer cables attached&nbsp; and or&nbsp; high voltage laser supplies may be employed , the internal rpu (pullup resistor) can be as high as 50 k .&nbsp; a lower impedence wont affect the readings and will give greater noise immunity.
just a thought , great project though
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

Richard:

Very good point. I hadn't considered the folks with high voltage supplies.
I will change the documentation to show 47K's as the resistors. Your quite
right that there really shouldn't be any downside to it, though Ill wait till
I hear from someone with higher values before making it official.
&nbsp; Im sure over time many such changes will take place , as this is my first
arduino project Im happy to hear such suggestions for change. So long as the
same values are used for all of the resistors the code should remain valid.

Thx
Art
DanL
Old Timer
Posts: 362
Joined: Wed Sep 10, 2014 1:35 pm

Re: Arduino based Laser Control

Post by DanL »

Hi art it looks like your code works with any of the ATmega32u4 boards what's quite good as some one could make a very small controller using a Micro board.
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Arduino based Laser Control

Post by ArtF »

DanL:

Yes, very true. Also, for those that dont want the capability of having the control panel itself, it woudnt
be difficult to modify the code to always be in NGrave mode with no buttons or LCD required. It
would&nbsp; need modification though to send a pulse count and frequency if one wants better cutting modes
which I haven't gone into as yet, its all been about photos so far.

&nbsp; While the pulse count is always left at 1 for photo engraving, this is because its high speed, where at most
1 pulse is used per step. I havent mentioned the low speed cutting where you increase the pulse count
so that multiple shots are fired per pexel at various frequencies, you can design the shot count and frequency
in a way to minimize charring and burning on various materials.

&nbsp; Its one of the reasons I started this project. In my shop laser,&nbsp; ( a mornstar) I cant do 3d sculpting because
it cannot handle varying power in a sweep, the panel can, and more importantly when I do a cut I cannot control
the frequency, though I can control the power. In most chinese lasers the power and speed is variable which is great
but the frequency is an important component of cutting without burning. When you allow a laser to rest, even for one
pulse, its power on the next pulse is a bit higher due to plasma cooling, and the material cools a bit as well,
which is another benefit of the panel approach.

&nbsp; SO now that I can do engraving, Im going to try focusing on 3d sculpting or texturing of material. I want to try
using one of Gearotic Celtic knot grey scale outputs to see if its possible to sculpt it by using the proper combination
of frequency, power and speed.&nbsp; Originally I used the YUN not only because I wanted wireless to program with, but I envisioned
using the SD card in the unix side to send the image data to, but the Arduino side is way too slow to get the data, so I went
serial com with Darwin instead. But since the SD card isnt needed and the wireless isnt required unless your like me
and experimenting with various techniques, any micro 32u4 would work fine, and I think you can get those for a few dollars on
ebay.

&nbsp; As I said though, eventually the external hardware can replace all of this if they wish, so whatever we do at least leads
the way for folks in the Pokeys , SmoothStepper and other external hardware&nbsp; camps to emulate our code and theories as we
find better ways to do things..&nbsp;

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

Re: Arduino based Laser Control

Post by ArtF »

Hi Guys

Playing with 3d sculpting.. first try with a single pass of a celtic knot output.
Weird looking, looks ghostly..

Art
Attachments
3dlaser.jpg
Post Reply

Who is online

Users browsing this forum: No registered users and 64 guests