|
|
|
@@ -14,15 +14,21 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
#include <Arduino.h> |
|
|
|
#include <esp32-hal.h> |
|
|
|
#include <esp32-hal-gpio.h> |
|
|
|
#include <ESP_FlexyStepper.h> |
|
|
|
#include <HardwareSerial.h> |
|
|
|
#include <pins_arduino.h> |
|
|
|
#include <RotaryEncoder.h> |
|
|
|
#include <Wire.h> |
|
|
|
#include <Adafruit_GFX.h> |
|
|
|
#include <ESP_FlexyStepper.h> |
|
|
|
#include <WString.h> |
|
|
|
|
|
|
|
#include "Display.h" |
|
|
|
#include "WLS.h" |
|
|
|
#include "ExEzButton.h" |
|
|
|
#include "RotaryControler.h" |
|
|
|
#include "RouterSetup.h" |
|
|
|
#include "Status.h" |
|
|
|
#include "WLS.h" |
|
|
|
|
|
|
|
#define SDA 22 |
|
|
|
#define SCX 23 |
|
|
|
@@ -53,15 +59,15 @@ int nullingTLS_intermediateState = 0; |
|
|
|
ExEzButton RedButton(RedBtn_Pin, false, 2000); |
|
|
|
ExEzButton GreenButton(GreenBtn_Pin, false, 2000); |
|
|
|
ExEzButton BlueButton(BlueBtn_Pin, false, 2000); |
|
|
|
ExEzButton RotarySwitch(RotEnc_Switch_Pin, true, 2000); |
|
|
|
//ExEzButton RotarySwitch(RotEnc_Switch_Pin, true, 2000); |
|
|
|
|
|
|
|
WLS WlsDetect(WLS_DETECT_Pin, true); |
|
|
|
WLS Wls(WLS_Pin); |
|
|
|
|
|
|
|
RotaryEncoder encoder(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotaryEncoder::LatchMode::FOUR3); |
|
|
|
RotaryControler RotaryControler(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotEnc_Switch_Pin); |
|
|
|
Display Display; |
|
|
|
ESP_FlexyStepper stepper; |
|
|
|
RouterSetup routerSetup; |
|
|
|
ESP_FlexyStepper Stepper; |
|
|
|
RouterSetup Router_Setup; |
|
|
|
|
|
|
|
Status actualStatus; |
|
|
|
Status originStatus; |
|
|
|
@@ -85,21 +91,21 @@ void limitSwitchHandler() { |
|
|
|
//*** Initialization routine. Reads the eeprom first and sets the (potentially new) configured values. |
|
|
|
//***************************************************************************************************** |
|
|
|
void Initialize() { |
|
|
|
routerSetup.readFromEEPROM(); |
|
|
|
routerSetup.printValues(); |
|
|
|
Router_Setup.readFromEEPROM(); |
|
|
|
Router_Setup.printValues(); |
|
|
|
|
|
|
|
Display.showInitialization(); |
|
|
|
//attach an interrupt to the IO pin of the limit switch and specify the handler function |
|
|
|
attachInterrupt(digitalPinToInterrupt(LIMIT_SWITCH), limitSwitchHandler, CHANGE); |
|
|
|
stepper.connectToPins(STEP, DIR); |
|
|
|
Stepper.connectToPins(STEP, DIR); |
|
|
|
// set the speed and acceleration rates for the stepper motor |
|
|
|
stepper.setSpeedInStepsPerSecond(routerSetup.getSpeed() * routerSetup.getStepsPerRev()); |
|
|
|
stepper.setStepsPerRevolution(routerSetup.getStepsPerRev()); |
|
|
|
stepper.setStepsPerMillimeter(routerSetup.getStepsPerRev() / routerSetup.getPitch()); |
|
|
|
stepper.setAccelerationInStepsPerSecondPerSecond(routerSetup.getAcceleration() * routerSetup.getStepsPerRev()); |
|
|
|
stepper.setDecelerationInStepsPerSecondPerSecond(routerSetup.getAcceleration() * routerSetup.getStepsPerRev()); |
|
|
|
stepper.startAsService(0); |
|
|
|
if (routerSetup.isToolChangOnPowerOn()) { |
|
|
|
Stepper.setSpeedInStepsPerSecond(Router_Setup.getSpeed() * Router_Setup.getStepsPerRev()); |
|
|
|
Stepper.setStepsPerRevolution(Router_Setup.getStepsPerRev()); |
|
|
|
Stepper.setStepsPerMillimeter(Router_Setup.getStepsPerRev() / Router_Setup.getPitch()); |
|
|
|
Stepper.setAccelerationInStepsPerSecondPerSecond(Router_Setup.getAcceleration() * Router_Setup.getStepsPerRev()); |
|
|
|
Stepper.setDecelerationInStepsPerSecondPerSecond(Router_Setup.getAcceleration() * Router_Setup.getStepsPerRev()); |
|
|
|
Stepper.startAsService(0); |
|
|
|
if (Router_Setup.isToolChangOnPowerOn()) { |
|
|
|
actualStatus = TOOL_CHANGE; |
|
|
|
} else { |
|
|
|
actualStatus = IDLE; |
|
|
|
@@ -133,7 +139,7 @@ void setup() { |
|
|
|
BlueButton.setDebounceTime(50); |
|
|
|
WlsDetect.setDebounceTime(50); |
|
|
|
Wls.setDebounceTime(50); |
|
|
|
RotarySwitch.setDebounceTime(50); |
|
|
|
RotaryControler.setDebounceTime(50); |
|
|
|
|
|
|
|
delay(1500); |
|
|
|
Display.showBrand(); |
|
|
|
@@ -150,7 +156,9 @@ void loop() { |
|
|
|
BlueButton.loop(); |
|
|
|
WlsDetect.loop(); |
|
|
|
Wls.loop(); |
|
|
|
RotarySwitch.loop(); |
|
|
|
RotaryControler.loop(); |
|
|
|
|
|
|
|
Display.showFrame(actualStatus); |
|
|
|
|
|
|
|
switch (actualStatus) { |
|
|
|
|
|
|
|
@@ -160,7 +168,7 @@ void loop() { |
|
|
|
|
|
|
|
case TOOL_CHANGE: |
|
|
|
printStatus("TOOL_CHANGE"); |
|
|
|
if (RotarySwitch.isLongPressed()) { |
|
|
|
if (RotaryControler.isSwitchLongPressed()) { |
|
|
|
actualStatus = CONFIGURATION; |
|
|
|
} else if (BlueButton.isPressed()) { |
|
|
|
actualStatus = IDLE; |
|
|
|
@@ -169,7 +177,7 @@ void loop() { |
|
|
|
|
|
|
|
case CONFIGURATION: |
|
|
|
printStatus("CONFIGURATION"); |
|
|
|
if (RotarySwitch.isLongPressed()) { |
|
|
|
if (RotaryControler.isSwitchLongPressed()) { |
|
|
|
actualStatus = INITIALIZATION; |
|
|
|
} else if (BlueButton.isPressed()) { |
|
|
|
actualStatus = IDLE; |
|
|
|
@@ -179,8 +187,8 @@ void loop() { |
|
|
|
case NULLING: |
|
|
|
printStatus("NULLING"); |
|
|
|
if (RedButton.isPressed()) { |
|
|
|
stepper.setCurrentPositionInMillimeters(0); |
|
|
|
stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
Stepper.setCurrentPositionInMillimeters(0); |
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
actualStatus = IDLE; |
|
|
|
} |
|
|
|
break; |
|
|
|
@@ -190,30 +198,30 @@ void loop() { |
|
|
|
if (nullingTLS_intermediateState == 0) { |
|
|
|
printStatus("NULLING_TLS,0"); |
|
|
|
//Move elevator to lowest point (lower limit switch triggers) |
|
|
|
stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_DOWNWARD); |
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_DOWNWARD); |
|
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
|
nullingTLS_intermediateState = 1; |
|
|
|
} else if (nullingTLS_intermediateState == 1) { |
|
|
|
printStatus("NULLING_TLS,1"); |
|
|
|
if (RedButton.isPressed()) { |
|
|
|
//Move elevator until it touch the TLS (WLS_PIN goes HIGH) |
|
|
|
stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_UPWARD); |
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(300 * MOVE_UPWARD); |
|
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
|
nullingTLS_intermediateState = 2; |
|
|
|
} |
|
|
|
} else if (nullingTLS_intermediateState == 2) { |
|
|
|
printStatus("NULLING_TLS,2"); |
|
|
|
//Move elevator back about toolLenghtSensorHeight (will be the 0-Position) |
|
|
|
stepper.clearLimitSwitchActive(); |
|
|
|
stepper.setTargetPositionRelativeInMillimeters(routerSetup.getToolLenghtSensorHeight() * MOVE_DOWNWARD); |
|
|
|
Serial.println(String(routerSetup.getToolLenghtSensorHeight() * MOVE_DOWNWARD, 2)); |
|
|
|
Stepper.clearLimitSwitchActive(); |
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(Router_Setup.getToolLenghtSensorHeight() * MOVE_DOWNWARD); |
|
|
|
Serial.println(String(Router_Setup.getToolLenghtSensorHeight() * MOVE_DOWNWARD, 2)); |
|
|
|
actualStatus = MOVING_ELEVATOR; |
|
|
|
nullingTLS_intermediateState = 3; |
|
|
|
} else { // nullingTLS_intermediateState is 3 |
|
|
|
printStatus("NULLING_TLS,3"); |
|
|
|
//Set the 0-Position as actual position |
|
|
|
stepper.setCurrentPositionInMillimeters(0); |
|
|
|
stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
Stepper.setCurrentPositionInMillimeters(0); |
|
|
|
Stepper.setTargetPositionRelativeInMillimeters(0); |
|
|
|
actualStatus = IDLE; |
|
|
|
nullingTLS_intermediateState = 0; |
|
|
|
} |
|
|
|
@@ -239,32 +247,32 @@ void loop() { |
|
|
|
printStatus("MOVING_ELEVATOR"); |
|
|
|
if (limitSwitchState == LOW) { |
|
|
|
// 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); |
|
|
|
actualStatus = RELEASE_SWITCH; |
|
|
|
} else { // limitSwitchState is HIGH |
|
|
|
if (stepper.getDistanceToTargetSigned() == 0) { |
|
|
|
if (Stepper.getDistanceToTargetSigned() == 0) { |
|
|
|
actualStatus = originStatus; |
|
|
|
delay(200); |
|
|
|
} else if (WlsDetect.isConnected()) { |
|
|
|
if (Wls.isPlugged()) { |
|
|
|
Serial.println("The Tool is away from WLS"); |
|
|
|
stepper.clearLimitSwitchActive(); |
|
|
|
Stepper.clearLimitSwitchActive(); |
|
|
|
} else if (Wls.isUnplugged()) { |
|
|
|
Serial.println("The Tool touched the WLS"); |
|
|
|
stepper.setLimitSwitchActive(stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); |
|
|
|
Stepper.setLimitSwitchActive(Stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); |
|
|
|
actualStatus = originStatus; |
|
|
|
delay(200); |
|
|
|
} |
|
|
|
} |
|
|
|
stepper.clearLimitSwitchActive(); |
|
|
|
previousDirection = stepper.getDirectionOfMotion(); |
|
|
|
Stepper.clearLimitSwitchActive(); |
|
|
|
previousDirection = Stepper.getDirectionOfMotion(); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case RELEASE_SWITCH: |
|
|
|
if (limitSwitchState == LOW) { |
|
|
|
stepper.moveRelativeInMillimeters(0.05 * previousDirection * -1); // move in opposite direction (away from switch) |
|
|
|
Stepper.moveRelativeInMillimeters(0.05 * previousDirection * -1); // move in opposite direction (away from switch) |
|
|
|
} else { |
|
|
|
actualStatus = originStatus; |
|
|
|
} |
|
|
|
@@ -321,13 +329,13 @@ void loop() { |
|
|
|
// } |
|
|
|
// |
|
|
|
// static int pos = 0; |
|
|
|
// encoder.tick(); |
|
|
|
// int newPos = encoder.getPosition(); |
|
|
|
//RotaryControler.tick(); |
|
|
|
//int newPos = RotaryControler.getPosition(); |
|
|
|
// if (pos != newPos) { |
|
|
|
// Serial.print("pos:"); |
|
|
|
// Serial.print(newPos); |
|
|
|
// Serial.print(" dir:"); |
|
|
|
// Serial.println((int) (encoder.getDirection())); |
|
|
|
// Serial.println((int) (RotaryControler.getDirection())); |
|
|
|
// pos = newPos; |
|
|
|
// } |
|
|
|
|