Changed Interrupt handling for RP2040 architectures to be similar to ESP32
Added check for proper USB Serial initialisation to examples
This commit is contained in:
@@ -1,7 +1,55 @@
|
||||
#include <AccelStepper.h> // Requires AccelStepper Library - http://www.airspayce.com/mikem/arduino/AccelStepper/
|
||||
#include <EncButton2.h> // Requires EncButton library - https://github.com/GyverLibs/EncButton
|
||||
#include <elapsedMillis.h> // Requires elapsedMillis library - https://github.com/pfeerick/elapsedMillis
|
||||
|
||||
#define NUM_LIFT_LEVELS 7
|
||||
#define OPTIMIZE_I2C 1
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO)
|
||||
#include <Wire.h>
|
||||
#include <SSD1306Ascii.h>
|
||||
#include <SSD1306AsciiWire.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#define NUM_LIFT_LEVELS 8
|
||||
|
||||
#define PROGRAM_NAME "Fahrstuhl"
|
||||
#define PROGRAM_VERSION "1.0"
|
||||
|
||||
#define EEPROM_BASE_ADDR 100
|
||||
#define EEPROM_VALID_DATA_SIGNATURE 0xA5A5
|
||||
// Uncomment the line below to force the EEPROM values to be reset to defaults
|
||||
//#define EEPROM_FORCE_RELOAD_DEFAULT_VALUES
|
||||
|
||||
#define BUTTON_LONG_PRESS_DELAY 2000
|
||||
|
||||
#if defined (ARDUINO_AVR_NANO) // Setting for Uwe's Fahrstuhl System
|
||||
|
||||
// Uncomment the next line to reverse the direction of the stepper movement
|
||||
//#define REVERSE_STEPPER_DIRECTION
|
||||
|
||||
#define HOME_SENSOR_PIN 7
|
||||
|
||||
#define STEPPER_PULSE_PIN 4
|
||||
#define STEPPER_ENABLE_PIN 5
|
||||
#define STEPPER_DIR_PIN 6
|
||||
|
||||
#define STEPPER_MAX_SPEED 2100
|
||||
#define STEPPER_NORMAL_ACCELERATION 5000
|
||||
#define STEPPER_MAX_POSITION 30000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism
|
||||
|
||||
#define BUTTON_MANUAL 8
|
||||
#define BUTTON_DOWN 9
|
||||
#define BUTTON_UP 10
|
||||
#define BUTTON_STOP_HOME 11
|
||||
|
||||
long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000, 19000, 22000}; // Default positions
|
||||
|
||||
#define OLED_DISPLAY_I2C_ADDRESS 0x3C
|
||||
|
||||
#else
|
||||
//#if defined(ARDUINO_AVR_UNO) // Setting for AJS Dev System
|
||||
|
||||
// Uncomment the next line to reverse the direction of the stepper movement
|
||||
#define REVERSE_STEPPER_DIRECTION
|
||||
|
||||
#define HOME_SENSOR_PIN 10
|
||||
|
||||
@@ -11,63 +59,164 @@
|
||||
|
||||
#define STEPPER_MAX_SPEED 2100
|
||||
#define STEPPER_NORMAL_ACCELERATION 5000
|
||||
#define STEPPER_MAX_POSITION 20000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism
|
||||
#define STEPPER_MAX_POSITION 30000U // Maximum Steps to allow the stepper to drive Up Saftey mechanism
|
||||
|
||||
#define BUTTON_OK A3
|
||||
#define BUTTON_DOWN A2
|
||||
#define BUTTON_UP A1
|
||||
#define BUTTON_HOME A0
|
||||
#define BUTTON_MANUAL A3
|
||||
#define BUTTON_DOWN A2
|
||||
#define BUTTON_UP A1
|
||||
#define BUTTON_STOP_HOME A0
|
||||
|
||||
long defaultPositions[NUM_LIFT_LEVELS] = {1000, 4000, 7000, 10000, 13000, 16000, 19000, 22000}; // Default positions
|
||||
|
||||
#define OLED_DISPLAY_I2C_ADDRESS 0x3C
|
||||
|
||||
#elif defined (ARDUINO_AVR_NANO)
|
||||
#endif
|
||||
|
||||
SSD1306AsciiWire oled;
|
||||
|
||||
#define STEPPER_INC_SPEED (STEPPER_MAX_SPEED / 10)
|
||||
#define STEPPER_MAN_SPEED_CHANGE_MILLIS 5
|
||||
#define STEPPER_EMERGENCY_STOP_ACCELERATION 100000
|
||||
#define LIFT_LEVEL_NOT_SET -1
|
||||
|
||||
typedef struct PERSISTENT_VALUES
|
||||
typedef struct
|
||||
{
|
||||
uint8_t numLiftLevels;
|
||||
uint8_t lastLiftLevel;
|
||||
long lastStepperPosition;
|
||||
long listPositions[NUM_LIFT_LEVELS];
|
||||
long levelPositions[NUM_LIFT_LEVELS];
|
||||
uint16_t objectSignature;
|
||||
} persistentValues;
|
||||
} PERSISTENT_VALUES;
|
||||
|
||||
#include <AccelStepper.h>
|
||||
#include <EncButton2.h>
|
||||
#include <elapsedMillis.h>
|
||||
PERSISTENT_VALUES persistentValues;
|
||||
|
||||
// Define a stepper and the pins it will use
|
||||
AccelStepper stepper(AccelStepper::DRIVER, STEPPER_PULSE_PIN, STEPPER_DIR_PIN, -1, -1, false);
|
||||
|
||||
EncButton2<EB_BTN> homeSensor(INPUT_PULLUP, HOME_SENSOR_PIN);
|
||||
|
||||
EncButton2<EB_BTN> btnOk(INPUT, BUTTON_OK);
|
||||
EncButton2<EB_BTN> btnManual(INPUT, BUTTON_MANUAL);
|
||||
EncButton2<EB_BTN> btnDown(INPUT, BUTTON_DOWN);
|
||||
EncButton2<EB_BTN> btnUp(INPUT, BUTTON_UP);
|
||||
EncButton2<EB_BTN> btnHome(INPUT, BUTTON_HOME);
|
||||
EncButton2<EB_BTN> btnStopHome(INPUT, BUTTON_STOP_HOME);
|
||||
|
||||
void displayLevel(int newLevel)
|
||||
{
|
||||
oled.setCursor(0,0);
|
||||
oled.set2X();
|
||||
oled.print("Level: ");
|
||||
oled.print(newLevel);
|
||||
oled.clearToEOL();
|
||||
}
|
||||
|
||||
void displayMessage(const char* Msg)
|
||||
{
|
||||
oled.setCursor(0,4);
|
||||
oled.set2X();
|
||||
oled.print(Msg); oled.clearToEOL();
|
||||
}
|
||||
|
||||
void displayMessageNumber(const char* Msg, int Number)
|
||||
{
|
||||
oled.setCursor(0,4);
|
||||
oled.set2X();
|
||||
oled.print(Msg);
|
||||
oled.print(Number);
|
||||
oled.clearToEOL();
|
||||
}
|
||||
|
||||
void displayPosition(long newPosition)
|
||||
{
|
||||
oled.setCursor(0,7);
|
||||
oled.set1X();
|
||||
oled.print("Pos: ");
|
||||
oled.print(newPosition);
|
||||
oled.clearToEOL();
|
||||
}
|
||||
|
||||
void initPersistentValues()
|
||||
{
|
||||
EEPROM.get(EEPROM_BASE_ADDR, persistentValues);
|
||||
|
||||
#ifdef EEPROM_FORCE_RELOAD_DEFAULT_VALUES
|
||||
persistentValues.objectSignature = 0;
|
||||
#endif
|
||||
|
||||
if(persistentValues.objectSignature != EEPROM_VALID_DATA_SIGNATURE)
|
||||
{
|
||||
Serial.println("initPersistentValues: set detault values");
|
||||
|
||||
persistentValues.numLiftLevels = NUM_LIFT_LEVELS;
|
||||
persistentValues.lastLiftLevel = 0;
|
||||
persistentValues.lastStepperPosition = 0;
|
||||
persistentValues.objectSignature = EEPROM_VALID_DATA_SIGNATURE;
|
||||
for(uint8_t i = 0; i < NUM_LIFT_LEVELS; i++)
|
||||
persistentValues.levelPositions[i] = defaultPositions[i];
|
||||
|
||||
EEPROM.put(EEPROM_BASE_ADDR, persistentValues);
|
||||
}
|
||||
else
|
||||
Serial.println("initPersistentValues: restored values from EEPROM");
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("\nStepper Motor Drive Test");
|
||||
uint8_t maxWaitLoops = 255;
|
||||
while(!Serial && maxWaitLoops--)
|
||||
delay(20);
|
||||
|
||||
Serial.println(); Serial.print(PROGRAM_NAME); Serial.print(" Version: "); Serial.println(PROGRAM_VERSION);
|
||||
|
||||
initPersistentValues();
|
||||
|
||||
Wire.begin();
|
||||
Wire.setClock(400000L);
|
||||
|
||||
oled.setFont(cp437font8x8);
|
||||
|
||||
oled.begin(&Adafruit128x64, OLED_DISPLAY_I2C_ADDRESS);
|
||||
oled.clear();
|
||||
oled.println(PROGRAM_NAME);
|
||||
oled.println();
|
||||
oled.print("Ver: "); oled.println(PROGRAM_VERSION);
|
||||
oled.println();
|
||||
oled.print("Max Levels: "); oled.println(NUM_LIFT_LEVELS);
|
||||
oled.println();
|
||||
oled.print("Used Levels: "); oled.println(persistentValues.numLiftLevels);
|
||||
delay(2000);
|
||||
oled.clear();
|
||||
displayLevel(persistentValues.lastLiftLevel + 1);
|
||||
displayPosition(persistentValues.lastStepperPosition);
|
||||
|
||||
stepper.setCurrentPosition(persistentValues.lastStepperPosition);
|
||||
|
||||
stepper.setEnablePin(STEPPER_ENABLE_PIN);
|
||||
#ifdef REVERSE_STEPPER_DIRECTION
|
||||
stepper.setPinsInverted(true, false, true);
|
||||
#else
|
||||
stepper.setPinsInverted(false, false, true);
|
||||
#endif
|
||||
|
||||
stepper.setMaxSpeed(STEPPER_MAX_SPEED);
|
||||
|
||||
btnOk.setHoldTimeout(2000);
|
||||
btnStopHome.setHoldTimeout(BUTTON_LONG_PRESS_DELAY);
|
||||
btnManual.setHoldTimeout(BUTTON_LONG_PRESS_DELAY);
|
||||
}
|
||||
|
||||
void moveStepper(long relative)
|
||||
void stepperMoveTo(long newPosition)
|
||||
{
|
||||
stepper.enableOutputs();
|
||||
stepper.setAcceleration(STEPPER_NORMAL_ACCELERATION);
|
||||
stepper.move(relative);
|
||||
stepper.moveTo(newPosition);
|
||||
}
|
||||
|
||||
void stepperMove(long newRelPosition)
|
||||
{
|
||||
stepper.enableOutputs();
|
||||
stepper.setAcceleration(STEPPER_NORMAL_ACCELERATION);
|
||||
stepper.move(newRelPosition);
|
||||
}
|
||||
void stopStepper(void)
|
||||
{
|
||||
stepper.setAcceleration(STEPPER_EMERGENCY_STOP_ACCELERATION);
|
||||
@@ -81,6 +230,7 @@ int lastSpeed = 0;
|
||||
int newSpeed = 0;
|
||||
bool wasRunning = false;
|
||||
bool configMode = false;
|
||||
bool homing = false;
|
||||
elapsedMillis lastSpeedChange = 0;
|
||||
|
||||
void loop()
|
||||
@@ -89,7 +239,7 @@ void loop()
|
||||
homeSensor.tick();
|
||||
if(homeSensor.state())
|
||||
{
|
||||
if(stepper.isRunning() && (lastSpeed <= 0))
|
||||
if((configMode || homing) && stepper.isRunning() && (lastSpeed <= 0))
|
||||
{
|
||||
stopStepper();
|
||||
|
||||
@@ -100,14 +250,27 @@ void loop()
|
||||
|
||||
newSpeed = 0;
|
||||
lastSpeed = newSpeed;
|
||||
stepper.setCurrentPosition(0);
|
||||
|
||||
persistentValues.lastLiftLevel = 0;
|
||||
persistentValues.lastStepperPosition = 0;
|
||||
stepper.setCurrentPosition(persistentValues.lastStepperPosition);
|
||||
|
||||
EEPROM.put(EEPROM_BASE_ADDR, persistentValues);
|
||||
|
||||
if(homing)
|
||||
{
|
||||
long newPos = persistentValues.levelPositions[persistentValues.lastLiftLevel];
|
||||
stepperMoveTo(newPos);
|
||||
Serial.print("Home Sensor Hit: Move To: "); Serial.print(persistentValues.lastLiftLevel); Serial.print(" Pos: "); Serial.println(newPos);
|
||||
homing = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure we haven't gone beyond the end point of the traverser.
|
||||
if(stepper.currentPosition() >= STEPPER_MAX_POSITION)
|
||||
{
|
||||
if(stepper.isRunning() && (lastSpeed >= 0))
|
||||
if(configMode && stepper.isRunning() && (lastSpeed >= 0))
|
||||
{
|
||||
stopStepper();
|
||||
|
||||
@@ -118,13 +281,16 @@ void loop()
|
||||
|
||||
newSpeed = 0;
|
||||
lastSpeed = newSpeed;
|
||||
|
||||
displayMessage("At Max");
|
||||
}
|
||||
}
|
||||
|
||||
btnHome.tick();
|
||||
if(btnHome.click())
|
||||
btnStopHome.tick();
|
||||
if(btnStopHome.press())
|
||||
{
|
||||
Serial.print("Home Click - Current Pos: "); Serial.println(stepper.currentPosition());
|
||||
Serial.print("StopHome Click - Current Pos: "); Serial.println(stepper.currentPosition());
|
||||
displayMessage("Stop");
|
||||
if(stepper.isRunning())
|
||||
{
|
||||
newSpeed = 0;
|
||||
@@ -132,39 +298,80 @@ void loop()
|
||||
}
|
||||
}
|
||||
|
||||
if(btnHome.held())
|
||||
if(btnStopHome.held())
|
||||
{
|
||||
Serial.println("Home Held: Moving to Home Position");
|
||||
Serial.println("StopHome Held: Moving to Home Position");
|
||||
displayMessage("Homing");
|
||||
homing = true;
|
||||
newSpeed = -STEPPER_MAX_SPEED;
|
||||
}
|
||||
|
||||
btnOk.tick();
|
||||
if(btnOk.click())
|
||||
btnManual.tick();
|
||||
if(btnManual.press())
|
||||
{
|
||||
Serial.print("Ok Click - Current Pos: "); Serial.println(stepper.currentPosition());
|
||||
Serial.print("Manual Press - Current Pos: "); Serial.println(stepper.currentPosition());
|
||||
if(configMode)
|
||||
{
|
||||
configMode = false;
|
||||
Serial.println("Home Click - Exit Manual Mode");
|
||||
}
|
||||
}
|
||||
|
||||
if(btnOk.held())
|
||||
if(btnManual.held())
|
||||
{
|
||||
Serial.print("Ok Held - Current Pos: "); Serial.println(stepper.currentPosition());
|
||||
Serial.print("Manual Held - Enter Manual Mode Pos: "); Serial.println(stepper.currentPosition());
|
||||
configMode = true;
|
||||
}
|
||||
|
||||
btnDown.tick();
|
||||
if(btnDown.click() && (stepper.currentPosition() < STEPPER_MAX_POSITION) && (lastSpeed <= (STEPPER_MAX_SPEED - STEPPER_INC_SPEED)))
|
||||
if(configMode)
|
||||
{
|
||||
newSpeed = lastSpeed + STEPPER_INC_SPEED;
|
||||
lastSpeedChange = STEPPER_MAN_SPEED_CHANGE_MILLIS;
|
||||
Serial.print("Down Click - Current Pos: "); Serial.print(stepper.currentPosition()); Serial.print(" New Speed: "); Serial.println(newSpeed);
|
||||
if((btnDown.press() || btnDown.step()) && (stepper.currentPosition() < STEPPER_MAX_POSITION) && (lastSpeed <= (STEPPER_MAX_SPEED - STEPPER_INC_SPEED)))
|
||||
{
|
||||
newSpeed = lastSpeed + STEPPER_INC_SPEED;
|
||||
lastSpeedChange = STEPPER_MAN_SPEED_CHANGE_MILLIS;
|
||||
Serial.print("Down Press - Current Pos: "); Serial.print(stepper.currentPosition()); Serial.print(" New Speed: "); Serial.println(newSpeed);
|
||||
|
||||
displayMessage("Down");
|
||||
}
|
||||
}
|
||||
|
||||
else if((btnDown.press() || btnDown.step()) && persistentValues.lastLiftLevel > 0)
|
||||
{
|
||||
Serial.print("Down Press - Current Level: "); Serial.print(persistentValues.lastLiftLevel);
|
||||
persistentValues.lastLiftLevel--;
|
||||
long newPos = persistentValues.levelPositions[persistentValues.lastLiftLevel];
|
||||
stepperMoveTo(newPos);
|
||||
Serial.print(" Move To: "); Serial.print(persistentValues.lastLiftLevel); Serial.print(" Pos: "); Serial.println(newPos);
|
||||
|
||||
displayMessageNumber("Dn To: ", persistentValues.lastLiftLevel + 1);
|
||||
}
|
||||
|
||||
btnUp.tick();
|
||||
if(btnUp.click() && (homeSensor.state() == 0) && (lastSpeed >= -(STEPPER_MAX_SPEED - STEPPER_INC_SPEED)))
|
||||
if(configMode)
|
||||
{
|
||||
newSpeed = lastSpeed - STEPPER_INC_SPEED;
|
||||
lastSpeedChange = STEPPER_MAN_SPEED_CHANGE_MILLIS;
|
||||
Serial.print("Up Click - Current Pos: "); Serial.print(stepper.currentPosition()); Serial.print(" New Speed: "); Serial.println(newSpeed);
|
||||
if((btnUp.press() || btnDown.step()) && (homeSensor.state() == 0) && (lastSpeed >= -(STEPPER_MAX_SPEED - STEPPER_INC_SPEED)))
|
||||
{
|
||||
newSpeed = lastSpeed - STEPPER_INC_SPEED;
|
||||
lastSpeedChange = STEPPER_MAN_SPEED_CHANGE_MILLIS;
|
||||
Serial.print("Up Press - Current Pos: "); Serial.print(stepper.currentPosition()); Serial.print(" New Speed: "); Serial.println(newSpeed);
|
||||
|
||||
displayMessage("Up");
|
||||
}
|
||||
}
|
||||
|
||||
else if((btnUp.press() || btnDown.step()) && (persistentValues.lastLiftLevel < (persistentValues.numLiftLevels - 1)))
|
||||
{
|
||||
Serial.print("Up Press - Current Level: "); Serial.print(persistentValues.lastLiftLevel);
|
||||
persistentValues.lastLiftLevel++;
|
||||
long newPos = persistentValues.levelPositions[persistentValues.lastLiftLevel];
|
||||
stepperMoveTo(newPos);
|
||||
Serial.print(" Move To: "); Serial.print(persistentValues.lastLiftLevel); Serial.print(" Pos: "); Serial.println(newPos);
|
||||
|
||||
displayMessageNumber("Up To: ", persistentValues.lastLiftLevel + 1);
|
||||
}
|
||||
|
||||
|
||||
if(lastSpeed != newSpeed)
|
||||
{
|
||||
// Serial.print("Speed Change: Last: "); Serial.print(lastSpeed); Serial.print(" New: "); Serial.print(newSpeed);
|
||||
@@ -192,7 +399,7 @@ void loop()
|
||||
// Serial.print(" Set New Speed: "); Serial.println(newSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(lastSpeed)
|
||||
stepper.runSpeed();
|
||||
|
||||
@@ -203,6 +410,12 @@ void loop()
|
||||
{
|
||||
Serial.println("Disable Outputs");
|
||||
stepper.disableOutputs();
|
||||
|
||||
displayLevel(persistentValues.lastLiftLevel + 1);
|
||||
displayMessage("");
|
||||
persistentValues.lastStepperPosition = stepper.currentPosition();
|
||||
displayPosition(persistentValues.lastStepperPosition);
|
||||
EEPROM.put(EEPROM_BASE_ADDR, persistentValues);
|
||||
}
|
||||
wasRunning = stepper.isRunning();
|
||||
}
|
||||
|
Reference in New Issue
Block a user