Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more. Accepting essential orders - here's how.
0

Arduino / CNC
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino / CNC

by gadiot on Thu May 29, 2008 6:16 am

Hey All,

I'm working on a project in which I use the Arduino board as a cnc-controller for a graphic output device (similar to a penplotter). I've got pretty much everything going, but only the serial communication is a bit in the way of having super smooth movement (I increased the arduino serial buffer, but it is still quite small,and refilling it causes a little delay in the movement of the stepper motors). What I really would like is to buffer a full plotting job on an SD card, and have the arduino read commands directly from that card. I noticed the Waveshield, and a similar thing would be perfect, to drag/drop a textfile onto the sd-card, and have the arduino read commands from that textfile line by line.

Would this be difficult to do? Any advice would be very much appreciated!
gadiot
 
Posts: 5
Joined: Thu May 29, 2008 5:56 am

by gadiot on Sat Jul 05, 2008 3:41 pm

Has anyone got a suggestion on how to get basic sd card read / write functionality at decent speeds? Would be quite helpful!

Thanks in advance.
gadiot
 
Posts: 5
Joined: Thu May 29, 2008 5:56 am

by westfw on Sun Jul 06, 2008 12:27 am

If the routines The Lady uses for SD access are fast enough for audio, I would think they should be plenty fast for CNC drivers as well.
Writing to SD via the usb connection in the aduino is much more of a problem, since you'll be limited to the max speed of the serial port on the arduino (the "usb" is just a usb/serial adapter.) 57600bps or so. You'll be much better off putting your SD card into a PC card interface and writing it at full PC/USB speed, and then moving it to the aduino for "plotting."

You know that The RepRap 3D printer project selected Arduino for use in their project; you may want t o take a look at what they've done.

On the other hand, I don't really understand why the existing arduino's serial port, even with its small buffer, isn't sufficient for CNC use. Usually motors are SO SLOW that even moderate (average) serial speeds are sufficient to keep movement commands backed up in the buffer waiting for the motors, rather than the other way around. Perhaps you have a logical flaw in your buffering scheme at the motor command level, or need a somewhat more efficient command set...

westfw
 
Posts: 1609
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

by gadiot on Sun Jul 06, 2008 3:07 am

Thank you for your reply, yes, I do know that the RepRappers are using the Arduino board as well. Their motors run at slow speeds though, because of the extruding process, my stepper motors often need to run at full speed.

About logical flaws or a more efficient command set, here's the basics of my code:

– I have a switch statement that is executed whenever the Serial.available > 0
– While drawing, for each command drawn (which can be quite tiny line segments at relatively high speeds), the arduino requests a new command by sending one character over the serial line. My move command is built up as follows: M1234512345#. The first five characters are the x coordinate, the next five are the y coordinate.

– The switch recognises the first character, say the M for movement, and executes the function readTag, which reads the right amount of bytes from the serial buffer. The function looks as follows:

void readTag(char Type, int Bytes){

// empty thisTag first
for (int i=0;i<11;i++){
thisTag[i] = 0x00;
}

// reset tagIndex
tagIndex = 0;


while (tagIndex < Bytes){

char thisChar = Serial.read();

if (thisChar == hash){
tagIndex++;
}
else {
thisTag[tagIndex] = thisChar;
tagIndex++;
}
}
}


Next it takes the result, the tag that was just read, and sends it to the move function, which cointains a good bresenham line drawing algorithm and pulses the steppers.

The biggest delay that I am experiencing (the drawing gets stuttery when the lines are drawn faster than the new tags are being read) must be in this process of requesting new serial data and reading the tags. One thing I do realize that sending relative position commands will decrease the size of the serial commands, but maybe there's a flaw elsewhere in my code. I hope you can point it out for me, that would be great.

Thanks in advance for reading this lengthy post!
gadiot
 
Posts: 5
Joined: Thu May 29, 2008 5:56 am

by westfw on Sun Jul 06, 2008 11:12 pm

Are you doing anything to prevent the serial buffers from overflowing? The sort of "logic error" I was looking for would have been in being "too slow" to allow the remote system to send additional commands, but the arduino by itself doesn't have any flow control...

At that speed are you running the serial port ?

westfw
 
Posts: 1609
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

by mtbf0 on Mon Jul 07, 2008 1:29 am

i'm guessing that you're talking over a usb port. i've read in other threads about buffering delays in the drivers for usb to serial converters. the problem isn't with the serial interface on the arduino but with the driver on your host waiting for enough characters to build a packet to send out. don't know how to fix it, but... uh, y'know?
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by gadiot on Mon Jul 07, 2008 1:58 am

What I do to prevent the buffer from overflowing is the following, I have increased the arduino buffer size to 384 bytes, next to that I send a limited amount of commands first, say 200 bytes, after that, each time a single command is read out of the buffer, and a new command is requested, in this way the buffer can never overflow.
gadiot
 
Posts: 5
Joined: Thu May 29, 2008 5:56 am

by westfw on Tue Jul 08, 2008 3:30 am

>> At that speed are you running the serial port ?

westfw
 
Posts: 1609
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

by gadiot on Tue Jul 08, 2008 3:37 am

I am running the serial connection at 28800 baud, this seems to be the most reliable speed. But, and correct me if I'm wrong, isn't it true that a 28800 serial connection has a max throughput of 2,8bytes/ms? If so, generating really small movement, say only 5 steps inbetween coördinates, with a delay inbetween the pulses to the steppers of 750 microseconds, would make a travel time inbetween coordinates of 3,75 ms, while the time it will take for the next coordinate to reach the serial buffer is 12 bytes/2,8 = 4,29 ms. I guess this can be the cause, what do you think?

(edit) or is it 28,8 bytes / ms at 28800 baud?
gadiot
 
Posts: 5
Joined: Thu May 29, 2008 5:56 am

by westfw on Wed Jul 09, 2008 4:13 am

2.8bytes/ms is correct. This COULD be the problem, but how common is it to have a long string of very short movements like that?

If you have a sample string of commands that exhibit the problem you're trying to solve, you should be able to analyze it pretty easily and see if it really should be failing to keep up, or whether there is some other problem. Or you could just instrument the pc-side sender to keep track of its own transmission rate; if it's AT the port rate, you need to speed up communications. If it's BELOW the port rate, then you have a different problem.

According to This AVR BRG calculator both 38.4kbps and 76.8kbps should work at least as reliably as 28.8kbps.

westfw
 
Posts: 1609
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Please be positive and constructive with your questions and comments.