Hello,
as I have promised in another thread: Arrays - Programming Questions - Arduino Forum I will post a part of the code for my CnC application here...
the problem which I currently have, is that I use a SD-Card to load the "G-Code" and after each 512 Bytes the SD-Card has to reload data, which causes a 1mS break. Does anyone know a workaround?
It has not a big impact on the CnC performamce - even tough I want to solve this issue somehow...
This is the stepper driver which I am using:
Picture of the cnc hardware prototype in action:
Testing the CnC hardware:
The self made CnC software (windows):
The code which I am going to post is merely related to read the data from the SD-Card and to feed the stepping devices.
There are two reasons why I post the code,
a) first I want to help others developing similar things and
b) maybe some of you have ideas how to improve performance...
Here is the code:
// Execute the CnC Project.
void MyProject() {
// Delay for the high Phase (used to trigger the stepper).
int highPhaseDelay = 0;
// Delay for the low Phase (used to trigger tge stepper).
int lowPhaseDelay = 0;
// Open the file that contains the CnC code... Format [...x156Xy-45Ya>A...] ... the x156X means 156 steps in the positive direction of the x axis, the y-45Y means 45 steps in the negative direction of the y axis the a>A indicates weather a realay gers triggerd or not...
File myFile = SD.open("pc.txt");
// If there is an file...
if (myFile) {
// Loops as long till the file is at its end.
while (myFile.available()) {
// Extracts all the data to execute one reference point (x, y coordinates and the state of the relay)...
if (MyGetDataMapFromFile(char(myFile.read())) == true) {
// Checks weather there is a change for the relay or not to the former iteration.
if (globalPlasmaActiv != globalPlasmaActivTemp) {
// Activates the relay
if (globalPlasmaActiv == '<') {
// Sets the stepping speed.
highPhaseDelay = globalTimePerHighPhaseForPlasmaActiv;
// Sets the stepping speed.
lowPhaseDelay = globalTimePerLowPhaseForPlasmaActiv;
// Led indication that the relay is activ.
digitalWrite(Visual_Plasma_Activ, HIGH);
// Activates the relay, that activates milling or laser cuttion or plasma cutting or what ever...
digitalWrite(Relai_PlasmaActiv, HIGH);
// start deleay time.
delay(globalStartDelayTime);
}
// Deactivate the relay.
if (globalPlasmaActiv == '>') {
// Sets the stepping speed.
highPhaseDelay = globalTimePerHighPhaseForPlasmaInactiv;
// Sets the stepping speed.
lowPhaseDelay = globalTimePerLowPhaseForPlasmaInactiv;
// Switch of the led.
digitalWrite(Visual_Plasma_Activ, LOW);
// Switch of the relay.
digitalWrite(Relai_PlasmaActiv, LOW);
}
}
// Trigger the Stepper for the X-Axis.
MyStepperX(globalStepsOnXAxis, highPhaseDelay, lowPhaseDelay);
// Trigger the Stepper for the Y-Axis.
MyStepperY(globalStepsOnYAxis, highPhaseDelay, lowPhaseDelay);
// Used for the programm logic...
globalPlasmaActivTemp = globalPlasmaActiv;
// Sends a sign to the windows application, to indicate that one reference point is done.
Serial.println("U");
}
}
// Deactivate the relay, if it is still activ.
digitalWrite(Relai_PlasmaActiv, LOW);
// Deactivate the led if it is still activ.
digitalWrite(Visual_Plasma_Activ, LOW);
// Close the file.
myFile.close();
// Sends a Sign to the windows application, that the projejct is finished.
Serial.println("L");
// Project is finished - do nothing...
loop();
}
}
// Extract the coordinates form the file.
boolean MyGetDataMapFromFile(char tempReceivedChar) {
// Start receiving coordinates for the X-Axis.
if (tempReceivedChar == 'X') {
// It is not finished for X.
globalMyGetDataMapFromFileXFinish = false;
}
// End receiving coordinates for the X-Axis.
if (tempReceivedChar == 'x') {
// Convert String to an Char Array.
globalMyGetDataMapFromFileCharacterString.toCharArray(globalMyGetDataMapFromFileCharArray, 10);
// Convert Char array to an integer.
globalStepsOnXAxis = atoi(globalMyGetDataMapFromFileCharArray);
// Reset the array.
memset(globalMyGetDataMapFromFileCharArray, 0, 10);
// Reset the string.
globalMyGetDataMapFromFileCharacterString = "";
// Coordinates for X-Axis were extracted.
globalMyGetDataMapFromFileXFinish = true;
}
// Same as for X...
if (tempReceivedChar == 'Y') {
// Same as for X...
globalMyGetDataMapFromFileYFinish = false;
}
// Same as for X...
if (tempReceivedChar == 'y') {
// Same as for X...
globalMyGetDataMapFromFileCharacterString.toCharArray(globalMyGetDataMapFromFileCharArray, 10);
// Same as for X...
globalStepsOnYAxis = atoi(globalMyGetDataMapFromFileCharArray);
// Same as for X...
memset(globalMyGetDataMapFromFileCharArray, 0, 10);
// Same as for X...
globalMyGetDataMapFromFileCharacterString = "";
// Same as for X...
globalMyGetDataMapFromFileYFinish = true;
}
// Receiving the sign, that triggers the relay.
if (globalMyGetDataMapFromFilePlasmaActiveInit == true) {
globalMyGetDataMapFromFilePlasmaActiveInit = false;
globalPlasmaActiv = tempReceivedChar;
}
// Receiving the sign, that triggers the relay.
if (tempReceivedChar == 'A') {
globalMyGetDataMapFromFileAFinish = false;
globalMyGetDataMapFromFilePlasmaActiveInit = true;
}
// Receiving the sign, that triggers the relay.
if (tempReceivedChar == 'a') {
globalMyGetDataMapFromFileAFinish = true;
}
// If the received character is a number.
if (tempReceivedChar == '1' || tempReceivedChar == '2' || tempReceivedChar == '3' || tempReceivedChar == '4' || tempReceivedChar == '5' || tempReceivedChar == '6' || tempReceivedChar == '7' || tempReceivedChar == '8' || tempReceivedChar == '9' || tempReceivedChar == '0' || tempReceivedChar == '-') {
// Convert Char Array to an string.
globalMyGetDataMapFromFileCharacterString.concat(tempReceivedChar);
}
// If all data for a reference point is received.
if (globalMyGetDataMapFromFileXFinish == true && globalMyGetDataMapFromFileYFinish == true && globalMyGetDataMapFromFileAFinish == true) {
// Prepare for next reference point.
globalMyGetDataMapFromFileXFinish = false;
globalMyGetDataMapFromFileYFinish = false;
globalMyGetDataMapFromFileAFinish = false;
// Current reference point finished.
return true;
}
else {
// Current reference point not finished.
return false;
}
}
Kind Regards,
Andreas