Testing Fix for Arduino 1.6+ compiler issue #1523

master
chrono 10 years ago
parent 08bc723a3c
commit fb75a9272d

@ -215,7 +215,7 @@ Here are some standard links for getting your machine calibrated:
// If your configuration is significantly different than this and you don't understand the issues involved, you probably // If your configuration is significantly different than this and you don't understand the issues involved, you probably
// shouldn't use bed PID until someone else verifies your hardware works. // shouldn't use bed PID until someone else verifies your hardware works.
// If this is enabled, find your own PID constants below. // If this is enabled, find your own PID constants below.
//#define PIDTEMPBED #define PIDTEMPBED
// //
//#define BED_LIMIT_SWITCHING //#define BED_LIMIT_SWITCHING
@ -226,17 +226,10 @@ Here are some standard links for getting your machine calibrated:
#define MAX_BED_POWER 255 // limits duty cycle to bed; 255=full current #define MAX_BED_POWER 255 // limits duty cycle to bed; 255=full current
#ifdef PIDTEMPBED #ifdef PIDTEMPBED
//120v 250W silicone heater into 4mm borosilicate (MendelMax 1.5+) // Felix Foil Heater
//from FOPDT model - kp=.39 Tp=405 Tdead=66, Tc set to 79.2, aggressive factor of .15 (vs .1, 1, 10) #define DEFAULT_bedKp 103.37
#define DEFAULT_bedKp 10.00 #define DEFAULT_bedKi 2.79
#define DEFAULT_bedKi .023 #define DEFAULT_bedKd 956.94
#define DEFAULT_bedKd 305.4
//120v 250W silicone heater into 4mm borosilicate (MendelMax 1.5+)
//from pidautotune
// #define DEFAULT_bedKp 97.1
// #define DEFAULT_bedKi 1.41
// #define DEFAULT_bedKd 1675.16
// FIND YOUR OWN: "M303 E-1 C8 S90" to run autotune on the bed at 90 degreesC for 8 cycles. // FIND YOUR OWN: "M303 E-1 C8 S90" to run autotune on the bed at 90 degreesC for 8 cycles.
#endif // PIDTEMPBED #endif // PIDTEMPBED
@ -280,15 +273,15 @@ your extruder heater takes 2 minutes to hit the target on heating.
// uncomment the 2 defines below: // uncomment the 2 defines below:
// Parameters for all extruder heaters // Parameters for all extruder heaters
//#define THERMAL_RUNAWAY_PROTECTION_PERIOD 40 //in seconds #define THERMAL_RUNAWAY_PROTECTION_PERIOD 60 //in seconds
//#define THERMAL_RUNAWAY_PROTECTION_HYSTERESIS 4 // in degree Celsius #define THERMAL_RUNAWAY_PROTECTION_HYSTERESIS 5 // in degree Celsius
// If you want to enable this feature for your bed heater, // If you want to enable this feature for your bed heater,
// uncomment the 2 defines below: // uncomment the 2 defines below:
// Parameters for the bed heater // Parameters for the bed heater
//#define THERMAL_RUNAWAY_PROTECTION_BED_PERIOD 20 //in seconds #define THERMAL_RUNAWAY_PROTECTION_BED_PERIOD 30 //in seconds
//#define THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS 2 // in degree Celsius #define THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS 5// in degree Celsius
//=========================================================================== //===========================================================================

@ -1,19 +1,19 @@
/* /*
temperature.c - temperature control temperature.c - temperature control
Part of Marlin Part of Marlin
Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -22,8 +22,8 @@
This firmware is a mashup between Sprinter and grbl. This firmware is a mashup between Sprinter and grbl.
(https://github.com/kliment/Sprinter) (https://github.com/kliment/Sprinter)
(https://github.com/simen/grbl/tree) (https://github.com/simen/grbl/tree)
It has preliminary support for Matthew Roberts advance algorithm It has preliminary support for Matthew Roberts advance algorithm
http://reprap.org/pipermail/reprap-dev/2011-May/003323.html http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
*/ */
@ -63,20 +63,20 @@ float current_temperature_bed = 0.0;
float bedKi=(DEFAULT_bedKi*PID_dT); float bedKi=(DEFAULT_bedKi*PID_dT);
float bedKd=(DEFAULT_bedKd/PID_dT); float bedKd=(DEFAULT_bedKd/PID_dT);
#endif //PIDTEMPBED #endif //PIDTEMPBED
#ifdef FAN_SOFT_PWM #ifdef FAN_SOFT_PWM
unsigned char fanSpeedSoftPwm; unsigned char fanSpeedSoftPwm;
#endif #endif
unsigned char soft_pwm_bed; unsigned char soft_pwm_bed;
#ifdef BABYSTEPPING #ifdef BABYSTEPPING
volatile int babystepsTodo[3]={0,0,0}; volatile int babystepsTodo[3]={0,0,0};
#endif #endif
#ifdef FILAMENT_SENSOR #ifdef FILAMENT_SENSOR
int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
#endif #endif
//=========================================================================== //===========================================================================
//=============================private variables============================ //=============================private variables============================
//=========================================================================== //===========================================================================
@ -109,7 +109,7 @@ static volatile bool temp_meas_ready = false;
static float temp_iState_min_bed; static float temp_iState_min_bed;
static float temp_iState_max_bed; static float temp_iState_max_bed;
#else //PIDTEMPBED #else //PIDTEMPBED
static unsigned long previous_millis_bed_heater; static unsigned long previous_millis_bed_heater;
#endif //PIDTEMPBED #endif //PIDTEMPBED
static unsigned char soft_pwm[EXTRUDERS]; static unsigned char soft_pwm[EXTRUDERS];
@ -120,7 +120,7 @@ static volatile bool temp_meas_ready = false;
(defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \ (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
static unsigned long extruder_autofan_last_check; static unsigned long extruder_autofan_last_check;
#endif #endif
#if EXTRUDERS > 4 #if EXTRUDERS > 4
# error Unsupported number of extruders # error Unsupported number of extruders
@ -227,9 +227,9 @@ void PID_autotune(float temp, int extruder, int ncycles)
SERIAL_ECHOLN("PID Autotune failed. Bad extruder number."); SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
return; return;
} }
SERIAL_ECHOLN("PID Autotune start"); SERIAL_ECHOLN("PID Autotune start");
disable_heater(); // switch off all heaters. disable_heater(); // switch off all heaters.
if (extruder<0) if (extruder<0)
@ -267,7 +267,7 @@ void PID_autotune(float temp, int extruder, int ncycles)
#endif #endif
if(heating == true && input > temp) { if(heating == true && input > temp) {
if(millis() - t2 > 5000) { if(millis() - t2 > 5000) {
heating=false; heating=false;
if (extruder<0) if (extruder<0)
soft_pwm_bed = (bias - d) >> 1; soft_pwm_bed = (bias - d) >> 1;
@ -330,7 +330,7 @@ void PID_autotune(float temp, int extruder, int ncycles)
cycles++; cycles++;
min=temp; min=temp;
} }
} }
} }
if(input > (temp + 20)) { if(input > (temp + 20)) {
SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high"); SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
@ -339,16 +339,16 @@ void PID_autotune(float temp, int extruder, int ncycles)
if(millis() - temp_millis > 2000) { if(millis() - temp_millis > 2000) {
int p; int p;
if (extruder<0){ if (extruder<0){
p=soft_pwm_bed; p=soft_pwm_bed;
SERIAL_PROTOCOLPGM("ok B:"); SERIAL_PROTOCOLPGM("ok B:");
}else{ }else{
p=soft_pwm[extruder]; p=soft_pwm[extruder];
SERIAL_PROTOCOLPGM("ok T:"); SERIAL_PROTOCOLPGM("ok T:");
} }
SERIAL_PROTOCOL(input); SERIAL_PROTOCOL(input);
SERIAL_PROTOCOLPGM(" @:"); SERIAL_PROTOCOLPGM(" @:");
SERIAL_PROTOCOLLN(p); SERIAL_PROTOCOLLN(p);
temp_millis = millis(); temp_millis = millis();
} }
@ -367,18 +367,18 @@ void PID_autotune(float temp, int extruder, int ncycles)
void updatePID() void updatePID()
{ {
#ifdef PIDTEMP #ifdef PIDTEMP
for(int e = 0; e < EXTRUDERS; e++) { for(int e = 0; e < EXTRUDERS; e++) {
temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e); temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
} }
#endif #endif
#ifdef PIDTEMPBED #ifdef PIDTEMPBED
temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi; temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
#endif #endif
} }
int getHeaterPower(int heater) { int getHeaterPower(int heater) {
if (heater<0) if (heater<0)
return soft_pwm_bed; return soft_pwm_bed;
return soft_pwm[heater]; return soft_pwm[heater];
} }
@ -387,16 +387,16 @@ int getHeaterPower(int heater) {
(defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
#if defined(FAN_PIN) && FAN_PIN > -1 #if defined(FAN_PIN) && FAN_PIN > -1
#if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
#error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN" #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
#endif #endif
#if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
#error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN" #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
#endif #endif
#if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
#error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN" #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
#endif #endif
#endif #endif
void setExtruderAutoFanState(int pin, bool state) void setExtruderAutoFanState(int pin, bool state)
{ {
@ -411,53 +411,53 @@ void checkExtruderAutoFans()
{ {
uint8_t fanState = 0; uint8_t fanState = 0;
// which fan pins need to be turned on? // which fan pins need to be turned on?
#if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE) if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
fanState |= 1; fanState |= 1;
#endif #endif
#if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE) if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
{ {
if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN) if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
fanState |= 1; fanState |= 1;
else else
fanState |= 2; fanState |= 2;
} }
#endif #endif
#if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE) if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
{ {
if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN) if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
fanState |= 1; fanState |= 1;
else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN) else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
fanState |= 2; fanState |= 2;
else else
fanState |= 4; fanState |= 4;
} }
#endif #endif
#if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE) if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE)
{ {
if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN) if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
fanState |= 1; fanState |= 1;
else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN) else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
fanState |= 2; fanState |= 2;
else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN) else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
fanState |= 4; fanState |= 4;
else else
fanState |= 8; fanState |= 8;
} }
#endif #endif
// update extruder auto fan states // update extruder auto fan states
#if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0); setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
#endif #endif
#if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0); setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
#endif #endif
#if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1 #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
&& EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
@ -479,7 +479,7 @@ void manage_heater()
float pid_output; float pid_output;
if(temp_meas_ready != true) //better readability if(temp_meas_ready != true) //better readability
return; return;
updateTemperaturesFromRawValues(); updateTemperaturesFromRawValues();
@ -492,7 +492,7 @@ void manage_heater()
} }
#endif //HEATER_0_USES_MAX6675 #endif //HEATER_0_USES_MAX6675
for(int e = 0; e < EXTRUDERS; e++) for(int e = 0; e < EXTRUDERS; e++)
{ {
#if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0 #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
@ -527,15 +527,16 @@ void manage_heater()
dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]); dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
pid_output = pTerm[e] + iTerm[e] - dTerm[e]; pid_output = pTerm[e] + iTerm[e] - dTerm[e];
if (pid_output > PID_MAX) { if (pid_output > PID_MAX) {
if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e];
pid_output=PID_MAX; pid_output=PID_MAX;
} else if (pid_output < 0){ } else if (pid_output < 0){
if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e];
pid_output=0; pid_output=0;
} }
} }
temp_dState[e] = pid_input; temp_dState[e] = pid_input;
#else #else
pid_output = constrain(target_temperature[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
@ -561,7 +562,7 @@ void manage_heater()
#endif #endif
// Check if temperature is within the correct range // Check if temperature is within the correct range
if((current_temperature[e] > minttemp[e]) && (current_temperature[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;
} }
@ -605,9 +606,9 @@ void manage_heater()
{ {
checkExtruderAutoFans(); checkExtruderAutoFans();
extruder_autofan_last_check = millis(); extruder_autofan_last_check = millis();
} }
#endif #endif
#ifndef PIDTEMPBED #ifndef PIDTEMPBED
if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL) if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
return; return;
@ -615,7 +616,7 @@ void manage_heater()
#endif #endif
#if TEMP_SENSOR_BED != 0 #if TEMP_SENSOR_BED != 0
#if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0 #if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, 9, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS); thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, 9, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS);
#endif #endif
@ -624,18 +625,18 @@ void manage_heater()
pid_input = current_temperature_bed; pid_input = current_temperature_bed;
#ifndef PID_OPENLOOP #ifndef PID_OPENLOOP
pid_error_bed = target_temperature_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);
iTerm_bed = bedKi * temp_iState_bed; iTerm_bed = bedKi * temp_iState_bed;
//K1 defined in Configuration.h in the PID settings //K1 defined in Configuration.h in the PID settings
#define K2 (1.0-K1) #define K2 (1.0-K1)
dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed); dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
temp_dState_bed = pid_input; temp_dState_bed = pid_input;
pid_output = pTerm_bed + iTerm_bed - dTerm_bed; pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
if (pid_output > MAX_BED_POWER) { if (pid_output > MAX_BED_POWER) {
if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
pid_output=MAX_BED_POWER; pid_output=MAX_BED_POWER;
@ -644,17 +645,17 @@ void manage_heater()
pid_output=0; pid_output=0;
} }
#else #else
pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER); pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
#endif //PID_OPENLOOP #endif //PID_OPENLOOP
if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP)) 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;
} }
else { else {
soft_pwm_bed = 0; soft_pwm_bed = 0;
} }
#elif !defined(BED_LIMIT_SWITCHING) #elif !defined(BED_LIMIT_SWITCHING)
// Check if temperature is within the correct range // Check if temperature is within the correct range
@ -664,7 +665,7 @@ void manage_heater()
{ {
soft_pwm_bed = 0; soft_pwm_bed = 0;
} }
else else
{ {
soft_pwm_bed = MAX_BED_POWER>>1; soft_pwm_bed = MAX_BED_POWER>>1;
} }
@ -694,27 +695,27 @@ void manage_heater()
} }
#endif #endif
#endif #endif
//code for controlling the extruder rate based on the width sensor //code for controlling the extruder rate based on the width sensor
#ifdef FILAMENT_SENSOR #ifdef FILAMENT_SENSOR
if(filament_sensor) if(filament_sensor)
{ {
meas_shift_index=delay_index1-meas_delay_cm; meas_shift_index=delay_index1-meas_delay_cm;
if(meas_shift_index<0) if(meas_shift_index<0)
meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
//get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
//then square it to get an area //then square it to get an area
if(meas_shift_index<0) if(meas_shift_index<0)
meas_shift_index=0; meas_shift_index=0;
else if (meas_shift_index>MAX_MEASUREMENT_DELAY) else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
meas_shift_index=MAX_MEASUREMENT_DELAY; meas_shift_index=MAX_MEASUREMENT_DELAY;
volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2); volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01) if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01; volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
} }
#endif #endif
} }
@ -733,7 +734,7 @@ static float analog2temp(int raw, uint8_t e) {
SERIAL_ERRORLNPGM(" - Invalid extruder number !"); SERIAL_ERRORLNPGM(" - Invalid extruder number !");
kill(); kill();
return 0.0; return 0.0;
} }
#ifdef HEATER_0_USES_MAX6675 #ifdef HEATER_0_USES_MAX6675
if (e == 0) if (e == 0)
{ {
@ -751,8 +752,8 @@ static float analog2temp(int raw, uint8_t e) {
{ {
if (PGM_RD_W((*tt)[i][0]) > raw) if (PGM_RD_W((*tt)[i][0]) > raw)
{ {
celsius = PGM_RD_W((*tt)[i-1][1]) + celsius = PGM_RD_W((*tt)[i-1][1]) +
(raw - PGM_RD_W((*tt)[i-1][0])) * (raw - PGM_RD_W((*tt)[i-1][0])) *
(float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) / (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
(float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0])); (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
break; break;
@ -778,8 +779,8 @@ static float analog2tempBed(int raw) {
{ {
if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
{ {
celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) + celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
(raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) * (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
(float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) / (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
(float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0])); (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
break; break;
@ -812,9 +813,9 @@ static void updateTemperaturesFromRawValues()
#ifdef TEMP_SENSOR_1_AS_REDUNDANT #ifdef TEMP_SENSOR_1_AS_REDUNDANT
redundant_temperature = analog2temp(redundant_temperature_raw, 1); redundant_temperature = analog2temp(redundant_temperature_raw, 1);
#endif #endif
#if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
filament_width_meas = analog2widthFil(); filament_width_meas = analog2widthFil();
#endif #endif
//Reset the watchdog after we know we have a temperature measurement. //Reset the watchdog after we know we have a temperature measurement.
watchdog_reset(); watchdog_reset();
@ -824,29 +825,29 @@ static void updateTemperaturesFromRawValues()
} }
// For converting raw Filament Width to milimeters // For converting raw Filament Width to milimeters
#ifdef FILAMENT_SENSOR #ifdef FILAMENT_SENSOR
float analog2widthFil() { float analog2widthFil() {
return current_raw_filwidth/16383.0*5.0; return current_raw_filwidth/16383.0*5.0;
//return current_raw_filwidth; //return current_raw_filwidth;
} }
// For converting raw Filament Width to a ratio // For converting raw Filament Width to a ratio
int widthFil_to_size_ratio() { int widthFil_to_size_ratio() {
float temp; float temp;
temp=filament_width_meas; temp=filament_width_meas;
if(filament_width_meas<MEASURED_LOWER_LIMIT) if(filament_width_meas<MEASURED_LOWER_LIMIT)
temp=filament_width_nominal; //assume sensor cut out temp=filament_width_nominal; //assume sensor cut out
else if (filament_width_meas>MEASURED_UPPER_LIMIT) else if (filament_width_meas>MEASURED_UPPER_LIMIT)
temp= MEASURED_UPPER_LIMIT; temp= MEASURED_UPPER_LIMIT;
return(filament_width_nominal/temp*100); return(filament_width_nominal/temp*100);
} }
#endif #endif
@ -857,13 +858,13 @@ void tp_init()
{ {
#if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1)) #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
//disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
MCUCR=(1<<JTD); MCUCR=(1<<JTD);
MCUCR=(1<<JTD); MCUCR=(1<<JTD);
#endif #endif
// Finish init of mult extruder arrays // Finish init of mult extruder arrays
for(int e = 0; e < EXTRUDERS; e++) { for(int e = 0; e < EXTRUDERS; e++) {
// populate with the first value // populate with the first value
maxttemp[e] = maxttemp[0]; maxttemp[e] = maxttemp[0];
#ifdef PIDTEMP #ifdef PIDTEMP
temp_iState_min[e] = 0.0; temp_iState_min[e] = 0.0;
@ -875,22 +876,22 @@ void tp_init()
#endif //PIDTEMPBED #endif //PIDTEMPBED
} }
#if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1) #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
SET_OUTPUT(HEATER_0_PIN); SET_OUTPUT(HEATER_0_PIN);
#endif #endif
#if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1) #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
SET_OUTPUT(HEATER_1_PIN); SET_OUTPUT(HEATER_1_PIN);
#endif #endif
#if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1) #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
SET_OUTPUT(HEATER_2_PIN); SET_OUTPUT(HEATER_2_PIN);
#endif #endif
#if defined(HEATER_3_PIN) && (HEATER_3_PIN > -1) #if defined(HEATER_3_PIN) && (HEATER_3_PIN > -1)
SET_OUTPUT(HEATER_3_PIN); SET_OUTPUT(HEATER_3_PIN);
#endif #endif
#if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1) #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
SET_OUTPUT(HEATER_BED_PIN); SET_OUTPUT(HEATER_BED_PIN);
#endif #endif
#if defined(FAN_PIN) && (FAN_PIN > -1) #if defined(FAN_PIN) && (FAN_PIN > -1)
SET_OUTPUT(FAN_PIN); SET_OUTPUT(FAN_PIN);
#ifdef FAST_PWM_FAN #ifdef FAST_PWM_FAN
setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
@ -898,24 +899,24 @@ void tp_init()
#ifdef FAN_SOFT_PWM #ifdef FAN_SOFT_PWM
soft_pwm_fan = fanSpeedSoftPwm / 2; soft_pwm_fan = fanSpeedSoftPwm / 2;
#endif #endif
#endif #endif
#ifdef HEATER_0_USES_MAX6675 #ifdef HEATER_0_USES_MAX6675
#ifndef SDSUPPORT #ifndef SDSUPPORT
SET_OUTPUT(SCK_PIN); SET_OUTPUT(SCK_PIN);
WRITE(SCK_PIN,0); WRITE(SCK_PIN,0);
SET_OUTPUT(MOSI_PIN); SET_OUTPUT(MOSI_PIN);
WRITE(MOSI_PIN,1); WRITE(MOSI_PIN,1);
SET_INPUT(MISO_PIN); SET_INPUT(MISO_PIN);
WRITE(MISO_PIN,1); WRITE(MISO_PIN,1);
#else #else
pinMode(SS_PIN, OUTPUT); pinMode(SS_PIN, OUTPUT);
digitalWrite(SS_PIN, HIGH); digitalWrite(SS_PIN, HIGH);
#endif #endif
SET_OUTPUT(MAX6675_SS); SET_OUTPUT(MAX6675_SS);
WRITE(MAX6675_SS,1); WRITE(MAX6675_SS,1);
@ -929,56 +930,56 @@ void tp_init()
#endif #endif
#if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1) #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
#if TEMP_0_PIN < 8 #if TEMP_0_PIN < 8
DIDR0 |= 1 << TEMP_0_PIN; DIDR0 |= 1 << TEMP_0_PIN;
#else #else
DIDR2 |= 1<<(TEMP_0_PIN - 8); DIDR2 |= 1<<(TEMP_0_PIN - 8);
#endif #endif
#endif #endif
#if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1) #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
#if TEMP_1_PIN < 8 #if TEMP_1_PIN < 8
DIDR0 |= 1<<TEMP_1_PIN; DIDR0 |= 1<<TEMP_1_PIN;
#else #else
DIDR2 |= 1<<(TEMP_1_PIN - 8); DIDR2 |= 1<<(TEMP_1_PIN - 8);
#endif #endif
#endif #endif
#if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1) #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
#if TEMP_2_PIN < 8 #if TEMP_2_PIN < 8
DIDR0 |= 1 << TEMP_2_PIN; DIDR0 |= 1 << TEMP_2_PIN;
#else #else
DIDR2 |= 1<<(TEMP_2_PIN - 8); DIDR2 |= 1<<(TEMP_2_PIN - 8);
#endif #endif
#endif #endif
#if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1) #if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
#if TEMP_3_PIN < 8 #if TEMP_3_PIN < 8
DIDR0 |= 1 << TEMP_3_PIN; DIDR0 |= 1 << TEMP_3_PIN;
#else #else
DIDR2 |= 1<<(TEMP_3_PIN - 8); DIDR2 |= 1<<(TEMP_3_PIN - 8);
#endif #endif
#endif #endif
#if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1) #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
#if TEMP_BED_PIN < 8 #if TEMP_BED_PIN < 8
DIDR0 |= 1<<TEMP_BED_PIN; DIDR0 |= 1<<TEMP_BED_PIN;
#else #else
DIDR2 |= 1<<(TEMP_BED_PIN - 8); DIDR2 |= 1<<(TEMP_BED_PIN - 8);
#endif #endif
#endif #endif
//Added for Filament Sensor //Added for Filament Sensor
#ifdef FILAMENT_SENSOR #ifdef FILAMENT_SENSOR
#if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1) #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
#if FILWIDTH_PIN < 8 #if FILWIDTH_PIN < 8
DIDR0 |= 1<<FILWIDTH_PIN; DIDR0 |= 1<<FILWIDTH_PIN;
#else #else
DIDR2 |= 1<<(FILWIDTH_PIN - 8); DIDR2 |= 1<<(FILWIDTH_PIN - 8);
#endif #endif
#endif #endif
#endif #endif
// Use timer0 for temperature measurement // Use timer0 for temperature measurement
// Interleave temperature interrupt with millies interrupt // Interleave temperature interrupt with millies interrupt
OCR0B = 128; OCR0B = 128;
TIMSK0 |= (1<<OCIE0B); TIMSK0 |= (1<<OCIE0B);
// Wait for temperature measurement to settle // Wait for temperature measurement to settle
delay(250); delay(250);
@ -1089,8 +1090,8 @@ void tp_init()
#endif //BED_MAXTEMP #endif //BED_MAXTEMP
} }
void setWatch() void setWatch()
{ {
#ifdef WATCH_TEMP_PERIOD #ifdef WATCH_TEMP_PERIOD
for (int e = 0; e < EXTRUDERS; e++) for (int e = 0; e < EXTRUDERS; e++)
{ {
@ -1098,9 +1099,9 @@ void setWatch()
{ {
watch_start_temp[e] = degHotend(e); watch_start_temp[e] = degHotend(e);
watchmillis[e] = millis(); watchmillis[e] = millis();
} }
} }
#endif #endif
} }
#if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0 #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
@ -1118,7 +1119,7 @@ void thermal_runaway_protection(int *state, unsigned long *timer, float temperat
SERIAL_ECHO(temperature); SERIAL_ECHO(temperature);
SERIAL_ECHO(" ; Target Temp:"); SERIAL_ECHO(" ; Target Temp:");
SERIAL_ECHO(target_temperature); SERIAL_ECHO(target_temperature);
SERIAL_ECHOLN(""); SERIAL_ECHOLN("");
*/ */
if ((target_temperature == 0) || thermal_runaway) if ((target_temperature == 0) || thermal_runaway)
{ {
@ -1138,7 +1139,7 @@ void thermal_runaway_protection(int *state, unsigned long *timer, float temperat
if (temperature >= (target_temperature - hysteresis_degc)) if (temperature >= (target_temperature - hysteresis_degc))
{ {
*timer = millis(); *timer = millis();
} }
else if ( (millis() - *timer) > ((unsigned long) period_seconds) * 1000) else if ( (millis() - *timer) > ((unsigned long) period_seconds) * 1000)
{ {
SERIAL_ERROR_START; SERIAL_ERROR_START;
@ -1173,23 +1174,23 @@ void disable_heater()
#if defined(TEMP_0_PIN) && TEMP_0_PIN > -1 #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
target_temperature[0]=0; target_temperature[0]=0;
soft_pwm[0]=0; soft_pwm[0]=0;
#if defined(HEATER_0_PIN) && HEATER_0_PIN > -1 #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
WRITE(HEATER_0_PIN,LOW); WRITE(HEATER_0_PIN,LOW);
#endif #endif
#endif #endif
#if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1 #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
target_temperature[1]=0; target_temperature[1]=0;
soft_pwm[1]=0; soft_pwm[1]=0;
#if defined(HEATER_1_PIN) && HEATER_1_PIN > -1 #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
WRITE(HEATER_1_PIN,LOW); WRITE(HEATER_1_PIN,LOW);
#endif #endif
#endif #endif
#if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2 #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
target_temperature[2]=0; target_temperature[2]=0;
soft_pwm[2]=0; soft_pwm[2]=0;
#if defined(HEATER_2_PIN) && HEATER_2_PIN > -1 #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
WRITE(HEATER_2_PIN,LOW); WRITE(HEATER_2_PIN,LOW);
#endif #endif
#endif #endif
@ -1197,19 +1198,19 @@ void disable_heater()
#if defined(TEMP_3_PIN) && TEMP_3_PIN > -1 && EXTRUDERS > 3 #if defined(TEMP_3_PIN) && TEMP_3_PIN > -1 && EXTRUDERS > 3
target_temperature[3]=0; target_temperature[3]=0;
soft_pwm[3]=0; soft_pwm[3]=0;
#if defined(HEATER_3_PIN) && HEATER_3_PIN > -1 #if defined(HEATER_3_PIN) && HEATER_3_PIN > -1
WRITE(HEATER_3_PIN,LOW); WRITE(HEATER_3_PIN,LOW);
#endif #endif
#endif #endif
#if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1 #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
target_temperature_bed=0; target_temperature_bed=0;
soft_pwm_bed=0; soft_pwm_bed=0;
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1 #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
WRITE(HEATER_BED_PIN,LOW); WRITE(HEATER_BED_PIN,LOW);
#endif #endif
#endif #endif
} }
void max_temp_error(uint8_t e) { void max_temp_error(uint8_t e) {
@ -1259,38 +1260,38 @@ int max6675_temp = 2000;
static int read_max6675() static int read_max6675()
{ {
if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL) if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
return max6675_temp; return max6675_temp;
max6675_previous_millis = millis(); max6675_previous_millis = millis();
max6675_temp = 0; max6675_temp = 0;
#ifdef PRR #ifdef PRR
PRR &= ~(1<<PRSPI); PRR &= ~(1<<PRSPI);
#elif defined(PRR0) #elif defined(PRR0)
PRR0 &= ~(1<<PRSPI); PRR0 &= ~(1<<PRSPI);
#endif #endif
SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0); SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
// enable TT_MAX6675 // enable TT_MAX6675
WRITE(MAX6675_SS, 0); WRITE(MAX6675_SS, 0);
// ensure 100ns delay - a bit extra is fine // ensure 100ns delay - a bit extra is fine
asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
// read MSB // read MSB
SPDR = 0; SPDR = 0;
for (;(SPSR & (1<<SPIF)) == 0;); for (;(SPSR & (1<<SPIF)) == 0;);
max6675_temp = SPDR; max6675_temp = SPDR;
max6675_temp <<= 8; max6675_temp <<= 8;
// read LSB // read LSB
SPDR = 0; SPDR = 0;
for (;(SPSR & (1<<SPIF)) == 0;); for (;(SPSR & (1<<SPIF)) == 0;);
max6675_temp |= SPDR; max6675_temp |= SPDR;
// disable TT_MAX6675 // disable TT_MAX6675
WRITE(MAX6675_SS, 1); WRITE(MAX6675_SS, 1);
@ -1299,7 +1300,7 @@ static int read_max6675()
// thermocouple open // thermocouple open
max6675_temp = 4000; max6675_temp = 4000;
} }
else else
{ {
max6675_temp = max6675_temp >> 3; max6675_temp = max6675_temp >> 3;
} }
@ -1327,21 +1328,21 @@ ISR(TIMER0_COMPB_vect)
static unsigned char slow_pwm_count = 0; static unsigned char slow_pwm_count = 0;
static unsigned char state_heater_0 = 0; static unsigned char state_heater_0 = 0;
static unsigned char state_timer_heater_0 = 0; static unsigned char state_timer_heater_0 = 0;
#endif #endif
#if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL) #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
static unsigned char soft_pwm_1; static unsigned char soft_pwm_1;
#ifdef SLOW_PWM_HEATERS #ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_1 = 0; static unsigned char state_heater_1 = 0;
static unsigned char state_timer_heater_1 = 0; static unsigned char state_timer_heater_1 = 0;
#endif #endif
#endif #endif
#if EXTRUDERS > 2 #if EXTRUDERS > 2
static unsigned char soft_pwm_2; static unsigned char soft_pwm_2;
#ifdef SLOW_PWM_HEATERS #ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_2 = 0; static unsigned char state_heater_2 = 0;
static unsigned char state_timer_heater_2 = 0; static unsigned char state_timer_heater_2 = 0;
#endif #endif
#endif #endif
#if EXTRUDERS > 3 #if EXTRUDERS > 3
static unsigned char soft_pwm_3; static unsigned char soft_pwm_3;
@ -1356,20 +1357,20 @@ ISR(TIMER0_COMPB_vect)
#ifdef SLOW_PWM_HEATERS #ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_b = 0; static unsigned char state_heater_b = 0;
static unsigned char state_timer_heater_b = 0; static unsigned char state_timer_heater_b = 0;
#endif
#endif #endif
#endif
#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1) #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
static unsigned long raw_filwidth_value = 0; //added for filament width sensor static unsigned long raw_filwidth_value = 0; //added for filament width sensor
#endif #endif
#ifndef SLOW_PWM_HEATERS #ifndef SLOW_PWM_HEATERS
/* /*
* standard PWM modulation * standard PWM modulation
*/ */
if(pwm_count == 0){ if(pwm_count == 0){
soft_pwm_0 = soft_pwm[0]; soft_pwm_0 = soft_pwm[0];
if(soft_pwm_0 > 0) { if(soft_pwm_0 > 0) {
WRITE(HEATER_0_PIN,1); WRITE(HEATER_0_PIN,1);
#ifdef HEATERS_PARALLEL #ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN,1); WRITE(HEATER_1_PIN,1);
@ -1399,7 +1400,7 @@ ISR(TIMER0_COMPB_vect)
if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0); if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
#endif #endif
} }
if(soft_pwm_0 < pwm_count) { if(soft_pwm_0 < pwm_count) {
WRITE(HEATER_0_PIN,0); WRITE(HEATER_0_PIN,0);
#ifdef HEATERS_PARALLEL #ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN,0); WRITE(HEATER_1_PIN,0);
@ -1422,10 +1423,10 @@ ISR(TIMER0_COMPB_vect)
#ifdef FAN_SOFT_PWM #ifdef FAN_SOFT_PWM
if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0); if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
#endif #endif
pwm_count += (1 << SOFT_PWM_SCALE); pwm_count += (1 << SOFT_PWM_SCALE);
pwm_count &= 0x7f; pwm_count &= 0x7f;
#else //ifndef SLOW_PWM_HEATERS #else //ifndef SLOW_PWM_HEATERS
/* /*
* SLOW PWM HEATERS * SLOW PWM HEATERS
@ -1436,84 +1437,84 @@ ISR(TIMER0_COMPB_vect)
#define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
#endif #endif
if (slow_pwm_count == 0) { if (slow_pwm_count == 0) {
// EXTRUDER 0 // EXTRUDER 0
soft_pwm_0 = soft_pwm[0]; soft_pwm_0 = soft_pwm[0];
if (soft_pwm_0 > 0) { if (soft_pwm_0 > 0) {
// turn ON heather only if the minimum time is up // turn ON heather only if the minimum time is up
if (state_timer_heater_0 == 0) { if (state_timer_heater_0 == 0) {
// if change state set timer // if change state set timer
if (state_heater_0 == 0) { if (state_heater_0 == 0) {
state_timer_heater_0 = MIN_STATE_TIME; state_timer_heater_0 = MIN_STATE_TIME;
} }
state_heater_0 = 1; state_heater_0 = 1;
WRITE(HEATER_0_PIN, 1); WRITE(HEATER_0_PIN, 1);
#ifdef HEATERS_PARALLEL #ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN, 1); WRITE(HEATER_1_PIN, 1);
#endif #endif
} }
} else { } else {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_0 == 0) { if (state_timer_heater_0 == 0) {
// if change state set timer // if change state set timer
if (state_heater_0 == 1) { if (state_heater_0 == 1) {
state_timer_heater_0 = MIN_STATE_TIME; state_timer_heater_0 = MIN_STATE_TIME;
} }
state_heater_0 = 0; state_heater_0 = 0;
WRITE(HEATER_0_PIN, 0); WRITE(HEATER_0_PIN, 0);
#ifdef HEATERS_PARALLEL #ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN, 0); WRITE(HEATER_1_PIN, 0);
#endif #endif
} }
} }
#if EXTRUDERS > 1 #if EXTRUDERS > 1
// EXTRUDER 1 // EXTRUDER 1
soft_pwm_1 = soft_pwm[1]; soft_pwm_1 = soft_pwm[1];
if (soft_pwm_1 > 0) { if (soft_pwm_1 > 0) {
// turn ON heather only if the minimum time is up // turn ON heather only if the minimum time is up
if (state_timer_heater_1 == 0) { if (state_timer_heater_1 == 0) {
// if change state set timer // if change state set timer
if (state_heater_1 == 0) { if (state_heater_1 == 0) {
state_timer_heater_1 = MIN_STATE_TIME; state_timer_heater_1 = MIN_STATE_TIME;
} }
state_heater_1 = 1; state_heater_1 = 1;
WRITE(HEATER_1_PIN, 1); WRITE(HEATER_1_PIN, 1);
} }
} else { } else {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_1 == 0) { if (state_timer_heater_1 == 0) {
// if change state set timer // if change state set timer
if (state_heater_1 == 1) { if (state_heater_1 == 1) {
state_timer_heater_1 = MIN_STATE_TIME; state_timer_heater_1 = MIN_STATE_TIME;
} }
state_heater_1 = 0; state_heater_1 = 0;
WRITE(HEATER_1_PIN, 0); WRITE(HEATER_1_PIN, 0);
} }
} }
#endif #endif
#if EXTRUDERS > 2 #if EXTRUDERS > 2
// EXTRUDER 2 // EXTRUDER 2
soft_pwm_2 = soft_pwm[2]; soft_pwm_2 = soft_pwm[2];
if (soft_pwm_2 > 0) { if (soft_pwm_2 > 0) {
// turn ON heather only if the minimum time is up // turn ON heather only if the minimum time is up
if (state_timer_heater_2 == 0) { if (state_timer_heater_2 == 0) {
// if change state set timer // if change state set timer
if (state_heater_2 == 0) { if (state_heater_2 == 0) {
state_timer_heater_2 = MIN_STATE_TIME; state_timer_heater_2 = MIN_STATE_TIME;
} }
state_heater_2 = 1; state_heater_2 = 1;
WRITE(HEATER_2_PIN, 1); WRITE(HEATER_2_PIN, 1);
} }
} else { } else {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_2 == 0) { if (state_timer_heater_2 == 0) {
// if change state set timer // if change state set timer
if (state_heater_2 == 1) { if (state_heater_2 == 1) {
state_timer_heater_2 = MIN_STATE_TIME; state_timer_heater_2 = MIN_STATE_TIME;
} }
state_heater_2 = 0; state_heater_2 = 0;
WRITE(HEATER_2_PIN, 0); WRITE(HEATER_2_PIN, 0);
} }
} }
#endif #endif
@ -1522,24 +1523,24 @@ ISR(TIMER0_COMPB_vect)
// EXTRUDER 3 // EXTRUDER 3
soft_pwm_3 = soft_pwm[3]; soft_pwm_3 = soft_pwm[3];
if (soft_pwm_3 > 0) { if (soft_pwm_3 > 0) {
// turn ON heather only if the minimum time is up // turn ON heather only if the minimum time is up
if (state_timer_heater_3 == 0) { if (state_timer_heater_3 == 0) {
// if change state set timer // if change state set timer
if (state_heater_3 == 0) { if (state_heater_3 == 0) {
state_timer_heater_3 = MIN_STATE_TIME; state_timer_heater_3 = MIN_STATE_TIME;
} }
state_heater_3 = 1; state_heater_3 = 1;
WRITE(HEATER_3_PIN, 1); WRITE(HEATER_3_PIN, 1);
} }
} else { } else {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_3 == 0) { if (state_timer_heater_3 == 0) {
// if change state set timer // if change state set timer
if (state_heater_3 == 1) { if (state_heater_3 == 1) {
state_timer_heater_3 = MIN_STATE_TIME; state_timer_heater_3 = MIN_STATE_TIME;
} }
state_heater_3 = 0; state_heater_3 = 0;
WRITE(HEATER_3_PIN, 0); WRITE(HEATER_3_PIN, 0);
} }
} }
#endif #endif
@ -1548,36 +1549,36 @@ ISR(TIMER0_COMPB_vect)
// BED // BED
soft_pwm_b = soft_pwm_bed; soft_pwm_b = soft_pwm_bed;
if (soft_pwm_b > 0) { if (soft_pwm_b > 0) {
// turn ON heather only if the minimum time is up // turn ON heather only if the minimum time is up
if (state_timer_heater_b == 0) { if (state_timer_heater_b == 0) {
// if change state set timer // if change state set timer
if (state_heater_b == 0) { if (state_heater_b == 0) {
state_timer_heater_b = MIN_STATE_TIME; state_timer_heater_b = MIN_STATE_TIME;
} }
state_heater_b = 1; state_heater_b = 1;
WRITE(HEATER_BED_PIN, 1); WRITE(HEATER_BED_PIN, 1);
} }
} else { } else {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_b == 0) { if (state_timer_heater_b == 0) {
// if change state set timer // if change state set timer
if (state_heater_b == 1) { if (state_heater_b == 1) {
state_timer_heater_b = MIN_STATE_TIME; state_timer_heater_b = MIN_STATE_TIME;
} }
state_heater_b = 0; state_heater_b = 0;
WRITE(HEATER_BED_PIN, 0); WRITE(HEATER_BED_PIN, 0);
} }
} }
#endif #endif
} // if (slow_pwm_count == 0) } // if (slow_pwm_count == 0)
// EXTRUDER 0 // EXTRUDER 0
if (soft_pwm_0 < slow_pwm_count) { if (soft_pwm_0 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_0 == 0) { if (state_timer_heater_0 == 0) {
// if change state set timer // if change state set timer
if (state_heater_0 == 1) { if (state_heater_0 == 1) {
state_timer_heater_0 = MIN_STATE_TIME; state_timer_heater_0 = MIN_STATE_TIME;
} }
state_heater_0 = 0; state_heater_0 = 0;
WRITE(HEATER_0_PIN, 0); WRITE(HEATER_0_PIN, 0);
@ -1586,30 +1587,30 @@ ISR(TIMER0_COMPB_vect)
#endif #endif
} }
} }
#if EXTRUDERS > 1 #if EXTRUDERS > 1
// EXTRUDER 1 // EXTRUDER 1
if (soft_pwm_1 < slow_pwm_count) { if (soft_pwm_1 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_1 == 0) { if (state_timer_heater_1 == 0) {
// if change state set timer // if change state set timer
if (state_heater_1 == 1) { if (state_heater_1 == 1) {
state_timer_heater_1 = MIN_STATE_TIME; state_timer_heater_1 = MIN_STATE_TIME;
} }
state_heater_1 = 0; state_heater_1 = 0;
WRITE(HEATER_1_PIN, 0); WRITE(HEATER_1_PIN, 0);
} }
} }
#endif #endif
#if EXTRUDERS > 2 #if EXTRUDERS > 2
// EXTRUDER 2 // EXTRUDER 2
if (soft_pwm_2 < slow_pwm_count) { if (soft_pwm_2 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_2 == 0) { if (state_timer_heater_2 == 0) {
// if change state set timer // if change state set timer
if (state_heater_2 == 1) { if (state_heater_2 == 1) {
state_timer_heater_2 = MIN_STATE_TIME; state_timer_heater_2 = MIN_STATE_TIME;
} }
state_heater_2 = 0; state_heater_2 = 0;
WRITE(HEATER_2_PIN, 0); WRITE(HEATER_2_PIN, 0);
@ -1620,33 +1621,33 @@ ISR(TIMER0_COMPB_vect)
#if EXTRUDERS > 3 #if EXTRUDERS > 3
// EXTRUDER 3 // EXTRUDER 3
if (soft_pwm_3 < slow_pwm_count) { if (soft_pwm_3 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_3 == 0) { if (state_timer_heater_3 == 0) {
// if change state set timer // if change state set timer
if (state_heater_3 == 1) { if (state_heater_3 == 1) {
state_timer_heater_3 = MIN_STATE_TIME; state_timer_heater_3 = MIN_STATE_TIME;
} }
state_heater_3 = 0; state_heater_3 = 0;
WRITE(HEATER_3_PIN, 0); WRITE(HEATER_3_PIN, 0);
} }
} }
#endif #endif
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1 #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// BED // BED
if (soft_pwm_b < slow_pwm_count) { if (soft_pwm_b < slow_pwm_count) {
// turn OFF heather only if the minimum time is up // turn OFF heather only if the minimum time is up
if (state_timer_heater_b == 0) { if (state_timer_heater_b == 0) {
// if change state set timer // if change state set timer
if (state_heater_b == 1) { if (state_heater_b == 1) {
state_timer_heater_b = MIN_STATE_TIME; state_timer_heater_b = MIN_STATE_TIME;
} }
state_heater_b = 0; state_heater_b = 0;
WRITE(HEATER_BED_PIN, 0); WRITE(HEATER_BED_PIN, 0);
} }
} }
#endif #endif
#ifdef FAN_SOFT_PWM #ifdef FAN_SOFT_PWM
if (pwm_count == 0){ if (pwm_count == 0){
soft_pwm_fan = fanSpeedSoftPwm / 2; soft_pwm_fan = fanSpeedSoftPwm / 2;
@ -1654,47 +1655,47 @@ ISR(TIMER0_COMPB_vect)
} }
if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0); if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
#endif #endif
pwm_count += (1 << SOFT_PWM_SCALE); pwm_count += (1 << SOFT_PWM_SCALE);
pwm_count &= 0x7f; pwm_count &= 0x7f;
// increment slow_pwm_count only every 64 pwm_count circa 65.5ms // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
if ((pwm_count % 64) == 0) { if ((pwm_count % 64) == 0) {
slow_pwm_count++; slow_pwm_count++;
slow_pwm_count &= 0x7f; slow_pwm_count &= 0x7f;
// Extruder 0 // Extruder 0
if (state_timer_heater_0 > 0) { if (state_timer_heater_0 > 0) {
state_timer_heater_0--; state_timer_heater_0--;
} }
#if EXTRUDERS > 1 #if EXTRUDERS > 1
// Extruder 1 // Extruder 1
if (state_timer_heater_1 > 0) if (state_timer_heater_1 > 0)
state_timer_heater_1--; state_timer_heater_1--;
#endif #endif
#if EXTRUDERS > 2 #if EXTRUDERS > 2
// Extruder 2 // Extruder 2
if (state_timer_heater_2 > 0) if (state_timer_heater_2 > 0)
state_timer_heater_2--; state_timer_heater_2--;
#endif #endif
#if EXTRUDERS > 3 #if EXTRUDERS > 3
// Extruder 3 // Extruder 3
if (state_timer_heater_3 > 0) if (state_timer_heater_3 > 0)
state_timer_heater_3--; state_timer_heater_3--;
#endif #endif
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1 #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// Bed // Bed
if (state_timer_heater_b > 0) if (state_timer_heater_b > 0)
state_timer_heater_b--; state_timer_heater_b--;
#endif #endif
} //if ((pwm_count % 64) == 0) { } //if ((pwm_count % 64) == 0) {
#endif //ifndef SLOW_PWM_HEATERS #endif //ifndef SLOW_PWM_HEATERS
switch(temp_state) { switch(temp_state) {
case 0: // Prepare TEMP_0 case 0: // Prepare TEMP_0
#if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1) #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
@ -1791,35 +1792,35 @@ ISR(TIMER0_COMPB_vect)
#endif #endif
temp_state = 10; //change so that Filament Width is also measured temp_state = 10; //change so that Filament Width is also measured
break; break;
case 10: //Prepare FILWIDTH case 10: //Prepare FILWIDTH
#if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1) #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
#if FILWIDTH_PIN>7 #if FILWIDTH_PIN>7
ADCSRB = 1<<MUX5; ADCSRB = 1<<MUX5;
#else #else
ADCSRB = 0; ADCSRB = 0;
#endif #endif
ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07)); ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion ADCSRA |= 1<<ADSC; // Start conversion
#endif #endif
lcd_buttons_update(); lcd_buttons_update();
temp_state = 11; temp_state = 11;
break; break;
case 11: //Measure FILWIDTH case 11: //Measure FILWIDTH
#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1) #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
//raw_filwidth_value += ADC; //remove to use an IIR filter approach //raw_filwidth_value += ADC; //remove to use an IIR filter approach
if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
{ {
raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128 raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
} }
#endif #endif
temp_state = 0; temp_state = 0;
temp_count++; temp_count++;
break; break;
case 12: //Startup, delay initial temp reading a tiny bit so the hardware can settle. case 12: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
temp_state = 0; temp_state = 0;
break; break;
@ -1828,7 +1829,7 @@ ISR(TIMER0_COMPB_vect)
// SERIAL_ERRORLNPGM("Temp measurement error!"); // SERIAL_ERRORLNPGM("Temp measurement error!");
// break; // break;
} }
if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
{ {
if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
@ -1851,12 +1852,12 @@ ISR(TIMER0_COMPB_vect)
current_temperature_bed_raw = raw_temp_bed_value; current_temperature_bed_raw = raw_temp_bed_value;
} }
//Add similar code for Filament Sensor - can be read any time since IIR filtering is used //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1) #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
#endif #endif
temp_meas_ready = true; temp_meas_ready = true;
temp_count = 0; temp_count = 0;
raw_temp_0_value = 0; raw_temp_0_value = 0;
@ -1947,12 +1948,12 @@ ISR(TIMER0_COMPB_vect)
} }
#endif #endif
} }
#ifdef BABYSTEPPING #ifdef BABYSTEPPING
for(uint8_t axis=0;axis<3;axis++) for(uint8_t axis=0;axis<3;axis++)
{ {
int curTodo=babystepsTodo[axis]; //get rid of volatile for performance int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
if(curTodo>0) if(curTodo>0)
{ {
babystep(axis,/*fwd*/true); babystep(axis,/*fwd*/true);
@ -1974,12 +1975,12 @@ ISR(TIMER0_COMPB_vect)
float scalePID_i(float i) float scalePID_i(float i)
{ {
return i*PID_dT; return i*PID_dT;
} }
float unscalePID_i(float i) float unscalePID_i(float i)
{ {
return i/PID_dT; return i/PID_dT;
} }
float scalePID_d(float d) float scalePID_d(float d)
@ -1989,7 +1990,7 @@ float scalePID_d(float d)
float unscalePID_d(float d) float unscalePID_d(float d)
{ {
return d*PID_dT; return d*PID_dT;
} }
#endif //PIDTEMP #endif //PIDTEMP

Loading…
Cancel
Save