From baafbee36ab6d548507b05173ffe9dd6181f5905 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Wed, 11 Aug 2021 18:07:05 +0300 Subject: [PATCH 01/10] USE_SOFTWARE_STEP_CNT --- src/config/config.h | 2 +- src/config/config_adv.h | 3 ++- src/hardware/motor.cpp | 16 +++++++++------- src/hardware/motor.h | 5 ++--- src/hardware/oled.cpp | 14 ++++++++++---- src/hardware/timers.cpp | 2 +- src/main/main.cpp | 8 ++++---- 7 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/config/config.h b/src/config/config.h index 82f557be..f28d0551 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -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 diff --git a/src/config/config_adv.h b/src/config/config_adv.h index f4bdd21e..5bef456c 100644 --- a/src/config/config_adv.h +++ b/src/config/config_adv.h @@ -23,6 +23,7 @@ typedef float real_t; // If the steps should be counted using a hardware counter #define USE_HARDWARE_STEP_CNT +#define USE_SOFTWARE_STEP_CNT // Board characteristics // ! Do not modify unless you know what you are doing! @@ -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 diff --git a/src/hardware/motor.cpp b/src/hardware/motor.cpp index a70d5a66..f9d076ab 100644 --- a/src/hardware/motor.cpp +++ b/src/hardware/motor.cpp @@ -223,7 +223,9 @@ 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() { return (this -> softStepCNT); @@ -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 @@ -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, bool useMultiplier) { +void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) { #else void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) { #endif @@ -510,11 +512,11 @@ void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) if (!useMultiplier) { // Only move one step per pulse when multiplier is disabled - stepChange = 1; + stepChange = dir * (this -> reversed); } else { // Move the number of steps specified by the microstep multiplier - stepChange = (this -> microstepMultiplier); + stepChange = (this -> microstepMultiplier) * dir * (this -> reversed); } /* @@ -538,7 +540,7 @@ void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) isStepping = false; #endif - #ifndef USE_HARDWARE_STEP_CNT + #ifdef USE_SOFTWARE_STEP_CNT // Update the desired angle if specified if (updateDesiredPos) { @@ -549,7 +551,7 @@ void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) // Invert the change based on the direction // Only moving one step in the specified direction - this -> currentStep += stepChange * dir * (this -> reversed); + this -> currentStep += stepChange; // Drive the coils to their destination this -> driveCoils(this -> currentStep); diff --git a/src/hardware/motor.h b/src/hardware/motor.h index 5c82e727..e88e8650 100644 --- a/src/hardware/motor.h +++ b/src/hardware/motor.h @@ -193,7 +193,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, bool useMultiplier = true); + void step(STEP_DIR dir, bool useMultiplier = true, bool updateDesiredPos = true); #else void step(STEP_DIR dir, bool useMultiplier = true, bool updateDesiredPos = true); #endif @@ -232,7 +232,6 @@ class StepperMotor { // TIM2 -> CNT is unsigned, stepOverflowOffset is unsigned, but ((TIM2 -> CNT) + stepOverflowOffset) is treated as signed value uint32_t stepOverflowOffset = 0; - // Things that shouldn't be accessed by the outside private: @@ -242,7 +241,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 diff --git a/src/hardware/oled.cpp b/src/hardware/oled.cpp index 54558d4e..e653494f 100644 --- a/src/hardware/oled.cpp +++ b/src/hardware/oled.cpp @@ -219,27 +219,33 @@ void displayMotorData() { // Check if the motor RPM can be updated. The update rate of the speed must be limited while using encoder speed estimation if (motor.encoder.sampleTimeExceeded()) { snprintf(outBuffer, OB_SIZE, "RPM:% 11.3f", motor.getEstimRPM(currentAbsAngle)); - writeOLEDString(0, 0, outBuffer, false); } #else // ! ENCODER_SPEED_ESTIMATION // No need to check, just sample it snprintf(outBuffer, OB_SIZE, "RPM:%11.3f", motor.getEncoderRPM()); - writeOLEDString(0, 0, outBuffer, false); #endif // ! ENCODER_SPEED_ESTIMATION + snprintf(outBuffer, OB_SIZE, "sStp:% 10ld", motor.getSoftStepCNT()); + + writeOLEDString(0, 0, outBuffer, false); + // Angle error - snprintf(outBuffer, OB_SIZE, "Err: % 10.2f", motor.getAngleError(currentAbsAngle)); + snprintf(outBuffer, OB_SIZE, "Err:% 11.2f", motor.getAngleError(currentAbsAngle)); writeOLEDString(0, LINE_HEIGHT, outBuffer, false); // Current angle of the motor - snprintf(outBuffer, OB_SIZE, "Deg: % 010.2f", currentAbsAngle); + snprintf(outBuffer, OB_SIZE, "Deg:% 11.2f", currentAbsAngle); writeOLEDString(0, LINE_HEIGHT * 2, outBuffer, false); // 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()); + writeOLEDString(0, LINE_HEIGHT * 3, outBuffer, true); } diff --git a/src/hardware/timers.cpp b/src/hardware/timers.cpp index ae07ba66..448aa0af 100644 --- a/src/hardware/timers.cpp +++ b/src/hardware/timers.cpp @@ -160,7 +160,7 @@ void disableInterrupts() { // Disable the interrupts if this is the first block if (interruptBlockCount == 0) { - __disable_irq(); + //__disable_irq(); syncInstructions(); } diff --git a/src/main/main.cpp b/src/main/main.cpp index 14b5f35d..5b4148c7 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -55,9 +55,6 @@ void setup() { initLED(); #endif - // Zero the encoder - motor.encoder.zero(); - // Encoder speed debugging #ifdef CHECK_ENCODER_SPEED while(true) { @@ -69,10 +66,13 @@ void setup() { #endif // Setup the motor for use (should be disabled at startup) - motor.setState(DISABLED, true); + motor.setState(ENABLED, true); //motor.setMicrostepping(16); //motor.setDesiredAngle(100); + // Zero the encoder + motor.encoder.zero(); + // Only run if the OLED is enabled #ifdef ENABLE_OLED From 22077289ec438b8efa38573d473839dbafde9cbf Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Thu, 12 Aug 2021 11:57:39 +0300 Subject: [PATCH 02/10] Update motor.cpp --- src/hardware/motor.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/hardware/motor.cpp b/src/hardware/motor.cpp index f9d076ab..750fc89e 100644 --- a/src/hardware/motor.cpp +++ b/src/hardware/motor.cpp @@ -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 } From 7b1242b2d3417580a16db1418acf36353d454b20 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Thu, 12 Aug 2021 12:59:21 +0300 Subject: [PATCH 03/10] Revert main.cpp --- src/main/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index 5b4148c7..14b5f35d 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -55,6 +55,9 @@ void setup() { initLED(); #endif + // Zero the encoder + motor.encoder.zero(); + // Encoder speed debugging #ifdef CHECK_ENCODER_SPEED while(true) { @@ -66,13 +69,10 @@ void setup() { #endif // Setup the motor for use (should be disabled at startup) - motor.setState(ENABLED, true); + motor.setState(DISABLED, true); //motor.setMicrostepping(16); //motor.setDesiredAngle(100); - // Zero the encoder - motor.encoder.zero(); - // Only run if the OLED is enabled #ifdef ENABLE_OLED From ba04cb99a69fb07f4336b663885af1aab50ebb94 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Thu, 12 Aug 2021 14:22:20 +0300 Subject: [PATCH 04/10] Update config_adv.h --- src/config/config_adv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/config_adv.h b/src/config/config_adv.h index 5bef456c..3ff9d5b9 100644 --- a/src/config/config_adv.h +++ b/src/config/config_adv.h @@ -44,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) From 2e0949ef7181a194319d805cad09546384dc4186 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Fri, 13 Aug 2021 08:54:44 +0300 Subject: [PATCH 05/10] Merge dev --- src/hardware/motor.cpp | 37 +++++++---------- src/hardware/motor.h | 12 +++--- src/hardware/timers.cpp | 16 ++++---- src/main/main.cpp | 88 ++++++++++++++++++++++------------------- src/software/macros.h | 2 + 5 files changed, 80 insertions(+), 75 deletions(-) diff --git a/src/hardware/motor.cpp b/src/hardware/motor.cpp index 750fc89e..8f79fe9f 100644 --- a/src/hardware/motor.cpp +++ b/src/hardware/motor.cpp @@ -492,9 +492,9 @@ 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, bool useMultiplier, bool updateDesiredPos) { +void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos) { #else -void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) { +void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos) { #endif #ifdef ENABLE_STEPPING_VELOCITY @@ -504,7 +504,7 @@ void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) prevStepingSampleTime = nowStepingSampleTime; nowStepingSampleTime = micros(); #endif - + /* // Declare a variable to calculate the step change with int32_t stepChange; @@ -512,13 +512,13 @@ void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) if (!useMultiplier) { // Only move one step per pulse when multiplier is disabled - stepChange = dir * (this -> reversed); + stepChange = 1; } else { // Move the number of steps specified by the microstep multiplier - stepChange = (this -> microstepMultiplier) * dir * (this -> reversed); + stepChange = (this -> microstepMultiplier); } - + */ /* // Invert the change based on the direction if (dir == PIN) { @@ -551,7 +551,7 @@ void StepperMotor::step(STEP_DIR dir, bool useMultiplier, bool updateDesiredPos) // Invert the change based on the direction // Only moving one step in the specified direction - this -> currentStep += stepChange; + this -> currentStep += stepChange * dir * (this -> reversed); // Drive the coils to their destination this -> driveCoils(this -> currentStep); @@ -589,18 +589,17 @@ void StepperMotor::driveCoils(int32_t steps) { if (coilAPower > 0) { // Set first channel for forward movement - setCoilA(COIL_STATE::FORWARD, coilAPower); + setCoilA(FORWARD, coilAPower); } else if (coilAPower < 0) { // Set first channel for backward movement - setCoilA(COIL_STATE::BACKWARD, -coilAPower); + setCoilA(BACKWARD, -coilAPower); } else { setCoilA(BRAKE); } - // Check the if the coil should be energized to move backward or forward if (coilBPower > 0) { @@ -658,10 +657,10 @@ void StepperMotor::driveCoilsAngle(float degAngle) { void StepperMotor::setCoilA(COIL_STATE desiredState, uint16_t current) { // Check if the desired coil state is different from the previous, if so, we need to set the output pins - if (desiredState != previousCoilStateA) { + if (previousCoilStateA != desiredState) { - // Disable the coil - analogSet(&PWMCurrentPinInfoA, 0); + // Update the previous state of the coil with the new one + previousCoilStateA = desiredState; // Decide the state of the direction pins if (desiredState == FORWARD) { @@ -680,9 +679,6 @@ void StepperMotor::setCoilA(COIL_STATE desiredState, uint16_t current) { GPIO_WRITE(COIL_A_DIR_1_PIN, LOW); GPIO_WRITE(COIL_A_DIR_2_PIN, LOW); } - - // Update the previous state of the coil with the new one - previousCoilStateA = desiredState; } // Update the output pin with the correct current @@ -694,10 +690,10 @@ void StepperMotor::setCoilA(COIL_STATE desiredState, uint16_t current) { void StepperMotor::setCoilB(COIL_STATE desiredState, uint16_t current) { // Check if the desired coil state is different from the previous, if so, we need to set the output pins - if (desiredState != previousCoilStateB) { + if (previousCoilStateB != desiredState) { - // Disable the coil - analogSet(&PWMCurrentPinInfoB, 0); + // Update the previous state of the coil with the new one + previousCoilStateB = desiredState; // Decide the state of the direction pins if (desiredState == FORWARD) { @@ -716,9 +712,6 @@ void StepperMotor::setCoilB(COIL_STATE desiredState, uint16_t current) { GPIO_WRITE(COIL_B_DIR_1_PIN, LOW); GPIO_WRITE(COIL_B_DIR_2_PIN, LOW); } - - // Update the previous state of the coil with the new one - previousCoilStateB = desiredState; } // Update the output pin with the correct current diff --git a/src/hardware/motor.h b/src/hardware/motor.h index e88e8650..b1763597 100644 --- a/src/hardware/motor.h +++ b/src/hardware/motor.h @@ -30,6 +30,7 @@ typedef enum { #if 1 // 89.9kHz + // 92.12kHz // Enumeration for stepping direction typedef enum { NEGATIVE = -1, @@ -37,6 +38,7 @@ typedef enum { } STEP_DIR; #else // 91.17kHz + // 92.24kHz #define NEGATIVE (-1) #define POSITIVE 1 typedef int32_t STEP_DIR; @@ -193,9 +195,9 @@ class StepperMotor { // Calculates the coil values for the motor and updates the set angle. #ifdef USE_HARDWARE_STEP_CNT - void step(STEP_DIR dir, bool useMultiplier = true, bool updateDesiredPos = true); + void step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos = true); #else - void step(STEP_DIR dir, bool useMultiplier = true, bool updateDesiredPos = true); + void step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos = true); #endif // Sets the coils to hold the motor at the desired step number @@ -232,6 +234,9 @@ class StepperMotor { // TIM2 -> CNT is unsigned, stepOverflowOffset is unsigned, but ((TIM2 -> CNT) + stepOverflowOffset) is treated as signed value uint32_t stepOverflowOffset = 0; + // Microstep multiplier (used to move a custom number of microsteps per step pulse) + uint32_t microstepMultiplier = DEFAULT_MICROSTEP_MULTIPLIER; + // Things that shouldn't be accessed by the outside private: @@ -301,9 +306,6 @@ class StepperMotor { // If the motor enable is inverted bool enableInverted = false; - // Microstep multiplier (used to move a custom number of microsteps per step pulse) - uint32_t microstepMultiplier = DEFAULT_MICROSTEP_MULTIPLIER; - // Analog info structures for PWM current pins analogInfo PWMCurrentPinInfoA; analogInfo PWMCurrentPinInfoB; diff --git a/src/hardware/timers.cpp b/src/hardware/timers.cpp index 448aa0af..e7049eca 100644 --- a/src/hardware/timers.cpp +++ b/src/hardware/timers.cpp @@ -252,7 +252,7 @@ void stepMotor() { #endif // Step the motor - motor.step((STEP_DIR)DIRECTION(GPIO_READ(DIRECTION_PIN))); + motor.step((STEP_DIR)DIRECTION(GPIO_READ(DIRECTION_PIN)), motor.microstepMultiplier); #ifdef CHECK_STEPPING_RATE GPIO_WRITE(LED_PIN, LOW); @@ -365,18 +365,18 @@ 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, false); + motor.step(POSITIVE, 1); #else - motor.step(POSITIVE, false, false); + motor.step(POSITIVE, 1, false); #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, false); + motor.step(NEGATIVE, 1); #else - motor.step(NEGATIVE, false, false); + motor.step(NEGATIVE, 1, false); #endif } } @@ -484,7 +484,7 @@ void stepScheduleHandler() { if (decrementRemainingSteps) { // Increment the motor in the correct direction - motor.step(scheduledStepDir); + motor.step(scheduledStepDir, motor.microstepMultiplier); // Increment the counter down (we completed a step) remainingScheduledSteps--; @@ -507,9 +507,9 @@ void stepScheduleHandler() { else { // Just step the motor in the desired direction #ifdef USE_HARDWARE_STEP_CNT - motor.step(scheduledStepDir, false); + motor.step(scheduledStepDir, 1); #else - motor.step(scheduledStepDir, false, false); + motor.step(scheduledStepDir, 1, false); #endif } } diff --git a/src/main/main.cpp b/src/main/main.cpp index 14b5f35d..3593c7c0 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -50,14 +50,6 @@ void setup() { MCO_GPIO_Init(); #endif - // Initialize the LED - #ifdef ENABLE_LED - initLED(); - #endif - - // Zero the encoder - motor.encoder.zero(); - // Encoder speed debugging #ifdef CHECK_ENCODER_SPEED while(true) { @@ -68,10 +60,41 @@ void setup() { } #endif - // Setup the motor for use (should be disabled at startup) - motor.setState(DISABLED, true); - //motor.setMicrostepping(16); - //motor.setDesiredAngle(100); + // Debugging for GPIO output switching + #ifdef CHECK_GPIO_OUTPUT_SWITCHING + PA_8_GPIO_Init(); + while(true) { + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + GPIO_WRITE(PA_8, HIGH); + + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + GPIO_WRITE(PA_8, LOW); + } + #endif + + // Initialize the LED + #ifdef ENABLE_LED + initLED(); + #endif + + // Zero the encoder + motor.encoder.zero(); // Only run if the OLED is enabled #ifdef ENABLE_OLED @@ -107,34 +130,6 @@ void setup() { initCAN(); #endif - // Debugging for GPIO output switching - #ifdef CHECK_GPIO_OUTPUT_SWITCHING - PA_8_GPIO_Init(); - while(true) { - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - GPIO_WRITE(PA_8, HIGH); - - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - GPIO_WRITE(PA_8, LOW); - } - #endif - // Clear the display, then write that we're using the closed loop mode //clearOLED(); //writeOLEDString(0, 0, "Close Loop Mode"); @@ -179,11 +174,13 @@ void setup() { if (getMenuDepth() > 0) { // Calibrate the motor (board reboots afterward) + // Reboot the chip motor.calibrate(); } #else // Just jump to calibrating the motor (board reboots afterward) + // Reboot the chip motor.calibrate(); #endif } @@ -226,6 +223,17 @@ void setup() { // Setup the motor timers and interrupts setupMotorTimers(); } + + // Setup the motor for use (should be enabled at startup) + // The jump when power is on + motor.setState(ENABLED, true); + + // Delay for move to power on position + // Needs the motor timers + delay(1000); + + // Zero the encoder after motor is enabled + motor.encoder.zero(); } diff --git a/src/software/macros.h b/src/software/macros.h index feb8743f..5b684815 100644 --- a/src/software/macros.h +++ b/src/software/macros.h @@ -40,6 +40,8 @@ // Direction (for reverse direction) #define DIRECTION(x) ((x) > 0 ? 1 : (-1)) // Note: zero 'x' is not allowed +#define SIGN(x) ((x) > 0 ? 1 : (x) < 0 ? (-1) : 0) + // Return angle in range [0..360[ (doesn't include 360) #define TO360(A) ((A) - DIRECTION(A) * round(abs(A) / 360) * 360) From 452a28454fabfa55b5d7e816d6a8836a4dda4d98 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Fri, 13 Aug 2021 09:10:43 +0300 Subject: [PATCH 06/10] Update motor.cpp --- src/hardware/motor.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hardware/motor.cpp b/src/hardware/motor.cpp index 8f79fe9f..a663c2c8 100644 --- a/src/hardware/motor.cpp +++ b/src/hardware/motor.cpp @@ -540,6 +540,8 @@ void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos) isStepping = false; #endif + stepChange *= dir * (this -> reversed); + #ifdef USE_SOFTWARE_STEP_CNT // Update the desired angle if specified if (updateDesiredPos) { @@ -551,7 +553,7 @@ void StepperMotor::step(STEP_DIR dir, int32_t stepChange, bool updateDesiredPos) // Invert the change based on the direction // Only moving one step in the specified direction - this -> currentStep += stepChange * dir * (this -> reversed); + this -> currentStep += stepChange; // Drive the coils to their destination this -> driveCoils(this -> currentStep); From 61edd5ac260c6d239cb38d40ed4cf9968c5e8dd2 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Fri, 13 Aug 2021 14:13:04 +0300 Subject: [PATCH 07/10] incomprehensible --- src/config/config.h | 2 +- src/config/config_adv.h | 2 +- src/hardware/oled.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config/config.h b/src/config/config.h index f28d0551..70403a91 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -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 diff --git a/src/config/config_adv.h b/src/config/config_adv.h index 3ff9d5b9..f2027a82 100644 --- a/src/config/config_adv.h +++ b/src/config/config_adv.h @@ -22,7 +22,7 @@ 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 diff --git a/src/hardware/oled.cpp b/src/hardware/oled.cpp index e653494f..af355f4c 100644 --- a/src/hardware/oled.cpp +++ b/src/hardware/oled.cpp @@ -228,7 +228,7 @@ void displayMotorData() { #endif // ! ENCODER_SPEED_ESTIMATION - snprintf(outBuffer, OB_SIZE, "sStp:% 10ld", motor.getSoftStepCNT()); + //snprintf(outBuffer, OB_SIZE, "sStp:% 10ld", motor.getSoftStepCNT()); writeOLEDString(0, 0, outBuffer, false); @@ -244,7 +244,7 @@ void displayMotorData() { 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, "hStp:% 10ld", motor.getHardStepCNT()); writeOLEDString(0, LINE_HEIGHT * 3, outBuffer, true); } From 5d4bc1c22fb20565f14763961b752c86f5b20741 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Fri, 13 Aug 2021 15:46:51 +0300 Subject: [PATCH 08/10] USE_SOFTWARE_STEP_CNT --- src/hardware/motor.cpp | 8 ++++---- src/hardware/motor.h | 2 +- src/hardware/timers.cpp | 22 ++++++++++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/hardware/motor.cpp b/src/hardware/motor.cpp index a663c2c8..87c8ae5e 100644 --- a/src/hardware/motor.cpp +++ b/src/hardware/motor.cpp @@ -227,7 +227,7 @@ void overflowHandler() { #ifdef USE_SOFTWARE_STEP_CNT // Returns the desired step of the motor -int32_t StepperMotor::getSoftStepCNT() { +int32_t StepperMotor::getSoftStepCNT() const { return (this -> softStepCNT); } @@ -349,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 diff --git a/src/hardware/motor.h b/src/hardware/motor.h index b1763597..512a9a0a 100644 --- a/src/hardware/motor.h +++ b/src/hardware/motor.h @@ -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); diff --git a/src/hardware/timers.cpp b/src/hardware/timers.cpp index e7049eca..7d5bfe50 100644 --- a/src/hardware/timers.cpp +++ b/src/hardware/timers.cpp @@ -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); @@ -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 } } @@ -452,6 +453,7 @@ void correctMotor() { GPIO_WRITE(LED_PIN, LOW); #endif } +#endif // Direct stepping @@ -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 } } From c0ee84a92b0bacc257686756c854ca5cdb2d3008 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Fri, 13 Aug 2021 18:15:41 +0300 Subject: [PATCH 09/10] Update oled.cpp --- src/hardware/oled.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hardware/oled.cpp b/src/hardware/oled.cpp index af355f4c..ae901da9 100644 --- a/src/hardware/oled.cpp +++ b/src/hardware/oled.cpp @@ -228,7 +228,7 @@ void displayMotorData() { #endif // ! ENCODER_SPEED_ESTIMATION - //snprintf(outBuffer, OB_SIZE, "sStp:% 10ld", motor.getSoftStepCNT()); + snprintf(outBuffer, OB_SIZE, "sStp:% 10ld", motor.getSoftStepCNT()); writeOLEDString(0, 0, outBuffer, false); @@ -245,6 +245,7 @@ void displayMotorData() { //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); } From 29f46d13f49341b6797b0e295fadd73dafbe6bf8 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sat, 14 Aug 2021 14:21:01 +0300 Subject: [PATCH 10/10] SET NVIC_SetPriorityGrouping(3) --- src/main/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/main.cpp b/src/main/main.cpp index 3593c7c0..f50ac2a1 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -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