refactor: wire gauge_config.h into sketch, remove GaugePins and hardcoded defaults
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "gauge_config.h"
|
||||||
|
|
||||||
static const uint8_t GAUGE_COUNT = 4;
|
|
||||||
static const uint16_t STEPPER_TIMER_HZ = 20000;
|
static const uint16_t STEPPER_TIMER_HZ = 20000;
|
||||||
static const int32_t STEP_RATE_SCALE = 65536L;
|
static const int32_t STEP_RATE_SCALE = 65536L;
|
||||||
static const float MAX_TIMER_STEP_RATE = (float)STEPPER_TIMER_HZ / 2.0f;
|
static const float MAX_TIMER_STEP_RATE = (float)STEPPER_TIMER_HZ / 2.0f;
|
||||||
@@ -12,23 +12,6 @@ static const float MAX_TIMER_STEP_RATE = (float)STEPPER_TIMER_HZ / 2.0f;
|
|||||||
#define DEBUG_PORT Serial
|
#define DEBUG_PORT Serial
|
||||||
static const unsigned long SERIAL_BAUD = 38400;
|
static const unsigned long SERIAL_BAUD = 38400;
|
||||||
|
|
||||||
struct GaugePins {
|
|
||||||
uint8_t dirPin;
|
|
||||||
uint8_t stepPin;
|
|
||||||
int8_t enablePin; // -1 means there is no enable pin
|
|
||||||
bool dirInverted;
|
|
||||||
bool stepActiveHigh;
|
|
||||||
bool enableActiveLow;
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr GaugePins gaugePins[GAUGE_COUNT] = {
|
|
||||||
// dir, step, en, dirInv, stepHigh, enActiveLow
|
|
||||||
{48, 49, -1, false, true, true}, // Gauge 0
|
|
||||||
{8, 9, -1, true, true, true}, // Gauge 1
|
|
||||||
{52, 53, -1, false, true, true}, // Gauge 2
|
|
||||||
{50, 51, -1, false, true, true}, // Gauge 3
|
|
||||||
};
|
|
||||||
|
|
||||||
enum HomingState : uint8_t {
|
enum HomingState : uint8_t {
|
||||||
HS_IDLE,
|
HS_IDLE,
|
||||||
HS_START,
|
HS_START,
|
||||||
@@ -42,13 +25,13 @@ struct Gauge {
|
|||||||
volatile long targetPos = 0;
|
volatile long targetPos = 0;
|
||||||
|
|
||||||
long minPos = 0;
|
long minPos = 0;
|
||||||
long maxPos = 3780;
|
long maxPos = 0;
|
||||||
long homingBackoffSteps = 3800; // Deliberately a touch past full reverse travel.
|
long homingBackoffSteps = 0;
|
||||||
|
|
||||||
float velocity = 0.0f;
|
float velocity = 0.0f;
|
||||||
float maxSpeed = 4000.0f;
|
float maxSpeed = 0.0f;
|
||||||
float accel = 6000.0f;
|
float accel = 0.0f;
|
||||||
float homingSpeed = 500.0f;
|
float homingSpeed = 0.0f;
|
||||||
|
|
||||||
unsigned long lastUpdateMicros = 0;
|
unsigned long lastUpdateMicros = 0;
|
||||||
|
|
||||||
@@ -104,12 +87,12 @@ inline void writePortBit(volatile uint8_t* port, uint8_t mask, bool high) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline void writeDirectionPin(uint8_t id, bool forward) {
|
inline void writeDirectionPin(uint8_t id, bool forward) {
|
||||||
bool level = gaugePins[id].dirInverted ? !forward : forward;
|
bool level = gaugeConfigs[id].dirInverted ? !forward : forward;
|
||||||
writePortBit(stepperHardware[id].dirPort, stepperHardware[id].dirMask, level);
|
writePortBit(stepperHardware[id].dirPort, stepperHardware[id].dirMask, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void writeStepPin(uint8_t id, bool active) {
|
inline void writeStepPin(uint8_t id, bool active) {
|
||||||
bool level = gaugePins[id].stepActiveHigh ? active : !active;
|
bool level = gaugeConfigs[id].stepActiveHigh ? active : !active;
|
||||||
writePortBit(stepperHardware[id].stepPort, stepperHardware[id].stepMask, level);
|
writePortBit(stepperHardware[id].stepPort, stepperHardware[id].stepMask, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,10 +132,10 @@ void stopTimerStepping(uint8_t id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void configureStepperHardware(uint8_t id) {
|
void configureStepperHardware(uint8_t id) {
|
||||||
stepperHardware[id].stepPort = portOutputRegister(digitalPinToPort(gaugePins[id].stepPin));
|
stepperHardware[id].stepPort = portOutputRegister(digitalPinToPort(gaugeConfigs[id].stepPin));
|
||||||
stepperHardware[id].stepMask = digitalPinToBitMask(gaugePins[id].stepPin);
|
stepperHardware[id].stepMask = digitalPinToBitMask(gaugeConfigs[id].stepPin);
|
||||||
stepperHardware[id].dirPort = portOutputRegister(digitalPinToPort(gaugePins[id].dirPin));
|
stepperHardware[id].dirPort = portOutputRegister(digitalPinToPort(gaugeConfigs[id].dirPin));
|
||||||
stepperHardware[id].dirMask = digitalPinToBitMask(gaugePins[id].dirPin);
|
stepperHardware[id].dirMask = digitalPinToBitMask(gaugeConfigs[id].dirPin);
|
||||||
}
|
}
|
||||||
|
|
||||||
void beginStepperTimer() {
|
void beginStepperTimer() {
|
||||||
@@ -288,10 +271,10 @@ void setEnable(uint8_t id, bool en) {
|
|||||||
if (id >= GAUGE_COUNT) return;
|
if (id >= GAUGE_COUNT) return;
|
||||||
gauges[id].enabled = en;
|
gauges[id].enabled = en;
|
||||||
|
|
||||||
int8_t pin = gaugePins[id].enablePin;
|
int8_t pin = gaugeConfigs[id].enablePin;
|
||||||
if (pin < 0) return;
|
if (pin < 0) return;
|
||||||
|
|
||||||
bool level = gaugePins[id].enableActiveLow ? !en : en;
|
bool level = gaugeConfigs[id].enableActiveLow ? !en : en;
|
||||||
digitalWrite(pin, level ? HIGH : LOW);
|
digitalWrite(pin, level ? HIGH : LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -718,18 +701,25 @@ void setup() {
|
|||||||
DEBUG_PORT.println("Gauge controller booting");
|
DEBUG_PORT.println("Gauge controller booting");
|
||||||
|
|
||||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||||
pinMode(gaugePins[i].dirPin, OUTPUT);
|
pinMode(gaugeConfigs[i].dirPin, OUTPUT);
|
||||||
pinMode(gaugePins[i].stepPin, OUTPUT);
|
pinMode(gaugeConfigs[i].stepPin, OUTPUT);
|
||||||
configureStepperHardware(i);
|
configureStepperHardware(i);
|
||||||
|
|
||||||
digitalWrite(gaugePins[i].dirPin, LOW);
|
digitalWrite(gaugeConfigs[i].dirPin, LOW);
|
||||||
digitalWrite(gaugePins[i].stepPin, gaugePins[i].stepActiveHigh ? LOW : HIGH);
|
digitalWrite(gaugeConfigs[i].stepPin, gaugeConfigs[i].stepActiveHigh ? LOW : HIGH);
|
||||||
|
|
||||||
if (gaugePins[i].enablePin >= 0) {
|
if (gaugeConfigs[i].enablePin >= 0) {
|
||||||
pinMode(gaugePins[i].enablePin, OUTPUT);
|
pinMode(gaugeConfigs[i].enablePin, OUTPUT);
|
||||||
setEnable(i, true);
|
setEnable(i, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gauges[i].minPos = gaugeConfigs[i].minPos;
|
||||||
|
gauges[i].maxPos = gaugeConfigs[i].maxPos;
|
||||||
|
gauges[i].homingBackoffSteps = gaugeConfigs[i].homingBackoffSteps;
|
||||||
|
gauges[i].maxSpeed = (float)gaugeConfigs[i].maxSpeed;
|
||||||
|
gauges[i].accel = (float)gaugeConfigs[i].accel;
|
||||||
|
gauges[i].homingSpeed = (float)gaugeConfigs[i].homingSpeed;
|
||||||
|
|
||||||
gauges[i].lastUpdateMicros = micros();
|
gauges[i].lastUpdateMicros = micros();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user