refactoring codebase
This commit is contained in:
134
arduino_code/Test/src/motor.cpp
Normal file
134
arduino_code/Test/src/motor.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include "motor.h"
|
||||
#include "config.h"
|
||||
#include <driver/timer.h>
|
||||
|
||||
volatile bool motor_enabled_logic = false;
|
||||
volatile unsigned long current_step_interval_us = 100000;
|
||||
volatile unsigned long total_steps = 0;
|
||||
|
||||
static int target_rpm = 0;
|
||||
static int current_rpm_display = 0;
|
||||
|
||||
static hw_timer_t *stepTimer = NULL;
|
||||
static bool is_ramping = false;
|
||||
static unsigned long ramp_start_ms = 0;
|
||||
static float start_speed_sps = 0;
|
||||
static float end_speed_sps = 0;
|
||||
|
||||
// --- ISR ---
|
||||
void IRAM_ATTR onStepTimer() {
|
||||
if (digitalRead(EN_PIN) == LOW && motor_enabled_logic) {
|
||||
GPIO.out_w1ts = (1 << STEP_PIN);
|
||||
ets_delay_us(1);
|
||||
GPIO.out_w1tc = (1 << STEP_PIN);
|
||||
total_steps++;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long getStepIntervalUs() {
|
||||
return current_step_interval_us;
|
||||
}
|
||||
|
||||
// --- Private Helpers ---
|
||||
static void updateRamp() {
|
||||
unsigned long now = millis();
|
||||
unsigned long elapsed = now - ramp_start_ms;
|
||||
|
||||
if (elapsed >= RAMP_DURATION_MS) {
|
||||
is_ramping = false;
|
||||
current_step_interval_us = (end_speed_sps > 0) ? (1000000UL / (unsigned long)end_speed_sps) : 100000;
|
||||
|
||||
if (end_speed_sps <= 0) {
|
||||
motor_enabled_logic = false;
|
||||
} else {
|
||||
motor_enabled_logic = true;
|
||||
}
|
||||
|
||||
timerAlarmWrite(stepTimer, current_step_interval_us, true);
|
||||
return;
|
||||
}
|
||||
|
||||
float progress = (float)elapsed / RAMP_DURATION_MS;
|
||||
float current_sps = start_speed_sps + (end_speed_sps - start_speed_sps) * progress;
|
||||
if (current_sps < 1) current_sps = 1;
|
||||
|
||||
unsigned long new_interval = 1000000UL / (unsigned long)current_sps;
|
||||
if (new_interval < 10) new_interval = 10;
|
||||
|
||||
current_step_interval_us = new_interval;
|
||||
timerAlarmWrite(stepTimer, new_interval, true);
|
||||
|
||||
unsigned long total_steps_per_rev = (unsigned long)STEPS_PER_REVOLUTION * MICROSTEP_FACTOR;
|
||||
current_rpm_display = ((unsigned long)current_sps * 60) / total_steps_per_rev;
|
||||
}
|
||||
|
||||
// --- Public API ---
|
||||
void motorInit() {
|
||||
pinMode(DIR_PIN, OUTPUT);
|
||||
pinMode(STEP_PIN, OUTPUT);
|
||||
pinMode(EN_PIN, OUTPUT);
|
||||
|
||||
digitalWrite(EN_PIN, HIGH);
|
||||
digitalWrite(DIR_PIN, HIGH);
|
||||
digitalWrite(STEP_PIN, LOW);
|
||||
|
||||
stepTimer = timerBegin(0, 80, true);
|
||||
timerAttachInterrupt(stepTimer, &onStepTimer, true);
|
||||
timerAlarmWrite(stepTimer, 100000, true);
|
||||
timerAlarmEnable(stepTimer);
|
||||
}
|
||||
|
||||
void motorLoop() {
|
||||
if (is_ramping) {
|
||||
updateRamp();
|
||||
}
|
||||
}
|
||||
|
||||
void resetSteps() {
|
||||
taskDISABLE_INTERRUPTS();
|
||||
total_steps = 0;
|
||||
taskENABLE_INTERRUPTS();
|
||||
}
|
||||
|
||||
unsigned long getMotorSteps() {
|
||||
unsigned long steps;
|
||||
taskDISABLE_INTERRUPTS();
|
||||
steps = total_steps;
|
||||
taskENABLE_INTERRUPTS();
|
||||
return steps;
|
||||
}
|
||||
|
||||
int getCurrentRPM() {
|
||||
return current_rpm_display;
|
||||
}
|
||||
|
||||
bool isMotorRunning() {
|
||||
return (motor_enabled_logic && current_step_interval_us < 100000);
|
||||
}
|
||||
|
||||
void setTargetRPM(int rpm) {
|
||||
target_rpm = rpm;
|
||||
|
||||
// Сброс шагов при старте с нуля
|
||||
if (rpm > 0 && current_rpm_display < 5) {
|
||||
resetSteps();
|
||||
}
|
||||
|
||||
unsigned long total_steps_per_rev = (unsigned long)STEPS_PER_REVOLUTION * MICROSTEP_FACTOR;
|
||||
|
||||
if (current_step_interval_us > 0 && current_step_interval_us < 100000) {
|
||||
start_speed_sps = 1000000.0f / current_step_interval_us;
|
||||
} else {
|
||||
start_speed_sps = 0;
|
||||
}
|
||||
|
||||
if (rpm > 0) {
|
||||
end_speed_sps = ((float)rpm * total_steps_per_rev) / 60.0f;
|
||||
} else {
|
||||
end_speed_sps = 0;
|
||||
}
|
||||
|
||||
ramp_start_ms = millis();
|
||||
is_ramping = true;
|
||||
motor_enabled_logic = true;
|
||||
}
|
||||
Reference in New Issue
Block a user