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