Big temperature code update. No longer converts back and forwards between temperature and raw sample value. Reducing complexity, removing code. Also named some variables better. While keeping the safety intact and functionality the same.

This commit is contained in:
daid303 2012-12-09 23:00:06 +01:00
parent ca7acbe6d9
commit 52158dffcc
3 changed files with 291 additions and 266 deletions

View File

@ -37,19 +37,14 @@
//=========================================================================== //===========================================================================
//=============================public variables============================ //=============================public variables============================
//=========================================================================== //===========================================================================
int target_raw[EXTRUDERS] = { 0 }; int target_temperature[EXTRUDERS] = { 0 };
int target_raw_bed = 0; int target_temperature_bed = 0;
#ifdef BED_LIMIT_SWITCHING int current_temperature_raw[EXTRUDERS] = { 0 };
int target_bed_low_temp =0; float current_temperature[EXTRUDERS] = { 0 };
int target_bed_high_temp =0; int current_temperature_bed_raw = 0;
#endif float current_temperature_bed = 0;
int current_raw[EXTRUDERS] = { 0 };
int current_raw_bed = 0;
#ifdef PIDTEMP #ifdef PIDTEMP
// used external
float pid_setpoint[EXTRUDERS] = { 0.0 };
float Kp=DEFAULT_Kp; float Kp=DEFAULT_Kp;
float Ki=(DEFAULT_Ki*PID_dT); float Ki=(DEFAULT_Ki*PID_dT);
float Kd=(DEFAULT_Kd/PID_dT); float Kd=(DEFAULT_Kd/PID_dT);
@ -59,9 +54,6 @@ int current_raw_bed = 0;
#endif //PIDTEMP #endif //PIDTEMP
#ifdef PIDTEMPBED #ifdef PIDTEMPBED
// used external
float pid_setpoint_bed = { 0.0 };
float bedKp=DEFAULT_bedKp; float bedKp=DEFAULT_bedKp;
float bedKi=(DEFAULT_bedKi*PID_dT); float bedKi=(DEFAULT_bedKi*PID_dT);
float bedKd=(DEFAULT_bedKd/PID_dT); float bedKd=(DEFAULT_bedKd/PID_dT);
@ -116,12 +108,20 @@ static volatile bool temp_meas_ready = false;
#endif #endif
// Init min and max temp with extreme values to prevent false errors during startup // Init min and max temp with extreme values to prevent false errors during startup
static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0, 0, 0); static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(16383, 16383, 16383); // the first value used for all static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
static int bed_minttemp = 0; static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
static int bed_maxttemp = 16383; static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS((void *)heater_0_temptable, (void *)heater_1_temptable, (void *)heater_2_temptable); //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
static int heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS(heater_0_temptable_len, heater_1_temptable_len, heater_2_temptable_len); #ifdef BED_MAXTEMP
static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
#endif
static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
static int heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
static float analog2temp(int raw, uint8_t e);
static float analog2tempBed(int raw);
static void updateTemperaturesFromRawValues();
#ifdef WATCH_TEMP_PERIOD #ifdef WATCH_TEMP_PERIOD
int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0); int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
@ -179,13 +179,9 @@ void PID_autotune(float temp, int extruder, int ncycles)
for(;;) { for(;;) {
if(temp_meas_ready == true) { // temp sample ready if(temp_meas_ready == true) { // temp sample ready
//Reset the watchdog after we know we have a temperature measurement. updateTemperaturesFromRawValues();
watchdog_reset();
CRITICAL_SECTION_START; input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
temp_meas_ready = false;
CRITICAL_SECTION_END;
input = (extruder<0)?analog2tempBed(current_raw_bed):analog2temp(current_raw[extruder], extruder);
max=max(max,input); max=max(max,input);
min=min(min,input); min=min(min,input);
@ -313,21 +309,16 @@ void manage_heater()
if(temp_meas_ready != true) //better readability if(temp_meas_ready != true) //better readability
return; return;
//Reset the watchdog after we know we have a temperature measurement. updateTemperaturesFromRawValues();
watchdog_reset();
CRITICAL_SECTION_START;
temp_meas_ready = false;
CRITICAL_SECTION_END;
for(int e = 0; e < EXTRUDERS; e++) for(int e = 0; e < EXTRUDERS; e++)
{ {
#ifdef PIDTEMP #ifdef PIDTEMP
pid_input = analog2temp(current_raw[e], e); pid_input = current_temperature[e];
#ifndef PID_OPENLOOP #ifndef PID_OPENLOOP
pid_error[e] = pid_setpoint[e] - pid_input; pid_error[e] = target_temperature[e] - pid_input;
if(pid_error[e] > 10) { if(pid_error[e] > 10) {
pid_output = PID_MAX; pid_output = PID_MAX;
pid_reset[e] = true; pid_reset[e] = true;
@ -354,20 +345,20 @@ void manage_heater()
pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX); pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
} }
#else #else
pid_output = constrain(pid_setpoint[e], 0, PID_MAX); pid_output = constrain(target_temperature[e], 0, PID_MAX);
#endif //PID_OPENLOOP #endif //PID_OPENLOOP
#ifdef PID_DEBUG #ifdef PID_DEBUG
SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]); SERIAL_ECHOLN(" PIDDEBUG "<<e<<": Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm[e]<<" iTerm "<<iTerm[e]<<" dTerm "<<dTerm[e]);
#endif //PID_DEBUG #endif //PID_DEBUG
#else /* PID off */ #else /* PID off */
pid_output = 0; pid_output = 0;
if(current_raw[e] < target_raw[e]) { if(current_temperature[e] < target_temperature[e]) {
pid_output = PID_MAX; pid_output = PID_MAX;
} }
#endif #endif
// Check if temperature is within the correct range // Check if temperature is within the correct range
if((current_raw[e] > minttemp[e]) && (current_raw[e] < maxttemp[e])) if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
{ {
soft_pwm[e] = (int)pid_output >> 1; soft_pwm[e] = (int)pid_output >> 1;
} }
@ -399,13 +390,13 @@ void manage_heater()
previous_millis_bed_heater = millis(); previous_millis_bed_heater = millis();
#endif #endif
#if TEMP_BED_PIN > -1 #if TEMP_SENSOR_BED != 0
#ifdef PIDTEMPBED #ifdef PIDTEMPBED
pid_input = analog2tempBed(current_raw_bed); pid_input = current_temperature_bed;
#ifndef PID_OPENLOOP #ifndef PID_OPENLOOP
pid_error_bed = pid_setpoint_bed - pid_input; pid_error_bed = target_temperature_bed - pid_input;
pTerm_bed = bedKp * pid_error_bed; pTerm_bed = bedKp * pid_error_bed;
temp_iState_bed += pid_error_bed; temp_iState_bed += pid_error_bed;
temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed); temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
@ -419,10 +410,10 @@ void manage_heater()
pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER); pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
#else #else
pid_output = constrain(pid_setpoint_bed, 0, MAX_BED_POWER); pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
#endif //PID_OPENLOOP #endif //PID_OPENLOOP
if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
{ {
soft_pwm_bed = (int)pid_output >> 1; soft_pwm_bed = (int)pid_output >> 1;
} }
@ -432,8 +423,9 @@ void manage_heater()
#elif not defined BED_LIMIT_SWITCHING #elif not defined BED_LIMIT_SWITCHING
// Check if temperature is within the correct range // Check if temperature is within the correct range
if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) { if((current_temperature_bed > BED_MAXTEMP) && (current_temperature_bed < BED_MINTEMP))
if(current_raw_bed >= target_raw_bed) {
if(current_temperature_bed >= target_temperature_bed)
{ {
soft_pwm_bed = 0; soft_pwm_bed = 0;
} }
@ -442,24 +434,26 @@ void manage_heater()
soft_pwm_bed = MAX_BED_POWER>>1; soft_pwm_bed = MAX_BED_POWER>>1;
} }
} }
else { else
{
soft_pwm_bed = 0; soft_pwm_bed = 0;
WRITE(HEATER_BED_PIN,LOW); WRITE(HEATER_BED_PIN,LOW);
} }
#else //#ifdef BED_LIMIT_SWITCHING #else //#ifdef BED_LIMIT_SWITCHING
// Check if temperature is within the correct band // Check if temperature is within the correct band
if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) { if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
if(current_raw_bed > target_bed_high_temp) {
if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
{ {
soft_pwm_bed = 0; soft_pwm_bed = 0;
} }
else else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
if(current_raw_bed <= target_bed_low_temp)
{ {
soft_pwm_bed = MAX_BED_POWER>>1; soft_pwm_bed = MAX_BED_POWER>>1;
} }
} }
else { else
{
soft_pwm_bed = 0; soft_pwm_bed = 0;
WRITE(HEATER_BED_PIN,LOW); WRITE(HEATER_BED_PIN,LOW);
} }
@ -468,86 +462,9 @@ void manage_heater()
} }
#define PGM_RD_W(x) (short)pgm_read_word(&x) #define PGM_RD_W(x) (short)pgm_read_word(&x)
// Takes hot end temperature value as input and returns corresponding raw value.
// For a thermistor, it uses the RepRap thermistor temp table.
// This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
// This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
int temp2analog(int celsius, uint8_t e) {
if(e >= EXTRUDERS)
{
SERIAL_ERROR_START;
SERIAL_ERROR((int)e);
SERIAL_ERRORLNPGM(" - Invalid extruder number!");
kill();
}
#ifdef HEATER_0_USES_MAX6675
if (e == 0)
{
return celsius * 4;
}
#endif
if(heater_ttbl_map[e] != 0)
{
int raw = 0;
byte i;
short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
for (i=1; i<heater_ttbllen_map[e]; i++)
{
if (PGM_RD_W((*tt)[i][1]) < celsius)
{
raw = PGM_RD_W((*tt)[i-1][0]) +
(celsius - PGM_RD_W((*tt)[i-1][1])) *
(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])) /
(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1]));
break;
}
}
// Overflow: Set to last value in the table
if (i == heater_ttbllen_map[e]) raw = PGM_RD_W((*tt)[i-1][0]);
return (1023 * OVERSAMPLENR) - raw;
}
return ((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 / (5.0 * 100.0) ) * OVERSAMPLENR;
}
// Takes bed temperature value as input and returns corresponding raw value.
// For a thermistor, it uses the RepRap thermistor temp table.
// This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.
// This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
int temp2analogBed(int celsius) {
#ifdef BED_USES_THERMISTOR
int raw = 0;
byte i;
for (i=1; i<bedtemptable_len; i++)
{
if (PGM_RD_W(bedtemptable[i][1]) < celsius)
{
raw = PGM_RD_W(bedtemptable[i-1][0]) +
(celsius - PGM_RD_W(bedtemptable[i-1][1])) *
(PGM_RD_W(bedtemptable[i][0]) - PGM_RD_W(bedtemptable[i-1][0])) /
(PGM_RD_W(bedtemptable[i][1]) - PGM_RD_W(bedtemptable[i-1][1]));
break;
}
}
// Overflow: Set to last value in the table
if (i == bedtemptable_len) raw = PGM_RD_W(bedtemptable[i-1][0]);
return (1023 * OVERSAMPLENR) - raw;
#elif defined BED_USES_AD595
return lround(((celsius-TEMP_SENSOR_AD595_OFFSET)/TEMP_SENSOR_AD595_GAIN) * (1024.0 * OVERSAMPLENR/ (5.0 * 100.0) ) );
#else
return 0;
#endif
}
// Derived from RepRap FiveD extruder::getTemperature() // Derived from RepRap FiveD extruder::getTemperature()
// For hot end temperature measurement. // For hot end temperature measurement.
float analog2temp(int raw, uint8_t e) { static float analog2temp(int raw, uint8_t e) {
if(e >= EXTRUDERS) if(e >= EXTRUDERS)
{ {
SERIAL_ERROR_START; SERIAL_ERROR_START;
@ -568,7 +485,6 @@ float analog2temp(int raw, uint8_t e) {
byte i; byte i;
short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]); short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
raw = (1023 * OVERSAMPLENR) - raw;
for (i=1; i<heater_ttbllen_map[e]; i++) for (i=1; i<heater_ttbllen_map[e]; i++)
{ {
if (PGM_RD_W((*tt)[i][0]) > raw) if (PGM_RD_W((*tt)[i][0]) > raw)
@ -591,13 +507,11 @@ float analog2temp(int raw, uint8_t e) {
// Derived from RepRap FiveD extruder::getTemperature() // Derived from RepRap FiveD extruder::getTemperature()
// For bed temperature measurement. // For bed temperature measurement.
float analog2tempBed(int raw) { static float analog2tempBed(int raw) {
#ifdef BED_USES_THERMISTOR #ifdef BED_USES_THERMISTOR
float celsius = 0; float celsius = 0;
byte i; byte i;
raw = (1023 * OVERSAMPLENR) - raw;
for (i=1; i<bedtemptable_len; i++) for (i=1; i<bedtemptable_len; i++)
{ {
if (PGM_RD_W(bedtemptable[i][0]) > raw) if (PGM_RD_W(bedtemptable[i][0]) > raw)
@ -621,6 +535,24 @@ float analog2tempBed(int raw) {
#endif #endif
} }
/* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
static void updateTemperaturesFromRawValues()
{
for(uint8_t e=0;e<EXTRUDERS;e++)
{
current_temperature[e] = analog2temp(current_temperature_raw[e], e);
}
current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
//Reset the watchdog after we know we have a temperature measurement.
watchdog_reset();
CRITICAL_SECTION_START;
temp_meas_ready = false;
CRITICAL_SECTION_END;
}
void tp_init() void tp_init()
{ {
// Finish init of mult extruder arrays // Finish init of mult extruder arrays
@ -716,31 +648,87 @@ void tp_init()
delay(250); delay(250);
#ifdef HEATER_0_MINTEMP #ifdef HEATER_0_MINTEMP
minttemp[0] = temp2analog(HEATER_0_MINTEMP, 0); minttemp[0] = HEATER_0_MINTEMP;
while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
#if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
minttemp_raw[0] += OVERSAMPLENR;
#else
minttemp_raw[0] -= OVERSAMPLENR;
#endif
}
#endif //MINTEMP #endif //MINTEMP
#ifdef HEATER_0_MAXTEMP #ifdef HEATER_0_MAXTEMP
maxttemp[0] = temp2analog(HEATER_0_MAXTEMP, 0); maxttemp[0] = HEATER_0_MAXTEMP;
while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
#if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
maxttemp_raw[0] -= OVERSAMPLENR;
#else
maxttemp_raw[0] += OVERSAMPLENR;
#endif
}
#endif //MAXTEMP #endif //MAXTEMP
#if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP) #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
minttemp[1] = temp2analog(HEATER_1_MINTEMP, 1); minttemp[1] = HEATER_1_MINTEMP;
while(analog2temp(minttemp_raw[1], 1) > HEATER_1_MINTEMP) {
#if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
minttemp_raw[1] += OVERSAMPLENR;
#else
minttemp_raw[1] -= OVERSAMPLENR;
#endif
}
#endif // MINTEMP 1 #endif // MINTEMP 1
#if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP) #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
maxttemp[1] = temp2analog(HEATER_1_MAXTEMP, 1); maxttemp[1] = HEATER_1_MAXTEMP;
while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
#if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
maxttemp_raw[1] -= OVERSAMPLENR;
#else
maxttemp_raw[1] += OVERSAMPLENR;
#endif
}
#endif //MAXTEMP 1 #endif //MAXTEMP 1
#if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP) #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
minttemp[2] = temp2analog(HEATER_2_MINTEMP, 2); minttemp[2] = HEATER_2_MINTEMP;
while(analog2temp(minttemp_raw[2], 2) > HEATER_2_MINTEMP) {
#if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
minttemp_raw[2] += OVERSAMPLENR;
#else
minttemp_raw[2] -= OVERSAMPLENR;
#endif
}
#endif //MINTEMP 2 #endif //MINTEMP 2
#if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP) #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
maxttemp[2] = temp2analog(HEATER_2_MAXTEMP, 2); maxttemp[2] = HEATER_2_MAXTEMP;
while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
#if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
maxttemp_raw[2] -= OVERSAMPLENR;
#else
maxttemp_raw[2] += OVERSAMPLENR;
#endif
}
#endif //MAXTEMP 2 #endif //MAXTEMP 2
#ifdef BED_MINTEMP #ifdef BED_MINTEMP
bed_minttemp = temp2analogBed(BED_MINTEMP); /* No bed MINTEMP error implemented?!? */ /*
while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
#if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
bed_minttemp_raw += OVERSAMPLENR;
#else
bed_minttemp_raw -= OVERSAMPLENR;
#endif
}
*/
#endif //BED_MINTEMP #endif //BED_MINTEMP
#ifdef BED_MAXTEMP #ifdef BED_MAXTEMP
bed_maxttemp = temp2analogBed(BED_MAXTEMP); while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
#if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
bed_maxttemp_raw -= OVERSAMPLENR;
#else
bed_maxttemp_raw += OVERSAMPLENR;
#endif
}
#endif //BED_MAXTEMP #endif //BED_MAXTEMP
} }
@ -765,7 +753,7 @@ void disable_heater()
setTargetHotend(0,i); setTargetHotend(0,i);
setTargetBed(0); setTargetBed(0);
#if TEMP_0_PIN > -1 #if TEMP_0_PIN > -1
target_raw[0]=0; target_temperature[0]=0;
soft_pwm[0]=0; soft_pwm[0]=0;
#if HEATER_0_PIN > -1 #if HEATER_0_PIN > -1
WRITE(HEATER_0_PIN,LOW); WRITE(HEATER_0_PIN,LOW);
@ -773,7 +761,7 @@ void disable_heater()
#endif #endif
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
target_raw[1]=0; target_temperature[1]=0;
soft_pwm[1]=0; soft_pwm[1]=0;
#if HEATER_1_PIN > -1 #if HEATER_1_PIN > -1
WRITE(HEATER_1_PIN,LOW); WRITE(HEATER_1_PIN,LOW);
@ -781,7 +769,7 @@ void disable_heater()
#endif #endif
#if TEMP_2_PIN > -1 #if TEMP_2_PIN > -1
target_raw[2]=0; target_temperature[2]=0;
soft_pwm[2]=0; soft_pwm[2]=0;
#if HEATER_2_PIN > -1 #if HEATER_2_PIN > -1
WRITE(HEATER_2_PIN,LOW); WRITE(HEATER_2_PIN,LOW);
@ -789,7 +777,7 @@ void disable_heater()
#endif #endif
#if TEMP_BED_PIN > -1 #if TEMP_BED_PIN > -1
target_raw_bed=0; target_temperature_bed=0;
soft_pwm_bed=0; soft_pwm_bed=0;
#if HEATER_BED_PIN > -1 #if HEATER_BED_PIN > -1
WRITE(HEATER_BED_PIN,LOW); WRITE(HEATER_BED_PIN,LOW);
@ -1031,33 +1019,16 @@ ISR(TIMER0_COMPB_vect)
if(temp_count >= 16) // 8 ms * 16 = 128ms. if(temp_count >= 16) // 8 ms * 16 = 128ms.
{ {
#if defined(HEATER_0_USES_AD595) || defined(HEATER_0_USES_MAX6675) if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
current_raw[0] = raw_temp_0_value; {
#else current_temperature_raw[0] = raw_temp_0_value;
current_raw[0] = 16383 - raw_temp_0_value;
#endif
#if EXTRUDERS > 1 #if EXTRUDERS > 1
#ifdef HEATER_1_USES_AD595 current_temperature_raw[1] = raw_temp_0_value;
current_raw[1] = raw_temp_1_value;
#else
current_raw[1] = 16383 - raw_temp_1_value;
#endif
#endif #endif
#if EXTRUDERS > 2 #if EXTRUDERS > 2
#ifdef HEATER_2_USES_AD595 current_temperature_raw[2] = raw_temp_0_value;
current_raw[2] = raw_temp_2_value;
#else
current_raw[2] = 16383 - raw_temp_2_value;
#endif
#endif #endif
}
#ifdef BED_USES_AD595
current_raw_bed = raw_temp_bed_value;
#else
current_raw_bed = 16383 - raw_temp_bed_value;
#endif
temp_meas_ready = true; temp_meas_ready = true;
temp_count = 0; temp_count = 0;
@ -1066,23 +1037,63 @@ ISR(TIMER0_COMPB_vect)
raw_temp_2_value = 0; raw_temp_2_value = 0;
raw_temp_bed_value = 0; raw_temp_bed_value = 0;
for(unsigned char e = 0; e < EXTRUDERS; e++) { #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
if(current_raw[e] >= maxttemp[e]) { if(current_temperature_raw[0] <= maxttemp_raw[0]) {
target_raw[e] = 0; #else
max_temp_error(e); if(current_temperature_raw[0] >= maxttemp_raw[0]) {
#endif
max_temp_error(0);
} }
if(current_raw[e] <= minttemp[e]) { #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
target_raw[e] = 0; if(current_temperature_raw[0] >= minttemp_raw[0]) {
min_temp_error(e); #else
if(current_temperature_raw[0] <= minttemp_raw[0]) {
#endif
min_temp_error(0);
} }
#if EXTRUDERS > 1
#if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
if(current_temperature_raw[1] <= maxttemp_raw[1]) {
#else
if(current_temperature_raw[1] >= maxttemp_raw[1]) {
#endif
max_temp_error(1);
} }
#if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
if(current_temperature_raw[1] >= minttemp_raw[1]) {
#else
if(current_temperature_raw[1] <= minttemp_raw[1]) {
#endif
min_temp_error(1);
}
#endif
#if EXTRUDERS > 2
#if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
if(current_temperature_raw[2] <= maxttemp_raw[2]) {
#else
if(current_temperature_raw[2] >= maxttemp_raw[2]) {
#endif
max_temp_error(2);
}
#if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
if(current_temperature_raw[2] >= minttemp_raw[2]) {
#else
if(current_temperature_raw[2] <= minttemp_raw[2]) {
#endif
min_temp_error(2);
}
#endif
#if defined(BED_MAXTEMP) && (HEATER_BED_PIN > -1) /* No bed MINTEMP error? */
if(current_raw_bed >= bed_maxttemp) { #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
target_raw_bed = 0; # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
if(current_temperature_bed <= bed_maxttemp_raw) {
#else
if(current_temperature_bed >= bed_maxttemp_raw) {
#endif
target_temperature_bed = 0;
bed_max_temp_error(); bed_max_temp_error();
} }
#endif #endif
} }
} }

View File

@ -33,27 +33,16 @@ void manage_heater(); //it is critical that this is called periodically.
//low leven conversion routines //low leven conversion routines
// do not use this routines and variables outsie of temperature.cpp // do not use this routines and variables outsie of temperature.cpp
int temp2analog(int celsius, uint8_t e); extern int target_temperature[EXTRUDERS];
int temp2analogBed(int celsius); extern float current_temperature[EXTRUDERS];
float analog2temp(int raw, uint8_t e); extern int target_temperature_bed;
float analog2tempBed(int raw); extern float current_temperature_bed;
extern int target_raw[EXTRUDERS];
extern int heatingtarget_raw[EXTRUDERS];
extern int current_raw[EXTRUDERS];
extern int target_raw_bed;
extern int current_raw_bed;
#ifdef BED_LIMIT_SWITCHING
extern int target_bed_low_temp ;
extern int target_bed_high_temp ;
#endif
#ifdef PIDTEMP #ifdef PIDTEMP
extern float Kp,Ki,Kd,Kc; extern float Kp,Ki,Kd,Kc;
extern float pid_setpoint[EXTRUDERS];
#endif #endif
#ifdef PIDTEMPBED #ifdef PIDTEMPBED
extern float bedKp,bedKi,bedKd; extern float bedKp,bedKi,bedKd;
extern float pid_setpoint_bed;
#endif #endif
//high level conversion routines, for use outside of temperature.cpp //high level conversion routines, for use outside of temperature.cpp
@ -61,61 +50,43 @@ extern int current_raw_bed;
//deg=degreeCelsius //deg=degreeCelsius
FORCE_INLINE float degHotend(uint8_t extruder) { FORCE_INLINE float degHotend(uint8_t extruder) {
return analog2temp(current_raw[extruder], extruder); return current_temperature[extruder];
}; };
FORCE_INLINE float degBed() { FORCE_INLINE float degBed() {
return analog2tempBed(current_raw_bed); return current_temperature_bed;
}; };
FORCE_INLINE float degTargetHotend(uint8_t extruder) { FORCE_INLINE float degTargetHotend(uint8_t extruder) {
return analog2temp(target_raw[extruder], extruder); return target_temperature[extruder];
}; };
FORCE_INLINE float degTargetBed() { FORCE_INLINE float degTargetBed() {
return analog2tempBed(target_raw_bed); return target_temperature_bed;
}; };
FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) { FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) {
target_raw[extruder] = temp2analog(celsius, extruder); target_temperature[extruder] = celsius;
#ifdef PIDTEMP
pid_setpoint[extruder] = celsius;
#endif //PIDTEMP
}; };
FORCE_INLINE void setTargetBed(const float &celsius) { FORCE_INLINE void setTargetBed(const float &celsius) {
target_temperature_bed = celsius;
target_raw_bed = temp2analogBed(celsius);
#ifdef PIDTEMPBED
pid_setpoint_bed = celsius;
#elif defined BED_LIMIT_SWITCHING
if(celsius>BED_HYSTERESIS)
{
target_bed_low_temp= temp2analogBed(celsius-BED_HYSTERESIS);
target_bed_high_temp= temp2analogBed(celsius+BED_HYSTERESIS);
}
else
{
target_bed_low_temp=0;
target_bed_high_temp=0;
}
#endif
}; };
FORCE_INLINE bool isHeatingHotend(uint8_t extruder){ FORCE_INLINE bool isHeatingHotend(uint8_t extruder){
return target_raw[extruder] > current_raw[extruder]; return target_temperature[extruder] > current_temperature[extruder];
}; };
FORCE_INLINE bool isHeatingBed() { FORCE_INLINE bool isHeatingBed() {
return target_raw_bed > current_raw_bed; return target_temperature_bed > current_temperature_bed;
}; };
FORCE_INLINE bool isCoolingHotend(uint8_t extruder) { FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
return target_raw[extruder] < current_raw[extruder]; return target_temperature[extruder] < current_temperature[extruder];
}; };
FORCE_INLINE bool isCoolingBed() { FORCE_INLINE bool isCoolingBed() {
return target_raw_bed < current_raw_bed; return target_temperature_bed < current_temperature_bed;
}; };
#define degHotend0() degHotend(0) #define degHotend0() degHotend(0)

View File

@ -461,49 +461,92 @@ const short temptable_55[][2] PROGMEM = {
#define TT_NAME(_N) _TT_NAME(_N) #define TT_NAME(_N) _TT_NAME(_N)
#ifdef THERMISTORHEATER_0 #ifdef THERMISTORHEATER_0
#define heater_0_temptable TT_NAME(THERMISTORHEATER_0) # define HEATER_0_TEMPTABLE TT_NAME(THERMISTORHEATER_0)
#define heater_0_temptable_len (sizeof(heater_0_temptable)/sizeof(*heater_0_temptable)) # define HEATER_0_TEMPTABLE_LEN (sizeof(HEATER_0_TEMPTABLE)/sizeof(*HEATER_0_TEMPTABLE))
#else #else
#ifdef HEATER_0_USES_THERMISTOR # ifdef HEATER_0_USES_THERMISTOR
#error No heater 0 thermistor table specified # error No heater 0 thermistor table specified
#else // HEATER_0_USES_THERMISTOR # else // HEATER_0_USES_THERMISTOR
#define heater_0_temptable 0 # define HEATER_0_TEMPTABLE NULL
#define heater_0_temptable_len 0 # define HEATER_0_TEMPTABLE_LEN 0
#endif // HEATER_0_USES_THERMISTOR # endif // HEATER_0_USES_THERMISTOR
#endif
//Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
#ifndef HEATER_0_RAW_HI_TEMP
# if HEATER_0_USES_THERMISTOR //In case of a thermistor the highest temperature results in the lowest ADC value
# define HEATER_0_RAW_HI_TEMP 0
# define HEATER_0_RAW_LO_TEMP 16383
# else //In case of an thermocouple the highest temperature results in the highest ADC value
# define HEATER_0_RAW_HI_TEMP 16383
# define HEATER_0_RAW_LO_TEMP 0
# endif
#endif #endif
#ifdef THERMISTORHEATER_1 #ifdef THERMISTORHEATER_1
#define heater_1_temptable TT_NAME(THERMISTORHEATER_1) # define HEATER_1_TEMPTABLE TT_NAME(THERMISTORHEATER_1)
#define heater_1_temptable_len (sizeof(heater_1_temptable)/sizeof(*heater_1_temptable)) # define HEATER_1_TEMPTABLE_LEN (sizeof(HEATER_1_TEMPTABLE)/sizeof(*HEATER_1_TEMPTABLE))
#else #else
#ifdef HEATER_1_USES_THERMISTOR # ifdef HEATER_1_USES_THERMISTOR
#error No heater 1 thermistor table specified # error No heater 1 thermistor table specified
#else // HEATER_1_USES_THERMISTOR # else // HEATER_1_USES_THERMISTOR
#define heater_1_temptable 0 # define HEATER_1_TEMPTABLE NULL
#define heater_1_temptable_len 0 # define HEATER_1_TEMPTABLE_LEN 0
#endif // HEATER_1_USES_THERMISTOR # endif // HEATER_1_USES_THERMISTOR
#endif
//Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
#ifndef HEATER_1_RAW_HI_TEMP
# if HEATER_1_USES_THERMISTOR //In case of a thermistor the highest temperature results in the lowest ADC value
# define HEATER_1_RAW_HI_TEMP 0
# define HEATER_1_RAW_LO_TEMP 16383
# else //In case of an thermocouple the highest temperature results in the highest ADC value
# define HEATER_1_RAW_HI_TEMP 16383
# define HEATER_1_RAW_LO_TEMP 0
# endif
#endif #endif
#ifdef THERMISTORHEATER_2 #ifdef THERMISTORHEATER_2
#define heater_2_temptable TT_NAME(THERMISTORHEATER_2) # define HEATER_2_TEMPTABLE TT_NAME(THERMISTORHEATER_2)
#define heater_2_temptable_len (sizeof(heater_2_temptable)/sizeof(*heater_2_temptable)) # define HEATER_2_TEMPTABLE_LEN (sizeof(HEATER_2_TEMPTABLE)/sizeof(*HEATER_2_TEMPTABLE))
#else #else
#ifdef HEATER_2_USES_THERMISTOR # ifdef HEATER_2_USES_THERMISTOR
#error No heater 2 thermistor table specified # error No heater 2 thermistor table specified
#else // HEATER_2_USES_THERMISTOR # else // HEATER_2_USES_THERMISTOR
#define heater_2_temptable 0 # define HEATER_2_TEMPTABLE NULL
#define heater_2_temptable_len 0 # define HEATER_2_TEMPTABLE_LEN 0
#endif // HEATER_2_USES_THERMISTOR # endif // HEATER_2_USES_THERMISTOR
#endif
//Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
#ifndef HEATER_2_RAW_HI_TEMP
# if HEATER_2_USES_THERMISTOR //In case of a thermistor the highest temperature results in the lowest ADC value
# define HEATER_2_RAW_HI_TEMP 0
# define HEATER_2_RAW_LO_TEMP 16383
# else //In case of an thermocouple the highest temperature results in the highest ADC value
# define HEATER_2_RAW_HI_TEMP 16383
# define HEATER_2_RAW_LO_TEMP 0
# endif
#endif #endif
#ifdef THERMISTORBED #ifdef THERMISTORBED
#define bedtemptable TT_NAME(THERMISTORBED) # define BEDTEMPTABLE TT_NAME(THERMISTORBED)
#define bedtemptable_len (sizeof(bedtemptable)/sizeof(*bedtemptable)) # define BEDTEMPTABLE_LEN (sizeof(BEDTEMPTABLE)/sizeof(*BEDTEMPTABLE))
#else #else
#ifdef BED_USES_THERMISTOR # ifdef BED_USES_THERMISTOR
#error No bed thermistor table specified # error No bed thermistor table specified
#endif // BED_USES_THERMISTOR # endif // BED_USES_THERMISTOR
#endif
//Set the high and low raw values for the heater, this indicates which raw value is a high or low temperature
#ifndef HEATER_BED_RAW_HI_TEMP
# if BED_USES_THERMISTOR //In case of a thermistor the highest temperature results in the lowest ADC value
# define HEATER_BED_RAW_HI_TEMP 0
# define HEATER_BED_RAW_LO_TEMP 16383
# else //In case of an thermocouple the highest temperature results in the highest ADC value
# define HEATER_BED_RAW_HI_TEMP 16383
# define HEATER_BED_RAW_LO_TEMP 0
# endif
#endif #endif
#endif //THERMISTORTABLES_H_ #endif //THERMISTORTABLES_H_