Skip to content
This repository was archived by the owner on Nov 3, 2024. It is now read-only.
4 changes: 2 additions & 2 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

// PID settings
// ! At this time, this feature is still under development
#define ENABLE_PID
//#define ENABLE_PID
#ifdef ENABLE_PID

// Default P, I, and D terms
Expand All @@ -137,7 +137,7 @@
#endif

// Direct step functionality (used to command motor to move over Serial/CAN)
#define ENABLE_DIRECT_STEPPING
//#define ENABLE_DIRECT_STEPPING
#ifdef ENABLE_DIRECT_STEPPING

// The default stepping rate (in Hz) to move in the event that no parameter is specified
Expand Down
7 changes: 4 additions & 3 deletions src/config/config_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ typedef float real_t;
#define REJECT_ENCODERS_LSB 2

// If the steps should be counted using a hardware counter
#define USE_HARDWARE_STEP_CNT
//#define USE_HARDWARE_STEP_CNT
#define USE_SOFTWARE_STEP_CNT

// Board characteristics
// ! Do not modify unless you know what you are doing!
Expand All @@ -43,7 +44,7 @@ typedef float real_t;
// This can be set to 72 and 128 with SYSCLK_SRC_HSE_8 (external oscillator)
// Can be set to 72 with SYSCLK_SRC_HSE_16 (external oscillator)
// Can be set to 64 with SYSCLK_SRC_HSI (internal oscillator)
#define SYSCLK_FREQ 128
#define SYSCLK_FREQ 72
#define SYSCLK_SRC_HSE_8

// The compare format and maximum value for PWM (lower values = higher max freq)
Expand Down Expand Up @@ -122,7 +123,7 @@ typedef float real_t;

// LED related debugging
#ifdef ENABLE_LED
//#define CHECK_STEPPING_RATE
#define CHECK_STEPPING_RATE
//#define CHECK_CORRECT_MOTOR_RATE
//#define CHECK_ENCODER_SPEED
#endif
Expand Down
42 changes: 22 additions & 20 deletions src/hardware/motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,40 +150,40 @@ int32_t StepperMotor::getStepPhase() {

// Returns the desired angle of the motor
float StepperMotor::getDesiredAngle() {
#ifdef USE_HARDWARE_STEP_CNT
return (microstepAngle * getHardStepCNT());
#else
#ifdef USE_SOFTWARE_STEP_CNT
return (microstepAngle * getSoftStepCNT());
#else
return (microstepAngle * getHardStepCNT());
#endif
}


// Sets the desired angle of the motor
void StepperMotor::setDesiredAngle(float newDesiredAngle) {
#ifdef USE_HARDWARE_STEP_CNT
setHardStepCNT(round(newDesiredAngle / microstepAngle));
#else
#ifdef USE_SOFTWARE_STEP_CNT
setSoftStepCNT(round(newDesiredAngle / microstepAngle));
#else
setHardStepCNT(round(newDesiredAngle / microstepAngle));
#endif
}


// Returns the desired step of the motor
int32_t StepperMotor::getDesiredStep() {
#ifdef USE_HARDWARE_STEP_CNT
return getHardStepCNT();
#else
#ifdef USE_SOFTWARE_STEP_CNT
return getSoftStepCNT();
#else
return getHardStepCNT();
#endif
}


// Sets the desired step of the motor
void StepperMotor::setDesiredStep(int32_t newDesiredStep) {
#ifdef USE_HARDWARE_STEP_CNT
setHardStepCNT(newDesiredStep);
#else
#ifdef USE_SOFTWARE_STEP_CNT
setSoftStepCNT(newDesiredStep);
#else
setHardStepCNT(newDesiredStep);
#endif
}

Expand Down Expand Up @@ -223,9 +223,11 @@ void overflowHandler() {
motor.stepOverflowOffset -= TIM_PERIOD;
}
}
#else // ! USE_HARDWARE_STEP_CNT
#endif // USE_HARDWARE_STEP_CNT

#ifdef USE_SOFTWARE_STEP_CNT
// Returns the desired step of the motor
int32_t StepperMotor::getSoftStepCNT() {
int32_t StepperMotor::getSoftStepCNT() const {
return (this -> softStepCNT);
}

Expand All @@ -234,7 +236,7 @@ int32_t StepperMotor::getSoftStepCNT() {
void StepperMotor::setSoftStepCNT(int32_t newStepCNT) {
this -> softStepCNT = newStepCNT;
}
#endif // ! USE_HARDWARE_STEP_CNT
#endif // USE_SOFTWARE_STEP_CNT


#ifdef ENABLE_DYNAMIC_CURRENT
Expand Down Expand Up @@ -347,10 +349,10 @@ void StepperMotor::setMicrostepping(uint8_t setMicrostepping, bool lock) {
float stepScalingFactor = (setMicrostepping / this -> microstepDivisor);

// Scale the step count
#ifdef USE_HARDWARE_STEP_CNT
setHardStepCNT(getHardStepCNT() * stepScalingFactor);
#else
#ifdef USE_SOFTWARE_STEP_CNT
setSoftStepCNT(getSoftStepCNT() * stepScalingFactor);
#else
setHardStepCNT(getHardStepCNT() * stepScalingFactor);
#endif

// Scale the microstep multiplier so that the full stepping level is maintained
Expand Down Expand Up @@ -490,7 +492,7 @@ void StepperMotor::simpleStep() {

// Computes the coil values for the next step position and increments the set angle
#ifdef USE_HARDWARE_STEP_CNT
void StepperMotor::step(STEP_DIR dir, int32_t stepChange) {
void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos) {
#else
void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos) {
#endif
Expand Down Expand Up @@ -540,7 +542,7 @@ void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos)

stepChange *= dir * (this -> reversed);

#ifndef USE_HARDWARE_STEP_CNT
#ifdef USE_SOFTWARE_STEP_CNT
// Update the desired angle if specified
if (updateDesiredPos) {

Expand Down
6 changes: 3 additions & 3 deletions src/hardware/motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class StepperMotor {
void setDesiredStep(int32_t newDesiredStep);

// Returns the desired step of the motor
int32_t getSoftStepCNT();
int32_t getSoftStepCNT() const;

// Sets the desired step of the motor
void setSoftStepCNT(int32_t newStepCNT);
Expand Down Expand Up @@ -195,7 +195,7 @@ class StepperMotor {

// Calculates the coil values for the motor and updates the set angle.
#ifdef USE_HARDWARE_STEP_CNT
void step(STEP_DIR dir, int32_t stepChange);
void step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos = true);
#else
void step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos = true);
#endif
Expand Down Expand Up @@ -246,7 +246,7 @@ class StepperMotor {
// Function that enables the motor
void enable();

#ifndef USE_HARDWARE_STEP_CNT
#ifdef USE_SOFTWARE_STEP_CNT
// Keeps the desired step of the motor
int32_t softStepCNT = 0;
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/hardware/oled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ void displayMotorData() {

#endif // ! ENCODER_SPEED_ESTIMATION

snprintf(outBuffer, OB_SIZE, "sStp:% 10ld", motor.getSoftStepCNT());

writeOLEDString(0, 0, outBuffer, false);

// Angle error
Expand All @@ -240,6 +242,11 @@ void displayMotorData() {

// Temp of the encoder (close to the motor temp)
snprintf(outBuffer, OB_SIZE, "Temp:%8.1f C", motor.encoder.getTemp());

//snprintf(outBuffer, OB_SIZE, "dStp:% 10ld", motor.getDesiredStep());
//snprintf(outBuffer, OB_SIZE, "hStp:% 10ld", motor.getHardStepCNT());
snprintf(outBuffer, OB_SIZE, "h-s: % 10ld", motor.getHardStepCNT()-motor.getSoftStepCNT());

writeOLEDString(0, LINE_HEIGHT * 3, outBuffer, true);
}

Expand Down
24 changes: 13 additions & 11 deletions src/hardware/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void disableInterrupts() {

// Disable the interrupts if this is the first block
if (interruptBlockCount == 0) {
__disable_irq();
//__disable_irq();
syncInstructions();
}

Expand Down Expand Up @@ -260,7 +260,8 @@ void stepMotor() {
}


// Need to declare a function to power the motor coils for the step interrupt
#ifndef DISABLE_CORRECTION_TIMER
// Need to declare a function to power the motor coils for the step interrupt
void correctMotor() {
#ifdef CHECK_CORRECT_MOTOR_RATE
GPIO_WRITE(LED_PIN, HIGH);
Expand Down Expand Up @@ -364,19 +365,19 @@ void correctMotor() {

// Motor is at a position smaller than the desired one
// Use the current angle to find the current step, then add 1
#ifdef USE_HARDWARE_STEP_CNT
motor.step(POSITIVE, 1);
#else
#ifdef USE_SOFTWARE_STEP_CNT
motor.step(POSITIVE, 1, false);
#else
motor.step(POSITIVE, 1);
#endif
}
else {
// Motor is at a position larger than the desired one
// Use the current angle to find the current step, then subtract 1
#ifdef USE_HARDWARE_STEP_CNT
motor.step(NEGATIVE, 1);
#else
#ifdef USE_SOFTWARE_STEP_CNT
motor.step(NEGATIVE, 1, false);
#else
motor.step(NEGATIVE, 1);
#endif
}
}
Expand Down Expand Up @@ -452,6 +453,7 @@ void correctMotor() {
GPIO_WRITE(LED_PIN, LOW);
#endif
}
#endif


// Direct stepping
Expand Down Expand Up @@ -506,10 +508,10 @@ void stepScheduleHandler() {
}
else {
// Just step the motor in the desired direction
#ifdef USE_HARDWARE_STEP_CNT
motor.step(scheduledStepDir, 1);
#else
#ifdef USE_SOFTWARE_STEP_CNT
motor.step(scheduledStepDir, 1, false);
#else
motor.step(scheduledStepDir, 1);
#endif
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void setup() {
// Set processor up
SystemInit();

NVIC_SetPriorityGrouping(3); // __NVIC_PRIO_BITS

// Configure the system clock
#if defined(SYSCLK_SRC_HSE_16)
#if SYSCLK_FREQ == 72
Expand Down