Progress on more simplicity
This commit is contained in:
parent
a9d468d87c
commit
76e516fa5d
|
@ -2,15 +2,78 @@
|
|||
#ifndef ESP32
|
||||
#include <Arduino_FreeRTOS.h>
|
||||
#endif
|
||||
#include "MotorControlPart.h"
|
||||
#include "StepperController.h"
|
||||
#include "ZCodeParser.h"
|
||||
#include "Shared/MotorMoveParams.h"
|
||||
#include "Shared/Steppers.h"
|
||||
|
||||
// [X, Y, Head]
|
||||
#define NUM_STEPPERS 3
|
||||
Stepper steppers[NUM_STEPPERS] = {
|
||||
{38, 55, 54, 0, false, false, 'x'},
|
||||
{56, 61, 60, 0, false, false, 'y'},
|
||||
{30, 34, 36, 0, false, false, 'e'},
|
||||
};
|
||||
|
||||
#pragma region Not gonna mess with this magic
|
||||
|
||||
#ifdef ESP32
|
||||
// Timer Handles
|
||||
hw_timer_t *timer1 = NULL;
|
||||
hw_timer_t *timer2 = NULL;
|
||||
hw_timer_t *timer3 = NULL;
|
||||
|
||||
// Interrupt Service Routines for each timer
|
||||
void IRAM_ATTR timer1ISR()
|
||||
{
|
||||
if (motorsEnabled[0])
|
||||
{
|
||||
digitalWrite(STEP_PINS[0], !digitalRead(STEP_PINS[0]));
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR timer2ISR()
|
||||
{
|
||||
if (motorsEnabled[1])
|
||||
{
|
||||
digitalWrite(STEP_PINS[1], !digitalRead(STEP_PINS[1]));
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR timer3ISR()
|
||||
{
|
||||
if (motorsEnabled[2])
|
||||
{
|
||||
digitalWrite(STEP_PINS[2], !digitalRead(STEP_PINS[2]));
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
// Timers for stepping the motors
|
||||
ISR(TIMER3_COMPA_vect) {
|
||||
if (steppers[0].steppingEnabled) {
|
||||
digitalWrite(steppers[0].stepPin, !digitalRead(steppers[0].stepPin));
|
||||
}
|
||||
}
|
||||
ISR(TIMER4_COMPA_vect) {
|
||||
if (steppers[1].steppingEnabled) {
|
||||
digitalWrite(steppers[1].stepPin, !digitalRead(steppers[1].stepPin));
|
||||
}
|
||||
}
|
||||
ISR(TIMER5_COMPA_vect) {
|
||||
if (steppers[2].steppingEnabled) {
|
||||
digitalWrite(steppers[2].stepPin, !digitalRead(steppers[2].stepPin));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#pragma endregion
|
||||
|
||||
void anotherFunc(void * params){
|
||||
MotorMoveParams *pam = (MotorMoveParams *)malloc(sizeof(MotorMoveParams));
|
||||
pam->motorChar = 'x';
|
||||
pam->moveTimeMs = 3000;
|
||||
xTaskCreate(MoveMotor, "Motor", 1024, (void *)pam, 10, NULL);
|
||||
// xTaskCreate(MoveMotor, "Motor", 1024, (void *)pam, 10, NULL);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(50)); // Allow time for other task to accept params
|
||||
free(pam);
|
||||
|
@ -20,7 +83,7 @@ void anotherFunc(void * params){
|
|||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
setupMotor();
|
||||
// setupMotor();
|
||||
|
||||
// Create tasks
|
||||
xTaskCreate(anotherFunc, "Other loop", 1024, NULL, 5, NULL);
|
||||
|
|
|
@ -1,182 +0,0 @@
|
|||
#include <Arduino.h>
|
||||
#ifndef ESP32
|
||||
#include <Arduino_FreeRTOS.h>
|
||||
#endif
|
||||
#include <Shared/ZCommand.h>
|
||||
#include <Shared/MotorMoveParams.h>
|
||||
#include <math.h>
|
||||
|
||||
// Pin used for the joystick
|
||||
#define JOYSTICK_PIN 63
|
||||
|
||||
struct CharMapping {
|
||||
char character;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct Stepper {
|
||||
uint8_t enPin;
|
||||
uint8_t dirPin;
|
||||
uint8_t stepPin;
|
||||
bool timerEnabled;
|
||||
bool motorEnabled;
|
||||
char axis;
|
||||
};
|
||||
|
||||
|
||||
// [X, Y, Head]
|
||||
#define NUM_STEPPERS 3
|
||||
Stepper steppers[NUM_STEPPERS] = {
|
||||
{38, 55, 54, false, false, 'x'},
|
||||
{56, 61, 60, false, false, 'y'},
|
||||
{30, 34, 36, false, false, 'e'},
|
||||
};
|
||||
|
||||
// Critical range for detecting joystick movement
|
||||
static const int JOY_CRITICAL_POINT = 25;
|
||||
|
||||
bool leftMovement = false;
|
||||
bool rightMovement = false;
|
||||
|
||||
#ifdef ESP32
|
||||
// Timer Handles
|
||||
hw_timer_t *timer1 = NULL;
|
||||
hw_timer_t *timer2 = NULL;
|
||||
hw_timer_t *timer3 = NULL;
|
||||
|
||||
// Interrupt Service Routines for each timer
|
||||
void IRAM_ATTR timer1ISR()
|
||||
{
|
||||
if (motorsEnabled[0])
|
||||
{
|
||||
digitalWrite(STEP_PINS[0], !digitalRead(STEP_PINS[0]));
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR timer2ISR()
|
||||
{
|
||||
if (motorsEnabled[1])
|
||||
{
|
||||
digitalWrite(STEP_PINS[1], !digitalRead(STEP_PINS[1]));
|
||||
}
|
||||
}
|
||||
|
||||
void IRAM_ATTR timer3ISR()
|
||||
{
|
||||
if (motorsEnabled[2])
|
||||
{
|
||||
digitalWrite(STEP_PINS[2], !digitalRead(STEP_PINS[2]));
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
// Timers for stepping the motors
|
||||
ISR(TIMER3_COMPA_vect) {
|
||||
if (steppers[0].timerEnabled) {
|
||||
digitalWrite(steppers[0].stepPin, !digitalRead(steppers[0].stepPin));
|
||||
}
|
||||
}
|
||||
ISR(TIMER4_COMPA_vect) {
|
||||
if (steppers[1].timerEnabled) {
|
||||
digitalWrite(steppers[1].stepPin, !digitalRead(steppers[1].stepPin));
|
||||
}
|
||||
}
|
||||
ISR(TIMER5_COMPA_vect) {
|
||||
if (steppers[2].timerEnabled) {
|
||||
digitalWrite(steppers[2].stepPin, !digitalRead(steppers[2].stepPin));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void ExecuteCommand(void *params){
|
||||
ZCommand receivedValue = *((ZCommand *)params);
|
||||
}
|
||||
|
||||
int getStepperIndex(const char axis){
|
||||
for (size_t i = 0; i < NUM_STEPPERS; i++)
|
||||
{
|
||||
if (steppers[i].axis == axis) {
|
||||
Serial.print("Found motor: ");
|
||||
Serial.println(i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void MoveMotor(void *params) {
|
||||
MotorMoveParams *motorParams = ((MotorMoveParams *) params);
|
||||
char motorChar = motorParams->motorChar;
|
||||
float moveTimeMs = motorParams->moveTimeMs;
|
||||
int isBackwards = signbit(moveTimeMs); // Negative means move backwards, positive means move forward.
|
||||
|
||||
int index = getStepperIndex(motorChar);
|
||||
Stepper *currentStepper = &steppers[index];
|
||||
|
||||
// Enable motor and set direction
|
||||
Serial.println("Start motor.");
|
||||
currentStepper->motorEnabled = true;
|
||||
currentStepper->timerEnabled = true;
|
||||
digitalWrite(currentStepper->enPin, !(currentStepper->motorEnabled)); // Inverted for some reason
|
||||
digitalWrite(currentStepper->dirPin, isBackwards);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(moveTimeMs));
|
||||
|
||||
// Stop motor
|
||||
Serial.println("Stop motor.");
|
||||
currentStepper->motorEnabled = false;
|
||||
currentStepper->timerEnabled = false;
|
||||
digitalWrite(currentStepper->enPin, !(currentStepper->motorEnabled)); // Inverted for some reason
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
|
||||
// Configures Timer1 for compare match interrupts
|
||||
void setupMotor() {
|
||||
for (size_t i = 0; i < NUM_STEPPERS; i++)
|
||||
{
|
||||
pinMode(steppers[i].enPin, OUTPUT);
|
||||
pinMode(steppers[i].dirPin, OUTPUT);
|
||||
pinMode(steppers[i].stepPin, OUTPUT);
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
// Timer 1 - Equivalent to TIMER1_COMPA_vect
|
||||
timer1 = timerBegin(0, 80, true); // Timer 0, prescaler 80 (1us per tick)
|
||||
timerAttachInterrupt(timer1, &timer1ISR, true);
|
||||
timerAlarmWrite(timer1, 500, true); // 500us interval
|
||||
timerAlarmEnable(timer1);
|
||||
|
||||
// Timer 2 - Equivalent to TIMER3_COMPA_vect
|
||||
timer2 = timerBegin(1, 80, true); // Timer 1, prescaler 80 (1us per tick)
|
||||
timerAttachInterrupt(timer2, &timer2ISR, true);
|
||||
timerAlarmWrite(timer2, 500, true); // 500us interval
|
||||
timerAlarmEnable(timer2);
|
||||
|
||||
// Timer 3 - Equivalent to TIMER4_COMPA_vect
|
||||
timer3 = timerBegin(2, 80, true); // Timer 2, prescaler 80 (1us per tick)
|
||||
timerAttachInterrupt(timer3, &timer3ISR, true);
|
||||
timerAlarmWrite(timer3, 500, true); // 500us interval
|
||||
timerAlarmEnable(timer3);
|
||||
#else
|
||||
cli();
|
||||
// Timer 3 (16-bit)
|
||||
TCCR3A = 0;
|
||||
TCCR3B = (1 << WGM32) | (1 << CS30); // CTC mode, no prescaler
|
||||
OCR3A = 499; // Compare match value
|
||||
TIMSK3 |= (1 << OCIE3A); // Enable Timer1 Compare A Match Interrupt
|
||||
|
||||
// Timer 4 (16-bit)
|
||||
TCCR4A = 0;
|
||||
TCCR4B = (1 << WGM42) | (1 << CS40); // CTC mode, no prescaler
|
||||
OCR4A = 499; // Compare match value
|
||||
TIMSK4 |= (1 << OCIE4A); // Enable Timer3 Compare A Match Interrupt
|
||||
|
||||
// Timer 5 (16-bit)
|
||||
TCCR5A = 0;
|
||||
TCCR5B = (1 << WGM42) | (1 << CS50); // CTC mode, no prescaler
|
||||
OCR5A = 499; // Compare match value
|
||||
TIMSK5 |= (1 << OCIE5A); // Enable Timer1 Compare A Match Interrupt
|
||||
sei();
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
struct Stepper {
|
||||
uint8_t enPin;
|
||||
uint8_t dirPin;
|
||||
uint8_t stepPin;
|
||||
int moveQueue;
|
||||
bool steppingEnabled;
|
||||
bool motorEnabled;
|
||||
char axis;
|
||||
};
|
|
@ -0,0 +1,38 @@
|
|||
#include "StepperController.h"
|
||||
|
||||
int StepperController::getStepperIndex(const char axis){
|
||||
for (size_t i = 0; i < NumOfSteppers; i++)
|
||||
{
|
||||
if (Steppers[i].axis == axis) {
|
||||
// Serial.print("Found motor: ");
|
||||
// Serial.println(i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void StepperController::SetMovementEnabled(const char axis, bool enabled) {
|
||||
int index = getStepperIndex(axis);
|
||||
Steppers[index].steppingEnabled = enabled;
|
||||
}
|
||||
|
||||
void StepperController::SetMoveQueue(const char axis, int moveTimeMs) {
|
||||
int index = getStepperIndex(axis);
|
||||
Steppers[index].moveQueue = moveTimeMs;
|
||||
}
|
||||
|
||||
void StepperController::QueueMove(const char axis, int moveTimeMs) {
|
||||
int index = getStepperIndex(axis);
|
||||
Steppers[index].moveQueue += moveTimeMs; // Fixed incorrect `=+` operator
|
||||
}
|
||||
|
||||
void StepperController::ClearMoveQueue(const char axis) { // Fixed function name typo
|
||||
int index = getStepperIndex(axis);
|
||||
Steppers[index].moveQueue = 0;
|
||||
}
|
||||
|
||||
void StepperController::SetMotorEnabled(const char axis, bool enabled) {
|
||||
int index = getStepperIndex(axis);
|
||||
Steppers[index].motorEnabled = enabled;
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
#ifndef STEPPER_CONTROLLER_H
|
||||
#define STEPPER_CONTROLLER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifndef ESP32
|
||||
#include <Arduino_FreeRTOS.h>
|
||||
#endif
|
||||
#include <Shared/ZCommand.h>
|
||||
#include <Shared/MotorMoveParams.h>
|
||||
#include <Shared/Steppers.h>
|
||||
|
||||
class StepperController
|
||||
{
|
||||
private:
|
||||
int NumOfSteppers;
|
||||
Stepper *Steppers;
|
||||
|
||||
int getStepperIndex(const char axis);
|
||||
public:
|
||||
StepperController(Stepper *steppers, int numOfSteppers = 3);
|
||||
|
||||
void SetMovementEnabled(const char axis, bool enabled);
|
||||
|
||||
void SetMoveQueue(const char axis, const int moveTimeMs);
|
||||
void QueueMove(const char axis, const int moveTimeMs);
|
||||
void ClearMoveQueue(const char axis);
|
||||
|
||||
void SetMotorEnabled(const char axis, bool enabled);
|
||||
|
||||
~StepperController();
|
||||
};
|
||||
|
||||
StepperController::StepperController(Stepper steppers[], int numOfSteppers = 3)
|
||||
{
|
||||
Steppers = steppers;
|
||||
NumOfSteppers = numOfSteppers;
|
||||
|
||||
for (size_t i = 0; i < NumOfSteppers; i++)
|
||||
{
|
||||
pinMode(steppers[i].enPin, OUTPUT);
|
||||
pinMode(steppers[i].dirPin, OUTPUT);
|
||||
pinMode(steppers[i].stepPin, OUTPUT);
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
// Timer 1 - Equivalent to TIMER1_COMPA_vect
|
||||
timer1 = timerBegin(0, 80, true); // Timer 0, prescaler 80 (1us per tick)
|
||||
timerAttachInterrupt(timer1, &timer1ISR, true);
|
||||
timerAlarmWrite(timer1, 500, true); // 500us interval
|
||||
timerAlarmEnable(timer1);
|
||||
|
||||
// Timer 2 - Equivalent to TIMER3_COMPA_vect
|
||||
timer2 = timerBegin(1, 80, true); // Timer 1, prescaler 80 (1us per tick)
|
||||
timerAttachInterrupt(timer2, &timer2ISR, true);
|
||||
timerAlarmWrite(timer2, 500, true); // 500us interval
|
||||
timerAlarmEnable(timer2);
|
||||
|
||||
// Timer 3 - Equivalent to TIMER4_COMPA_vect
|
||||
timer3 = timerBegin(2, 80, true); // Timer 2, prescaler 80 (1us per tick)
|
||||
timerAttachInterrupt(timer3, &timer3ISR, true);
|
||||
timerAlarmWrite(timer3, 500, true); // 500us interval
|
||||
timerAlarmEnable(timer3);
|
||||
#else
|
||||
cli();
|
||||
// Timer 3 (16-bit)
|
||||
TCCR3A = 0;
|
||||
TCCR3B = (1 << WGM32) | (1 << CS30); // CTC mode, no prescaler
|
||||
OCR3A = 499; // Compare match value
|
||||
TIMSK3 |= (1 << OCIE3A); // Enable Timer1 Compare A Match Interrupt
|
||||
|
||||
// Timer 4 (16-bit)
|
||||
TCCR4A = 0;
|
||||
TCCR4B = (1 << WGM42) | (1 << CS40); // CTC mode, no prescaler
|
||||
OCR4A = 499; // Compare match value
|
||||
TIMSK4 |= (1 << OCIE4A); // Enable Timer3 Compare A Match Interrupt
|
||||
|
||||
// Timer 5 (16-bit)
|
||||
TCCR5A = 0;
|
||||
TCCR5B = (1 << WGM42) | (1 << CS50); // CTC mode, no prescaler
|
||||
OCR5A = 499; // Compare match value
|
||||
TIMSK5 |= (1 << OCIE5A); // Enable Timer1 Compare A Match Interrupt
|
||||
sei();
|
||||
#endif
|
||||
}
|
||||
|
||||
StepperController::~StepperController()
|
||||
{
|
||||
// free(Steppers); // Meh, let's go for a memory leak. ez
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue