由于程序太长 我这个程序是前面的一部分 能读的人联系我 我发送文件 QQ:435085901 // Tonokip RepRap firmware rewrite basedoff of Hydra-mmm firmware. // Licence: GPL #include "fastio.h" //#include "Configuration.h" //#include "pins.h" #include "Configuration.h" #include "Sprinter.h" #include "SerialManager.h" #include <EEPROM.h> #ifdef SDSUPPORT #include "SdFat.h" #endif //http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes //Implemented Codes //------------------- // G0 -> G1 // G1 - Coordinated Movement X Y Z E // G4 - Dwell S<seconds> or P<milliseconds> // G28 - Home all Axis // G90 - Use Absolute Coordinates // G91 - Use Relative Coordinates // G92 - Set current position to cordinatesgiven //RepRap M Codes // M104 - Set extruder target temp // M105 - Read current temp // M106 - Fan on // M107 - Fan off // M109 - Wait for extruder current temp toreach target temp. // M114 - Display current position //Custom M Codes // M80 - Turn on Power Supply // M20 - List SD card // M21 - Init SD card // M22 - Release SD card // M23 - Select SD file (M23 filename.g) // M24 - Start/resume SD print // M25 - Pause SD print // M26 - Set SD position in bytes (M26 S12345) // M27 - Report SD print status // M28 - Start SD write (M28 filename.g) // M29 - Stop SD write // M81 - Turn off Power Supply // M82 - Set E codes absolute (default) // M83 - Set E codes relative while in Absolute Coordinates (G90) mode // M84 - Disable steppers until next move, // or use S<seconds> to specify an inactivity timeout, after whichthe steppers will be disabled. S0 todisable the timeout. // M85 - Set inactivity shutdown timer with parameter S<seconds>. Todisable set zero (default) // M92 - Set axis_steps_per_unit - same syntax as G92 // M115 -Capabilities string // M140 - Set bed target temp // M190 - Wait for bed current temp toreach target temp. // M201 - Set max acceleration in units/s^2for print moves (M201 X1000 Y1000) // M202 - Set max acceleration in units/s^2for travel moves (M202 X1000 Y1000) // M203 - Adjust Z height //Printer status variables int status = STATUS_OK; // int error_code = ERROR_CODE_NO_ERROR;//0=Nothing, 1=Heater thermistor error //Led counter (for blinking the led indifferent timings) int led_counter = 0; //Stepper Movement Variables char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z','E'}; bool move_direction[NUM_AXIS]; unsigned longaxis_previous_micros[NUM_AXIS]; unsigned long previous_micros = 0,previous_millis_heater, previous_millis_bed_heater; unsigned long move_steps_to_take[NUM_AXIS]; #ifdef RAMP_ACCELERATION unsigned long axis_max_interval[NUM_AXIS]; unsigned longaxis_steps_per_sqr_second[NUM_AXIS]; unsigned long axis_travel_steps_per_sqr_second[NUM_AXIS]; unsigned long max_interval; unsigned long steps_per_sqr_second,plateau_steps; #endif boolean acceleration_enabled = false,accelerating = false; unsigned long interval; float destination[NUM_AXIS] = {0.0, 0.0,0.0, 0.0}; float current_position[NUM_AXIS] = {0.0,0.0, 0.0, 0.0}; unsigned long steps_taken[NUM_AXIS]; long axis_interval[NUM_AXIS]; // for speeddelay bool home_all_axis = true; int feedrate = 1500, next_feedrate,saved_feedrate; float time_for_move; long gcode_N, gcode_LastN; bool relative_mode = false; //Determines Absolute or Relative Coordinates bool relative_mode_e = false; //Determines Absolute or Relative E Codeswhile in Absolute Coordinates mode. E is always relative in RelativeCoordinates mode. long timediff = 0; //experimental feedrate calc float d = 0; float axis_diff[NUM_AXIS] = {0, 0, 0, 0}; #ifdef STEP_DELAY_RATIO long long_step_delay_ratio = STEP_DELAY_RATIO * 100; #endif #define Z_ADJUST_BYTE 0 // comm variables #define MAX_CMD_SIZE 96 #define BUFSIZE 8 char cmdbuffer[BUFSIZE][MAX_CMD_SIZE]; bool fromsd[BUFSIZE]; int bufindr = 0; int bufindw = 0; int buflen = 0; int i = 0; char serial_char; int serial_count = 0; boolean comment_mode = false; char *strchr_pointer; // just a pointer tofind chars in the cmd string like X, Y, Z, E, etc // Manage heater variables. For athermistor or AD595 thermocouple, raw values refer to the // reading from the analog pin. For aMAX6675 thermocouple, the raw value is the temperature in 0.25 // degree increments (i.e. 100=25 deg). int target_raw = 0; int current_raw = 0; int target_bed_raw = 0; int current_bed_raw = 0; int tt = 0, bt = 0; #ifdef PIDTEMP inttemp_iState = 0; inttemp_dState = 0; intpTerm; intiTerm; intdTerm; intoutput; interror; int temp_iState_min = -pid_i_max / Ki; inttemp_iState_max = pid_i_max / Ki; #endif #ifdef SMOOTHING uint32_t nma = 0; #endif #ifdef WATCHPERIOD intwatch_raw = -1000; unsigned long watchmillis = 0; #endif #ifdef MINTEMP intminttemp = temp2analogh(MINTEMP); #endif #ifdef MAXTEMP int maxttemp = temp2analogh(MAXTEMP); #endif //Inactivity shutdown variables unsigned long previous_millis_cmd = 0; unsigned long max_inactive_time = 0; unsigned long stepper_inactive_time = 0; #ifdef SDSUPPORT Sd2Card card; SdVolume volume; SdFile root; SdFile file; uint32_t filesize = 0; uint32_t sdpos = 0; bool sdmode = false; bool sdactive = false; bool savetosd = false; int16_t n; void initsd(){ sdactive = false; #ifSDSS >- 1 if(root.isOpen()) root.close(); if (!card.init(SPI_FULL_SPEED,SDSS)){ //if (!card.init(SPI_HALF_SPEED,SDSS)) SerialMgr.cur()->println("SD init fail"); } else if (!volume.init(&card)) SerialMgr.cur()->println("volume.init failed"); else if (!root.openRoot(&volume)) SerialMgr.cur()->println("openRoot failed"); else sdactive = true; #endif } inline void write_command(char *buf){ char* begin = buf; char* npos = 0; char* end = buf + strlen(buf) - 1; file.writeError = false; if((npos = strchr(buf, 'N')) != NULL){ begin = strchr(npos, ' ') + 1; end = strchr(npos, '*') - 1; } end[1] = '\r'; end[2] = '\n'; end[3] = '\0'; //SerialMgr.cur()->println(begin); file.write(begin); if (file.writeError){ SerialMgr.cur()->println("error writing to file"); } } #endif void setup() { Serial.begin(BAUDRATE); Serial.println("start"); #ifdef BLUETOOTH BLUETOOTH_SERIAL.begin(BAUDRATE); #endif for(int i = 0; i < BUFSIZE; i++){ fromsd = false; } #ifLED_PIN > -1 SET_OUTPUT(LED_PIN); WRITE(LED_PIN,HIGH); #endif //Initialize Dir Pins #ifX_DIR_PIN > -1 SET_OUTPUT(X_DIR_PIN); #endif #ifY_DIR_PIN > -1 SET_OUTPUT(Y_DIR_PIN); #endif #ifZ_DIR_PIN > -1 SET_OUTPUT(Z_DIR_PIN); #endif #ifE_DIR_PIN > -1 SET_OUTPUT(E_DIR_PIN); #endif //Initialize Enable Pins - steppers default to disabled. #if(X_ENABLE_PIN > -1) SET_OUTPUT(X_ENABLE_PIN); if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH); #endif #if(Y_ENABLE_PIN > -1) SET_OUTPUT(Y_ENABLE_PIN); if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH); #endif #if(Z_ENABLE_PIN > -1) SET_OUTPUT(Z_ENABLE_PIN); if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH); #endif #if(E_ENABLE_PIN > -1) SET_OUTPUT(E_ENABLE_PIN); if(!E_ENABLE_ON) WRITE(E_ENABLE_PIN,HIGH); #endif //endstops and pullups #ifdef ENDSTOPPULLUPS #ifX_MIN_PIN > -1 SET_INPUT(X_MIN_PIN); WRITE(X_MIN_PIN,HIGH); #endif #ifX_MAX_PIN > -1 SET_INPUT(X_MAX_PIN); WRITE(X_MAX_PIN,HIGH); #endif #ifY_MIN_PIN > -1 SET_INPUT(Y_MIN_PIN); WRITE(Y_MIN_PIN,HIGH); #endif #ifY_MAX_PIN > -1 SET_INPUT(Y_MAX_PIN); WRITE(Y_MAX_PIN,HIGH); #endif #ifZ_MIN_PIN > -1 SET_INPUT(Z_MIN_PIN); WRITE(Z_MIN_PIN,HIGH); #endif #ifZ_MAX_PIN > -1 SET_INPUT(Z_MAX_PIN); WRITE(Z_MAX_PIN,HIGH); #endif #else #ifX_MIN_PIN > -1 SET_INPUT(X_MIN_PIN); #endif #ifX_MAX_PIN > -1 SET_INPUT(X_MAX_PIN); #endif #ifY_MIN_PIN > -1 SET_INPUT(Y_MIN_PIN); #endif #ifY_MAX_PIN > -1 SET_INPUT(Y_MAX_PIN); #endif #ifZ_MIN_PIN > -1 SET_INPUT(Z_MIN_PIN); #endif #ifZ_MAX_PIN > -1 SET_INPUT(Z_MAX_PIN); #endif /* #if PROBE_PIN > -1 SET_INPUT(PROBE_PIN); #endif */ #endif #if(FAN_PIN > -1) SET_OUTPUT(FAN_PIN); WRITE(FAN_PIN,FAN_INIT); #endif #if(HEATER_0_PIN > -1) SET_OUTPUT(HEATER_0_PIN); #endif #if(HEATER_1_PIN > -1) SET_OUTPUT(HEATER_1_PIN); #endif //Initialize Step Pins #if(X_STEP_PIN > -1) SET_OUTPUT(X_STEP_PIN); #endif #if(Y_STEP_PIN > -1) SET_OUTPUT(Y_STEP_PIN); #endif #if(Z_STEP_PIN > -1) SET_OUTPUT(Z_STEP_PIN); #endif #if(E_STEP_PIN > -1) SET_OUTPUT(E_STEP_PIN); #endif #ifdef RAMP_ACCELERATION for(inti=0; i < NUM_AXIS; i++){ axis_max_interval = 100000000.0 /(max_start_speed_units_per_second * axis_steps_per_unit); axis_steps_per_sqr_second = max_acceleration_units_per_sq_second *axis_steps_per_unit; axis_travel_steps_per_sqr_second =max_travel_acceleration_units_per_sq_second * axis_steps_per_unit; } #endif #ifdef HEATER_USES_MAX6675 SET_OUTPUT(SCK_PIN); WRITE(SCK_PIN,0); SET_OUTPUT(MOSI_PIN); WRITE(MOSI_PIN,1); SET_INPUT(MISO_PIN); WRITE(MISO_PIN,1); SET_OUTPUT(MAX6675_SS); WRITE(MAX6675_SS,1); #endif #ifdef SDSUPPORT //power to SD reader #ifSDPOWER > -1 SET_OUTPUT(SDPOWER); WRITE(SDPOWER,HIGH); #endif initsd(); #endif } void loop() { #ifdef BLUETOOTH if(BLUETOOTH_SERIAL.available()&& !serial_count) { SerialMgr.ChangeSerial(&BLUETOOTH_SERIAL); } else { if(Serial.available() && !serial_count) { SerialMgr.ChangeSerial(&Serial); } } #endif if(buflen<3) get_command(); if(buflen){ #ifdef SDSUPPORT if(savetosd){ if(strstr(cmdbuffer[bufindr],"M29") == NULL){ write_command(cmdbuffer[bufindr]); SerialMgr.cur()->println("ok"); }else{ file.sync(); file.close(); savetosd = false; SerialMgr.cur()->println("Done saving file."); } }else{ process_commands(); } #else process_commands(); #endif buflen = (buflen-1); bufindr = (bufindr + 1)%BUFSIZE; } //check heater every n milliseconds manage_heater(); manage_inactivity(1); } inline void get_command() { while( SerialMgr.cur()->available() > 0 && buflen < BUFSIZE) { serial_char = SerialMgr.cur()->read(); if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' ||serial_count >= (MAX_CMD_SIZE - 1) ) { if(!serial_count) return; //if empty line cmdbuffer[bufindw][serial_count] = 0; //terminate string if(!comment_mode){ fromsd[bufindw] = false; if(strstr(cmdbuffer[bufindw], "N") != NULL) { strchr_pointer = strchr(cmdbuffer[bufindw], 'N'); gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer -cmdbuffer[bufindw] + 1], NULL, 10)); if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw],"M110") == NULL) ) { SerialMgr.cur()->print("Serial Error: Line Number is not LastLine Number+1, Last Line:"); SerialMgr.cur()->println(gcode_LastN); //SerialMgr.cur()->println(gcode_N); FlushSerialRequestResend(); serial_count = 0; return; } if(strstr(cmdbuffer[bufindw], "*") != NULL) { byte checksum = 0; byte count = 0; while(cmdbuffer[bufindw][count] != '*') checksum =checksum^cmdbuffer[bufindw][count++]; strchr_pointer = strchr(cmdbuffer[bufindw], '*'); if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer -cmdbuffer[bufindw] + 1], NULL)) != checksum) { SerialMgr.cur()->print("Error: checksum mismatch, LastLine:"); SerialMgr.cur()->println(gcode_LastN); FlushSerialRequestResend(); serial_count = 0; return; } //if no errors, continue parsing } else { SerialMgr.cur()->print("Error: No Checksum with line number,Last Line:"); SerialMgr.cur()->println(gcode_LastN); FlushSerialRequestResend(); serial_count = 0; return; } gcode_LastN = gcode_N; //if no errors, continue parsing } else // if we don't receive 'N'but still see '*' { if((strstr(cmdbuffer[bufindw], "*") != NULL)) { SerialMgr.cur()->print("Error: No Line Number with checksum,Last Line:"); SerialMgr.cur()->println(gcode_LastN); serial_count = 0; return; } } if((strstr(cmdbuffer[bufindw],"G") != NULL)){ strchr_pointer= strchr(cmdbuffer[bufindw], 'G'); switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer- cmdbuffer[bufindw] + 1], NULL)))){ case0: case1: #ifdef SDSUPPORT if(savetosd) break; #endif SerialMgr.cur()->println("ok"); break; default: break; } } bufindw = (bufindw + 1)%BUFSIZE; buflen += 1; } comment_mode = false; //for new command serial_count = 0; //clear buffer } else { if(serial_char == ';') comment_mode = true; if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char; } } #ifdef SDSUPPORT if(!sdmode || serial_count!=0){ return; } while( filesize > sdpos && buflen < BUFSIZE) { n= file.read(); serial_char = (char)n; if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' ||serial_count >= (MAX_CMD_SIZE - 1) || n == -1) { sdpos = file.curPosition(); if(sdpos >= filesize){ sdmode = false; SerialMgr.cur()->println("Done printing file"); } if(!serial_count) return; //if empty line cmdbuffer[bufindw][serial_count] = 0; //terminate string if(!comment_mode){ fromsd[bufindw] = true; buflen += 1; bufindw = (bufindw + 1)%BUFSIZE; } comment_mode = false; //for new command serial_count = 0; //clear buffer } else { if(serial_char == ';') comment_mode = true; if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char; } } #endif } inline float code_value() { return(strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1],NULL)); } inline long code_value_long() { return(strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL,10)); } inline bool code_seen(char code_string[]) {return (strstr(cmdbuffer[bufindr], code_string) != NULL); } //Return True if the string was found inline bool code_seen(char code) { strchr_pointer = strchr(cmdbuffer[bufindr], code); return (strchr_pointer != NULL); //Return True if a character was found } inline void process_commands() { unsigned long codenum; //throw away variable char *starpos = NULL; if(code_seen('G')) { switch((int)code_value()) { case 0: // G0 -> G1 case 1: // G1 #if (defined DISABLE_CHECK_DURING_ACC) || (definedDISABLE_CHECK_DURING_MOVE) || (defined DISABLE_CHECK_DURING_TRAVEL) manage_heater(); #endif get_coordinates(); // For X Y Z E F prepare_move(); previous_millis_cmd = millis(); //ClearToSend(); return; //break; case 4: // G4 dwell codenum = 0; if(code_seen('P')) codenum = code_value(); // milliseconds to wait if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait codenum += millis(); // keeptrack of when we started waiting while(millis() < codenum ){ manage_heater(); } break; case 28: //G28 Home all Axis one at a time saved_feedrate = feedrate; for(int i=0; i < NUM_AXIS; i++) { destination = current_position; } feedrate = 0; home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1]))|| (code_seen(axis_codes[2]))); if((home_all_axis) || (code_seen(axis_codes[0]))) { if ((X_MIN_PIN > -1 && X_HOME_DIR==-1) || (X_MAX_PIN > -1&& X_HOME_DIR==1)){ current_position[0] = 0; destination[0] = 1.5 * X_MAX_LENGTH * X_HOME_DIR; feedrate = homing_feedrate[0]; prepare_move(); current_position[0] = 0; destination[0] = -5 * X_HOME_DIR; prepare_move(); destination[0] = 10 * X_HOME_DIR; prepare_move(); current_position[0] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH; destination[0] = current_position[0]; feedrate = 0; } } if((home_all_axis) || (code_seen(axis_codes[1]))) { if ((Y_MIN_PIN > -1 && Y_HOME_DIR==-1) || (Y_MAX_PIN > -1&& Y_HOME_DIR==1)){ current_position[1] = 0; destination[1] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR; feedrate = homing_feedrate[1]; prepare_move(); current_position[1] = 0; destination[1] = -5 * Y_HOME_DIR; prepare_move(); destination[1] = 10 * Y_HOME_DIR; prepare_move(); current_position[1] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH; destination[1] = current_position[1]; feedrate = 0; } } if((home_all_axis) || (code_seen(axis_codes[2]))) { if ((Z_MIN_PIN > -1 && Z_HOME_DIR==-1) || (Z_MAX_PIN > -1&& Z_HOME_DIR==1)){ current_position[2] = 0; destination[2] = 1.5 * Z_MAX_LENGTH * Z_HOME_DIR; feedrate = homing_feedrate[2]; prepare_move(); current_position[2] = 0; destination[2] = -2 * Z_HOME_DIR; prepare_move(); destination[2] = 10 * Z_HOME_DIR; #endif
|