diff --git a/software/firmware_module/module_universal/src/mctrl.c b/software/firmware_module/module_universal/src/mctrl.c index 359f4dd..91cd0d8 100644 --- a/software/firmware_module/module_universal/src/mctrl.c +++ b/software/firmware_module/module_universal/src/mctrl.c @@ -26,17 +26,17 @@ uint8_t motor_steps[4] = { }; #endif -uint8_t step_index = 0; // current index in motor_steps -uint8_t target_flap = 0; // target flap -uint16_t absolute_pos = 0; // absolute position in steps -uint16_t steps_since_home = 0; // steps since last home signal +uint8_t step_index = 0; // current index in motor_steps +uint8_t target_flap = 0; // target flap +uint16_t absolute_pos = 0; // absolute position in steps +uint16_t steps_since_home = 0; // steps since last home signal // homing util variables uint8_t homing = 0; // current homing step uint8_t lastSens = 0; // home sonsor signal from last tick // counter for auto powersaving -uint8_t ticksSinceMove = 0; +uint16_t ticksSinceMove = 0; // value to goto after the current is reached. 255 = NONE. uint8_t afterRotation = STEPS_AFTERROT; @@ -60,9 +60,12 @@ int STEPS_OFFSET = 0; // initialize motor controller void mctrl_init(int cal_offset) { - if (cal_offset < 800){ + if (cal_offset < 800) + { STEPS_OFFSET = STEPS_OFFSET_DEF; - }else{ + } + else + { STEPS_OFFSET = cal_offset; } DDRC = 0x0F; // set all pins as outputs @@ -83,9 +86,9 @@ void mctrl_init(int cal_offset) // setup timer for ISR TCCR1A = 0; - TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // CTC und Prescaler 64 - OCR1A = MISR_OCR1A; - TIMSK = 1 << OCIE1A; // Timerinterrupts aktivieren + TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // CTC und Prescaler 64 (CLK=250kHz) + OCR1A = MISR_OCR1A; // Equals roughly 431Hz (23.2ms Intervall) + TIMSK = 1 << OCIE1A; // Timerinterrupts aktivieren homing = 1; delta_err = malloc(ERROR_DATASETS * sizeof(uint16_t)); _delay_ms(MDELAY_STARTUP); @@ -119,7 +122,7 @@ void readVoltage() // MAIN service routine. Called by timer 1 ISR(TIMER1_COMPA_vect) { - timer_ticks ++; + timer_ticks++; readVoltage(); // read and check voltage if (sts_flag_pwrdwn == 1 || sts_flag_failsafe == 1) { @@ -158,7 +161,7 @@ ISR(TIMER1_COMPA_vect) if (absolute_pos <= 0) { homing = 0; - absolute_pos = STEPS_OFFSET; // set correct position again + absolute_pos = STEPS_OFFSET; // set correct position again } mctrl_step(); absolute_pos--; @@ -174,7 +177,6 @@ ISR(TIMER1_COMPA_vect) if (absolute_pos != target_pos) { // if target position is not reached, move motor - ticksSinceMove = 0; mctrl_step(); absolute_pos++; if (absolute_pos >= STEPS_PER_REV) @@ -213,18 +215,18 @@ ISR(TIMER1_COMPA_vect) else if (ticksSinceMove < 2) { // if motor has not been moved sts_flag_busy = 0; - } - else if (ticksSinceMove < MPWRSVG_TICKSTOP) - { // if motor has not been moved ticksSinceMove++; } - else - { // power off after 50 ticks - // PORTC = 0x00; // turn off stepper + else if (ticksSinceMove > MPWRSVG_TICKSTOP) + { // power off after MPWRSVG_TICKSTOP ticks + PORTC = 0x00; // turn off stepper + }else{ + ticksSinceMove++; } } } rc_tick(); // process counter tick, non-blocking + } // TODO @@ -305,6 +307,7 @@ void mctrl_step() { step_index++; steps_since_home++; + ticksSinceMove = 0; if (step_index > 3) { step_index = 0; diff --git a/software/firmware_module/module_universal/src/mctrl.h b/software/firmware_module/module_universal/src/mctrl.h index 1277e41..9c78b2b 100644 --- a/software/firmware_module/module_universal/src/mctrl.h +++ b/software/firmware_module/module_universal/src/mctrl.h @@ -12,37 +12,38 @@ #pragma once -#define STEPS_PER_REV 2025 // steps per revolution -#define STEPS_PER_FLAP 45 // steps per flap -#define STEPS_ADJ 0 // added per flap to compensate for motor power down -#define STEPS_OFFSET_DEF 1400 // ansolute offset between home and first flap -#define AMOUNTFLAPS 45 // amount of flaps installed in system -#define STEPS_AFTERROT 255 // value to goto after current target flap is reached -#define ERROR_DATASETS 8 // length of error array +#define STEPS_PER_REV 2025 // steps per revolution +#define STEPS_PER_FLAP 45 // steps per flap +#define STEPS_ADJ 0 // added per flap to compensate for motor power down +#define STEPS_OFFSET_DEF 1400 // ansolute offset between home and first flap +#define AMOUNTFLAPS 45 // amount of flaps installed in system +#define STEPS_AFTERROT 255 // value to goto after current target flap is reached +#define ERROR_DATASETS 8 // length of error array -#define MDELAY_STARTUP 1000 // delay to wait after motor startup -#define MHOME_TOLERANCE 1.5 // tolerance for intial homing procedure -#define MHOME_ERRDELTA 30 // maximum deviation between expected home and actual home -#define MVOLTAGE_FAULTRD 20 // max. amount of fault readings before flag is set -#define MVOLTAGE_LSTOP 128 // lower voltage threshold for fuse detection -#define MPWRSVG_TICKSTOP 50 // inactive ticks before motor shutdown +#define MDELAY_STARTUP 1000 // delay to wait after motor startup +#define MHOME_TOLERANCE 1.5 // tolerance for intial homing procedure +#define MHOME_ERRDELTA 30 // maximum deviation between expected home and actual home +#define MVOLTAGE_FAULTRD 20 // max. amount of fault readings before flag is set +#define MVOLTAGE_LSTOP 128 // lower voltage threshold for fuse detection +#define MPWRSVG_TICKSTOP 4310 // inactive ticks before motor shutdown (4310 ~ 10s) -#define MISR_OCR1A 580 // tick timer (defines rotation speed) +#define MISR_OCR1A 580 // tick timer (defines rotation speed. euqals to ISR freq ~ 431Hz) // 450, 480 also possible ? #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // __cplusplus -void mctrl_init(int cal_offset); -void mctrl_step(); -void mctrl_set(uint8_t flap, uint8_t fullRotation); + void mctrl_init(int cal_offset); + void mctrl_step(); + void mctrl_set(uint8_t flap, uint8_t fullRotation); -void getErr(int16_t* error); -uint8_t getSts(); -uint16_t getVoltage(); -void mctrl_power(uint8_t state); + void getErr(int16_t *error); + uint8_t getSts(); + uint16_t getVoltage(); + void mctrl_power(uint8_t state); -uint16_t timer_ticks; + uint16_t timer_ticks; #ifdef __cplusplus } #endif // __cplusplus