diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..15e63e0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ + +* text + +*.gcode -text + +*.gz -text +*.zip -text + +*.png -text +*.jpg -text +*.jpeg -text +*.pdf -text + diff --git a/extruder/config.h.dist b/extruder/config.h.dist index c842060..83d7707 100644 --- a/extruder/config.h.dist +++ b/extruder/config.h.dist @@ -1,95 +1,95 @@ /* Notice to developers: this file is intentionally included twice. */ - -/* - CPU clock rate -*/ -#ifndef F_CPU - #define F_CPU 16000000L -#endif - -/* - other fallbacks for the Arduino IDE -*/ -#define EXTRUDER -#define GEN3 - -#include "arduino.h" - -// controller index- bus is multidrop after all -#define THIS_CONTROLLER_NUM 0 - -//RS485 Interface pins -#define RX_ENABLE_PIN DIO4 -#define TX_ENABLE_PIN AIO2 - -// Control pins for the A3949 chips -#define H1D DIO7 -#define H1E DIO5 -#define H2D DIO8 -#define H2E DIO6 - -// PWM versions of the enable_pins -#define H1E_PWM OCR0B -#define H2E_PWM OCR0A - -//Step/Dir Pins from motherboard to extruder -//IMPORTANT: Assumes that the step pin is on PCIE0 -#define E_STEP_PIN DIO10 -#define E_DIR_PIN DIO9 - -//Trimpot is on AIO0, pin 23 -#define TRIM_POT AIO0 -#define TRIM_POT_CHANNEL 0 - -//Read analog voltage from thermistor -#define TEMP_PIN AIO3 -#define TEMP_PIN_CHANNEL 3 - -//Read analog voltage from thermistor -#define TEMP_BED_PIN AIO6 -#define TEMP_BED_PIN_CHANNEL 6 - - -#define REFERENCE REFERENCE_AVCC - -#define TEMP_THERMISTOR - -#define HEATER_PIN DIO11 -#define BED_PIN AIO1 -#define FAN_PIN DIO12 - -// extruder settings + +/* + CPU clock rate +*/ +#ifndef F_CPU + #define F_CPU 16000000L +#endif + +/* + other fallbacks for the Arduino IDE +*/ +#define EXTRUDER +#define GEN3 + +#include "arduino.h" + +// controller index- bus is multidrop after all +#define THIS_CONTROLLER_NUM 0 + +//RS485 Interface pins +#define RX_ENABLE_PIN DIO4 +#define TX_ENABLE_PIN AIO2 + +// Control pins for the A3949 chips +#define H1D DIO7 +#define H1E DIO5 +#define H2D DIO8 +#define H2E DIO6 + +// PWM versions of the enable_pins +#define H1E_PWM OCR0B +#define H2E_PWM OCR0A + +//Step/Dir Pins from motherboard to extruder +//IMPORTANT: Assumes that the step pin is on PCIE0 +#define E_STEP_PIN DIO10 +#define E_DIR_PIN DIO9 + +//Trimpot is on AIO0, pin 23 +#define TRIM_POT AIO0 +#define TRIM_POT_CHANNEL 0 + +//Read analog voltage from thermistor +#define TEMP_PIN AIO3 +#define TEMP_PIN_CHANNEL 3 + +//Read analog voltage from thermistor +#define TEMP_BED_PIN AIO6 +#define TEMP_BED_PIN_CHANNEL 6 + + +#define REFERENCE REFERENCE_AVCC + +#define TEMP_THERMISTOR + +#define HEATER_PIN DIO11 +#define BED_PIN AIO1 +#define FAN_PIN DIO12 + +// extruder settings #define TEMP_HYSTERESIS 5 -#define TEMP_RESIDENCY_TIME 60 - -#ifdef DEFINE_TEMP_SENSOR -DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, TEMP_PIN_CHANNEL, THERMISTOR_EXTRUDER) -DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, TEMP_BED_PIN_CHANNEL, THERMISTOR_EXTRUDER) -// dummy temp sensor so analog_mask includes trim pot -DEFINE_TEMP_SENSOR(noheater, TT_THERMISTOR, TRIM_POT_CHANNEL, 0) -#endif - -#ifdef DEFINE_HEATER -DEFINE_HEATER(extruder, DIO11) -DEFINE_HEATER(bed, AIO1) -#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 -// OC0B DIO5 -// OC1A DIO9 -// OC1B DIO10 -// OC2A DIO11 -// OC2B DIO3 - -#define TH_COUNT 8 -#define PID_SCALE 1024L - - -/* - Motors -*/ - -#define enable_motors() do { TCCR0A |= MASK(COM0A1) | MASK(COM0B1); } while (0) -#define disable_motors() do { TCCR0A &= ~MASK(COM0A1) & ~MASK(COM0B1); } while (0) +#define TEMP_RESIDENCY_TIME 60 + +#ifdef DEFINE_TEMP_SENSOR +DEFINE_TEMP_SENSOR(extruder, TT_THERMISTOR, TEMP_PIN_CHANNEL, THERMISTOR_EXTRUDER) +DEFINE_TEMP_SENSOR(bed, TT_THERMISTOR, TEMP_BED_PIN_CHANNEL, THERMISTOR_EXTRUDER) +// dummy temp sensor so analog_mask includes trim pot +DEFINE_TEMP_SENSOR(noheater, TT_THERMISTOR, TRIM_POT_CHANNEL, 0) +#endif + +#ifdef DEFINE_HEATER +DEFINE_HEATER(extruder, DIO11) +DEFINE_HEATER(bed, AIO1) +#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 +// OC0B DIO5 +// OC1A DIO9 +// OC1B DIO10 +// OC2A DIO11 +// OC2B DIO3 + +#define TH_COUNT 8 +#define PID_SCALE 1024L + + +/* + Motors +*/ + +#define enable_motors() do { TCCR0A |= MASK(COM0A1) | MASK(COM0B1); } while (0) +#define disable_motors() do { TCCR0A &= ~MASK(COM0A1) & ~MASK(COM0B1); } while (0) diff --git a/extruder/extruder.c b/extruder/extruder.c index b874710..b8633cc 100644 --- a/extruder/extruder.c +++ b/extruder/extruder.c @@ -1,238 +1,238 @@ -#include -#include - -#include - -#include "intercom.h" -#include "analog.h" -#include "config_wrapper.h" -#include "watchdog.h" -#include "heater.h" -#include "temp.h" -#include "timer.h" - -static uint8_t motor_pwm; - -void io_init(void) { - // setup I/O pins - WRITE(DEBUG_LED, 0); SET_OUTPUT(DEBUG_LED); - WRITE(H1D,0); SET_OUTPUT(H1D); - WRITE(H1E,0); SET_OUTPUT(H1E); - WRITE(H2D,0); SET_OUTPUT(H2D); - WRITE(H2E,0); SET_OUTPUT(H2E); - - SET_INPUT(TRIM_POT); - SET_INPUT(TEMP_PIN); - SET_INPUT(TEMP_BED_PIN); - SET_INPUT(E_STEP_PIN); - SET_INPUT(E_DIR_PIN); - - // use pull up resistors to avoid noise - WRITE(E_STEP_PIN, 1); - WRITE(E_DIR_PIN, 1); - - //Enable the RS485 transceiver - SET_OUTPUT(RX_ENABLE_PIN); - SET_OUTPUT(TX_ENABLE_PIN); - WRITE(RX_ENABLE_PIN,0); - disable_transmit(); - - #ifdef HEATER_PIN - WRITE(HEATER_PIN, 0); SET_OUTPUT(HEATER_PIN); - #endif - - #ifdef BED_PIN - WRITE(BED_PIN, 0); SET_OUTPUT(BED_PIN); - #endif - - #ifdef FAN_PIN - WRITE(FAN_PIN, 0); SET_OUTPUT(FAN_PIN); - #endif - -// #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 - - #if defined(H1E_PWM) && defined(H2E_PWM) - TCCR0A = MASK(WGM01) | MASK(WGM00); - TCCR0B = MASK(CS20); - TIMSK0 = 0; - OCR0A = 0; - OCR0B = 0; - #endif -} - -void motor_init(void) { - //Enable an interrupt to be triggered when the step pin changes - //This will be PCIE0 - PCICR = MASK(PCIE0); - PCMSK0 = MASK(PCINT2); -} - -ISR(PCINT0_vect) { - static uint8_t coil_pos, pwm; - - //if the step pin is high, we advance the motor - if (READ(E_STEP_PIN)) { - - //Turn on motors only on first tick to save power I guess - enable_motors(); - - //Advance the coil position - if (READ(E_DIR_PIN)) - coil_pos++; - else - coil_pos--; - - coil_pos &= 7; - - //Grab the latest motor power to use - pwm = motor_pwm; - - switch(coil_pos) { - case 0: - WRITE(H1D, 0); - WRITE(H2D, 0); - H1E_PWM = 0; - H2E_PWM = pwm; - break; - case 1: - WRITE(H1D, 1); - WRITE(H2D, 0); - H1E_PWM = pwm; - H2E_PWM = pwm; - break; - case 2: - WRITE(H1D, 1); - WRITE(H2D, 0); - H1E_PWM = pwm; - H2E_PWM = 0; - break; - case 3: - WRITE(H1D, 1); - WRITE(H2D, 1); - H1E_PWM = pwm; - H2E_PWM = pwm; - break; - case 4: - WRITE(H1D, 1); - WRITE(H2D, 1); - H1E_PWM = 0; - H2E_PWM = pwm; - break; - case 5: - WRITE(H1D, 0); - WRITE(H2D, 1); - H1E_PWM = pwm; - H2E_PWM = pwm; - break; - case 6: - WRITE(H1D, 0); - WRITE(H2D, 1); - H1E_PWM = pwm; - H2E_PWM = 0; - break; - case 7: - WRITE(H1D, 0); - WRITE(H2D, 0); - H1E_PWM = pwm; - H2E_PWM = pwm; - break; - } - } -} - -void init(void) { - // set up watchdog - wd_init(); - - // setup analog reading - analog_init(); - - // set up serial - intercom_init(); - - // set up inputs and outputs - io_init(); - - // temp sensor - temp_init(); - - // heater - heater_init(); - - // set up extruder motor driver - motor_init(); - - // set up clock - timer_init(); - - // enable interrupts - sei(); - - // reset watchdog - wd_reset(); -} - - -int main (void) -{ - init(); - - enable_heater(); - - // main loop - for (;;) - { - wd_reset(); - - //Read motor PWM - motor_pwm = analog_read(TRIM_POT_CHANNEL) >> 2; - - ifclock(CLOCK_FLAG_10MS) { - // check temperatures and manage heaters - temp_sensor_tick(); - } - - // check if we've had a new intercom packet - if (intercom_flags & FLAG_NEW_RX) { - intercom_flags &= ~FLAG_NEW_RX; - - switch (rx.packet.control_word) { - // M105- read temperatures - case 105: - send_temperature(0, temp_get(0)); - temp_set(0, read_temperature(0)); - send_temperature(1, temp_get(1)); - temp_set(1, read_temperature(1)); - start_send(); - break; - // M130 - set PID P factor - case 130: - pid_set_p(rx.packet.control_index, rx.packet.control_data_int32); - // M131 - set PID I factor - case 131: - pid_set_i(rx.packet.control_index, rx.packet.control_data_int32); - // M132 - set PID D factor - case 132: - pid_set_d(rx.packet.control_index, rx.packet.control_data_int32); - // M133 - set PID I limit - case 133: - pid_set_i_limit(rx.packet.control_index, rx.packet.control_data_int32); - // M134 - save PID values to eeprom - case 134: - heater_save_settings(); - break; - } - } +#include +#include + +#include + +#include "intercom.h" +#include "analog.h" +#include "config_wrapper.h" +#include "watchdog.h" +#include "heater.h" +#include "temp.h" +#include "timer.h" + +static uint8_t motor_pwm; + +void io_init(void) { + // setup I/O pins + WRITE(DEBUG_LED, 0); SET_OUTPUT(DEBUG_LED); + WRITE(H1D,0); SET_OUTPUT(H1D); + WRITE(H1E,0); SET_OUTPUT(H1E); + WRITE(H2D,0); SET_OUTPUT(H2D); + WRITE(H2E,0); SET_OUTPUT(H2E); + + SET_INPUT(TRIM_POT); + SET_INPUT(TEMP_PIN); + SET_INPUT(TEMP_BED_PIN); + SET_INPUT(E_STEP_PIN); + SET_INPUT(E_DIR_PIN); + + // use pull up resistors to avoid noise + WRITE(E_STEP_PIN, 1); + WRITE(E_DIR_PIN, 1); + + //Enable the RS485 transceiver + SET_OUTPUT(RX_ENABLE_PIN); + SET_OUTPUT(TX_ENABLE_PIN); + WRITE(RX_ENABLE_PIN,0); + disable_transmit(); + + #ifdef HEATER_PIN + WRITE(HEATER_PIN, 0); SET_OUTPUT(HEATER_PIN); + #endif + + #ifdef BED_PIN + WRITE(BED_PIN, 0); SET_OUTPUT(BED_PIN); + #endif + + #ifdef FAN_PIN + WRITE(FAN_PIN, 0); SET_OUTPUT(FAN_PIN); + #endif + +// #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 + + #if defined(H1E_PWM) && defined(H2E_PWM) + TCCR0A = MASK(WGM01) | MASK(WGM00); + TCCR0B = MASK(CS20); + TIMSK0 = 0; + OCR0A = 0; + OCR0B = 0; + #endif +} + +void motor_init(void) { + //Enable an interrupt to be triggered when the step pin changes + //This will be PCIE0 + PCICR = MASK(PCIE0); + PCMSK0 = MASK(PCINT2); +} + +ISR(PCINT0_vect) { + static uint8_t coil_pos, pwm; + + //if the step pin is high, we advance the motor + if (READ(E_STEP_PIN)) { + + //Turn on motors only on first tick to save power I guess + enable_motors(); + + //Advance the coil position + if (READ(E_DIR_PIN)) + coil_pos++; + else + coil_pos--; + + coil_pos &= 7; + + //Grab the latest motor power to use + pwm = motor_pwm; + + switch(coil_pos) { + case 0: + WRITE(H1D, 0); + WRITE(H2D, 0); + H1E_PWM = 0; + H2E_PWM = pwm; + break; + case 1: + WRITE(H1D, 1); + WRITE(H2D, 0); + H1E_PWM = pwm; + H2E_PWM = pwm; + break; + case 2: + WRITE(H1D, 1); + WRITE(H2D, 0); + H1E_PWM = pwm; + H2E_PWM = 0; + break; + case 3: + WRITE(H1D, 1); + WRITE(H2D, 1); + H1E_PWM = pwm; + H2E_PWM = pwm; + break; + case 4: + WRITE(H1D, 1); + WRITE(H2D, 1); + H1E_PWM = 0; + H2E_PWM = pwm; + break; + case 5: + WRITE(H1D, 0); + WRITE(H2D, 1); + H1E_PWM = pwm; + H2E_PWM = pwm; + break; + case 6: + WRITE(H1D, 0); + WRITE(H2D, 1); + H1E_PWM = pwm; + H2E_PWM = 0; + break; + case 7: + WRITE(H1D, 0); + WRITE(H2D, 0); + H1E_PWM = pwm; + H2E_PWM = pwm; + break; + } + } +} + +void init(void) { + // set up watchdog + wd_init(); + + // setup analog reading + analog_init(); + + // set up serial + intercom_init(); + + // set up inputs and outputs + io_init(); + + // temp sensor + temp_init(); + + // heater + heater_init(); + + // set up extruder motor driver + motor_init(); + + // set up clock + timer_init(); + + // enable interrupts + sei(); + + // reset watchdog + wd_reset(); +} + + +int main (void) +{ + init(); + + enable_heater(); + + // main loop + for (;;) + { + wd_reset(); + + //Read motor PWM + motor_pwm = analog_read(TRIM_POT_CHANNEL) >> 2; + + ifclock(CLOCK_FLAG_10MS) { + // check temperatures and manage heaters + temp_sensor_tick(); + } + + // check if we've had a new intercom packet + if (intercom_flags & FLAG_NEW_RX) { + intercom_flags &= ~FLAG_NEW_RX; + + switch (rx.packet.control_word) { + // M105- read temperatures + case 105: + send_temperature(0, temp_get(0)); + temp_set(0, read_temperature(0)); + send_temperature(1, temp_get(1)); + temp_set(1, read_temperature(1)); + start_send(); + break; + // M130 - set PID P factor + case 130: + pid_set_p(rx.packet.control_index, rx.packet.control_data_int32); + // M131 - set PID I factor + case 131: + pid_set_i(rx.packet.control_index, rx.packet.control_data_int32); + // M132 - set PID D factor + case 132: + pid_set_d(rx.packet.control_index, rx.packet.control_data_int32); + // M133 - set PID I limit + case 133: + pid_set_i_limit(rx.packet.control_index, rx.packet.control_data_int32); + // M134 - save PID values to eeprom + case 134: + heater_save_settings(); + break; + } + } if (intercom_flags & FLAG_TX_FINISHED) { WRITE(TX_ENABLE_PIN,0); } - } -} + } +}