|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
Steuerung für Frästisch_SFTools |
|
|
|
|
|
|
|
|
Steuerung f�r Fr�stisch_SFTools |
|
|
|
|
|
|
|
|
Diese Datei enthält den Code für die Implementierung der Steuerung eines Frästisches mit 2 Nema17 Schrittmotoren. |
|
|
|
|
|
Sie unterstützt: |
|
|
|
|
|
- Schnelles/langsames Verstellen der Fräserhöhe |
|
|
|
|
|
|
|
|
Diese Datei enth�lt den Code f�r die Implementierung der Steuerung eines Fr�stisches mit 2 Nema17 Schrittmotoren. |
|
|
|
|
|
Sie unterst�tzt: |
|
|
|
|
|
- Schnelles/langsames Verstellen der Fr�serh�he |
|
|
- Werkzeugwechsel |
|
|
- Werkzeugwechsel |
|
|
- Automatisches Nullen mit WLS |
|
|
- Automatisches Nullen mit WLS |
|
|
- Eintauchen mit vordefinierter Tiefe |
|
|
- Eintauchen mit vordefinierter Tiefe |
|
|
- Für obige Funktionen notwendige Konfiguration |
|
|
|
|
|
|
|
|
- F�r obige Funktionen notwendige Konfiguration |
|
|
|
|
|
|
|
|
Erstellt: 05.01.2021 |
|
|
Erstellt: 05.01.2021 |
|
|
Autor: Flo Smilari |
|
|
Autor: Flo Smilari |
|
|
|
|
|
|
|
|
#include <ESP_FlexyStepper.h> |
|
|
#include <ESP_FlexyStepper.h> |
|
|
#include <HardwareSerial.h> |
|
|
#include <HardwareSerial.h> |
|
|
#include <pins_arduino.h> |
|
|
#include <pins_arduino.h> |
|
|
#include <RotaryEncoder.h> |
|
|
|
|
|
#include <Wire.h> |
|
|
#include <Wire.h> |
|
|
#include <WString.h> |
|
|
#include <WString.h> |
|
|
|
|
|
|
|
|
#include "Display.h" |
|
|
#include "Display.h" |
|
|
#include "ExEzButton.h" |
|
|
#include "ExEzButton.h" |
|
|
#include "RotaryControler.h" |
|
|
#include "RotaryControler.h" |
|
|
|
|
|
#include "RouterElevator.h" |
|
|
#include "RouterSetup.h" |
|
|
#include "RouterSetup.h" |
|
|
#include "Status.h" |
|
|
#include "Status.h" |
|
|
#include "WLS.h" |
|
|
#include "WLS.h" |
|
|
|
|
|
|
|
|
static const int RotEnc_Clk_Pin = 32; |
|
|
static const int RotEnc_Clk_Pin = 32; |
|
|
static const int RotEnc_Dta_Pin = 33; |
|
|
static const int RotEnc_Dta_Pin = 33; |
|
|
|
|
|
|
|
|
static const float MOVE_DOWNWARD = -1; // motor rotation counter clock wise |
|
|
|
|
|
static const float MOVE_UPWARD = 1; // motor rotation clock wise |
|
|
|
|
|
|
|
|
static const int MOVE_DOWNWARD = -1; // motor rotation counter clock wise |
|
|
|
|
|
//static const int MOVE_UPWARD = 1; // motor rotation clock wise |
|
|
|
|
|
|
|
|
byte limitSwitchState = 1; |
|
|
|
|
|
int previousDirection = 0; |
|
|
|
|
|
|
|
|
//byte limitSwitchState = 1; |
|
|
|
|
|
//int previousDirection = 0; |
|
|
int nullingTLS_intermediateState = 0; |
|
|
int nullingTLS_intermediateState = 0; |
|
|
|
|
|
|
|
|
ExEzButton RedButton(RedBtn_Pin, false, 2000); |
|
|
ExEzButton RedButton(RedBtn_Pin, false, 2000); |
|
|
ExEzButton GreenButton(GreenBtn_Pin, false, 2000); |
|
|
ExEzButton GreenButton(GreenBtn_Pin, false, 2000); |
|
|
ExEzButton BlueButton(BlueBtn_Pin, false, 2000); |
|
|
ExEzButton BlueButton(BlueBtn_Pin, false, 2000); |
|
|
//ExEzButton RotarySwitch(RotEnc_Switch_Pin, true, 2000); |
|
|
|
|
|
|
|
|
|
|
|
WLS WlsDetect(WLS_DETECT_Pin, true); |
|
|
WLS WlsDetect(WLS_DETECT_Pin, true); |
|
|
WLS Wls(WLS_Pin); |
|
|
WLS Wls(WLS_Pin); |
|
|
|
|
|
|
|
|
Display Display; |
|
|
Display Display; |
|
|
ESP_FlexyStepper Stepper; |
|
|
ESP_FlexyStepper Stepper; |
|
|
RouterSetup Router_Setup; |
|
|
RouterSetup Router_Setup; |
|
|
|
|
|
RouterElevator Router_Elevator(Stepper, Display, WlsDetect, Wls, LIMIT_SWITCH, MOVE_DOWNWARD); |
|
|
|
|
|
|
|
|
Status actualStatus; |
|
|
Status actualStatus; |
|
|
Status originStatus; |
|
|
Status originStatus; |
|
|
|
|
|
|
|
|
//*** Limit switch interrupt routine |
|
|
//*** Limit switch interrupt routine |
|
|
//********************************** |
|
|
//********************************** |
|
|
void limitSwitchHandler() { |
|
|
void limitSwitchHandler() { |
|
|
limitSwitchState = digitalRead(LIMIT_SWITCH); |
|
|
|
|
|
|
|
|
// limitSwitchState = digitalRead(LIMIT_SWITCH); |
|
|
|
|
|
Router_Elevator.limitSwitchHandler(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//***************************************************************************************************** |
|
|
//***************************************************************************************************** |
|
|
|
|
|
|
|
|
Display.showInitialization(); |
|
|
Display.showInitialization(); |
|
|
//attach an interrupt to the IO pin of the limit switch and specify the handler function |
|
|
//attach an interrupt to the IO pin of the limit switch and specify the handler function |
|
|
attachInterrupt(digitalPinToInterrupt(LIMIT_SWITCH), limitSwitchHandler, CHANGE); |
|
|
attachInterrupt(digitalPinToInterrupt(LIMIT_SWITCH), limitSwitchHandler, CHANGE); |
|
|
|
|
|
|
|
|
Stepper.connectToPins(STEP, DIR); |
|
|
Stepper.connectToPins(STEP, DIR); |
|
|
// set the speed and acceleration rates for the stepper motor |
|
|
// set the speed and acceleration rates for the stepper motor |
|
|
Stepper.setSpeedInStepsPerSecond(Router_Setup.getSpeed() * Router_Setup.getStepsPerRev()); |
|
|
Stepper.setSpeedInStepsPerSecond(Router_Setup.getSpeed() * Router_Setup.getStepsPerRev()); |
|
|
|
|
|
|
|
|
Stepper.setStepsPerMillimeter(Router_Setup.getStepsPerRev() / Router_Setup.getPitch()); |
|
|
Stepper.setStepsPerMillimeter(Router_Setup.getStepsPerRev() / Router_Setup.getPitch()); |
|
|
Stepper.setAccelerationInStepsPerSecondPerSecond(Router_Setup.getAcceleration() * Router_Setup.getStepsPerRev()); |
|
|
Stepper.setAccelerationInStepsPerSecondPerSecond(Router_Setup.getAcceleration() * Router_Setup.getStepsPerRev()); |
|
|
Stepper.setDecelerationInStepsPerSecondPerSecond(Router_Setup.getAcceleration() * Router_Setup.getStepsPerRev()); |
|
|
Stepper.setDecelerationInStepsPerSecondPerSecond(Router_Setup.getAcceleration() * Router_Setup.getStepsPerRev()); |
|
|
Stepper.startAsService(0); |
|
|
|
|
|
|
|
|
if (!Stepper.isStartedAsService()) { |
|
|
|
|
|
Stepper.startAsService(0); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (Router_Setup.isToolChangOnPowerOn()) { |
|
|
if (Router_Setup.isToolChangOnPowerOn()) { |
|
|
actualStatus = TOOL_CHANGE; |
|
|
actualStatus = TOOL_CHANGE; |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
|
|
|
case NULLING: |
|
|
case NULLING: |
|
|
printStatus("NULLING"); |
|
|
printStatus("NULLING"); |
|
|
if (RedButton.isPressed()) { |
|
|
if (RedButton.isPressed()) { |
|
|
Stepper.setCurrentPositionInMillimeters(0); |
|
|
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
|
|
|
|
|
// Stepper.setCurrentPositionInMillimeters(0); |
|
|
|
|
|
// Stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
|
|
Router_Elevator.setZeroPosition(); |
|
|
actualStatus = IDLE; |
|
|
actualStatus = IDLE; |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
if (nullingTLS_intermediateState == 0) { |
|
|
if (nullingTLS_intermediateState == 0) { |
|
|
printStatus("NULLING_TLS,0"); |
|
|
printStatus("NULLING_TLS,0"); |
|
|
//Move elevator to lowest point (lower limit switch triggers) |
|
|
//Move elevator to lowest point (lower limit switch triggers) |
|
|
Stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_DOWNWARD); |
|
|
|
|
|
|
|
|
// Stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_DOWNWARD); |
|
|
|
|
|
Router_Elevator.moveToLowerLimitSwitch(); |
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
nullingTLS_intermediateState = 1; |
|
|
nullingTLS_intermediateState = 1; |
|
|
} else if (nullingTLS_intermediateState == 1) { |
|
|
} else if (nullingTLS_intermediateState == 1) { |
|
|
printStatus("NULLING_TLS,1"); |
|
|
printStatus("NULLING_TLS,1"); |
|
|
if (RedButton.isPressed()) { |
|
|
if (RedButton.isPressed()) { |
|
|
//Move elevator until it touch the TLS (WLS_PIN goes HIGH) |
|
|
//Move elevator until it touch the TLS (WLS_PIN goes HIGH) |
|
|
Stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_UPWARD); |
|
|
|
|
|
|
|
|
// Stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_UPWARD); |
|
|
|
|
|
Router_Elevator.moveToUpperLimitSwitch(); |
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
nullingTLS_intermediateState = 2; |
|
|
nullingTLS_intermediateState = 2; |
|
|
} |
|
|
} |
|
|
} else if (nullingTLS_intermediateState == 2) { |
|
|
} else if (nullingTLS_intermediateState == 2) { |
|
|
printStatus("NULLING_TLS,2"); |
|
|
printStatus("NULLING_TLS,2"); |
|
|
//Move elevator back about toolLenghtSensorHeight (will be the 0-Position) |
|
|
//Move elevator back about toolLenghtSensorHeight (will be the 0-Position) |
|
|
Stepper.clearLimitSwitchActive(); |
|
|
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(Router_Setup.getToolLenghtSensorHeight() * MOVE_DOWNWARD); |
|
|
|
|
|
|
|
|
Router_Elevator.clearLimitSwitch(); |
|
|
|
|
|
Router_Elevator.moveRelativeInMillimeters(Router_Setup.getToolLenghtSensorHeight() * MOVE_DOWNWARD); |
|
|
Serial.println(String(Router_Setup.getToolLenghtSensorHeight() * MOVE_DOWNWARD, 2)); |
|
|
Serial.println(String(Router_Setup.getToolLenghtSensorHeight() * MOVE_DOWNWARD, 2)); |
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
nullingTLS_intermediateState = 3; |
|
|
nullingTLS_intermediateState = 3; |
|
|
} else { // nullingTLS_intermediateState is 3 |
|
|
} else { // nullingTLS_intermediateState is 3 |
|
|
printStatus("NULLING_TLS,3"); |
|
|
printStatus("NULLING_TLS,3"); |
|
|
//Set the 0-Position as actual position |
|
|
//Set the 0-Position as actual position |
|
|
Stepper.setCurrentPositionInMillimeters(0); |
|
|
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
|
|
|
|
|
// Stepper.setCurrentPositionInMillimeters(0); |
|
|
|
|
|
// Stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
|
|
Router_Elevator.setZeroPosition(); |
|
|
actualStatus = IDLE; |
|
|
actualStatus = IDLE; |
|
|
nullingTLS_intermediateState = 0; |
|
|
nullingTLS_intermediateState = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case MOVING_ELEVATOR: |
|
|
case MOVING_ELEVATOR: |
|
|
printStatus("MOVING_ELEVATOR"); |
|
|
printStatus("MOVING_ELEVATOR"); |
|
|
if (limitSwitchState == LOW) { |
|
|
|
|
|
|
|
|
// if (limitSwitchState == LOW) { |
|
|
|
|
|
if (Router_Elevator.isLimitSwitchTriggerd()) { |
|
|
// this will cause to stop any motion that is currently going on and block further movement in the same direction as long as the switch is active |
|
|
// this will cause to stop any motion that is currently going on and block further movement in the same direction as long as the switch is active |
|
|
Stepper.setLimitSwitchActive(Stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); |
|
|
|
|
|
|
|
|
// Stepper.setLimitSwitchActive(Stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); |
|
|
delay(200); |
|
|
delay(200); |
|
|
actualStatus = RELEASE_SWITCH; |
|
|
actualStatus = RELEASE_SWITCH; |
|
|
} else { // limitSwitchState is HIGH |
|
|
} else { // limitSwitchState is HIGH |
|
|
if (Stepper.getDistanceToTargetSigned() == 0) { |
|
|
|
|
|
|
|
|
// if (Stepper.getDistanceToTargetSigned() == 0) { |
|
|
|
|
|
if (Router_Elevator.isTargetPositionReached()) { |
|
|
|
|
|
Serial.println("isTargetPositionReached"); |
|
|
|
|
|
actualStatus = originStatus; |
|
|
|
|
|
delay(200); |
|
|
|
|
|
} else if (Router_Elevator.isWLSTriggerd()) { |
|
|
|
|
|
// } else if (WlsDetect.isConnected()) { |
|
|
|
|
|
// if (Wls.isPlugged()) { |
|
|
|
|
|
// Serial.println("The Tool is away from WLS"); |
|
|
|
|
|
// Stepper.clearLimitSwitchActive(); |
|
|
|
|
|
// } else if (Wls.isUnplugged()) { |
|
|
|
|
|
// Serial.println("The Tool touched the WLS"); |
|
|
|
|
|
// Stepper.setLimitSwitchActive(Stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); |
|
|
actualStatus = originStatus; |
|
|
actualStatus = originStatus; |
|
|
delay(200); |
|
|
delay(200); |
|
|
} else if (WlsDetect.isConnected()) { |
|
|
|
|
|
if (Wls.isPlugged()) { |
|
|
|
|
|
Serial.println("The Tool is away from WLS"); |
|
|
|
|
|
Stepper.clearLimitSwitchActive(); |
|
|
|
|
|
} else if (Wls.isUnplugged()) { |
|
|
|
|
|
Serial.println("The Tool touched the WLS"); |
|
|
|
|
|
Stepper.setLimitSwitchActive(Stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); |
|
|
|
|
|
actualStatus = originStatus; |
|
|
|
|
|
delay(200); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// } |
|
|
} |
|
|
} |
|
|
Stepper.clearLimitSwitchActive(); |
|
|
|
|
|
previousDirection = Stepper.getDirectionOfMotion(); |
|
|
|
|
|
|
|
|
// Stepper.clearLimitSwitchActive(); |
|
|
|
|
|
// previousDirection = Stepper.getDirectionOfMotion(); |
|
|
|
|
|
Router_Elevator.clearLimitSwitch(); |
|
|
|
|
|
Router_Elevator.checkDirection(); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case RELEASE_SWITCH: |
|
|
case RELEASE_SWITCH: |
|
|
if (limitSwitchState == LOW) { |
|
|
|
|
|
Stepper.moveRelativeInMillimeters(0.05 * previousDirection * -1); // move in opposite direction (away from switch) |
|
|
|
|
|
|
|
|
printStatus("RELEASE_SWITCH"); |
|
|
|
|
|
// if (limitSwitchState == LOW) { |
|
|
|
|
|
if (Router_Elevator.isLimitSwitchTriggerd()) { |
|
|
|
|
|
// Stepper.moveRelativeInMillimeters(0.05 * previousDirection * -1); // move in opposite direction (away from switch) |
|
|
|
|
|
Router_Elevator.tryReleaseLimitSwitch(); |
|
|
} else { |
|
|
} else { |
|
|
actualStatus = originStatus; |
|
|
actualStatus = originStatus; |
|
|
} |
|
|
} |