Skip to content

Commit 8d597ad

Browse files
committed
added automatic tool length detection
1 parent a885dbb commit 8d597ad

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

cnc_ctrl_v1/CNC_Functions.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#define YSERVO 6
3737
#define ZSERVO 7
3838

39+
#define SENSEPIN 53
40+
3941
#define TOLERANCE .3//this sets how close to the target point the tool must be before it moves on.
4042
#define MOVETOLERANCE .2 //this sets how close the machine must be to the target line at any given moment
4143

@@ -1159,6 +1161,7 @@ void G10(String readString){
11591161
int ManualControl(String readString){
11601162
String readString2 = readString;
11611163
int stringLength = readString2.length();
1164+
long tmptime = millis();
11621165
while(1){
11631166
if (Serial.available()){
11641167
while (Serial.available()) {
@@ -1174,23 +1177,51 @@ int ManualControl(String readString){
11741177
if(stringLength > 0){
11751178
Serial.println(readString2);
11761179

1177-
if(readString2 == "Exit Manual Control"){
1180+
if(readString2 == "Exit Manual Control "){
11781181
Serial.println("Test Complete");
11791182
return(1);
11801183
}
1181-
if(readString2.indexOf('X') > 2){
1184+
if(readString2.indexOf('X') > 2 && readString2.indexOf('B') == 0){
11821185
x.write((readString2.substring(5)).toInt());
1186+
tmptime = millis();
11831187
}
1184-
if(readString2.indexOf('Y') > 2){
1188+
if(readString2.indexOf('Y') > 2 && readString2.indexOf('B') == 0){
11851189
y.write((readString2.substring(5)).toInt());
1190+
tmptime = millis();
11861191
}
1187-
if(readString2.indexOf('Z') > 2){
1192+
if(readString2.indexOf('Z') > 2 && readString2.indexOf('B') == 0){
11881193
z.write((readString2.substring(5)).toInt());
1194+
tmptime = millis();
11891195
}
11901196

11911197
Serial.println("gready");
11921198
}
1199+
if(millis() - tmptime > 5000){
1200+
Serial.println("Test Complete - Timed Out");
1201+
return(1);
1202+
}
11931203

11941204
readString2 = "";
11951205
}
11961206
}
1207+
1208+
float toolOffset(int pin){
1209+
long tmptime = millis();
1210+
long tmpTimeout = millis();
1211+
while(1){
1212+
tmptime = millis();
1213+
location.ztarget = location.ztarget - .05;
1214+
while(millis() - tmptime < 100){ //This is just a delay which doesn't loose the machine's position.
1215+
SetPos(&location);
1216+
SetTarget(location.xtarget, location.ytarget, location.ztarget, &location, 123);
1217+
1218+
if(digitalRead(pin) == 0){ //The surface has been found
1219+
return(location.zpos);
1220+
}
1221+
}
1222+
if(millis() - tmpTimeout > 3000){ //if it has to move down for more than three seconds it will time out
1223+
Serial.println("Surface not found, position the tool closer to the sufrace and try again.");
1224+
return(location.zpos);
1225+
}
1226+
}
1227+
}

cnc_ctrl_v1/cnc_ctrl_v1.ino

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void setup(){
6767
pinMode(xpot, INPUT);
6868
pinMode(xpot, INPUT);
6969
pinMode(zpot, INPUT);
70-
pinMode(53, OUTPUT);
70+
pinMode(SENSEPIN, INPUT_PULLUP);
7171
//card.init(SPI_HALF_SPEED, chipSelect); //setup SD card
7272
//volume.init(card);
7373
//lcdBegin(); //Initialize the LCD
@@ -108,6 +108,7 @@ void estop(){
108108
void loop(){
109109
readString = "";
110110

111+
111112
if (Serial.available()){
112113
while (Serial.available()) {
113114
delay(1); //delay to allow buffer to fill
@@ -270,14 +271,22 @@ void loop(){
270271
}
271272

272273
if(readString.substring(0, 3) == "B06"){
273-
Serial.println("speed set recognized");
274274
ManualControl(readString);
275275
location.xtarget = location.xpos;
276276
location.ytarget = location.ypos;
277277
location.ztarget = location.zpos;
278278
readString = "";
279279
Serial.println("gready");
280280
}
281+
if(readString.substring(0, 3) == "B07"){
282+
float ofset = toolOffset(SENSEPIN);
283+
location.zpos = 0.0;
284+
location.ztarget = location.zpos - ofset;
285+
Move(location.xtarget, location.ytarget, location.ztarget, 127);
286+
time = millis();
287+
readString = "";
288+
Serial.println("gready");
289+
}
281290

282291
if(readString == "Test Encoders"){
283292
testEncoders();

0 commit comments

Comments
 (0)