clean up stepper

main
radex 2024-02-20 15:48:57 +01:00
parent a6d9ad9817
commit 7955846d86
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
4 changed files with 44 additions and 20 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -1,29 +1,43 @@
#include <Arduino.h>
#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;
}

View File

@ -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();