From 7955846d868c446a256372013aa65b1257376b82 Mon Sep 17 00:00:00 2001 From: radex Date: Tue, 20 Feb 2024 15:48:57 +0100 Subject: [PATCH] clean up stepper --- firmware/src/constants.h | 5 +++++ firmware/src/main.cpp | 10 +++++----- firmware/src/stepper.cpp | 40 +++++++++++++++++++++++++++------------- firmware/src/stepper.h | 9 +++++++-- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/firmware/src/constants.h b/firmware/src/constants.h index c7bee69..fe3e5ec 100644 --- a/firmware/src/constants.h +++ b/firmware/src/constants.h @@ -32,4 +32,9 @@ #define STEPPER_ENABLE A0 #define STEPPER_STEP A1 +#define STEPPER_STEP_PORT PORTC +#define STEPPER_STEP_BIT 1 << 1 #define STEPPER_DIR A2 + +#define STEPPER_STEPS_PER_REV 200 +#define STEPPER_MICROSTEPS 2 diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index ba11ea0..dfbf3f9 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -15,7 +15,9 @@ void setup() { setupDebug(); setupEncoder(); setupTimer(); - setupStepper(); + if (!setupStepper()) { + fatal(); + } Wire.begin(); Wire.setClock(800000); @@ -30,7 +32,6 @@ void setup() { void loop() { debugScanI2C(); - demoTimer(); char debugKey = Serial.available() > 0 ? Serial.read() : 0; @@ -59,9 +60,9 @@ void loop() { } else if (key == KEY_C) { beep(); } else if (digit == 4) { - demo_moveBy(-80000); + demo_moveBy(-8000); } else if (digit == 5) { - demo_moveBy(80000); + demo_moveBy(8000); } else if (digit == 7) { auto micros1 = micros(); // 0.1ms @@ -93,7 +94,6 @@ void loop() { if (encoderEvent) { tick(); Serial.println(encoderEvent == ENCODER_CLOCKWISE ? "Clockwise" : "Counter-clockwise"); - // demo_moveBy(encoderEvent == ENCODER_CLOCKWISE ? 800 : -800); changeFreq(encoderEvent == ENCODER_CLOCKWISE ? 1 : -1); Serial.println(OCR1A); } diff --git a/firmware/src/stepper.cpp b/firmware/src/stepper.cpp index 19ff9e5..d65cbfd 100644 --- a/firmware/src/stepper.cpp +++ b/firmware/src/stepper.cpp @@ -1,29 +1,43 @@ #include #include "constants.h" +#include "debug.h" +#include "stepper.h" /* A4988 Stepper Motor Driver. - NOTE: minimum pulse width 1us high, 1us low - */ -void setupStepper() { +bool setupStepper() { pinMode(STEPPER_ENABLE, OUTPUT); pinMode(STEPPER_DIR, OUTPUT); pinMode(STEPPER_STEP, OUTPUT); - // low = enabled - digitalWrite(STEPPER_ENABLE, LOW); + setStepperEnabled(false); + + // sanity check + if (!((int) portOutputRegister(digitalPinToPort(STEPPER_STEP)) == (int) &STEPPER_STEP_PORT + && digitalPinToBitMask(STEPPER_STEP) == STEPPER_STEP_BIT + )) { + Serial.println("Stepper step pin is misconfigured"); + return false; + } + + return true; } -void demoStepper(bool dir) { - digitalWrite(STEPPER_DIR, dir); - for (int i = 0; i < 3200; i++) { - digitalWrite(STEPPER_STEP, HIGH); - delayMicroseconds(25); - digitalWrite(STEPPER_STEP, LOW); - delayMicroseconds(25); - } +void setStepperEnabled(bool enabled) { + digitalWrite(STEPPER_ENABLE, !enabled); +} + +void setStepperDirection(bool dir) { + digitalWrite(STEPPER_DIR, !dir); +} + +inline void step() { + // NOTE: minimum pulse width 1us high, 1us low + STEPPER_STEP_PORT |= STEPPER_STEP_BIT; + delayMicroseconds(1); + STEPPER_STEP_PORT &= ~STEPPER_STEP_BIT; } diff --git a/firmware/src/stepper.h b/firmware/src/stepper.h index ca3002e..17d3235 100644 --- a/firmware/src/stepper.h +++ b/firmware/src/stepper.h @@ -1,4 +1,9 @@ #pragma once -void setupStepper(); -void demoStepper(bool dir); +#define RIGHT 1 +#define LEFT 0 + +bool setupStepper(); +void setStepperEnabled(bool enabled); +void setStepperDirection(bool dir); +inline void step();