Revert "this is not working. Do you maybe know why?"

This reverts commit 8bcdb9f5f0.
This commit is contained in:
Bernhard Kubicek 2011-11-05 16:49:29 +01:00
parent 8bcdb9f5f0
commit b9ea0651ec
8 changed files with 182 additions and 268 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,Heater::Kp); EEPROM_writeAnything(i,Kp);
EEPROM_writeAnything(i,Heater::Ki); EEPROM_writeAnything(i,Ki);
EEPROM_writeAnything(i,Heater::Kd); EEPROM_writeAnything(i,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; //read and ignore.. float Kp,Ki,Kd;
#endif #endif
EEPROM_readAnything(i,Heater::Kp); EEPROM_readAnything(i,Kp);
EEPROM_readAnything(i,Heater::Ki); EEPROM_readAnything(i,Ki);
EEPROM_readAnything(i,Heater::Kd); EEPROM_readAnything(i,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(Heater::Kp,3) << " I" << _FLOAT(Heater::Ki,3) << " D" << _FLOAT(Heater::Kd,3)); ECHOLN(" M301 P" << _FLOAT(Kp,3) << " I" << _FLOAT(Ki,3) << " D" << _FLOAT(Kd,3));
#endif #endif
} }

View File

@ -10,7 +10,6 @@
#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,7 +40,6 @@
#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
@ -264,7 +263,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 is now done by the constructor of the Heater class tp_init(); // Initialize temperature loop
//checkautostart(); //checkautostart();
} }
@ -368,7 +367,7 @@ void loop()
bufindr = (bufindr + 1)%BUFSIZE; bufindr = (bufindr + 1)%BUFSIZE;
} }
//check heater every n milliseconds //check heater every n milliseconds
Heater::manage_heater(); manage_heater();
manage_inactivity(1); manage_inactivity(1);
LCD_STATUS; LCD_STATUS;
} }
@ -548,7 +547,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 ){
Heater::manage_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
@ -802,9 +801,12 @@ inline void process_commands()
} }
break; break;
case 104: // M104 case 104: // M104
if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_HOTEND,code_value()); if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND] = temp2analog(code_value());
#ifdef PIDTEMP
pid_setpoint = code_value();
#endif //PIDTEM
#ifdef WATCHPERIOD #ifdef WATCHPERIOD
if(Heater::isHeating(TEMPSENSOR_HOTEND)){ if(target_raw[TEMPSENSOR_HOTEND] > current_raw[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{
@ -813,14 +815,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')) Heater::setCelsius(TEMPSENSOR_BED,code_value()); if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analogBed(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 = Heater::celsius(TEMPSENSOR_HOTEND); tt = analog2temp(current_raw[TEMPSENSOR_HOTEND]);
#endif #endif
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
bt = Heater::celsius(TEMPSENSOR_BED); bt = analog2tempBed(current_raw[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,12 +835,12 @@ inline void process_commands()
#if TEMP_1_PIN > -1 #if TEMP_1_PIN > -1
Serial.println(bt); Serial.println(bt);
#else #else
Serial.println(Heater::HeaterPower); Serial.println(HeaterPower);
#endif #endif
#else #else
Serial.println(); Serial.println();
#endif #endif
#else< #else
Serial.println(); Serial.println();
#endif #endif
#else #else
@ -848,12 +850,14 @@ 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')) Heater::setCelsius(TEMPSENSOR_HOTEND,code_value()); if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND] = temp2analog(code_value());
#ifdef PIDTEMP
pid_setpoint = code_value();
#endif //PIDTEM
#ifdef WATCHPERIOD #ifdef WATCHPERIOD
if(Heater::isHeating(TEMPSENSOR_HOTEND)){ if(target_raw[TEMPSENSOR_HOTEND]>current_raw[TEMPSENSOR_HOTEND]){
watchmillis = max(1,millis()); watchmillis = max(1,millis());
watch_raw[TEMPSENSOR_HOTEND] = Heater::current_raw[TEMPSENSOR_HOTEND]; watch_raw[TEMPSENSOR_HOTEND] = current_raw[TEMPSENSOR_HOTEND];
} else { } else {
watchmillis = 0; watchmillis = 0;
} }
@ -861,31 +865,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 = Heater::isHeating(TEMPSENSOR_HOTEND); // true if heating, false if cooling bool target_direction = (current_raw[0] < target_raw[0]); // 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 ? Heater::isHeating(TEMPSENSOR_HOTEND) : Heater::isCooling(TEMPSENSOR_HOTEND)) || while((target_direction ? (current_raw[0] < target_raw[0]) : (current_raw[0] > target_raw[0])) ||
(residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) { (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
#else #else
while ( target_direction ? Heater::isHeating(TEMPSENSOR_HOTEND) : Heater::isCooling(TEMPSENSOR_HOTEND) ) { while ( target_direction ? (current_raw[0] < target_raw[0]) : (current_raw[0] > target_raw[0]) ) {
#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( Heater::celsius(TEMPSENSOR_HOTEND) ); Serial.println( analog2temp(current_raw[TEMPSENSOR_HOTEND]) );
codenum = millis(); codenum = millis();
} }
Heater::manage_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 && !Heater::isHeating(TEMPSENSOR_HOTEND)) || if ((residencyStart == -1 && target_direction && current_raw[0] >= target_raw[0]) ||
(residencyStart == -1 && !target_direction && !Heater::isCooling(TEMPSENSOR_HOTEND)) || (residencyStart == -1 && !target_direction && current_raw[0] <= target_raw[0]) ||
(residencyStart > -1 && labs(Heater::celsius(TEMPSENSOR_HOTEND) - Heater::celsiusTarget(TEMPSENSOR_HOTEND)) > TEMP_HYSTERESIS) ) { (residencyStart > -1 && labs(analog2temp(current_raw[0]) - analog2temp(target_raw[0])) > TEMP_HYSTERESIS) ) {
residencyStart = millis(); residencyStart = millis();
} }
#endif //TEMP_RESIDENCY_TIME #endif //TEMP_RESIDENCY_TIME
@ -895,22 +899,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')) Heater::setCelsius(TEMPSENSOR_BED,code_value()); if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analog(code_value());
codenum = millis(); codenum = millis();
while(Heater::isHeating(TEMPSENSOR_BED)) while(current_raw[TEMPSENSOR_BED] < target_raw[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=Heater::celsius(TEMPSENSOR_HOTEND); float tt=analog2temp(current_raw[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( Heater::celsius(TEMPSENSOR_BED) ); Serial.println( analog2temp(current_raw[TEMPSENSOR_BED]) );
codenum = millis(); codenum = millis();
} }
Heater::manage_heater(); manage_heater();
} }
#endif #endif
break; break;
@ -1062,13 +1066,9 @@ inline void process_commands()
break; break;
#ifdef PIDTEMP #ifdef PIDTEMP
case 301: // M301 case 301: // M301
if(code_seen('P')) Heater::Kp = code_value(); if(code_seen('P')) Kp = code_value();
if(code_seen('I')) Heater::Ki = code_value()*PID_dT; if(code_seen('I')) Ki = code_value()*PID_dT;
if(code_seen('D')) Heater::Kd = code_value()/PID_dT; if(code_seen('D')) 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
Heater::setCelsius(TEMPSENSOR_HOTEND,0); target_raw[0]=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
Heater::setCelsius(TEMPSENSOR_BED,0); target_raw[1]=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
Heater::setCelsius(TEMPSENSOR_AUX,0); target_raw[2]=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) {
htr.manage_heater(); 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()) {
htr.manage_heater(); manage_heater();
manage_inactivity(1); manage_inactivity(1);
LCD_STATUS; LCD_STATUS;
} }

View File

@ -37,11 +37,52 @@
#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;
void static Heater::manage_heater() float Kp=DEFAULT_Kp;
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();
@ -49,11 +90,11 @@ void static Heater::manage_heater()
float pid_input; float pid_input;
float pid_output; float pid_output;
if(htr.temp_meas_ready != true) //better readability if(temp_meas_ready != true) //better readability
return; return;
CRITICAL_SECTION_START; CRITICAL_SECTION_START;
htr.temp_meas_ready = false; temp_meas_ready = false;
CRITICAL_SECTION_END; CRITICAL_SECTION_END;
#ifdef PIDTEMP #ifdef PIDTEMP
@ -135,8 +176,7 @@ 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 const static temp2analog(const int celsius) float temp2analog(int celsius) {
{
#ifdef HEATER_USES_THERMISTOR_1 #ifdef HEATER_USES_THERMISTOR_1
int raw = 0; int raw = 0;
byte i; byte i;
@ -167,8 +207,7 @@ float const static temp2analog(const 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 const static temp2analogBed(const int celsius) float temp2analogBed(int celsius) {
{
#ifdef BED_USES_THERMISTOR #ifdef BED_USES_THERMISTOR
int raw = 0; int raw = 0;
@ -198,7 +237,7 @@ float const static temp2analogBed(const 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 const static Heater::analog2temp(const int raw) { float analog2temp(int raw) {
#ifdef HEATER_1_USES_THERMISTOR #ifdef HEATER_1_USES_THERMISTOR
int celsius = 0; int celsius = 0;
byte i; byte i;
@ -227,7 +266,7 @@ float const static Heater::analog2temp(const int raw) {
// Derived from RepRap FiveD extruder::getTemperature() // Derived from RepRap FiveD extruder::getTemperature()
// For bed temperature measurement. // For bed temperature measurement.
float const static Heater::analog2tempBed(const int raw) { float analog2tempBed(int raw) {
#ifdef BED_USES_THERMISTOR #ifdef BED_USES_THERMISTOR
int celsius = 0; int celsius = 0;
byte i; byte i;
@ -257,28 +296,8 @@ float const static Heater::analog2tempBed(const int raw) {
#endif #endif
} }
Heater::Heater() void tp_init()
{ {
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
@ -292,14 +311,6 @@ Heater::Heater()
#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
@ -396,39 +407,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
htr.current_raw[0] = raw_temp_0_value; current_raw[0] = raw_temp_0_value;
#else #else
htr.current_raw[0] = 16383 - raw_temp_0_value; current_raw[0] = 16383 - raw_temp_0_value;
#endif #endif
#ifdef HEATER_2_USES_AD595 #ifdef HEATER_2_USES_AD595
htr.current_raw[2] = raw_temp_2_value; current_raw[2] = raw_temp_2_value;
#else #else
htr.current_raw[2] = 16383 - raw_temp_2_value; current_raw[2] = 16383 - raw_temp_2_value;
#endif #endif
#ifdef BED_USES_AD595 #ifdef BED_USES_AD595
htr.current_raw[1] = raw_temp_1_value; current_raw[1] = raw_temp_1_value;
#else #else
htr.current_raw[1] = 16383 - raw_temp_1_value; current_raw[1] = 16383 - raw_temp_1_value;
#endif #endif
htr.temp_meas_ready = true; 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(htr.current_raw[TEMPSENSOR_HOTEND] >= htr.maxttemp) { if(current_raw[TEMPSENSOR_HOTEND] >= maxttemp) {
htr.target_raw[TEMPSENSOR_HOTEND] = 0; 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(htr.current_raw[TEMPSENSOR_AUX] >= htr.maxttemp) { if(current_raw[TEMPSENSOR_AUX] >= maxttemp) {
htr.target_raw[TEMPSENSOR_AUX] = 0; 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 !!");
} }
@ -436,15 +447,15 @@ ISR(TIMER0_COMPB_vect)
#endif //MAXTEMP #endif //MAXTEMP
#ifdef MINTEMP #ifdef MINTEMP
#if (HEATER_0_PIN > -1) #if (HEATER_0_PIN > -1)
if(htr.current_raw[TEMPSENSOR_HOTEND] <= htr.minttemp) { if(current_raw[TEMPSENSOR_HOTEND] <= minttemp) {
htr.target_raw[TEMPSENSOR_HOTEND] = 0; 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(htr.current_raw[TEMPSENSOR_AUX] <= htr.minttemp) { if(current_raw[TEMPSENSOR_AUX] <= minttemp) {
htr.target_raw[TEMPSENSOR_AUX] = 0; 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 !!");
} }
@ -452,8 +463,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(htr.current_raw[1] <= htr.bed_minttemp) { if(current_raw[1] <= bed_minttemp) {
htr.target_raw[1] = 0; 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 !!");
} }
@ -461,8 +472,8 @@ ISR(TIMER0_COMPB_vect)
#endif #endif
#ifdef BED_MAXTEMP #ifdef BED_MAXTEMP
#if (HEATER_1_PIN > -1) #if (HEATER_1_PIN > -1)
if(htr.current_raw[1] >= htr.bed_maxttemp) { if(current_raw[1] >= bed_maxttemp) {
htr.target_raw[1] = 0; 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 !!");
} }
@ -470,6 +481,3 @@ ISR(TIMER0_COMPB_vect)
#endif #endif
} }
} }
//Heater htr;

View File

@ -25,116 +25,14 @@
#ifdef PID_ADD_EXTRUSION_RATE #ifdef PID_ADD_EXTRUSION_RATE
#include "stepper.h" #include "stepper.h"
#endif #endif
void tp_init();
enum TempSensor {TEMPSENSOR_HOTEND=0,TEMPSENSOR_BED=1, TEMPSENSOR_AUX=2}; void manage_heater();
//int temp2analogu(int celsius, 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 analog2tempu(int raw, const short table[][2], int numtemps);
// all the variables are static, so that of the compiler optimization is more easy. float temp2analog(int celsius);
// I honestly hope that this increases readability and structure. float temp2analogBed(int celsius);
// none of the variables or routines should be called from an secondary process/interrupt with the exceptino of current_raw[]. float analog2temp(int 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
@ -143,5 +41,18 @@ extern Heater htr; //this creates the single, global instance
#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 oldcurrent=-1; static int oldcurrentraw=-1;
static int oldtarget=-1; static int oldtargetraw=-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,36 +252,33 @@ void MainMenu::showStatus()
#endif #endif
} }
int tt=Heater::celsius(TEMPSENSOR_HOTEND);
if((abs(tt-oldcurrent)>1)||force_lcd_update) if((abs(current_raw[TEMPSENSOR_HOTEND]-oldcurrentraw)>3)||force_lcd_update)
{ {
lcd.setCursor(1,0); lcd.setCursor(1,0);
lcd.print(ftostr3(tt)); lcd.print(ftostr3(analog2temp(current_raw[TEMPSENSOR_HOTEND])));
oldcurrent=tt; oldcurrentraw=current_raw[TEMPSENSOR_HOTEND];
} }
int ttg=Heater::celsiusTarget(TEMPSENSOR_HOTEND); if((target_raw[TEMPSENSOR_HOTEND]!=oldtargetraw)||force_lcd_update)
if((ttg!=oldtarget)||force_lcd_update)
{ {
lcd.setCursor(5,0); lcd.setCursor(5,0);
lcd.print(ftostr3(ttg)); lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND])));
oldtarget=ttg; oldtargetraw=target_raw[TEMPSENSOR_HOTEND];
} }
#if defined BED_USES_THERMISTOR || defined BED_USES_AD595 #if defined BED_USES_THERMISTOR || defined BED_USES_AD595
static int oldcurrentbed=-1; static int oldcurrentbedraw=-1;
static int oldtargetbed=-1; static int oldtargetbedraw=-1;
int tb=Heater::celsius(TEMPSENSOR_BED); if((current_bed_raw!=oldcurrentbedraw)||force_lcd_update)
if((tb!=oldcurrentbed)||force_lcd_update)
{ {
lcd.setCursor(1,0); lcd.setCursor(1,0);
lcd.print(ftostr3(tb)); lcd.print(ftostr3(analog2temp(current_bed_raw)));
oldcurrentbed=tb; oldcurrentraw=current_raw[TEMPSENSOR_BED];
} }
int tg=Heater::celsiusTarget(TEMPSENSOR_BED); if((target_bed_raw!=oldtargebedtraw)||force_lcd_update)
if((tg!=oldtargebed)||force_lcd_update)
{ {
lcd.setCursor(5,0); lcd.setCursor(5,0);
lcd.print(Heater::celsiusTarget(TEMPSENSOR_BED)); lcd.print(ftostr3(analog2temp(target_bed_raw)));
oldtargebed=tg; oldtargetraw=target_bed_raw;
} }
#endif #endif
//starttime=2; //starttime=2;
@ -330,8 +327,8 @@ void MainMenu::showStatus()
messagetext[0]='\0'; messagetext[0]='\0';
} }
#else //smaller LCDS---------------------------------- #else //smaller LCDS----------------------------------
static int oldcurrent=-1; static int oldcurrentraw=-1;
static int oldtarget=-1; static int oldtargetraw=-1;
if(force_lcd_update) //initial display of content if(force_lcd_update) //initial display of content
{ {
encoderpos=feedmultiply; encoderpos=feedmultiply;
@ -341,19 +338,18 @@ void MainMenu::showStatus()
#endif #endif
} }
int tt=Heater::celsius(TEMPSENSOR_HOTEND);
if((abs(tt-oldcurrent)>1)||force_lcd_update) if((abs(current_raw[TEMPSENSOR_HOTEND]-oldcurrentraw)>3)||force_lcd_update)
{ {
lcd.setCursor(1,0); lcd.setCursor(1,0);
lcd.print(ftostr3(tt)); lcd.print(ftostr3(analog2temp(current_raw[TEMPSENSOR_HOTEND])));
oldcurrent=tt; oldcurrentraw=current_raw[TEMPSENSOR_HOTEND];
} }
int ttg=Heater::celsiusTarget(TEMPSENSOR_HOTEND); if((target_raw[TEMPSENSOR_HOTEND]!=oldtargetraw)||force_lcd_update)
if((ttg!=oldtarget)||force_lcd_update)
{ {
lcd.setCursor(5,0); lcd.setCursor(5,0);
lcd.print(ftostr3(ttg)); lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND])));
oldtarge=ttg; oldtargetraw=target_raw[TEMPSENSOR_HOTEND];
} }
if(messagetext[0]!='\0') if(messagetext[0]!='\0')
@ -430,7 +426,7 @@ void MainMenu::showPrepare()
if((activeline==line) && CLICKED) if((activeline==line) && CLICKED)
{ {
BLOCK BLOCK
Heater::setCelsius(TEMPSENSOR_HOTEND, 170); target_raw[TEMPSENSOR_HOTEND] = temp2analog(170);
beepshort(); beepshort();
} }
}break; }break;
@ -535,7 +531,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(Heater::celsiusTarget(TEMPSENSOR_HOTEND))); lcd.setCursor(13,line);lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND])));
} }
if((activeline==line) ) if((activeline==line) )
@ -545,11 +541,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)Heater::celsiusTarget(TEMPSENSOR_HOTEND); encoderpos=(int)analog2temp(target_raw[TEMPSENSOR_HOTEND]);
} }
else else
{ {
Heater::setCelsius(TEMPSENSOR_HOTEND,encoderpos); target_raw[TEMPSENSOR_HOTEND] = temp2analog(encoderpos);
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
beepshort(); beepshort();
} }
@ -673,7 +669,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(Heater::Kp)); lcd.setCursor(13,line);lcd.print(itostr4(Kp));
} }
if((activeline==line) ) if((activeline==line) )
@ -683,11 +679,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)Heater::Kp/5; encoderpos=(int)Kp/5;
} }
else else
{ {
Heater::Kp= encoderpos*5; Kp= encoderpos*5;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }
@ -707,7 +703,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(Heater::Ki)); lcd.setCursor(13,line);lcd.print(ftostr51(Ki));
} }
if((activeline==line) ) if((activeline==line) )
@ -717,11 +713,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)(Heater::Ki*10); encoderpos=(int)(Ki*10);
} }
else else
{ {
Heater::Ki= encoderpos/10.; Ki= encoderpos/10.;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }
@ -741,7 +737,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(Heater::Kd)); lcd.setCursor(13,line);lcd.print(itostr4(Kd));
} }
if((activeline==line) ) if((activeline==line) )
@ -751,11 +747,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)(Heater::Kd/5.); encoderpos=(int)Kd/5;
} }
else else
{ {
Heater::Kd= encoderpos*5; Kd= encoderpos*5;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }
@ -778,7 +774,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(Heater::Kc)); lcd.setCursor(13,line);lcd.print(itostr3(Kc));
} }
if((activeline==line) ) if((activeline==line) )
@ -788,11 +784,11 @@ void MainMenu::showControl()
linechanging=!linechanging; linechanging=!linechanging;
if(linechanging) if(linechanging)
{ {
encoderpos=(int)Heater::Kc; encoderpos=(int)Kc;
} }
else else
{ {
Heater::Kc= encoderpos; Kc= encoderpos;
encoderpos=activeline*lcdslow; encoderpos=activeline*lcdslow;
} }