diff --git a/extruder/Makefile b/extruder/Makefile index dd18c29..5b4518b 100644 --- a/extruder/Makefile +++ b/extruder/Makefile @@ -31,7 +31,7 @@ PROGRAM = extruder -SOURCES = $(PROGRAM).c intercom.c delay.c analog.c watchdog.c +SOURCES = $(PROGRAM).c intercom.c delay.c analog.c watchdog.c heater.c temp.c ############################################################################## # # diff --git a/extruder/config.h.dist b/extruder/config.h.dist index b11dfb8..cd6d41a 100644 --- a/extruder/config.h.dist +++ b/extruder/config.h.dist @@ -26,15 +26,49 @@ #define TRIM_POT AIO0 #define TRIM_POT_CHANNEL 0 -//Debug LED will blink on RS485 transmission -#define DEBUG_LED DIO13 - //Read analog voltage from thermistor #define TEMP_PIN AIO3 #define TEMP_PIN_CHANNEL 3 #define REFERENCE REFERENCE_AVCC +#define ANALOG_MASK (MASK(TRIM_POT_CHANNEL) | MASK(TEMP_PIN_CHANNEL)) + +#define TEMP_THERMISTOR + +// extruder settings +#define TEMP_HYSTERESIS 20 +#define TEMP_RESIDENCY_TIME 60 + +#define NUM_TEMP_SENSORS 1 +#ifdef TEMP_C +/***************************************************************************\ +* * +* Fill in the following struct according to your hardware * +* * +* If your temperature sensor has no associated heater, enter '255' as the * +* heater index. * +* * +* for GEN3 set temp_type to TT_INTERCOM, temp_pin to 0 and heater index to * +* 255 * +* * +\***************************************************************************/ + +struct { + uint8_t temp_type; + uint8_t temp_pin; + + uint8_t heater_index; +} temp_sensors[NUM_TEMP_SENSORS] = +{ + { + TT_THERMISTOR, + PINC3, + 0 + } +}; +#endif + // list of PWM-able pins and corresponding timers // timer1 is used for step timing so don't use OC1A/OC1B (DIO9/DIO10) // OC0A DIO6 @@ -44,12 +78,46 @@ // OC2A DIO11 // OC2B DIO3 -#define HEATER_PIN DIO11 -#define HEATER_PWM OCR2A +#define TH_COUNT 8 +#define PID_SCALE 1024L -#define BED_PIN DIO12 +#define NUM_HEATERS 2 +#ifdef HEATER_C +/***************************************************************************\ +* * +* Fill in the following struct according to your hardware * +* * +* For the atmega168/328, timer/pin mappings are as follows * +* * +* OCR0A - PD6 * +* OCR0B - PD5 * +* OCR2A - PB3 * +* OCR2B - PD3 * +* * +\***************************************************************************/ +struct { + volatile uint8_t *heater_port; + uint8_t heater_pin; + volatile uint8_t *heater_pwm; +} heaters[NUM_HEATERS] = +{ + { + &PORTD, + PIND6, + &OCR0A + }, + { + &PORTB, + PINB4, + 0 + } +}; +#endif -#define ANALOG_MASK (MASK(TRIM_POT_CHANNEL) | MASK(TEMP_PIN_CHANNEL)) +// #define HEATER_PIN DIO11 +// #define HEATER_PWM OCR2A +// +// #define BED_PIN DIO12 /* Intercom @@ -68,12 +136,12 @@ Heater */ -#ifdef HEATER_PWM - #define enable_heater() do { TCCR2A |= MASK(COM2A1); } while (0) - #define disable_heater() do { TCCR2A &= ~MASK(COM2A1); } while (0) -#else - #define enable_heater() WRITE(HEATER_PIN, 1) - #define disable_heater() WRITE(HEATER_PIN, 0) -#endif +// #ifdef HEATER_PWM +// #define enable_heater() do { TCCR2A |= MASK(COM2A1); } while (0) +// #define disable_heater() do { TCCR2A &= ~MASK(COM2A1); } while (0) +// #else +// #define enable_heater() WRITE(HEATER_PIN, 1) +// #define disable_heater() WRITE(HEATER_PIN, 0) +// #endif #endif /* _CONFIG_H */ diff --git a/extruder/debug.h b/extruder/debug.h new file mode 120000 index 0000000..845c7cc --- /dev/null +++ b/extruder/debug.h @@ -0,0 +1 @@ +../debug.h \ No newline at end of file diff --git a/extruder/extruder.c b/extruder/extruder.c index 7d11b70..cc4bbb0 100644 --- a/extruder/extruder.c +++ b/extruder/extruder.c @@ -7,33 +7,11 @@ #include "analog.h" #include "config.h" #include "watchdog.h" +#include "heater.h" +#include "temp.h" static uint8_t motor_pwm; -#define NUMTEMPS 20 -short temptable[NUMTEMPS][2] = { - {1, 841}, - {54, 255}, - {107, 209}, - {160, 184}, - {213, 166}, - {266, 153}, - {319, 142}, - {372, 132}, - {425, 124}, - {478, 116}, - {531, 108}, - {584, 101}, - {637, 93}, - {690, 86}, - {743, 78}, - {796, 70}, - {849, 61}, - {902, 50}, - {955, 34}, - {1008, 3} -}; - void io_init(void) { // setup I/O pins WRITE(DEBUG_LED, 0); SET_OUTPUT(DEBUG_LED); @@ -60,14 +38,14 @@ void io_init(void) { WRITE(BED_PIN, 0); SET_OUTPUT(BED_PIN); #endif - #if defined(HEATER_PWM) || defined(FAN_PWM) || defined(BED_PWM) +// #if defined(HEATER_PWM) || defined(FAN_PWM) || defined(BED_PWM) // setup PWM timer: fast PWM, no prescaler TCCR2A = MASK(WGM21) | MASK(WGM20); TCCR2B = MASK(CS22); TIMSK2 = 0; OCR2A = 0; OCR2B = 0; - #endif +// #endif #if defined(H1E_PWM) && defined(H2E_PWM) TCCR0A = MASK(WGM01) | MASK(WGM00); @@ -174,6 +152,12 @@ void init(void) { // set up inputs and outputs io_init(); + // temp sensor + temp_init(); + + // heater + heater_init(); + // set up extruder motor driver motor_init(); @@ -187,9 +171,6 @@ void init(void) { int main (void) { - static uint8_t i; - static uint16_t raw_temp; - init(); enable_heater(); @@ -204,26 +185,9 @@ int main (void) //Read motor PWM motor_pwm = analog_read(TRIM_POT_CHANNEL) >> 2; - //Read current temperature - raw_temp = analog_read(TEMP_PIN_CHANNEL); + temp_sensor_tick(); - //Calculate real temperature based on lookup table - for (i = 1; i < NUMTEMPS; i++) { - if (temptable[i][0] > raw_temp) { - raw_temp = temptable[i][1] + - (temptable[i][0] - raw_temp) * (temptable[i-1][1] - temptable[i][1]) / (temptable[i][0] - temptable[i-1][0]); - - break; - } - } - - //Clamp for overflows - if (i == NUMTEMPS) raw_temp = temptable[NUMTEMPS-1][1]; - if (raw_temp > 255) raw_temp = 255; - - //Update the intercom values - update_send_cmd(raw_temp); - - HEATER_PWM = get_read_cmd(); + update_send_cmd(temp_get(0) >> 2); + temp_set(0, get_read_cmd()); } } diff --git a/extruder/heater.c b/extruder/heater.c new file mode 120000 index 0000000..acfa7db --- /dev/null +++ b/extruder/heater.c @@ -0,0 +1 @@ +../heater.c \ No newline at end of file diff --git a/extruder/heater.h b/extruder/heater.h new file mode 120000 index 0000000..22c6069 --- /dev/null +++ b/extruder/heater.h @@ -0,0 +1 @@ +../heater.h \ No newline at end of file diff --git a/extruder/temp.c b/extruder/temp.c new file mode 120000 index 0000000..f59d719 --- /dev/null +++ b/extruder/temp.c @@ -0,0 +1 @@ +../temp.c \ No newline at end of file diff --git a/extruder/temp.h b/extruder/temp.h new file mode 120000 index 0000000..5f7682d --- /dev/null +++ b/extruder/temp.h @@ -0,0 +1 @@ +../temp.h \ No newline at end of file diff --git a/heater.c b/heater.c index 68c6798..6c35b39 100644 --- a/heater.c +++ b/heater.c @@ -4,9 +4,11 @@ #include #include "arduino.h" -#include "timer.h" +// #include "timer.h" #include "debug.h" -#include "sersendf.h" +#ifdef DEBUG + #include "sersendf.h" +#endif #define HEATER_C #include "config.h" @@ -124,10 +126,12 @@ void heater_tick(uint8_t h, uint16_t current_temp, uint16_t target_temp) { heaters_runtime[h].pid_output = 0; else heaters_runtime[h].pid_output = pid_output_intermed & 0xFF; - + + #ifdef DEBUG if (debug_flags & DEBUG_PID) sersendf_P(PSTR("T{E:%d, P:%d * %ld = %ld / I:%d * %ld = %ld / D:%d * %ld = %ld # O: %ld = %u}\n"), t_error, heaters_runtime[h].heater_p, heaters_pid[h].p_factor, (int32_t) heaters_runtime[h].heater_p * heaters_pid[h].p_factor / PID_SCALE, heaters_runtime[h].heater_i, heaters_pid[h].i_factor, (int32_t) heaters_runtime[h].heater_i * heaters_pid[h].i_factor / PID_SCALE, heaters_runtime[h].heater_d, heaters_pid[h].d_factor, (int32_t) heaters_runtime[h].heater_d * heaters_pid[h].d_factor / PID_SCALE, pid_output_intermed, heaters_runtime[h].pid_output); - + #endif + heater_set(h, heaters_runtime[h].pid_output); } diff --git a/mendel.c b/mendel.c index 76e8305..e1b2af7 100644 --- a/mendel.c +++ b/mendel.c @@ -35,15 +35,33 @@ void io_init(void) { WRITE(X_STEP_PIN, 0); SET_OUTPUT(X_STEP_PIN); WRITE(X_DIR_PIN, 0); SET_OUTPUT(X_DIR_PIN); WRITE(X_MIN_PIN, 1); SET_INPUT(X_MIN_PIN); + #ifdef X_MAX_PIN + WRITE(X_MAX_PIN, 1); SET_INPUT(X_MAX_PIN); + #endif + #ifdef X_ENABLE_PIN + WRITE(X_ENABLE_PIN, 1); SET_OUTPUT(X_ENABLE_PIN); + #endif WRITE(Y_STEP_PIN, 0); SET_OUTPUT(Y_STEP_PIN); WRITE(Y_DIR_PIN, 0); SET_OUTPUT(Y_DIR_PIN); WRITE(Y_MIN_PIN, 1); SET_INPUT(Y_MIN_PIN); - + #ifdef Y_MAX_PIN + WRITE(Y_MAX_PIN, 1); SET_INPUT(Y_MAX_PIN); + #endif + #ifdef Y_ENABLE_PIN + WRITE(Y_ENABLE_PIN, 1); SET_OUTPUT(Y_ENABLE_PIN); + #endif + WRITE(Z_STEP_PIN, 0); SET_OUTPUT(Z_STEP_PIN); WRITE(Z_DIR_PIN, 0); SET_OUTPUT(Z_DIR_PIN); WRITE(Z_MIN_PIN, 1); SET_INPUT(Z_MIN_PIN); - + #ifdef Z_MAX_PIN + WRITE(Z_MAX_PIN, 1); SET_INPUT(Z_MAX_PIN); + #endif + #ifdef Z_ENABLE_PIN + WRITE(Z_ENABLE_PIN, 1); SET_OUTPUT(Z_ENABLE_PIN); + #endif + WRITE(E_STEP_PIN, 0); SET_OUTPUT(E_STEP_PIN); WRITE(E_DIR_PIN, 0); SET_OUTPUT(E_DIR_PIN); diff --git a/temp.c b/temp.c index 92edd3c..dca7a4f 100644 --- a/temp.c +++ b/temp.c @@ -5,9 +5,11 @@ #include #include "arduino.h" -#include "timer.h" +// #include "timer.h" #include "debug.h" -#include "sersendf.h" +#ifndef EXTRUDER + #include "sersendf.h" +#endif #include "heater.h" #ifdef GEN3 #include "intercom.h" @@ -189,7 +191,7 @@ void temp_sensor_tick() { temp = temptable[NUMTEMPS-1][1] * 4; temp_sensors_runtime[i].next_read_time = 0; - } + } while (0); break; #endif /* TEMP_THERMISTOR */ @@ -253,6 +255,11 @@ void temp_set(uint8_t index, uint16_t temperature) { #endif } +uint16_t temp_get(uint8_t index) { + return temp_sensors_runtime[index].last_read_temp; +} + +#ifndef EXTRUDER void temp_print(uint8_t index) { uint8_t c = 0; @@ -260,3 +267,4 @@ void temp_print(uint8_t index) { sersendf_P(PSTR("T: %u.%u\n"), temp_sensors_runtime[index].last_read_temp >> 2, c); } +#endif diff --git a/temp.h b/temp.h index c912760..b00ee95 100644 --- a/temp.h +++ b/temp.h @@ -20,6 +20,8 @@ void temp_sensor_tick(void); uint8_t temp_achieved(void); void temp_set(uint8_t index, uint16_t temperature); +uint16_t temp_get(uint8_t index); + void temp_print(uint8_t index); uint16_t temp_read(uint8_t index);