Welcome! Log In Create A New Profile

Advanced

Arduino gets stuck with G-codes

Posted by kkannan 
Arduino gets stuck with G-codes
October 08, 2009 04:15PM
Hi All,

I am implementing the reprap firmware for a small milling machine with v1.3 code. As i don't need the extruder and the associated parts i removed them. Now i have the basic code with communications section unmodified to drive a 3 axis machine. Currently testing g-codes by sending G1 commands to test a single axis by using bipolar floppy drive motor.

I've written a small comm program in Java to send g-codes via serial-usb using rxtx serial jar files. The problem is, if i send a single command the diecimilla board just ignores it. If i keep sending then the stepper moves to the desired position. However, after sometime it falls out of step and i get the err 'huh? G11' or some other invalid g-code even though i've never send anything apart from 'G1 X10 y0'.
I fiddled with the baud rate and such but no luck.

Is it simply sending series of g-codes or more to it ?

I appreciate any help to solve this problem.

Thanks
Kannan
Re: Arduino gets stuck with G-codes
October 08, 2009 05:18PM
I'm not too much of a java programmer but to get any code help you'll have to post all or parts of it! winking smiley
Re: Arduino gets stuck with G-codes
October 09, 2009 02:09AM
Hi Arvin,

Its a rather simple class directly copied from rxtx.org code examples. What it does is repeatedly send two g-codes, one to move forward and another to retract the x-axis. An output stream obtained from SerialPort is passed on to this class and started in a Thread.

/** */
class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}

public void run ()
{
try
{ while(true){
out.write("G1 X-10\n".getBytes());
out.write("G1 X10\n".getBytes());
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}

and here is the arduino loop() method which is an unmodified version of reprap firmware. I know this code works, because if i send a single command from the Arduino serial monitor the axis moves perfectly. However, i want to send tons of g-codes from the software.

void loop()
{
char c;

//read in characters if we got them.
if (Serial.available() > 0)
{
c = Serial.read();
no_data = 0;

//newlines are ends of commands.
if (c != '\n')
{
// Start of comment - ignore any bytes received from now on
if (c == '(')
comment = true;

// If we're not in comment mode, add it to our array.
if (!comment)
{
word[serial_count] = c;
serial_count++;
}
if (c == ')')
comment = false; // End of comment - start listening again
}
bytes_received = true;
}
//mark no data if nothing heard for 100 milliseconds
else
{
if ((millis() - idle_time) >= 100)
{
no_data++;
idle_time = millis();
}
}

//if theres a pause or we got a real command, do it
if (bytes_received && (c == '\n' || no_data ))
{
//process our command!
process_string(word, serial_count);

//clear command.
init_process_string();
}

//no data? turn off steppers
if (no_data > 10 )
disable_steppers();
}

I've written a dxf viewer and g-code generator using Java 2D and send commands to arduino over the serial port, and here is where i encounter the above mentioned problem.

Single command doesn't go through, multiple send jams the arduino after sometime and rx led on the board is lit continuously. It is something with the communications but i am not able to figure it.

Do i need to delay it or send the next code after getting confirmation from arduino ? What does the command buffer in firmware do? I guess it queues up the g-codes parsed and receives the next command when it is ready.

Here is the feedback from firmware code from the board.

Process String
huh? G100 --- I swear i never sent G100 sad smiley
Done
Process String
Doing DDA
Doing DDA
Done


Hope that helps to understand the issue i am talking about.

Thanks
Kannan
Re: Arduino gets stuck with G-codes
October 09, 2009 03:37AM
what OS are you running on? Linux/Windows/Mac will affect the diagnostics that people might want to suggest.

At least initially, you need to identify whether the problem is in the PC ( and the java and rxtx code) , or in the firmware ( and the way it responds to the GCode/s ). Can you "capture" the exact serial-over-usb data that is being sent to the arduino?

If your code *really* is only sending two Gcode/s, can you try doing it suing a different method ( eg , in linux, you can normally echo/cat the simple Gcode data to your /dev/ttyUSB0 device if it's configured correctly. ( I find that opening a copy of the Arduino programming software and letting it sit there open while I echo stuff in a different terminal window is an easy way. )

If you are sending the g-code/s too quickly, you might be causing the firmware to freak-out? eg: serial_count might be getting too big, or word[serial_count] might be exceeding the assigned size of the word array.

It might be totally un-related to your code. it may be the particular version/s of the code and/or OS in use. eg: Gcc Version 4.3.0 has a major bug which affects multiplication of long int's. This is the default version in Ubuntu Intrepid (8.10). Check using "avr-gcc --version" and upgrade if necessary. see: [www.arduino.cc]


just ideas. you need to diagnose it further.


Buzz.
emt
Re: Arduino gets stuck with G-codes
October 09, 2009 04:07AM
Hi

I would test with a simple terminal programme to start with.

I can run my axes with single commands from a terminal window.

G01 X10. F100 moves X axis 10mm at 100mm a minute.

BUT I think it depends which firmware you started with.


Regards

Ian
Re: Arduino gets stuck with G-codes
October 09, 2009 08:01AM
Since you have tested a single line from the arduino terminal and got it to work, I suspect you are overloading the serial receive of the arduino.

while(true){
out.write("G1 X-10\n".getBytes());
out.write("G1 X10\n".getBytes());
}

while(true) will be sending those two lines as fast as the serial connection will allow and the arduino is limited as to how much it can buffer till it overflows.

Does the arduino firmware NAK when it's ready for a new line? Look at what the arduino is doing after it receives a 'newline' ie '\n'. I suspect it gets confused because it's getting more data before it has processed what it already has.
Re: Arduino gets stuck with G-codes
October 09, 2009 08:17AM
Hi

buzz, emt thanks for the response and ideas.

I am running vista home premium and RXTX-1.2-7 inside Jdeveloper V9.0.5.1.

I have no problem issuing single commands from Arduino(0010 alpha) serial monitor. Even under the java program, the echo i am receiving is correct initially. I think the problem arises after the buffer overrun or something, the g-codes are garbled and the arduino starts grinding.

Wonder how the reprap java host handles the communication. Is there a way to implement request/response kind of protocol ? I mean the arduino executes the first one and requests the second and so on.

Arvin, you are right, the arduino is bombarded with more commands in a short time. I am thinking of making it to ask only when it is done with the first one.

Thanks
Kannan

Edited 1 time(s). Last edit at 10/09/2009 08:21AM by kkannan.
Re: Arduino gets stuck with G-codes
October 09, 2009 03:23PM
Which version of the arduino firmware are you using? I don't think it matters though since I remember seeing it in the first one too...that is, the arduino sends "ok" when it has finished a command. You need to read serial after sending a line and check for that response before sending the next line.
Re: Arduino gets stuck with G-codes
October 10, 2009 03:11AM
Hi Arvin,

Thanks for your input, the header on the programs reads something like this

// Yep, this is actually -*- c++ -*-

// Arduino G-code Interpreter
// v1.0 by Mike Ellery - initial software ([email protected])
// v1.1 by Zach Hoeken - cleaned up and did lots of tweaks ([email protected])
// v1.2 by Chris Meighan - cleanup / G2&G3 support ([email protected])
// v1.3 by Zach Hoeken - added thermocouple support and multi-sample temp readings. ([email protected])

in any case i couldn't see any mechanism for it to notify the host except sending 'Done' at the end of executing the previous command. Reading the input stream is asynchronous in nature and the timings are little unpredictable. I can't keep the tool down until this confirmation is received. So, i put in a large delay between successive sends and it seems to do the job. I suspect the reprap host must be doing something similar but in a sophisticated way. I'll dig in the source and post the solution if i find it.

Thanks
Kannan
Re: Arduino gets stuck with G-codes
October 10, 2009 05:22AM
it's the "ok" response that the firmware sends back to the PC that is normally used. some recent firmware/s have a buffer to support 4 G code movements / instructions being send and actioned in the DDA at once. Most older firmwares only have one DDA object.

BUzz.
Re: Arduino gets stuck with G-codes
October 10, 2009 07:55AM
Hi buzz

Probably i am using the older one, it sends back 'Done'. I should use this response before sending the next g-code. Somehow i assumed i can keep shoving commands and it will work sad smiley

Appreciate your help.

Thanks
Kannan
Re: Arduino gets stuck with G-codes
October 11, 2009 08:40AM
Hi All,

Just thought to show how i settled the problem finally, not a very optimized code, but works without any problem.

This method accepts Java List of g-codes, opens a serial port and spoon feeds all g-codes until it is done.

Cheers
Kannan

/**
   * Send the g-codes to the machine
   * @param gcodes
   */
   public void sendToMachine(List gcodes)throws Exception
   {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM4");
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
            
            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream(); 
                
                byte[] buffer = new byte[1024];
                int len = -1;
                String matchString = "started";
                int count = 0;
                try
                {
                    String val = "";
                    while ( (len = in.read(buffer)) > -1 )
                    {
                        if(len > 0)
                            val += new String(buffer,0,len);

                        if(val.endsWith("\n")){
                            if(val.startsWith(matchString))
                            {
                                if(matchString.equals("started")) // initing
                                    matchString = "Done";         // after that it is all 'Done' only
                                    
                                out.write(gcodes.get(count).toString().getBytes());
                                count++;
                            }
                                val = "";
                        }
                        
                            if(count == gcodes.size())
                                break;
                    }
                            serialPort.close();
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }       
            }
        }
   }

Edited 1 time(s). Last edit at 10/11/2009 08:42AM by kkannan.
Sorry, only registered users may post in this forum.

Click here to login