move temp_residency back to temp.h

This commit is contained in:
Michael Moon 2010-09-12 16:07:28 +10:00
parent 273d63f242
commit 9e21f6c811
3 changed files with 15 additions and 94 deletions

View File

@ -640,6 +640,7 @@ void process_gcode_command(GCODE_COMMAND *gcmd) {
serwrite_int32(current_position.F);
serial_writechar('\n');
break;
// M130- heater P factor
case 130:
if (gcmd->seen_S)

View File

@ -30,8 +30,6 @@ int16_t EEMEM EE_i_limit;
uint16_t temp_history[TH_COUNT] __attribute__ ((__section__ (".bss")));
uint8_t th_p = 0;
uint8_t temp_residency = 0;
#ifndef ABSDELTA
#define ABSDELTA(a, b) (((a) >= (b))?((a) - (b)):((b) - (a)))
#endif
@ -58,16 +56,11 @@ void heater_save_settings() {
}
void heater_tick(int16_t current_temp, int16_t target_temp) {
int16_t t_error = target_temp - current_temp;
temp_history[th_p++] = current_temp;
th_p &= (TH_COUNT - 1);
if (ABSDELTA(current_temp, target_temp) > TEMP_HYSTERESIS)
temp_residency = 0;
else if (temp_residency < TEMP_RESIDENCY_TIME)
temp_residency++;
int16_t t_error = target_temp - current_temp;
// PID stuff
// proportional
heater_p = t_error;
@ -86,11 +79,11 @@ void heater_tick(int16_t current_temp, int16_t target_temp) {
// combine factors
int32_t pid_output_intermed = (
(
(((int32_t) heater_p) * p_factor) +
(((int32_t) heater_i) * i_factor) +
(((int32_t) heater_d) * d_factor)
) / PID_SCALE
(
(((int32_t) heater_p) * p_factor) +
(((int32_t) heater_i) * i_factor) +
(((int32_t) heater_d) * d_factor)
) / PID_SCALE
);
// rebase and limit factors
@ -114,9 +107,3 @@ void heater_tick(int16_t current_temp, int16_t target_temp) {
disable_heater();
#endif
}
uint8_t temp_achieved() {
if (temp_residency >= TEMP_RESIDENCY_TIME)
return 255;
return 0;
}

81
temp.c
View File

@ -34,33 +34,11 @@
uint16_t current_temp = 0;
uint16_t target_temp = 0;
/*int16_t heater_p = 0;
int16_t heater_i = 0;
int16_t heater_d = 0;
#define DEFAULT_P 8192
#define DEFAULT_I 512
#define DEFAULT_D -24576
#define DEFAULT_I_LIMIT 384
int32_t p_factor = 0;
int32_t i_factor = 0;
int32_t d_factor = 0;
int16_t i_limit = 0;
*/
// int32_t EEMEM EE_p_factor;
// int32_t EEMEM EE_i_factor;
// int32_t EEMEM EE_d_factor;
// int16_t EEMEM EE_i_limit;
uint8_t temp_flags = 0;
#define TEMP_FLAG_PRESENT 1
#define TEMP_FLAG_TCOPEN 2
/*uint8_t temp_residency = 0;
*/
// #define TH_COUNT 8
// uint16_t temp_history[TH_COUNT] __attribute__ ((__section__ (".bss")));
// uint8_t th_p = 0;
uint8_t temp_residency = 0;
#ifndef ABSDELTA
#define ABSDELTA(a, b) (((a) >= (b))?((a) - (b)):((b) - (a)))
@ -149,60 +127,15 @@ void temp_tick() {
heater_tick(current_temp, target_temp);
/* temp_history[th_p++] = current_temp;
th_p &= (TH_COUNT - 1);
if (ABSDELTA(current_temp, target_temp) > TEMP_HYSTERESIS)
temp_residency = 0;
else if (temp_residency < TEMP_RESIDENCY_TIME)
temp_residency++;
int16_t t_error = target_temp - current_temp;
// PID stuff
// proportional
heater_p = t_error;
// integral
heater_i += t_error;
// prevent integrator wind-up
if (heater_i > i_limit)
heater_i = i_limit;
else if (heater_i < -i_limit)
heater_i = -i_limit;
// derivative
// note: D follows temp rather than error so there's no large derivative when the target changes
heater_d = current_temp - temp_history[th_p];
// combine factors
int32_t pid_output_intermed = (
(
(((int32_t) heater_p) * p_factor) +
(((int32_t) heater_i) * i_factor) +
(((int32_t) heater_d) * d_factor)
) / PID_SCALE
);
// rebase and limit factors
uint8_t pid_output;
if (pid_output_intermed > 255)
pid_output = 255;
else if (pid_output_intermed < 0)
pid_output = 0;
else
pid_output = pid_output_intermed & 0xFF;
if (debug_flags & DEBUG_PID)
sersendf_P(PSTR("T{E:%d, P:%d * %ld = %ld / I:%d * %ld = %ld / D:%d * %ld = %ld # O: %ld = %u}\n"), t_error, heater_p, p_factor, (int32_t) heater_p * p_factor / PID_SCALE, heater_i, i_factor, (int32_t) heater_i * i_factor / PID_SCALE, heater_d, d_factor, (int32_t) heater_d * d_factor / PID_SCALE, pid_output_intermed, pid_output);
#ifdef HEATER_PWM
HEATER_PWM = pid_output;
#else
if (pid_output >= 8)
enable_heater();
else
disable_heater();
#endif*/
}
}
uint8_t temp_achieved() {
if (temp_residency >= TEMP_RESIDENCY_TIME)
return 255;
return 0;
}