Hi All,
This is the first project that I've had to communicate between a separate board and and Arduino via TTL. I'm building a little CNC bed for a sculpture that uses a Peter Norburg SD4D stepper controller card and two bipolar 3A stepper motors. The Norburg card is nice for this, as you can throw it commands and it will automatically sync the motors, plus automatically calculate arcs and circles for the bed. The board I'm using is below:
The Norburg card sends an "" charachter after its completed a move to tell you that its ready for the next command. So, for instance, if I wanted my bed to move 1000 steps in the X direction, I would first send it "1000X", wait for the "" response, then send it "G", which tells it to go. After the motors complete this movement, it responds with a "*" again. I have the RX (pin 0) connected to the SO (Serial Out) and TX(pin 1) to SI(Serial In) on the Norburg. The stepper board comes with a Simple Serial program where you can issue line commands via USB. I've done this and can get both motors to follow simple X & Y commands, so I know the connection between the stepper board, stepper drivers and motors is working fine.
So, I've searched the internets for some code to poll for the "*" and found the following. This works, but its patchy. Say, it may get through 70% of the commands, but then just hangs. I've got a few things that I'm unfamiliar with or have reoccuring problems:
- I/O & USB problems at Upload: I seem to keep getting "avrdude: stk500_getsync(): not in sync: resp=0x00" message when I try up upload new code when my IO wires are connected. Any way around this?
2)Serial Monitor vs I/O: Whats the different between Serial.print and Serial.write? If I'm using Serial.Print to show whats up on the Serial Monitor, are these also getting sent to the stepper controller? Or, is it only when Serial.write is used that data is sent via the I/O pins? Because when I'm just printing my operations via Serial.Print at the beginning of my code, the motors seem to be freaking out a little bit. Actually, just opening or closing the Serial Monitor window seems to have an effect on my code. If I comment out the Serial.Print commands, the motors seems to stay idle.
- As I mentioned above this code gets part way through the command list, but then just hangs. The Serial Monitor shows that "*" charachters are being returned. But for some reason it just balls up. Any ideas on how to debug this so that I can see if what commands are being issued to the stepper board?
/*
CNC Bed Sketch
This program drives a Peter Norburg SD4DEU Stepper Driver board running SD4DNCRouter firmware connecting at 9600 Baud.
*/
//******************Constants***********
char* stepCommand[] = { //Load up CNC Patterns into string array
"1000X",
"G",
"-1000X",
"G",
"0X",
"G",
"1000Y",
"G",
"-1000Y",
"G",
"0Y",
"G"
};
const int numCommands = (sizeof(stepCommand)/sizeof(char*)); //Calculate array size
//******************Variables***********
String readString;
String writeString;
int commandNumber = 0;
//****************Function Prototypes******
//***************Setup******************************
void setup() {
Serial.begin(9600); //Begin serial communication at 9600
pinMode(0, INPUT); //0 Pin is RX
pinMode(1, OUTPUT); //1 Pin is TX
//Serial.write ("200R"); //Set Run Rate to 200 steps/sec
//Serial.write("-12?"); //This line returns SD4D firmware version. Uses to check polling code below
Serial.println("Starting Serial"); //Signal start of communication
Serial.print("String Array Size = ");
Serial.println(numCommands);
Serial.println("<<<<<Commands Below>>>>>>"); //This just prints the given string commands to see if they are being sent correctly
for (int i = 0; i < numCommands; i++){
Serial.println(stepCommand[i]);
delay(500);
}
Serial.print("<<<<<Commands Above>>>>>");
delay(1000);
}
//*************Main Program Loop************************
void loop() {
while(Serial.available() > 0){
delay(100);
char c = Serial.read(); //get one byte from buffer
readString += c;
if(c== '*'){
Serial.write(stepCommand[commandNumber]);
commandNumber++;
if(commandNumber == numCommands){ //if the max number of commands is reached...
commandNumber = 0; //reset command number back to zero
}
//delay(500);
break;
}
}
if(readString.length() > 0){
Serial.println(readString);
readString = ""; //clears variable for new input
}
}
//*********************FUNCTIONS***************************
//**************************************************************************
This is what comes up on my Serial Monitor:
h1000X (The "h" command isn't in the above code. I added that and its a status ping for the stepper board)
I4* (the I4 is the status return)
G
*
-1000X
*
G
*
0X
*
G
*
1000Y
*
G
*
-1000Y
I deleted a LOT of carriage returns from the Serial Monitor. The carriage returns increase as the commands are issued. By the time I get to where the code balls up, its all carriage returns. I don't know what that means.
Thanks for any help! Let me know if I'm leaving out any info....
--. Karl