diff --git a/ArduinoSlaveExtruder/ArduinoSlaveExtruder.pde b/ArduinoSlaveExtruder/ArduinoSlaveExtruder.pde index d6bbc69..62cae42 100644 --- a/ArduinoSlaveExtruder/ArduinoSlaveExtruder.pde +++ b/ArduinoSlaveExtruder/ArduinoSlaveExtruder.pde @@ -33,7 +33,6 @@ #include "PacketProcessor.h" #include "Extruder.h" #include "Heater.h" -#include "BuildPlatform.h" void init_serial(); void initialize(); @@ -80,8 +79,12 @@ void loop() //manage our extruder stuff. if (!is_tool_paused) { - manage_extruder_temperature(); - manage_platform_temperature(); + extruder_heater.manage_temperature(); + +#ifdef HAS_HEATED_BUILD_PLATFORM + platform_heater.manage_temperature(); +#endif + } } diff --git a/ArduinoSlaveExtruder/BuildPlatform.cpp b/ArduinoSlaveExtruder/BuildPlatform.cpp deleted file mode 100644 index ace106a..0000000 --- a/ArduinoSlaveExtruder/BuildPlatform.cpp +++ /dev/null @@ -1,162 +0,0 @@ -#include "Configuration.h" -#include "BuildPlatform.h" -#include "Variables.h" -#include "ThermistorTable.h" -#include "Heater.h" // for sample_temperature - -void set_platform_temperature(int temp) -{ - platform_target_temperature = temp; - platform_max_temperature = (int)((float)temp * 1.1); -} - -#ifdef PLATFORMTHERMISTOR_PIN -int read_platform_thermistor(); -#endif // PLATFORMTHERMISTOR_PIN - -#ifdef PLATFORMTHERMOCOUPLE_PIN -int read_platform_thermocouple(); -#endif // PLATFORMTHERMOCOUPLE_PIN - -/** - * Samples the temperature and converts it to degrees celsius. - * Returns degrees celsius. - */ -int get_platform_temperature() -{ -#ifdef PLATFORMTHERMISTOR_PIN - return read_platform_thermistor(); -#endif -#ifdef PLATFORMTHERMOCOUPLE_PIN - return read_platform_thermocouple(); -#endif -} - -/* -* This function gives us the temperature from the thermistor in Celsius - */ -#ifdef PLATFORMTHERMISTOR_PIN -int read_platform_thermistor() -{ - int raw = sample_temperature(PLATFORMTHERMISTOR_PIN); - - int celsius = 0; - byte i; - - for (i=1; i raw) - { - celsius = temptable[i-1][1] + - (raw - temptable[i-1][0]) * - (temptable[i][1] - temptable[i-1][1]) / - (temptable[i][0] - temptable[i-1][0]); - - if (celsius > 255) - celsius = 255; - - break; - } - } - - // Overflow: We just clamp to 0 degrees celsius - if (i == NUMTEMPS) - celsius = 0; - - return celsius; -} -#endif - -/* -* This function gives us the temperature from the thermocouple in Celsius - */ -#ifdef PLATFORMTHERMOCOUPLE_PIN -int read_platform_thermocouple() -{ - return ( 5.0 * sample_temperature(PLATFORMTHERMOCOUPLE_PIN) * 100.0) / 1024.0; -} -#endif - -int platform_temp_update(int dt); - -/*! - Manages motor and heater based on measured temperature: - o If temp is too low, don't start the motor - o Adjust the heater power to keep the temperature at the target - */ -void manage_platform_temperature() -{ - int output, dt; - unsigned long time; - - //make sure we know what our temp is. - platform_current_temperature = get_platform_temperature(); - - // ignoring millis rollover for now - time = millis(); - dt = time - platform_temp_prev_time; - - if (dt > TEMP_UPDATE_INTERVAL) - { - platform_temp_prev_time = time; - output = platform_temp_update(dt); - analogWrite(PLATFORMHEATER_PIN,output); - } -} - - -#if TEMP_PID -int platform_temp_update(int dt) -{ - int output; - int error; - float pTerm, iTerm, dTerm; - - if (platform_temp_control_enabled) { - error = platform_target_temperature - platform_current_temperature; - - pTerm = platform_temp_pGain * error; - - platform_temp_iState += error; - platform_temp_iState = constrain(platform_temp_iState, platform_temp_iState_min, platform_temp_iState_max); - iTerm = platform_temp_iGain * platform_temp_iState; - - dTerm = platform_temp_dGain * (platform_current_temperature - platform_temp_dState); - platform_temp_dState = platform_current_temperature; - - output = pTerm + iTerm - dTerm; - output = constrain(output, 0, 255); - } else { - output = 0; - } - return output; -} - -void platform_temp_pid_update_windup() -{ - platform_temp_iState_min = -TEMP_PID_INTEGRAL_DRIVE_MAX/platform_temp_iGain; - platform_temp_iState_max = TEMP_PID_INTEGRAL_DRIVE_MAX/platform_temp_iGain; -} - -# else -int platform_temp_update(int dt) -{ - int output; - - if (platform_temp_control_enabled) { - //put the heater into high mode if we're not at our target. - if (platform_current_temperature < platform_target_temperature) - output = heater_high; - //put the heater on low if we're at our target. - else if (platform_current_temperature < platform_max_temperature) - output = heater_low; - //turn the heater off if we're above our max. - else - output = 0; - } else { - output = 0; - } - return output; -} -#endif /* TEMP_PID */ - diff --git a/ArduinoSlaveExtruder/BuildPlatform.h b/ArduinoSlaveExtruder/BuildPlatform.h deleted file mode 100644 index 6088a8a..0000000 --- a/ArduinoSlaveExtruder/BuildPlatform.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef BUILDPLATFORM_H -#define BUILDPLATFORM_H - -#include - -void set_platform_temperature(int temp); - -void manage_platform_temperature(); -void platform_temp_pid_update_windup(); - -#endif // BUILDPLATFORM_H diff --git a/ArduinoSlaveExtruder/Configuration.h.dist b/ArduinoSlaveExtruder/Configuration.h.dist index 35c1fda..05b116c 100644 --- a/ArduinoSlaveExtruder/Configuration.h.dist +++ b/ArduinoSlaveExtruder/Configuration.h.dist @@ -31,6 +31,12 @@ #define MOTOR_REVERSE_DURATION 300 #define MOTOR_FORWARD_DURATION 300 +/**************************************************************************************** + * Is there a heated build platform? + ****************************************************************************************/ + +#define HAS_HEATED_BUILD_PLATFORM 1 + /**************************************************************************************** * Define the PID behavior for the heater ****************************************************************************************/ @@ -84,11 +90,17 @@ #define SERVO1_PIN 9 #define SERVO2_PIN 10 -#define HEATER_PIN 11 -#define FAN_PIN 12 -#define VALVE_PIN 15 +#define EXTRUDER_HEATER_PIN 11 +#define EXTRUDER_THERMISTOR_PIN 3 -#define THERMISTOR_PIN 3 +#ifdef HAS_HEATED_BUILD_PLATFORM + #define PLATFORM_HEATER_PIN 15 + #define PLATFORM_THERMISTOR_PIN 6 +#else + #define VALVE_PIN 15 +#endif + +#define FAN_PIN 12 #define DEBUG_PIN 13 diff --git a/ArduinoSlaveExtruder/Extruder.cpp b/ArduinoSlaveExtruder/Extruder.cpp index 965bf03..cc4c12c 100644 --- a/ArduinoSlaveExtruder/Extruder.cpp +++ b/ArduinoSlaveExtruder/Extruder.cpp @@ -1,7 +1,6 @@ #include "Configuration.h" #include "Extruder.h" #include "Heater.h" -#include "BuildPlatform.h" #include "Variables.h" #include "Timer1.h" #include "PacketProcessor.h" @@ -36,27 +35,18 @@ void init_extruder() iGain = SPEED_INITIAL_IGAIN; dGain = SPEED_INITIAL_DGAIN; - // Two Temp Zones - // 1. Extruder - extruder_temp_control_enabled = true; -#if TEMP_PID - extruder_temp_iState = 0; - extruder_temp_dState = 0; - extruder_temp_pGain = TEMP_PID_PGAIN; - extruder_temp_iGain = TEMP_PID_IGAIN; - extruder_temp_dGain = TEMP_PID_DGAIN; - extruder_temp_pid_update_windup(); -#endif - - // 2. Plateform - platform_temp_control_enabled = true; -#if TEMP_PID - platform_temp_iState = 0; - platform_temp_dState = 0; - platform_temp_pGain = TEMP_PID_PGAIN; - platform_temp_iGain = TEMP_PID_IGAIN; - platform_temp_dGain = TEMP_PID_DGAIN; - platform_temp_pid_update_windup(); +#ifdef EXTRUDER_THERMOCOUPLER_PIN + extruder_heater.init(EXTRUDER_THERMOCOUPLER_PIN, EXTRUDER_HEATER_PIN, true); +#else + extruder_heater.init(EXTRUDER_THERMISTOR_PIN, EXTRUDER_HEATER_PIN, false); +#endif + +#ifdef HAS_HEATED_BUILD_PLATFORM + #ifdef PLATFORM_THERMOCOUPLER_PIN + platform_heater.init(PLATFORM_THERMOCOUPLER_PIN, PLATFORM_HEATER_PIN, true); + #else + platform_heater.init(PLATFORM_THERMISTOR_PIN, PLATFORM_HEATER_PIN, false); + #endif #endif //encoder pins are for reading. @@ -83,26 +73,34 @@ void init_extruder() digitalWrite(MOTOR_2_DIR_PIN, HIGH); //setup our various accessory pins. - pinMode(HEATER_PIN, OUTPUT); + pinMode(EXTRUDER_HEATER_PIN, OUTPUT); pinMode(FAN_PIN, OUTPUT); - //pinMode(VALVE_PIN, OUTPUT); - pinMode(PLATFORMHEATER_PIN, OUTPUT); +#ifdef HAS_HEATED_BUILD_PLATFORM + pinMode(PLATFORM_HEATER_PIN, OUTPUT); +#else + pinMode(VALVE_PIN, OUTPUT); +#endif //turn them all off - digitalWrite(HEATER_PIN, LOW); + digitalWrite(EXTRUDER_HEATER_PIN, LOW); digitalWrite(FAN_PIN, LOW); -// digitalWrite(VALVE_PIN, LOW); - digitalWrite(PLATFORMHEATER_PIN, LOW); +#ifdef HAS_HEATED_BUILD_PLATFORM + digitalWrite(PLATFORM_HEATER_PIN, LOW); +#else + digitalWrite(VALVE_PIN, LOW); +#endif //setup our debug pin. pinMode(DEBUG_PIN, OUTPUT); digitalWrite(DEBUG_PIN, LOW); //default to zero. - set_extruder_temperature(0); + extruder_heater.set_target_temperature(0); +#ifdef HAS_HEATED_BUILD_PLATFORM //default build platform to 60 C - set_platform_temperature(60); + platform_heater.set_target_temperature(60); +#endif setupTimer1Interrupt(); } @@ -284,20 +282,22 @@ void disable_fan() digitalWrite(FAN_PIN, LOW); } +#ifndef HAS_HEATED_BUILD_PLATFORM void open_valve() { - //digitalWrite(VALVE_PIN, HIGH); + digitalWrite(VALVE_PIN, HIGH); } void close_valve() { - //digitalWrite(VALVE_PIN, LOW); + digitalWrite(VALVE_PIN, LOW); } +#endif byte is_tool_ready() { //are we within 5% of the temperature? - if (extruder_current_temperature > (int)(extruder_target_temperature * 0.95)) + if (extruder_heater.hasReachedTargetTemperature()) return 1; else return 0; diff --git a/ArduinoSlaveExtruder/Extruder.h b/ArduinoSlaveExtruder/Extruder.h index 9549c0e..037389d 100644 --- a/ArduinoSlaveExtruder/Extruder.h +++ b/ArduinoSlaveExtruder/Extruder.h @@ -19,8 +19,10 @@ void disable_motor_2(); void enable_fan(); void disable_fan(); +#ifndef HAS_HEATED_BUILD_PLATFORM void open_valve(); void close_valve(); +#endif byte is_tool_ready(); diff --git a/ArduinoSlaveExtruder/Heater.cpp b/ArduinoSlaveExtruder/Heater.cpp index a2e47fa..9681a18 100644 --- a/ArduinoSlaveExtruder/Heater.cpp +++ b/ArduinoSlaveExtruder/Heater.cpp @@ -3,41 +3,57 @@ #include "Variables.h" #include "ThermistorTable.h" -void set_extruder_temperature(int temp) +void Heater::init(int inPin, int outPin, bool isThermocoupler) { - extruder_target_temperature = temp; - extruder_max_temperature = (int)((float)temp * 1.1); + usesThermocoupler = isThermocoupler; + inputPin = inPin; + outputPin = outPin; + + temp_control_enabled = true; +#if TEMP_PID + temp_iState = 0; + temp_dState = 0; + temp_pGain = TEMP_PID_PGAIN; + temp_iGain = TEMP_PID_IGAIN; + temp_dGain = TEMP_PID_DGAIN; + + temp_pid_update_windup(); +#endif + + current_temperature = 0; + target_temperature = 0; + max_temperature = 0; } -#ifdef THERMISTOR_PIN -int read_thermistor(); -#endif // THERMISTOR_PIN +void Heater::set_target_temperature(int temp) +{ + target_temperature = temp; + max_temperature = (int)((float)temp * 1.1); +} -#ifdef THERMOCOUPLE_PIN -int read_thermocouple(); -#endif // THERMOCOUPLE_PIN +bool Heater::hasReachedTargetTemperature() +{ + return (current_temperature > (int)(target_temperature * 0.95)); +} /** * Samples the temperature and converts it to degrees celsius. * Returns degrees celsius. */ -int get_temperature() +int Heater::get_current_temperature() { -#ifdef THERMISTOR_PIN - return read_thermistor(); -#endif -#ifdef THERMOCOUPLE_PIN - return read_thermocouple(); -#endif + if(usesThermocoupler) + return read_thermocouple(); + else + return read_thermistor(); } /* * This function gives us the temperature from the thermistor in Celsius */ -#ifdef THERMISTOR_PIN -int read_thermistor() +int Heater::read_thermistor() { - int raw = sample_temperature(THERMISTOR_PIN); + int raw = sample_temperature(); int celsius = 0; byte i; @@ -64,28 +80,25 @@ int read_thermistor() return celsius; } -#endif /* * This function gives us the temperature from the thermocouple in Celsius */ -#ifdef THERMOCOUPLE_PIN -int read_thermocouple() +int Heater::read_thermocouple() { - return ( 5.0 * sample_temperature(THERMOCOUPLE_PIN) * 100.0) / 1024.0; + return ( 5.0 * sample_temperature() * 100.0) / 1024.0; } -#endif /* * This function gives us an averaged sample of the analog temperature pin. */ -int sample_temperature(uint8_t pin) +int Heater::sample_temperature() { int raw = 0; //read in a certain number of samples for (byte i=0; i TEMP_UPDATE_INTERVAL) { - extruder_temp_prev_time = time; + temp_prev_time = time; output = temp_update(dt); - analogWrite(HEATER_PIN,output); + analogWrite(outputPin, output); } } #if TEMP_PID -int temp_update(int dt) +int Heater::temp_update(int dt) { int output; int error; float pTerm, iTerm, dTerm; - if (extruder_temp_control_enabled) { - error = extruder_target_temperature - extruder_current_temperature; + if (temp_control_enabled) { + error = target_temperature - current_temperature; - pTerm = extruder_temp_pGain * error; + pTerm = temp_pGain * error; - extruder_temp_iState += error; - extruder_temp_iState = constrain(extruder_temp_iState, extruder_temp_iState_min, extruder_temp_iState_max); - iTerm = extruder_temp_iGain * extruder_temp_iState; + temp_iState += error; + temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max); + iTerm = temp_iGain * temp_iState; - dTerm = extruder_temp_dGain * (extruder_current_temperature - extruder_temp_dState); - extruder_temp_dState = extruder_current_temperature; + dTerm = temp_dGain * (current_temperature - temp_dState); + temp_dState = current_temperature; output = pTerm + iTerm - dTerm; output = constrain(output, 0, 255); @@ -149,23 +161,24 @@ int temp_update(int dt) return output; } -void extruder_temp_pid_update_windup() +void Heater::temp_pid_update_windup() { - extruder_temp_iState_min = -TEMP_PID_INTEGRAL_DRIVE_MAX/extruder_temp_iGain; - extruder_temp_iState_max = TEMP_PID_INTEGRAL_DRIVE_MAX/extruder_temp_iGain; + temp_iState_min = -TEMP_PID_INTEGRAL_DRIVE_MAX/temp_iGain; + temp_iState_max = TEMP_PID_INTEGRAL_DRIVE_MAX/temp_iGain; } -# else -int temp_update(int dt) +#else + +int Heater::temp_update(int dt) { int output; - if (extruder_temp_control_enabled) { + if (temp_control_enabled) { //put the heater into high mode if we're not at our target. - if (extruder_current_temperature < extruder_target_temperature) + if (current_temperature < target_temperature) output = heater_high; //put the heater on low if we're at our target. - else if (extruder_current_temperature < extruder_max_temperature) + else if (current_temperature < max_temperature) output = heater_low; //turn the heater off if we're above our max. else diff --git a/ArduinoSlaveExtruder/Heater.h b/ArduinoSlaveExtruder/Heater.h index 6150045..432e3d5 100644 --- a/ArduinoSlaveExtruder/Heater.h +++ b/ArduinoSlaveExtruder/Heater.h @@ -3,10 +3,46 @@ #include -void set_extruder_temperature(int temp); -int sample_temperature(uint8_t pin); +class Heater +{ + private: + bool usesThermocoupler; + int inputPin; + int outputPin; + + int current_temperature; + int target_temperature; + int max_temperature; -void manage_extruder_temperature(); -void extruder_temp_pid_update_windup(); + bool temp_control_enabled; + unsigned long temp_prev_time; // ms + +#if TEMP_PID + float temp_pGain; + float temp_iGain; + float temp_dGain; + + int temp_dState; + long temp_iState; + float temp_iState_max; // set in update_windup + float temp_iState_min; // set in update_windup +#endif + + protected: + int read_thermistor(); + int read_thermocouple(); + int sample_temperature(); + int temp_update(int dt); + + public: + void init(int inPin, int outPin, bool isThermocoupler); + + int get_current_temperature(); + void set_target_temperature(int temp); + bool hasReachedTargetTemperature(); + + void manage_temperature(); + void temp_pid_update_windup(); +}; #endif // HEATER_H diff --git a/ArduinoSlaveExtruder/PacketProcessor.cpp b/ArduinoSlaveExtruder/PacketProcessor.cpp index 878e440..c3587cc 100644 --- a/ArduinoSlaveExtruder/PacketProcessor.cpp +++ b/ArduinoSlaveExtruder/PacketProcessor.cpp @@ -150,23 +150,25 @@ void handle_query() //WORKING case SLAVE_CMD_GET_TEMP: - masterPacket.add_16(extruder_current_temperature); + masterPacket.add_16(extruder_heater.get_current_temperature()); break; //WORKING case SLAVE_CMD_SET_TEMP: - extruder_target_temperature = masterPacket.get_16(2); + extruder_heater.set_target_temperature(masterPacket.get_16(2)); break; - //NEEDS TESTING +#ifdef HAS_HEATED_BUILD_PLATFORM + //WORKING case SLAVE_CMD_GET_PLATFORM_TEMP: - masterPacket.add_16(platform_current_temperature); + masterPacket.add_16(platform_heater.get_current_temperature()); break; - //NEEDS TESTING + //WORKING case SLAVE_CMD_SET_PLATFORM_TEMP: - platform_target_temperature = masterPacket.get_16(2); + platform_heater.set_target_temperature(masterPacket.get_16(2)); break; +#endif //WORKING case SLAVE_CMD_SET_MOTOR_1_PWM: @@ -259,7 +261,8 @@ void handle_query() disable_fan(); break; - //WORKING +#ifndef HAS_HEATED_BUILD_PLATFORM +//WORKING case SLAVE_CMD_TOGGLE_VALVE: temp = masterPacket.get_8(2); if (temp & 1) @@ -267,6 +270,7 @@ void handle_query() else close_valve(); break; +#endif //WORKING case SLAVE_CMD_SET_SERVO_1_POS: diff --git a/ArduinoSlaveExtruder/Variables.cpp b/ArduinoSlaveExtruder/Variables.cpp index df51ae5..25b5cb6 100644 --- a/ArduinoSlaveExtruder/Variables.cpp +++ b/ArduinoSlaveExtruder/Variables.cpp @@ -7,16 +7,11 @@ unsigned int master_version = 0; //are we paused? bool is_tool_paused = false; -// Two Temp Zones -// 1. Extruder -int extruder_current_temperature = 0; -int extruder_target_temperature = 0; -int extruder_max_temperature = 0; - -// 2. Platform -int platform_current_temperature = 0; -int platform_target_temperature = 0; -int platform_max_temperature = 0; +// The temperature controller(s) +Heater extruder_heater; +#ifdef HAS_HEATED_BUILD_PLATFORM +Heater platform_heater; +#endif int heater_low = 64; int heater_high = 255; diff --git a/ArduinoSlaveExtruder/Variables.h b/ArduinoSlaveExtruder/Variables.h index f3347f9..cccbb3d 100644 --- a/ArduinoSlaveExtruder/Variables.h +++ b/ArduinoSlaveExtruder/Variables.h @@ -5,6 +5,7 @@ #include "Datatypes.h" #include #include "WProgram.h" +#include "Heater.h" //this is the version of our host software extern unsigned int master_version; @@ -13,15 +14,10 @@ extern unsigned int master_version; extern bool is_tool_paused; // Two Temp Zones -// 1. Extruder -extern int extruder_current_temperature; -extern int extruder_target_temperature; -extern int extruder_max_temperature; - -// 2. Platform -extern int platform_current_temperature; -extern int platform_target_temperature; -extern int platform_max_temperature; +extern Heater extruder_heater; +#ifdef HAS_HEATED_BUILD_PLATFORM +extern Heater platform_heater; +#endif extern int heater_low; extern int heater_high; @@ -65,36 +61,4 @@ extern volatile long stepper_ticks; extern volatile byte stepper_high_pwm; extern volatile byte stepper_low_pwm; -// Two Temp Zones - -// 1. Extruder -extern boolean extruder_temp_control_enabled; -extern unsigned long extruder_temp_prev_time; // ms - -#if TEMP_PID -extern float extruder_temp_pGain; -extern float extruder_temp_iGain; -extern float extruder_temp_dGain; - -extern int extruder_temp_dState; -extern long extruder_temp_iState; -extern float extruder_temp_iState_max; // set in update_windup -extern float extruder_temp_iState_min; // set in update_windup -#endif - -// 2. Platform -extern boolean platform_temp_control_enabled; -extern unsigned long platform_temp_prev_time; // ms - -#if TEMP_PID -extern float platform_temp_pGain; -extern float platform_temp_iGain; -extern float platform_temp_dGain; - -extern int platform_temp_dState; -extern long platform_temp_iState; -extern float platform_temp_iState_max; // set in update_windup -extern float platform_temp_iState_min; // set in update_windup -#endif - #endif // VARIABLES_H diff --git a/ArduinoSlaveExtruder/Version.h b/ArduinoSlaveExtruder/Version.h index c8ca198..7655ead 100644 --- a/ArduinoSlaveExtruder/Version.h +++ b/ArduinoSlaveExtruder/Version.h @@ -2,6 +2,6 @@ #define VERSION_H //this is our firmware version -#define FIRMWARE_VERSION 106 +#define FIRMWARE_VERSION 107 #endif // VERSION_H