2014-05-05 12:50:13 -06:00
|
|
|
|
// Do not remove the include below
|
|
|
|
|
#include "farmbot_arduino_controller.h"
|
2014-05-15 11:44:28 -06:00
|
|
|
|
#include "pins.h"
|
2014-05-16 14:27:16 -06:00
|
|
|
|
#include "Config.h"
|
2015-06-14 15:08:57 -06:00
|
|
|
|
#include "StepperControl.h"
|
2017-04-15 15:32:01 -06:00
|
|
|
|
#include "ServoControl.h"
|
2016-04-13 14:35:45 -06:00
|
|
|
|
#include "PinGuard.h"
|
2017-03-27 13:58:25 -06:00
|
|
|
|
#include "MemoryFree.h"
|
2017-04-20 04:34:39 -06:00
|
|
|
|
#include "Debug.h"
|
2017-04-26 14:45:05 -06:00
|
|
|
|
#include "CurrentState.h"
|
2018-01-29 13:59:02 -07:00
|
|
|
|
#include <SPI.h>
|
2017-03-27 13:58:25 -06:00
|
|
|
|
|
2019-05-03 14:10:42 -06:00
|
|
|
|
#if !defined(FARMDUINO_EXP_V20)
|
|
|
|
|
#include "TimerOne.h"
|
2019-03-30 15:24:49 -06:00
|
|
|
|
#endif
|
2019-03-13 13:57:43 -06:00
|
|
|
|
|
|
|
|
|
bool stepperInit = false;
|
|
|
|
|
bool stepperFlip = false;
|
|
|
|
|
|
2014-05-05 12:50:13 -06:00
|
|
|
|
static char commandEndChar = 0x0A;
|
2017-04-19 08:12:12 -06:00
|
|
|
|
static GCodeProcessor *gCodeProcessor = new GCodeProcessor();
|
2014-05-05 12:50:13 -06:00
|
|
|
|
|
2015-04-02 15:12:44 -06:00
|
|
|
|
unsigned long lastAction;
|
|
|
|
|
unsigned long currentTime;
|
2017-04-17 13:25:27 -06:00
|
|
|
|
unsigned long cycleCounter = 0;
|
2017-04-26 14:45:05 -06:00
|
|
|
|
bool previousEmergencyStop = false;
|
2015-04-02 15:12:44 -06:00
|
|
|
|
|
2017-11-29 14:50:59 -07:00
|
|
|
|
unsigned long pinGuardLastCheck;
|
|
|
|
|
unsigned long pinGuardCurrentTime;
|
|
|
|
|
|
2017-04-15 15:32:01 -06:00
|
|
|
|
int lastParamChangeNr = 0;
|
|
|
|
|
|
2016-12-03 13:58:27 -07:00
|
|
|
|
String commandString = "";
|
|
|
|
|
char incomingChar = 0;
|
|
|
|
|
char incomingCommandArray[INCOMING_CMD_BUF_SIZE];
|
|
|
|
|
int incomingCommandPointer = 0;
|
|
|
|
|
|
2016-05-08 14:35:50 -06:00
|
|
|
|
// Blink led routine used for testing
|
2016-05-02 15:14:01 -06:00
|
|
|
|
bool blink = false;
|
2017-04-19 08:12:12 -06:00
|
|
|
|
void blinkLed()
|
|
|
|
|
{
|
|
|
|
|
blink = !blink;
|
|
|
|
|
digitalWrite(LED_PIN, blink);
|
2016-05-02 15:14:01 -06:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 14:35:45 -06:00
|
|
|
|
// Interrupt handling for:
|
|
|
|
|
// - movement
|
|
|
|
|
// - encoders
|
|
|
|
|
// - pin guard
|
|
|
|
|
//
|
2017-05-01 13:57:05 -06:00
|
|
|
|
|
|
|
|
|
unsigned long interruptStartTime = 0;
|
|
|
|
|
unsigned long interruptStopTime = 0;
|
|
|
|
|
unsigned long interruptDuration = 0;
|
|
|
|
|
unsigned long interruptDurationMax = 0;
|
|
|
|
|
|
2016-04-13 14:35:45 -06:00
|
|
|
|
bool interruptBusy = false;
|
|
|
|
|
int interruptSecondTimer = 0;
|
2019-05-03 14:10:42 -06:00
|
|
|
|
|
|
|
|
|
#if !defined(FARMDUINO_EXP_V20)
|
2017-04-19 08:12:12 -06:00
|
|
|
|
void interrupt(void)
|
|
|
|
|
{
|
2017-04-20 04:34:39 -06:00
|
|
|
|
if (!debugInterrupt)
|
2017-04-19 08:12:12 -06:00
|
|
|
|
{
|
2017-11-29 14:50:59 -07:00
|
|
|
|
//interruptSecondTimer++;
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
2017-04-20 04:34:39 -06:00
|
|
|
|
if (interruptBusy == false)
|
2017-04-19 08:12:12 -06:00
|
|
|
|
{
|
2017-11-29 14:50:59 -07:00
|
|
|
|
//interruptStartTime = micros();
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
2017-04-20 04:34:39 -06:00
|
|
|
|
interruptBusy = true;
|
|
|
|
|
StepperControl::getInstance()->handleMovementInterrupt();
|
2019-03-30 15:24:49 -06:00
|
|
|
|
interruptBusy = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-03 14:10:42 -06:00
|
|
|
|
#endif
|
2017-04-20 04:34:39 -06:00
|
|
|
|
|
2019-05-03 14:10:42 -06:00
|
|
|
|
#if defined(FARMDUINO_EXP_V20)
|
2019-03-30 15:24:49 -06:00
|
|
|
|
ISR(TIMER2_OVF_vect) {
|
2017-05-01 13:57:05 -06:00
|
|
|
|
|
2019-05-03 14:10:42 -06:00
|
|
|
|
if (interruptBusy == false)
|
2019-03-30 15:24:49 -06:00
|
|
|
|
{
|
2019-05-03 14:10:42 -06:00
|
|
|
|
interruptBusy = true;
|
|
|
|
|
StepperControl::getInstance()->handleMovementInterrupt();
|
|
|
|
|
interruptBusy = false;
|
2017-04-19 08:12:12 -06:00
|
|
|
|
}
|
2016-04-13 14:35:45 -06:00
|
|
|
|
}
|
2019-05-03 14:10:42 -06:00
|
|
|
|
#endif
|
2019-03-30 15:24:49 -06:00
|
|
|
|
|
2014-05-05 12:50:13 -06:00
|
|
|
|
//The setup function is called once at startup of the sketch
|
2017-04-19 08:12:12 -06:00
|
|
|
|
void setup()
|
|
|
|
|
{
|
2017-05-30 13:06:26 -06:00
|
|
|
|
#ifdef RAMPS_V14
|
|
|
|
|
|
|
|
|
|
// Setup pin input/output settings
|
|
|
|
|
pinMode(X_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(E_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(E_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(E_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(X_ENCDR_A, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_ENCDR_B, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_ENCDR_A_Q, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_ENCDR_B_Q, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Y_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Y_ENCDR_A, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_ENCDR_B, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_ENCDR_A_Q, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_ENCDR_B_Q, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Z_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Z_ENCDR_A, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_ENCDR_B, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_ENCDR_A_Q, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_ENCDR_B_Q, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(HEATER_0_PIN, OUTPUT);
|
|
|
|
|
pinMode(HEATER_1_PIN, OUTPUT);
|
|
|
|
|
pinMode(FAN_PIN, OUTPUT);
|
|
|
|
|
pinMode(LED_PIN, OUTPUT);
|
|
|
|
|
|
|
|
|
|
pinMode(UTM_C, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_D, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_E, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_F, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_G, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_H, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_I, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_J, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_K, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_L, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
// Aux 1 pins to safer state
|
|
|
|
|
pinMode(AUX1_00, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX1_01, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX1_57, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX1_58, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
// Aux 3 pins to safer state
|
|
|
|
|
pinMode(AUX3_49, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX3_50, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX3_51, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
// Aux 4 pins to safer state
|
|
|
|
|
pinMode(AUX4_43, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX4_45, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX4_47, INPUT_PULLUP);
|
|
|
|
|
pinMode(AUX4_32, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
//pinMode(SERVO_0_PIN , OUTPUT);
|
|
|
|
|
//pinMode(SERVO_1_PIN , OUTPUT);
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-01-29 13:59:02 -07:00
|
|
|
|
#if defined(FARMDUINO_V10) || defined(FARMDUINO_V14)
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
|
|
|
|
// Setup pin input/output settings
|
|
|
|
|
pinMode(X_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_ENABLE_PIN, OUTPUT);
|
2017-12-10 14:01:13 -07:00
|
|
|
|
|
2017-05-30 13:06:26 -06:00
|
|
|
|
pinMode(E_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(E_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(E_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(X_ENCDR_A, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_ENCDR_B, INPUT_PULLUP);
|
2017-12-01 14:27:18 -07:00
|
|
|
|
//pinMode(X_ENCDR_A_Q, INPUT_PULLUP);
|
|
|
|
|
//pinMode(X_ENCDR_B_Q, INPUT_PULLUP);
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
|
|
|
|
pinMode(Y_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Y_ENCDR_A, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_ENCDR_B, INPUT_PULLUP);
|
2017-12-01 14:27:18 -07:00
|
|
|
|
//pinMode(Y_ENCDR_A_Q, INPUT_PULLUP);
|
|
|
|
|
//pinMode(Y_ENCDR_B_Q, INPUT_PULLUP);
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
|
|
|
|
pinMode(Z_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Z_ENCDR_A, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_ENCDR_B, INPUT_PULLUP);
|
2017-12-01 14:27:18 -07:00
|
|
|
|
//pinMode(Z_ENCDR_A_Q, INPUT_PULLUP);
|
|
|
|
|
//pinMode(Z_ENCDR_B_Q, INPUT_PULLUP);
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
2017-07-07 19:36:31 -06:00
|
|
|
|
pinMode(AUX_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(AUX_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(AUX_ENABLE_PIN, OUTPUT);
|
2017-09-18 13:50:32 -06:00
|
|
|
|
digitalWrite(AUX_ENABLE_PIN, HIGH);
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
|
|
|
|
pinMode(LED_PIN, OUTPUT);
|
2017-07-07 19:36:31 -06:00
|
|
|
|
pinMode(VACUUM_PIN, OUTPUT);
|
|
|
|
|
pinMode(WATER_PIN, OUTPUT);
|
|
|
|
|
pinMode(LIGHTING_PIN, OUTPUT);
|
2017-07-07 21:01:02 -06:00
|
|
|
|
pinMode(PERIPHERAL_4_PIN, OUTPUT);
|
|
|
|
|
pinMode(PERIPHERAL_5_PIN, OUTPUT);
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
|
|
|
|
pinMode(UTM_C, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_D, INPUT_PULLUP);
|
2017-12-01 14:27:18 -07:00
|
|
|
|
if (UTM_E > 0) { pinMode(UTM_E, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_F > 0) { pinMode(UTM_F, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_G > 0) { pinMode(UTM_G, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_H > 0) { pinMode(UTM_H, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_I > 0) { pinMode(UTM_I, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_J > 0) { pinMode(UTM_J, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_K > 0) { pinMode(UTM_K, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_L > 0) { pinMode(UTM_L, INPUT_PULLUP); };
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
2017-07-07 19:36:31 -06:00
|
|
|
|
pinMode(SERVO_0_PIN, OUTPUT);
|
|
|
|
|
pinMode(SERVO_1_PIN, OUTPUT);
|
|
|
|
|
pinMode(SERVO_2_PIN, OUTPUT);
|
|
|
|
|
pinMode(SERVO_3_PIN, OUTPUT);
|
2017-05-30 13:06:26 -06:00
|
|
|
|
|
|
|
|
|
#endif
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
2018-01-29 13:59:02 -07:00
|
|
|
|
#if defined(FARMDUINO_V14)
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
2018-01-29 13:59:02 -07:00
|
|
|
|
pinMode(READ_ENA_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(NSS_PIN, OUTPUT);
|
|
|
|
|
digitalWrite(NSS_PIN, HIGH);
|
|
|
|
|
|
|
|
|
|
SPI.setBitOrder(MSBFIRST);
|
|
|
|
|
SPI.setDataMode(SPI_MODE0);
|
|
|
|
|
SPI.setClockDivider(SPI_CLOCK_DIV4);
|
|
|
|
|
SPI.begin();
|
|
|
|
|
|
|
|
|
|
#endif
|
2019-03-30 15:24:49 -06:00
|
|
|
|
|
2019-06-02 09:39:32 -06:00
|
|
|
|
#if defined(FARMDUINO_EXP_V20)
|
|
|
|
|
|
|
|
|
|
// Motor step, directio and pin is setup using the control chip library
|
|
|
|
|
// This board also does not use encoders
|
|
|
|
|
|
|
|
|
|
pinMode(X_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(X_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(X_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(E_ENABLE_PIN, OUTPUT);
|
|
|
|
|
|
|
|
|
|
pinMode(Y_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(Y_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(Y_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(Z_ENABLE_PIN, OUTPUT);
|
|
|
|
|
pinMode(Z_MIN_PIN, INPUT_PULLUP);
|
|
|
|
|
pinMode(Z_MAX_PIN, INPUT_PULLUP);
|
|
|
|
|
|
|
|
|
|
pinMode(AUX_STEP_PIN, OUTPUT);
|
|
|
|
|
pinMode(AUX_DIR_PIN, OUTPUT);
|
|
|
|
|
pinMode(AUX_ENABLE_PIN, OUTPUT);
|
|
|
|
|
|
|
|
|
|
pinMode(LED_PIN, OUTPUT);
|
|
|
|
|
pinMode(VACUUM_PIN, OUTPUT);
|
|
|
|
|
pinMode(WATER_PIN, OUTPUT);
|
|
|
|
|
pinMode(LIGHTING_PIN, OUTPUT);
|
|
|
|
|
pinMode(PERIPHERAL_4_PIN, OUTPUT);
|
|
|
|
|
pinMode(PERIPHERAL_5_PIN, OUTPUT);
|
|
|
|
|
|
|
|
|
|
pinMode(UTM_C, INPUT_PULLUP);
|
|
|
|
|
pinMode(UTM_D, INPUT_PULLUP);
|
|
|
|
|
if (UTM_E > 0) { pinMode(UTM_E, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_F > 0) { pinMode(UTM_F, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_G > 0) { pinMode(UTM_G, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_H > 0) { pinMode(UTM_H, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_I > 0) { pinMode(UTM_I, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_J > 0) { pinMode(UTM_J, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_K > 0) { pinMode(UTM_K, INPUT_PULLUP); };
|
|
|
|
|
if (UTM_L > 0) { pinMode(UTM_L, INPUT_PULLUP); };
|
|
|
|
|
|
|
|
|
|
pinMode(SERVO_0_PIN, OUTPUT);
|
|
|
|
|
pinMode(SERVO_1_PIN, OUTPUT);
|
|
|
|
|
pinMode(SERVO_2_PIN, OUTPUT);
|
|
|
|
|
pinMode(SERVO_3_PIN, OUTPUT);
|
|
|
|
|
|
|
|
|
|
#endif
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
2019-06-02 09:39:32 -06:00
|
|
|
|
digitalWrite(X_ENABLE_PIN, HIGH);
|
|
|
|
|
digitalWrite(E_ENABLE_PIN, HIGH);
|
|
|
|
|
digitalWrite(Y_ENABLE_PIN, HIGH);
|
|
|
|
|
digitalWrite(Z_ENABLE_PIN, HIGH);
|
|
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
2017-04-19 08:12:12 -06:00
|
|
|
|
delay(100);
|
|
|
|
|
|
|
|
|
|
// Start the motor handling
|
|
|
|
|
//ServoControl::getInstance()->attach();
|
|
|
|
|
|
2017-04-26 14:45:05 -06:00
|
|
|
|
// Load motor settings
|
|
|
|
|
StepperControl::getInstance()->loadSettings();
|
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
// Dump all values to the serial interface
|
2017-04-26 15:47:58 -06:00
|
|
|
|
// ParameterList::getInstance()->readAllValues();
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
|
|
|
|
// Get the settings for the pin guard
|
|
|
|
|
PinGuard::getInstance()->loadConfig();
|
2017-11-29 14:50:59 -07:00
|
|
|
|
pinGuardLastCheck = millis();
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
|
|
|
|
// Start the interrupt used for moving
|
|
|
|
|
// Interrupt management code library written by Paul Stoffregen
|
|
|
|
|
// The default time 100 micro seconds
|
|
|
|
|
|
2019-03-13 13:57:43 -06:00
|
|
|
|
#if !defined(FARMDUINO_EXP_V20)
|
|
|
|
|
Timer1.attachInterrupt(interrupt);
|
|
|
|
|
Timer1.initialize(MOVEMENT_INTERRUPT_SPEED);
|
|
|
|
|
Timer1.start();
|
|
|
|
|
#endif
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
2019-06-02 09:39:32 -06:00
|
|
|
|
#if defined(FARMDUINO_EXP_V20)
|
2019-03-30 15:24:49 -06:00
|
|
|
|
TIMSK2 = (TIMSK2 & B11111110) | 0x01; // Enable timer overflow
|
|
|
|
|
TCCR2B = (TCCR2B & B11111000) | 0x01; // Set divider to 1
|
|
|
|
|
OCR2A = 4; // Set overflow to 4 for total of 64 <20>s
|
2019-06-02 09:39:32 -06:00
|
|
|
|
#endif
|
2019-03-30 15:24:49 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
// Initialize the inactivity check
|
|
|
|
|
lastAction = millis();
|
2017-04-26 14:45:05 -06:00
|
|
|
|
|
2017-12-10 14:01:13 -07:00
|
|
|
|
pinGuardCurrentTime = millis();
|
|
|
|
|
pinGuardLastCheck = millis();
|
|
|
|
|
|
2018-02-06 12:22:57 -07:00
|
|
|
|
if
|
|
|
|
|
(
|
|
|
|
|
(ParameterList::getInstance()->getValue(PARAM_CONFIG_OK) == 1) &&
|
|
|
|
|
(ParameterList::getInstance()->getValue(MOVEMENT_HOME_AT_BOOT_Z) == 1)
|
|
|
|
|
)
|
2017-04-26 14:45:05 -06:00
|
|
|
|
{
|
2017-05-14 14:09:07 -06:00
|
|
|
|
Serial.print("R99 HOME Z ON STARTUP\r\n");
|
|
|
|
|
StepperControl::getInstance()->moveToCoords(0, 0, 0, 0, 0, 0, false, false, true);
|
2017-04-26 14:45:05 -06:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 12:22:57 -07:00
|
|
|
|
if
|
|
|
|
|
(
|
|
|
|
|
(ParameterList::getInstance()->getValue(PARAM_CONFIG_OK) == 1) &&
|
|
|
|
|
(ParameterList::getInstance()->getValue(MOVEMENT_HOME_AT_BOOT_Y) == 1)
|
|
|
|
|
)
|
2017-04-26 14:45:05 -06:00
|
|
|
|
{
|
2017-05-07 06:58:33 -06:00
|
|
|
|
Serial.print("R99 HOME Y ON STARTUP\r\n");
|
2017-04-26 14:45:05 -06:00
|
|
|
|
StepperControl::getInstance()->moveToCoords(0, 0, 0, 0, 0, 0, false, true, false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-06 12:22:57 -07:00
|
|
|
|
if
|
|
|
|
|
(
|
|
|
|
|
(ParameterList::getInstance()->getValue(PARAM_CONFIG_OK) == 1) &&
|
|
|
|
|
(ParameterList::getInstance()->getValue(MOVEMENT_HOME_AT_BOOT_X) == 1)
|
|
|
|
|
)
|
2017-04-26 14:45:05 -06:00
|
|
|
|
{
|
2017-05-14 14:09:07 -06:00
|
|
|
|
Serial.print("R99 HOME X ON STARTUP\r\n");
|
|
|
|
|
StepperControl::getInstance()->moveToCoords(0, 0, 0, 0, 0, 0, true, false, false);
|
2017-04-26 14:45:05 -06:00
|
|
|
|
}
|
2017-05-01 13:57:05 -06:00
|
|
|
|
|
2019-03-13 13:57:43 -06:00
|
|
|
|
|
2019-06-02 09:39:32 -06:00
|
|
|
|
#if defined(FARMDUINO_EXP_V20)
|
|
|
|
|
// initialise the motors
|
|
|
|
|
StepperControl::getInstance()->initTMC2130();
|
|
|
|
|
StepperControl::getInstance()->loadSettingsTMC2130();
|
|
|
|
|
#endif
|
2019-05-03 14:10:42 -06:00
|
|
|
|
|
|
|
|
|
Serial.print("R99 ARDUINO STARTUP COMPLETE\r\n");
|
|
|
|
|
|
|
|
|
|
//StepperControl::getInstance()->test2();
|
2014-05-05 12:50:13 -06:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 07:16:35 -06:00
|
|
|
|
char commandChar[INCOMING_CMD_BUF_SIZE + 1];
|
|
|
|
|
|
2014-05-05 12:50:13 -06:00
|
|
|
|
// The loop function is called in an endless loop
|
2017-04-19 08:12:12 -06:00
|
|
|
|
void loop()
|
|
|
|
|
{
|
|
|
|
|
|
2019-06-02 09:39:32 -06:00
|
|
|
|
//Serial.print(millis());
|
|
|
|
|
//Serial.print("\r\n");
|
|
|
|
|
//delay(1000);
|
|
|
|
|
|
2019-03-30 15:24:49 -06:00
|
|
|
|
//StepperControl::getInstance()->test();
|
2019-05-03 14:10:42 -06:00
|
|
|
|
//delayMicroseconds(100);
|
2019-03-13 13:57:43 -06:00
|
|
|
|
|
2019-05-03 14:10:42 -06:00
|
|
|
|
//digitalWrite(LED_PIN, true);
|
|
|
|
|
//delay(250);
|
|
|
|
|
//digitalWrite(LED_PIN, false);
|
|
|
|
|
//delay(250);
|
2019-03-13 13:57:43 -06:00
|
|
|
|
|
2017-04-20 04:34:39 -06:00
|
|
|
|
if (debugInterrupt)
|
|
|
|
|
{
|
|
|
|
|
StepperControl::getInstance()->handleMovementInterrupt();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 01:29:27 -07:00
|
|
|
|
#if defined(FARMDUINO_V14)
|
|
|
|
|
// Check encoders out of interrupt for farmduino 1.4
|
|
|
|
|
StepperControl::getInstance()->checkEncoders();
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-12-10 14:01:13 -07:00
|
|
|
|
pinGuardCurrentTime = millis();
|
|
|
|
|
if (pinGuardCurrentTime < pinGuardLastCheck)
|
2017-04-19 08:12:12 -06:00
|
|
|
|
{
|
2017-12-10 14:01:13 -07:00
|
|
|
|
pinGuardLastCheck = pinGuardCurrentTime;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pinGuardCurrentTime - pinGuardLastCheck >= 1000)
|
2017-11-29 14:50:59 -07:00
|
|
|
|
{
|
2017-12-10 14:01:13 -07:00
|
|
|
|
pinGuardLastCheck += 1000;
|
|
|
|
|
|
|
|
|
|
// check the pins for timeouts
|
|
|
|
|
PinGuard::getInstance()->checkPins();
|
2017-11-29 14:50:59 -07:00
|
|
|
|
}
|
2017-12-10 14:01:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Serial.available())
|
|
|
|
|
{
|
2017-04-19 08:12:12 -06:00
|
|
|
|
// Save current time stamp for timeout actions
|
|
|
|
|
lastAction = millis();
|
|
|
|
|
|
|
|
|
|
// Get the input and start processing on receiving 'new line'
|
|
|
|
|
incomingChar = Serial.read();
|
2017-04-26 14:45:05 -06:00
|
|
|
|
|
|
|
|
|
// Filter out emergency stop.
|
2017-04-23 12:50:33 -06:00
|
|
|
|
if (!(incomingChar == 69 || incomingChar == 101))
|
|
|
|
|
{
|
|
|
|
|
incomingCommandArray[incomingCommandPointer] = incomingChar;
|
|
|
|
|
incomingCommandPointer++;
|
|
|
|
|
}
|
2017-04-26 14:45:05 -06:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CurrentState::getInstance()->setEmergencyStop();
|
|
|
|
|
}
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
|
|
|
|
// If the string is getting to long, cap it off with a new line and let it process anyway
|
|
|
|
|
if (incomingCommandPointer >= INCOMING_CMD_BUF_SIZE - 1)
|
|
|
|
|
{
|
|
|
|
|
incomingChar = '\n';
|
|
|
|
|
incomingCommandArray[incomingCommandPointer] = incomingChar;
|
|
|
|
|
incomingCommandPointer++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (incomingChar == '\n' || incomingCommandPointer >= INCOMING_CMD_BUF_SIZE)
|
|
|
|
|
{
|
|
|
|
|
|
2017-07-30 07:16:35 -06:00
|
|
|
|
//char commandChar[incomingCommandPointer + 1];
|
2017-04-19 08:12:12 -06:00
|
|
|
|
for (int i = 0; i < incomingCommandPointer - 1; i++)
|
|
|
|
|
{
|
2017-07-30 07:16:35 -06:00
|
|
|
|
commandChar[i] = incomingCommandArray[i];
|
2017-04-19 08:12:12 -06:00
|
|
|
|
}
|
2017-07-30 07:16:35 -06:00
|
|
|
|
commandChar[incomingCommandPointer-1] = '\0';
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
|
|
|
|
if (incomingCommandPointer > 1)
|
|
|
|
|
{
|
|
|
|
|
|
2017-07-30 07:16:35 -06:00
|
|
|
|
// Report back the received command
|
|
|
|
|
Serial.print(COMM_REPORT_CMD_ECHO);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print("*");
|
|
|
|
|
Serial.print(commandChar);
|
|
|
|
|
Serial.print("*");
|
|
|
|
|
Serial.print("\r\n");
|
2017-04-19 08:12:12 -06:00
|
|
|
|
|
|
|
|
|
// Create a command and let it execute
|
|
|
|
|
Command *command = new Command(commandChar);
|
|
|
|
|
|
2017-07-27 14:00:11 -06:00
|
|
|
|
// Log the values if needed for debugging
|
2017-04-23 12:50:33 -06:00
|
|
|
|
if (LOGGING || debugMessages)
|
2017-04-19 08:12:12 -06:00
|
|
|
|
{
|
|
|
|
|
command->print();
|
|
|
|
|
}
|
2017-05-02 12:35:16 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
gCodeProcessor->execute(command);
|
2017-05-02 12:35:16 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
free(command);
|
2017-04-26 14:45:05 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
}
|
2015-11-02 14:15:49 -07:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
incomingCommandPointer = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-15 15:32:01 -06:00
|
|
|
|
|
2017-04-26 14:45:05 -06:00
|
|
|
|
// In case of emergency stop, disable movement and
|
|
|
|
|
// shut down the pins used
|
|
|
|
|
if (previousEmergencyStop == false && CurrentState::getInstance()->isEmergencyStop())
|
|
|
|
|
{
|
2017-05-14 14:09:07 -06:00
|
|
|
|
StepperControl::getInstance()->disableMotorsEmergency();
|
2017-04-26 14:45:05 -06:00
|
|
|
|
PinControl::getInstance()->resetPinsUsed();
|
2017-04-28 12:36:56 -06:00
|
|
|
|
if (debugMessages)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(COMM_REPORT_COMMENT);
|
|
|
|
|
Serial.print(" Going to safe state");
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
}
|
2017-04-26 14:45:05 -06:00
|
|
|
|
}
|
2017-04-28 12:36:56 -06:00
|
|
|
|
previousEmergencyStop = CurrentState::getInstance()->isEmergencyStop();
|
2017-05-02 12:35:16 -06:00
|
|
|
|
|
2019-05-03 14:10:42 -06:00
|
|
|
|
// Check if parameters are changed, and if so load the new settings
|
2017-04-19 08:12:12 -06:00
|
|
|
|
if (lastParamChangeNr != ParameterList::getInstance()->paramChangeNumber())
|
|
|
|
|
{
|
|
|
|
|
lastParamChangeNr = ParameterList::getInstance()->paramChangeNumber();
|
2017-04-15 15:32:01 -06:00
|
|
|
|
|
2017-05-01 13:57:05 -06:00
|
|
|
|
if (debugMessages)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(COMM_REPORT_COMMENT);
|
|
|
|
|
Serial.print(" loading parameters");
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
}
|
2017-04-15 15:32:01 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
StepperControl::getInstance()->loadSettings();
|
|
|
|
|
PinGuard::getInstance()->loadConfig();
|
|
|
|
|
}
|
2017-04-15 15:32:01 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
// Do periodic checks and feedback
|
2017-02-01 14:03:49 -07:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
currentTime = millis();
|
|
|
|
|
if (currentTime < lastAction)
|
|
|
|
|
{
|
2015-04-01 15:18:05 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
// If the device timer overruns, reset the idle counter
|
|
|
|
|
lastAction = millis();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-03-27 13:58:25 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
if ((currentTime - lastAction) > 5000)
|
|
|
|
|
{
|
|
|
|
|
// After an idle time, send the idle message
|
2016-10-02 15:43:39 -06:00
|
|
|
|
|
2017-04-28 12:36:56 -06:00
|
|
|
|
if (CurrentState::getInstance()->isEmergencyStop())
|
|
|
|
|
{
|
|
|
|
|
Serial.print(COMM_REPORT_EMERGENCY_STOP);
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
|
|
|
|
|
if (debugMessages)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(COMM_REPORT_COMMENT);
|
|
|
|
|
Serial.print(" Emergency stop engaged");
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Serial.print(COMM_REPORT_CMD_IDLE);
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
}
|
2017-03-18 15:52:10 -06:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
StepperControl::getInstance()->storePosition();
|
|
|
|
|
CurrentState::getInstance()->printPosition();
|
2016-12-03 13:58:27 -07:00
|
|
|
|
|
2017-05-10 12:52:38 -06:00
|
|
|
|
StepperControl::getInstance()->reportEncoders();
|
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
CurrentState::getInstance()->storeEndStops();
|
|
|
|
|
CurrentState::getInstance()->printEndStops();
|
2017-03-27 13:58:25 -06:00
|
|
|
|
|
2017-05-02 12:35:16 -06:00
|
|
|
|
if (debugMessages)
|
|
|
|
|
{
|
2017-04-20 04:34:39 -06:00
|
|
|
|
Serial.print(COMM_REPORT_COMMENT);
|
|
|
|
|
Serial.print(" MEM ");
|
|
|
|
|
Serial.print(freeMemory());
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
|
2017-05-01 13:57:05 -06:00
|
|
|
|
Serial.print(COMM_REPORT_COMMENT);
|
|
|
|
|
Serial.print(" IND DUR ");
|
|
|
|
|
Serial.print(interruptDuration);
|
|
|
|
|
Serial.print(" MAX ");
|
|
|
|
|
Serial.print(interruptDurationMax);
|
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
|
|
|
|
|
2017-04-20 04:34:39 -06:00
|
|
|
|
StepperControl::getInstance()->test();
|
2017-05-02 12:35:16 -06:00
|
|
|
|
}
|
2017-04-20 04:34:39 -06:00
|
|
|
|
|
2018-02-06 12:22:57 -07:00
|
|
|
|
if (ParameterList::getInstance()->getValue(PARAM_CONFIG_OK) != 1)
|
|
|
|
|
{
|
|
|
|
|
Serial.print(COMM_REPORT_NO_CONFIG);
|
2018-03-13 12:48:22 -06:00
|
|
|
|
CurrentState::getInstance()->printQAndNewLine();
|
2018-02-06 12:22:57 -07:00
|
|
|
|
}
|
2016-12-03 13:58:27 -07:00
|
|
|
|
|
2017-04-19 08:12:12 -06:00
|
|
|
|
cycleCounter++;
|
|
|
|
|
lastAction = millis();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-02 09:39:32 -06:00
|
|
|
|
|
2014-05-05 12:50:13 -06:00
|
|
|
|
}
|