this is not working. Do you maybe know why?

This commit is contained in:
Bernhard Kubicek 2011-11-05 16:43:44 +01:00
parent 40e8081623
commit 8bcdb9f5f0
8 changed files with 268 additions and 182 deletions

View file

@ -48,9 +48,9 @@ void StoreSettings() {
EEPROM_writeAnything(i,max_xy_jerk); EEPROM_writeAnything(i,max_xy_jerk);
EEPROM_writeAnything(i,max_z_jerk); EEPROM_writeAnything(i,max_z_jerk);
#ifdef PIDTEMP #ifdef PIDTEMP
EEPROM_writeAnything(i,Kp); EEPROM_writeAnything(i,Heater::Kp);
EEPROM_writeAnything(i,Ki); EEPROM_writeAnything(i,Heater::Ki);
EEPROM_writeAnything(i,Kd); EEPROM_writeAnything(i,Heater::Kd);
#else #else
EEPROM_writeAnything(i,3000); EEPROM_writeAnything(i,3000);
EEPROM_writeAnything(i,0); EEPROM_writeAnything(i,0);
@ -81,11 +81,11 @@ void RetrieveSettings(bool def=false){ // if def=true, the default values will
EEPROM_readAnything(i,max_xy_jerk); EEPROM_readAnything(i,max_xy_jerk);
EEPROM_readAnything(i,max_z_jerk); EEPROM_readAnything(i,max_z_jerk);
#ifndef PIDTEMP #ifndef PIDTEMP
float Kp,Ki,Kd; float Kp,Ki,Kd; //read and ignore..
#endif #endif
EEPROM_readAnything(i,Kp); EEPROM_readAnything(i,Heater::Kp);
EEPROM_readAnything(i,Ki); EEPROM_readAnything(i,Heater::Ki);
EEPROM_readAnything(i,Kd); EEPROM_readAnything(i,Heater::Kd);
ECHOLN("Stored settings retreived:"); ECHOLN("Stored settings retreived:");
} }
@ -119,7 +119,7 @@ void RetrieveSettings(bool def=false){ // if def=true, the default values will
ECHOLN(" M205 S" <<_FLOAT(minimumfeedrate/60,2) << " T" << _FLOAT(mintravelfeedrate/60,2) << " B" << _FLOAT(minsegmenttime,2) << " X" << _FLOAT(max_xy_jerk/60,2) << " Z" << _FLOAT(max_z_jerk/60,2)); ECHOLN(" M205 S" <<_FLOAT(minimumfeedrate/60,2) << " T" << _FLOAT(mintravelfeedrate/60,2) << " B" << _FLOAT(minsegmenttime,2) << " X" << _FLOAT(max_xy_jerk/60,2) << " Z" << _FLOAT(max_z_jerk/60,2));
#ifdef PIDTEMP #ifdef PIDTEMP
ECHOLN("PID settings:"); ECHOLN("PID settings:");
ECHOLN(" M301 P" << _FLOAT(Kp,3) << " I" << _FLOAT(Ki,3) << " D" << _FLOAT(Kd,3)); ECHOLN(" M301 P" << _FLOAT(Heater::Kp,3) << " I" << _FLOAT(Heater::Ki,3) << " D" << _FLOAT(Heater::Kd,3));
#endif #endif
} }

View file

@ -10,6 +10,7 @@
#define ECHO(x) Serial << "echo: " << x; #define ECHO(x) Serial << "echo: " << x;
#define ECHOLN(x) Serial << "echo: "<<x<<endl; #define ECHOLN(x) Serial << "echo: "<<x<<endl;
void get_command(); void get_command();
void process_commands(); void process_commands();

View file

@ -40,6 +40,7 @@
#include "Simplelcd.h" #include "Simplelcd.h"
#endif #endif
Heater htr;
char version_string[] = "1.0.0 Alpha 1"; char version_string[] = "1.0.0 Alpha 1";
#ifdef SDSUPPORT #ifdef SDSUPPORT
@ -263,7 +264,7 @@ void setup()
#endif //SDSUPPORT #endif //SDSUPPORT
plan_init(); // Initialize planner; plan_init(); // Initialize planner;
st_init(); // Initialize stepper; st_init(); // Initialize stepper;
tp_init(); // Initialize temperature loop //tp_init(); // Initialize temperature loop is now done by the constructor of the Heater class
//checkautostart(); //checkautostart();
} }
@ -367,7 +368,7 @@ void loop()
bufindr = (bufindr + 1)%BUFSIZE; bufindr = (bufindr + 1)%BUFSIZE;
} }
//check heater every n milliseconds //check heater every n milliseconds
manage_heater(); Heater::manage_heater();
manage_inactivity(1); manage_inactivity(1);
LCD_STATUS; LCD_STATUS;
} }
@ -547,7 +548,7 @@ inline void process_commands()
if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
codenum += millis(); // keep track of when we started waiting codenum += millis(); // keep track of when we started waiting
while(millis() < codenum ){ while(millis() < codenum ){
manage_heater(); Heater::manage_heater();
} }
break; break;
case 28: //G28 Home all Axis one at a time case 28: //G28 Home all Axis one at a time
@ -801,12 +802,9 @@ inline void process_commands()
} }
break; break;
case 104: // M104 case 104: // M104
if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND] = temp2analog(code_value()); if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_HOTEND,code_value());
#ifdef PIDTEMP
pid_setpoint = code_value();
#endif //PIDTEM
#ifdef WATCHPERIOD #ifdef WATCHPERIOD
if(target_raw[TEMPSENSOR_HOTEND] > current_raw[TEMPSENSOR_HOTEND]){ if(Heater::isHeating(TEMPSENSOR_HOTEND)){
watchmillis = max(1,millis()); watchmillis = max(1,millis());
watch_raw[TEMPSENSOR_HOTEND] = current_raw[TEMPSENSOR_HOTEND]; watch_raw[TEMPSENSOR_HOTEND] = current_raw[TEMPSENSOR_HOTEND];
}else{ }else{
@ -815,14 +813,14 @@ inline void process_commands()
#endif #endif
break; break;
case 140: // M140 set bed temp case 140: // M140 set bed temp
if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analogBed(code_value()); if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_BED,code_value());
break; break;
case 105: // M105 case 105: // M105
#if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595) #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
tt = analog2temp(current_raw[TEMPSENSOR_HOTEND]); tt = Heater::celsius(TEMPSENSOR_HOTEND);
#endif #endif
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
bt = analog2tempBed(current_raw[TEMPSENSOR_BED]); bt = Heater::celsius(TEMPSENSOR_BED);
#endif #endif
#if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595) #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
Serial.print("ok T:"); Serial.print("ok T:");
@ -833,14 +831,14 @@ inline void process_commands()
#ifdef PIDTEMP #ifdef PIDTEMP
Serial.print(" B:"); Serial.print(" B:");
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
Serial.println(bt); Serial.println(bt);
#else #else
Serial.println(HeaterPower); Serial.println(Heater::HeaterPower);
#endif #endif
#else #else
Serial.println(); Serial.println();
#endif #endif
#else #else<
Serial.println(); Serial.println();
#endif #endif
#else #else
@ -850,14 +848,12 @@ inline void process_commands()
//break; //break;
case 109: {// M109 - Wait for extruder heater to reach target. case 109: {// M109 - Wait for extruder heater to reach target.
LCD_MESSAGE("Heating..."); LCD_MESSAGE("Heating...");
if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND] = temp2analog(code_value()); if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_HOTEND,code_value());
#ifdef PIDTEMP
pid_setpoint = code_value();
#endif //PIDTEM
#ifdef WATCHPERIOD #ifdef WATCHPERIOD
if(target_raw[TEMPSENSOR_HOTEND]>current_raw[TEMPSENSOR_HOTEND]){ if(Heater::isHeating(TEMPSENSOR_HOTEND)){
watchmillis = max(1,millis()); watchmillis = max(1,millis());
watch_raw[TEMPSENSOR_HOTEND] = current_raw[TEMPSENSOR_HOTEND]; watch_raw[TEMPSENSOR_HOTEND] = Heater::current_raw[TEMPSENSOR_HOTEND];
} else { } else {
watchmillis = 0; watchmillis = 0;
} }
@ -865,31 +861,31 @@ inline void process_commands()
codenum = millis(); codenum = millis();
/* See if we are heating up or cooling down */ /* See if we are heating up or cooling down */
bool target_direction = (current_raw[0] < target_raw[0]); // true if heating, false if cooling bool target_direction = Heater::isHeating(TEMPSENSOR_HOTEND); // true if heating, false if cooling
#ifdef TEMP_RESIDENCY_TIME #ifdef TEMP_RESIDENCY_TIME
long residencyStart; long residencyStart;
residencyStart = -1; residencyStart = -1;
/* continue to loop until we have reached the target temp /* continue to loop until we have reached the target temp
_and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */ _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
while((target_direction ? (current_raw[0] < target_raw[0]) : (current_raw[0] > target_raw[0])) || while((target_direction ? Heater::isHeating(TEMPSENSOR_HOTEND) : Heater::isCooling(TEMPSENSOR_HOTEND)) ||
(residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) { (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
#else #else
while ( target_direction ? (current_raw[0] < target_raw[0]) : (current_raw[0] > target_raw[0]) ) { while ( target_direction ? Heater::isHeating(TEMPSENSOR_HOTEND) : Heater::isCooling(TEMPSENSOR_HOTEND) ) {
#endif //TEMP_RESIDENCY_TIME #endif //TEMP_RESIDENCY_TIME
if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up/cooling down if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up/cooling down
Serial.print("T:"); Serial.print("T:");
Serial.println( analog2temp(current_raw[TEMPSENSOR_HOTEND]) ); Serial.println( Heater::celsius(TEMPSENSOR_HOTEND) );
codenum = millis(); codenum = millis();
} }
manage_heater(); Heater::manage_heater();
LCD_STATUS; LCD_STATUS;
#ifdef TEMP_RESIDENCY_TIME #ifdef TEMP_RESIDENCY_TIME
/* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
or when current temp falls outside the hysteresis after target temp was reached */ or when current temp falls outside the hysteresis after target temp was reached */
if ((residencyStart == -1 && target_direction && current_raw[0] >= target_raw[0]) || if ((residencyStart == -1 && target_direction && !Heater::isHeating(TEMPSENSOR_HOTEND)) ||
(residencyStart == -1 && !target_direction && current_raw[0] <= target_raw[0]) || (residencyStart == -1 && !target_direction && !Heater::isCooling(TEMPSENSOR_HOTEND)) ||
(residencyStart > -1 && labs(analog2temp(current_raw[0]) - analog2temp(target_raw[0])) > TEMP_HYSTERESIS) ) { (residencyStart > -1 && labs(Heater::celsius(TEMPSENSOR_HOTEND) - Heater::celsiusTarget(TEMPSENSOR_HOTEND)) > TEMP_HYSTERESIS) ) {
residencyStart = millis(); residencyStart = millis();
} }
#endif //TEMP_RESIDENCY_TIME #endif //TEMP_RESIDENCY_TIME
@ -899,22 +895,22 @@ inline void process_commands()
break; break;
case 190: // M190 - Wait bed for heater to reach target. case 190: // M190 - Wait bed for heater to reach target.
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analog(code_value()); if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_BED,code_value());
codenum = millis(); codenum = millis();
while(current_raw[TEMPSENSOR_BED] < target_raw[TEMPSENSOR_BED]) while(Heater::isHeating(TEMPSENSOR_BED))
{ {
if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up. if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
{ {
float tt=analog2temp(current_raw[TEMPSENSOR_HOTEND]); float tt=Heater::celsius(TEMPSENSOR_HOTEND);
Serial.print("T:"); Serial.print("T:");
Serial.println( tt ); Serial.println( tt );
Serial.print("ok T:"); Serial.print("ok T:");
Serial.print( tt ); Serial.print( tt );
Serial.print(" B:"); Serial.print(" B:");
Serial.println( analog2temp(current_raw[TEMPSENSOR_BED]) ); Serial.println( Heater::celsius(TEMPSENSOR_BED) );
codenum = millis(); codenum = millis();
} }
manage_heater(); Heater::manage_heater();
} }
#endif #endif
break; break;
@ -1066,9 +1062,13 @@ inline void process_commands()
break; break;
#ifdef PIDTEMP #ifdef PIDTEMP
case 301: // M301 case 301: // M301
if(code_seen('P')) Kp = code_value(); if(code_seen('P')) Heater::Kp = code_value();
if(code_seen('I')) Ki = code_value()*PID_dT; if(code_seen('I')) Heater::Ki = code_value()*PID_dT;
if(code_seen('D')) Kd = code_value()/PID_dT; if(code_seen('D')) Heater::Kd = code_value()/PID_dT;
#ifdef PID_ADD_EXTRUSION_RATE
if(code_seen('C')) Heater::Kc = code_value();
#endif
// ECHOLN("Kp "<<_FLOAT(Kp,2)); // ECHOLN("Kp "<<_FLOAT(Kp,2));
// ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2)); // ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2));
// ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2)); // ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2));
@ -1194,19 +1194,19 @@ void wd_reset() {
inline void kill() inline void kill()
{ {
#if TEMP_0_PIN > -1 #if TEMP_0_PIN > -1
target_raw[0]=0; Heater::setCelsius(TEMPSENSOR_HOTEND,0);
#if HEATER_0_PIN > -1 #if HEATER_0_PIN > -1
WRITE(HEATER_0_PIN,LOW); WRITE(HEATER_0_PIN,LOW);
#endif #endif
#endif #endif
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
target_raw[1]=0; Heater::setCelsius(TEMPSENSOR_BED,0);
#if HEATER_1_PIN > -1 #if HEATER_1_PIN > -1
WRITE(HEATER_1_PIN,LOW); WRITE(HEATER_1_PIN,LOW);
#endif #endif
#endif #endif
#if TEMP_2_PIN > -1 #if TEMP_2_PIN > -1
target_raw[2]=0; Heater::setCelsius(TEMPSENSOR_AUX,0);
#if HEATER_2_PIN > -1 #if HEATER_2_PIN > -1
WRITE(HEATER_2_PIN,LOW); WRITE(HEATER_2_PIN,LOW);
#endif #endif

View file

@ -388,7 +388,7 @@ void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {
// If the buffer is full: good! That means we are well ahead of the robot. // If the buffer is full: good! That means we are well ahead of the robot.
// Rest here until there is room in the buffer. // Rest here until there is room in the buffer.
while(block_buffer_tail == next_buffer_head) { while(block_buffer_tail == next_buffer_head) {
manage_heater(); htr.manage_heater();
manage_inactivity(1); manage_inactivity(1);
LCD_STATUS; LCD_STATUS;
} }

View file

@ -585,7 +585,7 @@ void st_init()
void st_synchronize() void st_synchronize()
{ {
while(plan_get_current_block()) { while(plan_get_current_block()) {
manage_heater(); htr.manage_heater();
manage_inactivity(1); manage_inactivity(1);
LCD_STATUS; LCD_STATUS;
} }

View file

@ -37,52 +37,11 @@
#include "streaming.h" #include "streaming.h"
#include "temperature.h" #include "temperature.h"
int target_bed_raw = 0;
int current_bed_raw = 0;
int target_raw[3] = {0, 0, 0};
int current_raw[3] = {0, 0, 0};
unsigned char temp_meas_ready = false;
unsigned long previous_millis_heater, previous_millis_bed_heater;
#ifdef PIDTEMP
double temp_iState = 0;
double temp_dState = 0;
double pTerm;
double iTerm;
double dTerm;
//int output;
double pid_error;
double temp_iState_min;
double temp_iState_max;
double pid_setpoint = 0.0;
double pid_input;
double pid_output;
bool pid_reset;
float HeaterPower;
float Kp=DEFAULT_Kp; void static Heater::manage_heater()
float Ki=DEFAULT_Ki;
float Kd=DEFAULT_Kd;
float Kc=DEFAULT_Kc;
#endif //PIDTEMP
#ifdef MINTEMP
int minttemp = temp2analog(MINTEMP);
#endif //MINTEMP
#ifdef MAXTEMP
int maxttemp = temp2analog(MAXTEMP);
#endif //MAXTEMP
#ifdef BED_MINTEMP
int bed_minttemp = temp2analog(BED_MINTEMP);
#endif //BED_MINTEMP
#ifdef BED_MAXTEMP
int bed_maxttemp = temp2analog(BED_MAXTEMP);
#endif //BED_MAXTEMP
void manage_heater()
{ {
#ifdef USE_WATCHDOG #ifdef USE_WATCHDOG
wd_reset(); wd_reset();
@ -90,11 +49,11 @@ void manage_heater()
float pid_input; float pid_input;
float pid_output; float pid_output;
if(temp_meas_ready != true) //better readability if(htr.temp_meas_ready != true) //better readability
return; return;
CRITICAL_SECTION_START; CRITICAL_SECTION_START;
temp_meas_ready = false; htr.temp_meas_ready = false;
CRITICAL_SECTION_END; CRITICAL_SECTION_END;
#ifdef PIDTEMP #ifdef PIDTEMP
@ -176,7 +135,8 @@ CRITICAL_SECTION_END;
// For a thermistor, it uses the RepRap thermistor temp table. // 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 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. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
float temp2analog(int celsius) { float const static temp2analog(const int celsius)
{
#ifdef HEATER_USES_THERMISTOR_1 #ifdef HEATER_USES_THERMISTOR_1
int raw = 0; int raw = 0;
byte i; byte i;
@ -207,7 +167,8 @@ float temp2analog(int celsius) {
// For a thermistor, it uses the RepRap thermistor temp table. // 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 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. // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.
float temp2analogBed(int celsius) { float const static temp2analogBed(const int celsius)
{
#ifdef BED_USES_THERMISTOR #ifdef BED_USES_THERMISTOR
int raw = 0; int raw = 0;
@ -237,7 +198,7 @@ float temp2analogBed(int celsius) {
// 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) { float const static Heater::analog2temp(const int raw) {
#ifdef HEATER_1_USES_THERMISTOR #ifdef HEATER_1_USES_THERMISTOR
int celsius = 0; int celsius = 0;
byte i; byte i;
@ -266,7 +227,7 @@ float analog2temp(int raw) {
// Derived from RepRap FiveD extruder::getTemperature() // Derived from RepRap FiveD extruder::getTemperature()
// For bed temperature measurement. // For bed temperature measurement.
float analog2tempBed(int raw) { float const static Heater::analog2tempBed(const int raw) {
#ifdef BED_USES_THERMISTOR #ifdef BED_USES_THERMISTOR
int celsius = 0; int celsius = 0;
byte i; byte i;
@ -296,8 +257,28 @@ float analog2tempBed(int raw) {
#endif #endif
} }
void tp_init() Heater::Heater()
{ {
for(short i=0;i<3;i++)
{
target_raw[i]=0;
current_raw[i] =0;
}
htr.temp_meas_ready = false;
#ifdef MINTEMP
minttemp = temp2analog(MINTEMP);
#endif //MINTEMP
#ifdef MAXTEMP
maxttemp = temp2analog(MAXTEMP);
#endif //MAXTEMP
#ifdef BED_MINTEMP
bed_minttemp = temp2analog(BED_MINTEMP);
#endif //BED_MINTEMP
#ifdef BED_MAXTEMP
bed_maxttemp = temp2analog(BED_MAXTEMP);
#endif //BED_MAXTEMP
#if (HEATER_0_PIN > -1) #if (HEATER_0_PIN > -1)
SET_OUTPUT(HEATER_0_PIN); SET_OUTPUT(HEATER_0_PIN);
#endif #endif
@ -311,6 +292,14 @@ void tp_init()
#ifdef PIDTEMP #ifdef PIDTEMP
temp_iState_min = 0.0; temp_iState_min = 0.0;
temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki; temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;
temp_iState = 0;
temp_dState = 0;
Kp=DEFAULT_Kp;
Ki=DEFAULT_Ki;
Kd=DEFAULT_Kd;
Kc=DEFAULT_Kc;
pid_setpoint = 0.0;
#endif //PIDTEMP #endif //PIDTEMP
// Set analog inputs // Set analog inputs
@ -407,39 +396,39 @@ ISR(TIMER0_COMPB_vect)
if(temp_count >= 16) // 6 ms * 16 = 96ms. if(temp_count >= 16) // 6 ms * 16 = 96ms.
{ {
#ifdef HEATER_1_USES_AD595 #ifdef HEATER_1_USES_AD595
current_raw[0] = raw_temp_0_value; htr.current_raw[0] = raw_temp_0_value;
#else #else
current_raw[0] = 16383 - raw_temp_0_value; htr.current_raw[0] = 16383 - raw_temp_0_value;
#endif #endif
#ifdef HEATER_2_USES_AD595 #ifdef HEATER_2_USES_AD595
current_raw[2] = raw_temp_2_value; htr.current_raw[2] = raw_temp_2_value;
#else #else
current_raw[2] = 16383 - raw_temp_2_value; htr.current_raw[2] = 16383 - raw_temp_2_value;
#endif #endif
#ifdef BED_USES_AD595 #ifdef BED_USES_AD595
current_raw[1] = raw_temp_1_value; htr.current_raw[1] = raw_temp_1_value;
#else #else
current_raw[1] = 16383 - raw_temp_1_value; htr.current_raw[1] = 16383 - raw_temp_1_value;
#endif #endif
temp_meas_ready = true; htr.temp_meas_ready = true;
temp_count = 0; temp_count = 0;
raw_temp_0_value = 0; raw_temp_0_value = 0;
raw_temp_1_value = 0; raw_temp_1_value = 0;
raw_temp_2_value = 0; raw_temp_2_value = 0;
#ifdef MAXTEMP #ifdef MAXTEMP
#if (HEATER_0_PIN > -1) #if (HEATER_0_PIN > -1)
if(current_raw[TEMPSENSOR_HOTEND] >= maxttemp) { if(htr.current_raw[TEMPSENSOR_HOTEND] >= htr.maxttemp) {
target_raw[TEMPSENSOR_HOTEND] = 0; htr.target_raw[TEMPSENSOR_HOTEND] = 0;
analogWrite(HEATER_0_PIN, 0); analogWrite(HEATER_0_PIN, 0);
Serial.println("!! Temperature extruder 0 switched off. MAXTEMP triggered !!"); Serial.println("!! Temperature extruder 0 switched off. MAXTEMP triggered !!");
} }
#endif #endif
#if (HEATER_2_PIN > -1) #if (HEATER_2_PIN > -1)
if(current_raw[TEMPSENSOR_AUX] >= maxttemp) { if(htr.current_raw[TEMPSENSOR_AUX] >= htr.maxttemp) {
target_raw[TEMPSENSOR_AUX] = 0; htr.target_raw[TEMPSENSOR_AUX] = 0;
analogWrite(HEATER_2_PIN, 0); analogWrite(HEATER_2_PIN, 0);
Serial.println("!! Temperature extruder 1 switched off. MAXTEMP triggered !!"); Serial.println("!! Temperature extruder 1 switched off. MAXTEMP triggered !!");
} }
@ -447,15 +436,15 @@ ISR(TIMER0_COMPB_vect)
#endif //MAXTEMP #endif //MAXTEMP
#ifdef MINTEMP #ifdef MINTEMP
#if (HEATER_0_PIN > -1) #if (HEATER_0_PIN > -1)
if(current_raw[TEMPSENSOR_HOTEND] <= minttemp) { if(htr.current_raw[TEMPSENSOR_HOTEND] <= htr.minttemp) {
target_raw[TEMPSENSOR_HOTEND] = 0; htr.target_raw[TEMPSENSOR_HOTEND] = 0;
analogWrite(HEATER_0_PIN, 0); analogWrite(HEATER_0_PIN, 0);
Serial.println("!! Temperature extruder 0 switched off. MINTEMP triggered !!"); Serial.println("!! Temperature extruder 0 switched off. MINTEMP triggered !!");
} }
#endif #endif
#if (HEATER_2_PIN > -1) #if (HEATER_2_PIN > -1)
if(current_raw[TEMPSENSOR_AUX] <= minttemp) { if(htr.current_raw[TEMPSENSOR_AUX] <= htr.minttemp) {
target_raw[TEMPSENSOR_AUX] = 0; htr.target_raw[TEMPSENSOR_AUX] = 0;
analogWrite(HEATER_2_PIN, 0); analogWrite(HEATER_2_PIN, 0);
Serial.println("!! Temperature extruder 1 switched off. MINTEMP triggered !!"); Serial.println("!! Temperature extruder 1 switched off. MINTEMP triggered !!");
} }
@ -463,8 +452,8 @@ ISR(TIMER0_COMPB_vect)
#endif //MAXTEMP #endif //MAXTEMP
#ifdef BED_MINTEMP #ifdef BED_MINTEMP
#if (HEATER_1_PIN > -1) #if (HEATER_1_PIN > -1)
if(current_raw[1] <= bed_minttemp) { if(htr.current_raw[1] <= htr.bed_minttemp) {
target_raw[1] = 0; htr.target_raw[1] = 0;
WRITE(HEATER_1_PIN, 0); WRITE(HEATER_1_PIN, 0);
Serial.println("!! Temperatur heated bed switched off. MINTEMP triggered !!"); Serial.println("!! Temperatur heated bed switched off. MINTEMP triggered !!");
} }
@ -472,8 +461,8 @@ ISR(TIMER0_COMPB_vect)
#endif #endif
#ifdef BED_MAXTEMP #ifdef BED_MAXTEMP
#if (HEATER_1_PIN > -1) #if (HEATER_1_PIN > -1)
if(current_raw[1] >= bed_maxttemp) { if(htr.current_raw[1] >= htr.bed_maxttemp) {
target_raw[1] = 0; htr.target_raw[1] = 0;
WRITE(HEATER_1_PIN, 0); WRITE(HEATER_1_PIN, 0);
Serial.println("!! Temperature heated bed switched off. MAXTEMP triggered !!"); Serial.println("!! Temperature heated bed switched off. MAXTEMP triggered !!");
} }
@ -481,3 +470,6 @@ ISR(TIMER0_COMPB_vect)
#endif #endif
} }
} }
//Heater htr;

View file

@ -25,14 +25,116 @@
#ifdef PID_ADD_EXTRUSION_RATE #ifdef PID_ADD_EXTRUSION_RATE
#include "stepper.h" #include "stepper.h"
#endif #endif
void tp_init();
void manage_heater(); enum TempSensor {TEMPSENSOR_HOTEND=0,TEMPSENSOR_BED=1, TEMPSENSOR_AUX=2};
//int temp2analogu(int celsius, const short table[][2], int numtemps);
//float analog2tempu(int raw, const short table[][2], int numtemps); // ther must be only one instance of this class, and it is created in temperature.cpp by itself and is called "htr".
float temp2analog(int celsius); // all the variables are static, so that of the compiler optimization is more easy.
float temp2analogBed(int celsius); // I honestly hope that this increases readability and structure.
float analog2temp(int raw); // none of the variables or routines should be called from an secondary process/interrupt with the exceptino of current_raw[].
float analog2tempBed(int raw);
class Heater
{
public:
Heater(); //treplaces tp_init();
~Heater();
void static manage_heater(); /// it is critical that this is called continously.
// conversion routines, const since they don't change any class variables.
float const static temp2analog(const int celsius);
float const static temp2analogBed(const int celsius);
float const static analog2temp(const int raw);
float const static analog2tempBed(const int raw);
inline float const static celsius(const TempSensor s)
{
if(s==TEMPSENSOR_BED)
return analog2tempBed(Heater::current_raw[s]);
else
return analog2temp(Heater::current_raw[s]);
};
inline float const static celsiusTarget(const TempSensor s)
{
if(s==TEMPSENSOR_BED)
return analog2tempBed(Heater::target_raw[s]);
else
return analog2temp(Heater::target_raw[s]);
};
inline float static setCelsius(const TempSensor s, const int celsius)
{
#ifdef PIDTEMP
if(s==TEMPSENSOR_HOTEND)
Heater::pid_setpoint = celsius;
#endif //PIDTEM
if(s==TEMPSENSOR_BED)
Heater::target_raw[s] = temp2analog(celsius);
else
Heater::target_raw[s] = temp2analogBed(celsius);
};
inline bool const static isHeating(TempSensor s)
{ return (Heater::target_raw[s]>Heater::current_raw[s]);};
inline bool const static isCooling(TempSensor s)
{ return (Heater::target_raw[s]<Heater::current_raw[s]);};
public:
#ifdef PIDTEMP
static float Kp;
static float Ki;
static float Kd;
static float Kc;
#endif
static int target_raw[3];
static float pid_setpoint;
volatile static int current_raw[3]; //this are written by an ISR, so volatile.
volatile static bool temp_meas_ready ; //also this is set by the ISR
private:
static unsigned long previous_millis_heater, previous_millis_bed_heater;
#ifdef PIDTEMP
static float temp_iState;
static float temp_dState;
static float pTerm;
static float iTerm;
static float dTerm;
//int output;
static float pid_error;
static float temp_iState_min;
static float temp_iState_max;
static float pid_input;
static float pid_output;
static bool pid_reset;
static float HeaterPower;
#endif //PIDTEMP
public: //but only accesed from the ISR hence not volatile
#ifdef MINTEMP
static int minttemp;
#endif //MINTEMP
#ifdef MAXTEMP
static int maxttemp;
#endif //MAXTEMP
#ifdef BED_MINTEMP
static int bed_minttemp ;
#endif //BED_MINTEMP
#ifdef BED_MAXTEMP
static int bed_maxttemp;
#endif //BED_MAXTEMP
};
extern Heater htr; //this creates the single, global instance
#ifdef HEATER_USES_THERMISTOR #ifdef HEATER_USES_THERMISTOR
#define HEATERSOURCE 1 #define HEATERSOURCE 1
@ -41,18 +143,5 @@ float analog2tempBed(int raw);
#define BEDSOURCE 1 #define BEDSOURCE 1
#endif #endif
//#define temp2analogh( c ) temp2analogu((c),temptable,NUMTEMPS)
//#define analog2temp( c ) analog2tempu((c),temptable,NUMTEMPS
extern float Kp;
extern float Ki;
extern float Kd;
extern float Kc;
enum {TEMPSENSOR_HOTEND=0,TEMPSENSOR_BED=1, TEMPSENSOR_AUX=2};
extern int target_raw[3];
extern int current_raw[3];
extern double pid_setpoint;
#endif #endif

View file

@ -1,5 +1,5 @@
#include "ultralcd.h" #include "ultralcd.h"
#include "temperature.h"
#ifdef ULTRA_LCD #ifdef ULTRA_LCD
extern volatile int feedmultiply; extern volatile int feedmultiply;
@ -238,8 +238,8 @@ extern volatile bool feedmultiplychanged;
void MainMenu::showStatus() void MainMenu::showStatus()
{ {
#if LCD_HEIGHT==4 #if LCD_HEIGHT==4
static int oldcurrentraw=-1; static int oldcurrent=-1;
static int oldtargetraw=-1; static int oldtarget=-1;
//force_lcd_update=true; //force_lcd_update=true;
if(force_lcd_update||feedmultiplychanged) //initial display of content if(force_lcd_update||feedmultiplychanged) //initial display of content
{ {
@ -252,33 +252,36 @@ void MainMenu::showStatus()
#endif #endif
} }
int tt=Heater::celsius(TEMPSENSOR_HOTEND);
if((abs(current_raw[TEMPSENSOR_HOTEND]-oldcurrentraw)>3)||force_lcd_update) if((abs(tt-oldcurrent)>1)||force_lcd_update)
{ {
lcd.setCursor(1,0); lcd.setCursor(1,0);
lcd.print(ftostr3(analog2temp(current_raw[TEMPSENSOR_HOTEND]))); lcd.print(ftostr3(tt));
oldcurrentraw=current_raw[TEMPSENSOR_HOTEND]; oldcurrent=tt;
} }
if((target_raw[TEMPSENSOR_HOTEND]!=oldtargetraw)||force_lcd_update) int ttg=Heater::celsiusTarget(TEMPSENSOR_HOTEND);
if((ttg!=oldtarget)||force_lcd_update)
{ {
lcd.setCursor(5,0); lcd.setCursor(5,0);
lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND]))); lcd.print(ftostr3(ttg));
oldtargetraw=target_raw[TEMPSENSOR_HOTEND]; oldtarget=ttg;
} }
#if defined BED_USES_THERMISTOR || defined BED_USES_AD595 #if defined BED_USES_THERMISTOR || defined BED_USES_AD595
static int oldcurrentbedraw=-1; static int oldcurrentbed=-1;
static int oldtargetbedraw=-1; static int oldtargetbed=-1;
if((current_bed_raw!=oldcurrentbedraw)||force_lcd_update) int tb=Heater::celsius(TEMPSENSOR_BED);
if((tb!=oldcurrentbed)||force_lcd_update)
{ {
lcd.setCursor(1,0); lcd.setCursor(1,0);
lcd.print(ftostr3(analog2temp(current_bed_raw))); lcd.print(ftostr3(tb));
oldcurrentraw=current_raw[TEMPSENSOR_BED]; oldcurrentbed=tb;
} }
if((target_bed_raw!=oldtargebedtraw)||force_lcd_update) int tg=Heater::celsiusTarget(TEMPSENSOR_BED);
if((tg!=oldtargebed)||force_lcd_update)
{ {
lcd.setCursor(5,0); lcd.setCursor(5,0);
lcd.print(ftostr3(analog2temp(target_bed_raw))); lcd.print(Heater::celsiusTarget(TEMPSENSOR_BED));
oldtargetraw=target_bed_raw; oldtargebed=tg;
} }
#endif #endif
//starttime=2; //starttime=2;
@ -327,8 +330,8 @@ void MainMenu::showStatus()
messagetext[0]='\0'; messagetext[0]='\0';
} }
#else //smaller LCDS---------------------------------- #else //smaller LCDS----------------------------------
static int oldcurrentraw=-1; static int oldcurrent=-1;
static int oldtargetraw=-1; static int oldtarget=-1;
if(force_lcd_update) //initial display of content if(force_lcd_update) //initial display of content
{ {
encoderpos=feedmultiply; encoderpos=feedmultiply;
@ -338,18 +341,19 @@ void MainMenu::showStatus()
#endif #endif
} }
int tt=Heater::celsius(TEMPSENSOR_HOTEND);
if((abs(current_raw[TEMPSENSOR_HOTEND]-oldcurrentraw)>3)||force_lcd_update) if((abs(tt-oldcurrent)>1)||force_lcd_update)
{ {
lcd.setCursor(1,0); lcd.setCursor(1,0);
lcd.print(ftostr3(analog2temp(current_raw[TEMPSENSOR_HOTEND]))); lcd.print(ftostr3(tt));
oldcurrentraw=current_raw[TEMPSENSOR_HOTEND]; oldcurrent=tt;
} }
if((target_raw[TEMPSENSOR_HOTEND]!=oldtargetraw)||force_lcd_update) int ttg=Heater::celsiusTarget(TEMPSENSOR_HOTEND);
if((ttg!=oldtarget)||force_lcd_update)
{ {
lcd.setCursor(5,0); lcd.setCursor(5,0);
lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND]))); lcd.print(ftostr3(ttg));
oldtargetraw=target_raw[TEMPSENSOR_HOTEND]; oldtarge=ttg;
} }
if(messagetext[0]!='\0') if(messagetext[0]!='\0')
@ -426,7 +430,7 @@ void MainMenu::showPrepare()
if((activeline==line) && CLICKED) if((activeline==line) && CLICKED)
{ {
BLOCK BLOCK
target_raw[TEMPSENSOR_HOTEND] = temp2analog(170); Heater::setCelsius(TEMPSENSOR_HOTEND, 170);
beepshort(); beepshort();
} }
}break; }break;
@ -531,7 +535,7 @@ void MainMenu::showControl()
if(force_lcd_update) if(force_lcd_update)
{ {
lcd.setCursor(0,line);lcd.print(" \002Nozzle:"); lcd.setCursor(0,line);lcd.print(" \002Nozzle:");
lcd.setCursor(13,line);lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND]))); lcd.setCursor(13,line);lcd.print(ftostr3(Heater::celsiusTarget(TEMPSENSOR_HOTEND)));
} }
if((activeline==line) ) if((activeline==line) )
@ -541,11 +545,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)analog2temp(target_raw[TEMPSENSOR_HOTEND]); encoderpos=(int)Heater::celsiusTarget(TEMPSENSOR_HOTEND);
} }
else else
{ {
target_raw[TEMPSENSOR_HOTEND] = temp2analog(encoderpos); Heater::setCelsius(TEMPSENSOR_HOTEND,encoderpos);
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
beepshort(); beepshort();
} }
@ -669,7 +673,7 @@ void MainMenu::showControl()
if(force_lcd_update) if(force_lcd_update)
{ {
lcd.setCursor(0,line);lcd.print(" PID-P: "); lcd.setCursor(0,line);lcd.print(" PID-P: ");
lcd.setCursor(13,line);lcd.print(itostr4(Kp)); lcd.setCursor(13,line);lcd.print(itostr4(Heater::Kp));
} }
if((activeline==line) ) if((activeline==line) )
@ -679,11 +683,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)Kp/5; encoderpos=(int)Heater::Kp/5;
} }
else else
{ {
Kp= encoderpos*5; Heater::Kp= encoderpos*5;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }
@ -703,7 +707,7 @@ void MainMenu::showControl()
if(force_lcd_update) if(force_lcd_update)
{ {
lcd.setCursor(0,line);lcd.print(" PID-I: "); lcd.setCursor(0,line);lcd.print(" PID-I: ");
lcd.setCursor(13,line);lcd.print(ftostr51(Ki)); lcd.setCursor(13,line);lcd.print(ftostr51(Heater::Ki));
} }
if((activeline==line) ) if((activeline==line) )
@ -713,11 +717,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)(Ki*10); encoderpos=(int)(Heater::Ki*10);
} }
else else
{ {
Ki= encoderpos/10.; Heater::Ki= encoderpos/10.;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }
@ -737,7 +741,7 @@ void MainMenu::showControl()
if(force_lcd_update) if(force_lcd_update)
{ {
lcd.setCursor(0,line);lcd.print(" PID-D: "); lcd.setCursor(0,line);lcd.print(" PID-D: ");
lcd.setCursor(13,line);lcd.print(itostr4(Kd)); lcd.setCursor(13,line);lcd.print(itostr4(Heater::Kd));
} }
if((activeline==line) ) if((activeline==line) )
@ -747,11 +751,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)Kd/5; encoderpos=(int)(Heater::Kd/5.);
} }
else else
{ {
Kd= encoderpos*5; Heater::Kd= encoderpos*5;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }
@ -774,7 +778,7 @@ void MainMenu::showControl()
if(force_lcd_update) if(force_lcd_update)
{ {
lcd.setCursor(0,line);lcd.print(" PID-C: "); lcd.setCursor(0,line);lcd.print(" PID-C: ");
lcd.setCursor(13,line);lcd.print(itostr3(Kc)); lcd.setCursor(13,line);lcd.print(itostr3(Heater::Kc));
} }
if((activeline==line) ) if((activeline==line) )
@ -784,11 +788,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)Kc; encoderpos=(int)Heater::Kc;
} }
else else
{ {
Kc= encoderpos; Heater::Kc= encoderpos;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }