//iLabProto.pde a 3D print extruder firmware for Mach3 // ////////////////////////////////////////////////////////////////////////// // The iLab 3D extruder for Mach3 - Copyright (C) 2010 by the // // University of Southern California (USC) and the iLab at USC. // // See http://www.thingiverse.com/thing:5233 for information about // // this project. // ////////////////////////////////////////////////////////////////////////// // // // The iLab 3D extruder is free software; you can redistribute it // // and/or modify it under the terms of the GNU General Public License // // as published by the Free Software Foundation; either version 2 of // // the License, or (at your option) any later version. // // // // The iLab 3D extruder is distributed in the hope that it will // // be useful, but WITHOUT ANY WARRANTY; without even the implied // // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // // PURPOSE. See the GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public License // // along with the iLab 3D extruder for Mach3; if not, write // // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // // Boston, MA 02111-1307 USA. // ////////////////////////////////////////////////////////////////////////// // // Primary maintainer for this file: Chin-kai Chang // $Id: iLabProto.pde 2010-12-20 01:56:43Z kai $ // ////////////////////////////////////////////////////////////////////////// #define MOTOR_1_SPEED_PIN 5 #define MOTOR_1_DIR_PIN 7 #define MOTOR_2_SPEED_PIN 6 #define MOTOR_2_DIR_PIN 8 #define SERVO1_PIN 9 #define SERVO2_PIN 10 #define HEATER_PIN 11 #define FAN_PIN 12 #define VALVE_PIN 15 #define THERMISTOR_PIN 3 #define DEBUG_PIN 13 #define DELAY_FOR_STOP 5 #define TEMPERATURE_SAMPLES 5 #define NUMTEMPS 20 bool useParallelInterface = false; 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 setup() { pinMode(MOTOR_1_SPEED_PIN, OUTPUT); pinMode(MOTOR_1_DIR_PIN, OUTPUT); pinMode(MOTOR_2_SPEED_PIN, OUTPUT); pinMode(MOTOR_2_DIR_PIN, OUTPUT); pinMode(HEATER_PIN, OUTPUT); pinMode(FAN_PIN, OUTPUT); pinMode(VALVE_PIN, OUTPUT); pinMode(DEBUG_PIN, OUTPUT); pinMode(SERVO1_PIN, INPUT); digitalWrite(SERVO1_PIN,HIGH); digitalWrite(MOTOR_1_DIR_PIN,HIGH); //Set default forward //start up the serial Serial.begin(115200); Serial.println("OK"); } /////////////////////////////////////////////////////////////////// // This function gives us an averaged sample of the analog temperature pin. int sample_temperature(uint8_t pin) { int raw = 0; //read in a certain number of samples for (byte i=0; 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; } /////////////////////////////////////////////////////////////////// // Read an ascii number terminated by a CR (0x13) from the serial int serialInt() { char bytes[32]; int idx = -1; do { idx += 1; do { bytes[idx] = Serial.read();} while(bytes[idx] == -1); } while(bytes[idx] != 13 && bytes[idx] != 10); bytes[idx] = 0; return atoi(bytes); } int tempSet = 220; int feedrate = 0; int brakeSet = 300;//ms int forwardSet = 300;//ms boolean motor_reversal_state = false; int motor_reversal_count = 0; /////////////////////////////////////////////////////////////////// //basically we want to delay unless there is a start command issued. void cancellable_delay(unsigned int duration, byte state) { if (motor_reversal_state) { for (unsigned int i=0; i0) cancellable_delay(DELAY_FOR_STOP,0); //Reverse motor for a bit if(brakeSet > 0){ digitalWrite(MOTOR_1_DIR_PIN,LOW);//Backward analogWrite(MOTOR_1_SPEED_PIN, feedrate); cancellable_delay(brakeSet,1); } //Wait for it to stop if(DELAY_FOR_STOP > 0 ) cancellable_delay(DELAY_FOR_STOP,0); //forward motor for a bit if(forwardSet > 0){ digitalWrite(MOTOR_1_DIR_PIN,HIGH);//Backward analogWrite(MOTOR_1_SPEED_PIN, feedrate); cancellable_delay(forwardSet,2); } motor_reversal_count = 0; //finally stop it. analogWrite(MOTOR_1_SPEED_PIN, 0); } motor_reversal_state = false; } /////////////////////////////////////////////////////////////////// void enable_motor() { motor_reversal_state = true; analogWrite(MOTOR_1_SPEED_PIN,feedrate); } /////////////////////////////////////////////////////////////////// void loop() { int temperature = read_thermistor(); int tempError = tempSet - temperature; boolean hot = false; //Temp control if(tempSet == 0) { analogWrite(HEATER_PIN, 0); } else { if(tempError > 5){ analogWrite(HEATER_PIN, 255); hot = false; }else if(tempError <= 0){ analogWrite(HEATER_PIN, 0); hot = true; } } if(useParallelInterface) { //Parallel Port motor control if(digitalRead(SERVO1_PIN) == LOW){ enable_motor(); }else{ reverse_motor(); } } char cmd = Serial.read(); if(cmd != -1) { switch(cmd) { case 'M': // Set the feed rate and turn on the motor useParallelInterface = false; feedrate = serialInt(); if(feedrate > 0){ digitalWrite(MOTOR_1_DIR_PIN,HIGH);//Forward }else{ digitalWrite(MOTOR_1_DIR_PIN,LOW);//Backward feedrate = -feedrate; } analogWrite(MOTOR_1_SPEED_PIN, -feedrate); break; case 'P': // Just set the speed, but don't set the motor yet useParallelInterface = true; feedrate = serialInt(); if(feedrate > 0) digitalWrite(MOTOR_1_DIR_PIN,HIGH);//Forward else{ digitalWrite(MOTOR_1_DIR_PIN,LOW);//Backward feedrate = -feedrate; } break; case 'O': //Turn on the motor analogWrite(MOTOR_1_SPEED_PIN, feedrate); break; case 'F': //Turn off the motor analogWrite(MOTOR_1_SPEED_PIN, 0); break; case 'T': // Set the temperature tempSet = serialInt(); break; case 'R': // Read the temperature Serial.println((int)temperature); break; case 'B': // Set the Motor brake time (ms) brakeSet = serialInt(); break; case 'C': // Set the Motor forward time time after brake(ms) forwardSet = serialInt(); break; } } }