Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // Endstops disabled (set to NC in pins.h) 00002 00003 // Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware. 00004 // Licence: GPL 00005 // ported to mbed by R. Bohne (rene.bohne@gmail.com) 00006 00007 #include "mbed.h" 00008 #include "pins.h" 00009 #include "configuration.h" 00010 #include "ThermistorTable.h" 00011 00012 00013 #define DEBUGGING false 00014 00015 #define X_TIME_FOR_MOVE ((float)x_steps_to_take / (x_steps_per_unit*feedrate/60000000)) 00016 #define Y_TIME_FOR_MOVE ((float)y_steps_to_take / (y_steps_per_unit*feedrate/60000000)) 00017 #define Z_TIME_FOR_MOVE ((float)z_steps_to_take / (z_steps_per_unit*feedrate/60000000)) 00018 #define E_TIME_FOR_MOVE ((float)e_steps_to_take / (e_steps_per_unit*feedrate/60000000)) 00019 00020 00021 DigitalOut led1(LED1);//x 00022 DigitalOut led2(LED2);//y 00023 DigitalOut led3(LED3);//z 00024 DigitalOut led4(LED4);//e 00025 00026 DigitalOut p_fan(FAN_PIN); 00027 00028 DigitalOut p_X_enable(X_ENABLE_PIN); 00029 DigitalOut p_X_dir(X_DIR_PIN); 00030 DigitalOut p_X_step(X_STEP_PIN); 00031 DigitalIn p_X_min(X_MIN_PIN); 00032 DigitalIn p_X_max(X_MAX_PIN); 00033 00034 DigitalOut p_Y_enable(Y_ENABLE_PIN); 00035 DigitalOut p_Y_dir(Y_DIR_PIN); 00036 DigitalOut p_Y_step(Y_STEP_PIN); 00037 DigitalIn p_Y_min(Y_MIN_PIN); 00038 DigitalIn p_Y_max(Y_MAX_PIN); 00039 00040 DigitalOut p_Z_enable(Z_ENABLE_PIN); 00041 DigitalOut p_Z_dir(Z_DIR_PIN); 00042 DigitalOut p_Z_step(Z_STEP_PIN); 00043 DigitalIn p_Z_min(Z_MIN_PIN); 00044 DigitalIn p_Z_max(Z_MAX_PIN); 00045 00046 DigitalOut p_E_enable(E_ENABLE_PIN); 00047 DigitalOut p_E_dir(E_DIR_PIN); 00048 DigitalOut p_E_step(E_STEP_PIN); 00049 00050 DigitalOut p_heater0(HEATER_0_PIN); 00051 DigitalOut p_heater1(HEATER_1_PIN);//heated-build-platform 00052 00053 AnalogIn p_temp0(TEMP_0_PIN); 00054 AnalogIn p_temp1(TEMP_1_PIN);//heated-build-platform thermistor 00055 00056 00057 Serial pc(USBTX, USBRX); 00058 00059 Timer timer; 00060 00061 int millis() { 00062 return timer.read_ms(); 00063 } 00064 00065 int micros() { 00066 return timer.read_us(); 00067 } 00068 00069 int max(int a, int b) { 00070 if (a>b) { 00071 return a; 00072 } 00073 return b; 00074 } 00075 00076 // Takes temperature value as input and returns corresponding analog value from RepRap thermistor temp table. 00077 // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value. 00078 // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware. 00079 float temp2analog(int celsius) { 00080 if (USE_THERMISTOR){ 00081 int raw = 0; 00082 int i; 00083 00084 for (i=1; i<NUMTEMPS; i++) { 00085 if (temptable[i][1] < celsius) { 00086 raw = temptable[i-1][0]; 00087 break; 00088 } 00089 } 00090 00091 // Overflow: Set to last value in the table (25 deg. Celsius) 00092 if (i == NUMTEMPS) raw = temptable[i-1][0]; 00093 00094 return raw; 00095 } 00096 } 00097 00098 // calculated by hand 00099 float analog2temp(int raw) { 00100 if (USE_THERMISTOR) { 00101 int celsius = 0; 00102 int i; 00103 00104 for (i=1; i<NUMTEMPS; i++) { 00105 if (temptable[i][0] > raw) { 00106 celsius = temptable[i-1][1]; 00107 break; 00108 } 00109 } 00110 00111 // Overflow: Set to last value in the table (25 deg. Celsius) 00112 if (i == NUMTEMPS) celsius = temptable[i-1][1]; 00113 00114 return celsius; 00115 } 00116 } 00117 00118 // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html 00119 // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes 00120 00121 //Implemented Codes 00122 //------------------- 00123 // G0 -> G1 00124 // G1 - Coordinated Movement X Y Z E 00125 // G4 - Dwell S<seconds> or P<milliseconds> 00126 // G90 - Use Absolute Coordinates 00127 // G91 - Use Relative Coordinates 00128 // G92 - Set current position to cordinates given 00129 00130 //RepRap M Codes 00131 // M104 - Set target temp 00132 // M105 - Read current temp 00133 // M106 - Fan on 00134 // M107 - Fan off 00135 // M109 - Wait for current temp to reach target temp. 00136 00137 //Custom M Codes 00138 // M80 - Turn on Power Supply 00139 // M81 - Turn off Power Supply 00140 // M82 - Set E codes absolute (default) 00141 // M83 - Set E codes relative while in Absolute Coordinates (G90) mode 00142 // M84 - Disable steppers until next move 00143 // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default) 00144 // M86 - If Endstop is Not Activated then Abort Print. Specify X and/or Y 00145 // M92 - Set axis_steps_per_unit - same syntax as G92 00146 // M93 - Read previous_micros 00147 00148 //Stepper Movement Variables 00149 bool direction_x, direction_y, direction_z, direction_e; 00150 int previous_micros=0, previous_micros_x=0, previous_micros_y=0, previous_micros_z=0, previous_micros_e=0, previous_millis_heater; 00151 int x_steps_to_take, y_steps_to_take, z_steps_to_take, e_steps_to_take; 00152 float destination_x =0.0, destination_y = 0.0, destination_z = 0.0, destination_e = 0.0; 00153 float current_x = 0.0, current_y = 0.0, current_z = 0.0, current_e = 0.0; 00154 float x_interval, y_interval, z_interval, e_interval; // for speed delay 00155 float feedrate = 1500, next_feedrate; 00156 float time_for_move; 00157 int gcode_N, gcode_LastN; 00158 bool relative_mode = false; //Determines Absolute or Relative Coordinates 00159 bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode. 00160 00161 int x_steps_remaining; 00162 int y_steps_remaining; 00163 int z_steps_remaining; 00164 int e_steps_remaining; 00165 00166 // comm variables 00167 #define MAX_CMD_SIZE 256 00168 char cmdbuffer[MAX_CMD_SIZE]; 00169 char serial_char; 00170 int serial_count = 0; 00171 bool comment_mode = false; 00172 char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc 00173 00174 //manage heater variables 00175 int target_raw = 0; 00176 int current_raw; 00177 00178 //for heated-build-platform 00179 int target_raw1 = 0; 00180 int current_raw1; 00181 00182 00183 //Inactivity shutdown variables 00184 int previous_millis_cmd=0; 00185 int max_inactive_time = 0; 00186 00187 //timer.read_us overflows every 30 seconds, so we want to reset everything... 00188 void reset_timers() { 00189 previous_micros = 0; 00190 previous_micros_x = 0; 00191 previous_micros_y = 0; 00192 previous_micros_z = 0; 00193 previous_micros_e = 0; 00194 00195 timer.stop(); 00196 timer.reset(); 00197 timer.start(); 00198 } 00199 00200 00201 void check_x_min_endstop() { 00202 if (X_MIN_PIN != NC) { 00203 if (!direction_x) { 00204 if (p_X_min.read() != ENDSTOPS_INVERTING) { 00205 x_steps_remaining=0; 00206 } 00207 } 00208 } 00209 } 00210 00211 void check_y_min_endstop() { 00212 if (Y_MIN_PIN != NC) { 00213 if (!direction_y) { 00214 if (p_Y_min.read() != ENDSTOPS_INVERTING) { 00215 y_steps_remaining=0; 00216 } 00217 } 00218 } 00219 } 00220 00221 void check_z_min_endstop() { 00222 if (Z_MIN_PIN != NC) { 00223 if (!direction_z) { 00224 if (p_Z_min.read() != ENDSTOPS_INVERTING) { 00225 z_steps_remaining=0; 00226 } 00227 } 00228 } 00229 } 00230 00231 00232 00233 //manages heaters for hot-end and heated-build-platform 00234 void manage_heater() { 00235 00236 if (TEMP_0_PIN != NC) { 00237 current_raw = 0; 00238 for(int i=0;i<3;i++) 00239 { 00240 int _raw = p_temp0.read_u16(); 00241 if((current_raw == 65535) && (_raw==65535)) 00242 { 00243 //do nothing 00244 } 00245 else if((current_raw == 65535) && (_raw<65535)) 00246 { 00247 current_raw = _raw; 00248 } 00249 else 00250 { 00251 long l = current_raw + _raw; 00252 l = l/2; 00253 current_raw = (int) l; 00254 } 00255 } 00256 //pc.printf("currentRaw: %d \t targetRaw: %d\n", current_raw, target_raw); 00257 00258 00259 if(current_raw == 65535) 00260 { 00261 pc.printf("thermistor0 disconnected!!!\n"); 00262 p_heater0 = 0; 00263 } 00264 else 00265 { 00266 00267 if((target_raw >0) && (current_raw > target_raw)) 00268 { 00269 p_heater0 = 1; 00270 //pc.printf("currentRaw: %d \t targetRaw: %d\n", current_raw, target_raw); 00271 } 00272 else 00273 { 00274 p_heater0 = 0; 00275 } 00276 } 00277 00278 } 00279 00280 //thermistor for heated-build-platform 00281 if (TEMP_1_PIN != NC) { 00282 current_raw1 = 0; 00283 for(int i=0;i<3;i++) 00284 { 00285 int _raw1 = p_temp1.read_u16(); 00286 if((current_raw1 == 65535) && (_raw1==65535)) 00287 { 00288 //do nothing 00289 } 00290 else if((current_raw1 == 65535) && (_raw1<65535)) 00291 { 00292 current_raw1 = _raw1; 00293 } 00294 else 00295 { 00296 long l = current_raw1 + _raw1; 00297 l = l/2; 00298 current_raw1 = (int) l; 00299 } 00300 } 00301 //pc.printf("currentRaw1: %d \t targetRaw1: %d\n", current_raw1, target_raw1); 00302 00303 00304 if(current_raw1 == 65535) 00305 { 00306 pc.printf("thermistor1 disconnected!!!\n"); 00307 p_heater1 = 0; 00308 } 00309 else 00310 { 00311 00312 if((target_raw1 >0) && (current_raw1 > target_raw1)) 00313 { 00314 p_heater1 = 1; 00315 //pc.printf("currentRaw: %d \t targetRaw: %d\n", current_raw, target_raw); 00316 } 00317 else 00318 { 00319 p_heater1 = 0; 00320 } 00321 } 00322 00323 } 00324 00325 /* 00326 if (TEMP_0_PIN != NC) { 00327 current_raw = (p_temp0.read_u16() >> 6) ; 00328 00329 if (USE_THERMISTOR) {// If using thermistor, when the heater is colder than targer temp, we get a higher analog reading than target, 00330 current_raw = 0xFFFF - current_raw; // this switches it up so that the reading appears lower than target for the control logic. 00331 } 00332 00333 if (current_raw >= target_raw) { 00334 p_heater0 = 0; 00335 } else { 00336 p_heater0 = 1; 00337 } 00338 } 00339 */ 00340 00341 } 00342 00343 00344 void do_x_step() { 00345 if (X_STEP_PIN != NC) { 00346 p_X_step = 1; 00347 wait_us(2); 00348 p_X_step = 0; 00349 //wait_us(2); 00350 previous_micros_x = micros(); 00351 } 00352 } 00353 00354 void do_y_step() { 00355 if (Y_STEP_PIN != NC) { 00356 p_Y_step = 1; 00357 wait_us(2); 00358 p_Y_step = 0; 00359 //wait_us(2); 00360 previous_micros_y = micros(); 00361 } 00362 } 00363 00364 void do_z_step() { 00365 if (Z_STEP_PIN != NC) { 00366 p_Z_step = 1; 00367 wait_us(2); 00368 p_Z_step = 0; 00369 //wait_us(2); 00370 previous_micros_z = micros(); 00371 } 00372 } 00373 00374 void do_e_step() { 00375 if (E_STEP_PIN != NC) { 00376 p_E_step = 1; 00377 wait_us(2); 00378 p_E_step = 0; 00379 //wait_us(2); 00380 previous_micros_e = micros(); 00381 } 00382 } 00383 00384 00385 void disable_x() { 00386 if (X_ENABLE_PIN != NC) { 00387 p_X_enable = !X_ENABLE_ON; 00388 } 00389 led1=0; 00390 } 00391 00392 void disable_y() { 00393 if (Y_ENABLE_PIN != NC) { 00394 p_Y_enable = !Y_ENABLE_ON; 00395 } 00396 led2=0; 00397 } 00398 00399 void disable_z() { 00400 if (Z_ENABLE_PIN != NC) { 00401 p_Z_enable = !Z_ENABLE_ON; 00402 } 00403 led3=0; 00404 } 00405 00406 void disable_e() { 00407 if (E_ENABLE_PIN != NC) { 00408 p_E_enable = !E_ENABLE_ON; 00409 } 00410 led4=0; 00411 } 00412 00413 void enable_x() { 00414 if (X_ENABLE_PIN != NC) { 00415 p_X_enable = X_ENABLE_ON; 00416 } 00417 } 00418 00419 void enable_y() { 00420 if (Y_ENABLE_PIN != NC) { 00421 p_Y_enable = Y_ENABLE_ON; 00422 } 00423 } 00424 00425 void enable_z() { 00426 if (Z_ENABLE_PIN != NC) { 00427 p_Z_enable = Z_ENABLE_ON; 00428 } 00429 } 00430 00431 void enable_e() { 00432 if (E_ENABLE_PIN != NC) { 00433 p_E_enable = E_ENABLE_ON; 00434 } 00435 } 00436 00437 void kill(int debug) { 00438 00439 /* 00440 if (HEATER_0_PIN != NC) { 00441 p_heater0 = 0; 00442 } 00443 */ 00444 00445 disable_x(); 00446 disable_y(); 00447 disable_z(); 00448 disable_e(); 00449 00450 if (PS_ON_PIN != NC) { 00451 //pinMode(PS_ON_PIN,INPUT); 00452 } 00453 00454 while (1) { 00455 switch (debug) { 00456 case 1: 00457 pc.printf("Inactivity Shutdown, Last Line: "); 00458 break; 00459 case 2: 00460 pc.printf("Linear Move Abort, Last Line: "); 00461 break; 00462 case 3: 00463 pc.printf("Homing X Min Stop Fail, Last Line: "); 00464 break; 00465 case 4: 00466 pc.printf("Homing Y Min Stop Fail, Last Line: "); 00467 break; 00468 } 00469 pc.printf("%s \n",gcode_LastN); 00470 wait(5); // 5 Second delay 00471 } 00472 } 00473 00474 void manage_inactivity(int debug) { 00475 if ( (millis()-previous_millis_cmd) > max_inactive_time ) { 00476 if (max_inactive_time) { 00477 kill(debug); 00478 } 00479 } 00480 } 00481 00482 00483 void linear_move() { // make linear move with preset speeds and destinations, see G0 and G1 00484 //Determine direction of movement 00485 if (destination_x > current_x) { 00486 p_X_dir = !INVERT_X_DIR; 00487 } else { 00488 p_X_dir = INVERT_X_DIR; 00489 } 00490 00491 if (destination_y > current_y) { 00492 p_Y_dir = !INVERT_Y_DIR; 00493 } else { 00494 p_Y_dir = INVERT_Y_DIR; 00495 } 00496 00497 if (destination_z > current_z) { 00498 p_Z_dir = !INVERT_Z_DIR; 00499 } else { 00500 p_Z_dir = INVERT_Z_DIR; 00501 } 00502 00503 if (destination_e > current_e) { 00504 p_E_dir = !INVERT_E_DIR; 00505 } else { 00506 p_E_dir = INVERT_E_DIR; 00507 } 00508 00509 //Only enable axis that are moving. If the axis doesn't need to move then it can stay disabled depending on configuration. 00510 if (x_steps_remaining) enable_x(); 00511 if (y_steps_remaining) enable_y(); 00512 if (z_steps_remaining) enable_z(); 00513 if (e_steps_remaining) enable_e(); 00514 00515 check_x_min_endstop(); 00516 check_y_min_endstop(); 00517 check_z_min_endstop(); 00518 00519 previous_millis_heater = millis(); 00520 00521 while (x_steps_remaining + y_steps_remaining + z_steps_remaining + e_steps_remaining > 0) { // move until no more steps remain 00522 if (x_steps_remaining>0) { 00523 if ((micros()-previous_micros_x) >= x_interval) { 00524 do_x_step(); 00525 x_steps_remaining--; 00526 } 00527 check_x_min_endstop(); 00528 led1 = 1; 00529 } else { 00530 led1 = 0; 00531 wait_us(2); 00532 } 00533 00534 if (y_steps_remaining>0) { 00535 if ((micros()-previous_micros_y) >= y_interval) { 00536 do_y_step(); 00537 y_steps_remaining--; 00538 } 00539 check_y_min_endstop(); 00540 led2=1; 00541 } else { 00542 led2=0; 00543 wait_us(2); 00544 } 00545 00546 if (z_steps_remaining>0) { 00547 if ((micros()-previous_micros_z) >= z_interval) { 00548 do_z_step(); 00549 z_steps_remaining--; 00550 } 00551 check_z_min_endstop(); 00552 led3=1; 00553 } else { 00554 led3=0; 00555 wait_us(2); 00556 } 00557 00558 if (e_steps_remaining>0) { 00559 if ((micros()-previous_micros_e) >= e_interval) { 00560 do_e_step(); 00561 e_steps_remaining--; 00562 led4=1; 00563 } 00564 } else { 00565 led4=0; 00566 wait_us(2); 00567 } 00568 00569 if ( (millis() - previous_millis_heater) >= 500 ) { 00570 manage_heater(); 00571 previous_millis_heater = millis(); 00572 00573 manage_inactivity(2); 00574 } 00575 00576 wait_us(2); 00577 } 00578 00579 led1=0; 00580 led2=0; 00581 led3=0; 00582 led4=0; 00583 00584 if (DISABLE_X) disable_x(); 00585 if (DISABLE_Y) disable_y(); 00586 if (DISABLE_Z) disable_z(); 00587 if (DISABLE_E) disable_e(); 00588 00589 // Update current position partly based on direction, we probably can combine this with the direction code above... 00590 if (destination_x > current_x) current_x = current_x + x_steps_to_take/x_steps_per_unit; 00591 else current_x = current_x - x_steps_to_take/x_steps_per_unit; 00592 if (destination_y > current_y) current_y = current_y + y_steps_to_take/y_steps_per_unit; 00593 else current_y = current_y - y_steps_to_take/y_steps_per_unit; 00594 if (destination_z > current_z) current_z = current_z + z_steps_to_take/z_steps_per_unit; 00595 else current_z = current_z - z_steps_to_take/z_steps_per_unit; 00596 if (destination_e > current_e) current_e = current_e + e_steps_to_take/e_steps_per_unit; 00597 else current_e = current_e - e_steps_to_take/e_steps_per_unit; 00598 } 00599 00600 00601 00602 00603 void ClearToSend() { 00604 previous_millis_cmd = millis(); 00605 pc.printf("ok\n"); 00606 } 00607 00608 00609 void FlushSerialRequestResend() { 00610 pc.printf("Resend: %d\n",(gcode_LastN+1)); 00611 //char cmdbuffer[100]="Resend:"; 00612 //ltoa(gcode_LastN+1, cmdbuffer+7, 10); 00613 //pc.flush(); 00614 //pc.printf(cmdbuffer); 00615 ClearToSend(); 00616 } 00617 00618 00619 //#define code_num (strtod(&cmdbuffer[strchr_pointer - cmdbuffer + 1], NULL)) 00620 //inline void code_search(char code) { strchr_pointer = strchr(cmdbuffer, code); } 00621 float code_value() { 00622 return (strtod(&cmdbuffer[strchr_pointer - cmdbuffer + 1], NULL)); 00623 } 00624 00625 long code_value_long() { 00626 return (strtol(&cmdbuffer[strchr_pointer - cmdbuffer + 1], NULL, 10)); 00627 } 00628 00629 bool code_seen(char code_string[]) { 00630 return (strstr(cmdbuffer, code_string) != NULL); //Return True if the string was found 00631 } 00632 00633 bool code_seen(char code) { 00634 strchr_pointer = strchr(cmdbuffer, code); 00635 return (strchr_pointer != NULL); //Return True if a character was found 00636 } 00637 00638 void get_coordinates() { 00639 if (code_seen('X')) destination_x = (float)code_value() + relative_mode*current_x; 00640 else destination_x = current_x; //Are these else lines really needed? 00641 if (code_seen('Y')) destination_y = (float)code_value() + relative_mode*current_y; 00642 else destination_y = current_y; 00643 if (code_seen('Z')) destination_z = (float)code_value() + relative_mode*current_z; 00644 else destination_z = current_z; 00645 if (code_seen('E')) destination_e = (float)code_value() + (relative_mode_e || relative_mode)*current_e; 00646 else destination_e = current_e; 00647 if (code_seen('F')) { 00648 next_feedrate = code_value(); 00649 if (next_feedrate > 0.0) feedrate = next_feedrate; 00650 } 00651 00652 //Find direction 00653 if (destination_x >= current_x) direction_x=1; 00654 else direction_x=0; 00655 if (destination_y >= current_y) direction_y=1; 00656 else direction_y=0; 00657 if (destination_z >= current_z) direction_z=1; 00658 else direction_z=0; 00659 if (destination_e >= current_e) direction_e=1; 00660 else direction_e=0; 00661 00662 00663 if (min_software_endstops) { 00664 if (destination_x < 0) destination_x = 0.0; 00665 if (destination_y < 0) destination_y = 0.0; 00666 if (destination_z < 0) destination_z = 0.0; 00667 } 00668 00669 if (max_software_endstops) { 00670 if (destination_x > X_MAX_LENGTH) destination_x = X_MAX_LENGTH; 00671 if (destination_y > Y_MAX_LENGTH) destination_y = Y_MAX_LENGTH; 00672 if (destination_z > Z_MAX_LENGTH) destination_z = Z_MAX_LENGTH; 00673 } 00674 00675 if (feedrate > max_feedrate) feedrate = max_feedrate; 00676 } 00677 00678 void process_commands() { 00679 unsigned long codenum; //throw away variable 00680 00681 if (code_seen('N')) { 00682 gcode_N = code_value_long(); 00683 if (gcode_N != gcode_LastN+1 && (strstr(cmdbuffer, "M110") == NULL) ) { 00684 gcode_LastN=0; 00685 pc.printf("ok"); 00686 //if(gcode_N != gcode_LastN+1 && !code_seen("M110") ) { //Hmm, compile size is different between using this vs the line above even though it should be the same thing. Keeping old method. 00687 //pc.printf("Serial Error: Line Number is not Last Line Number+1, Last Line:"); 00688 //pc.printf("%d\n",gcode_LastN); 00689 //FlushSerialRequestResend(); 00690 return; 00691 } 00692 00693 if (code_seen('*')) { 00694 int checksum = 0; 00695 int count=0; 00696 while (cmdbuffer[count] != '*') checksum = checksum^cmdbuffer[count++]; 00697 00698 if ( (int)code_value() != checksum) { 00699 //pc.printf("Error: checksum mismatch, Last Line:"); 00700 //pc.printf("%d\n",gcode_LastN); 00701 //FlushSerialRequestResend(); 00702 return; 00703 } 00704 //if no errors, continue parsing 00705 } else { 00706 //pc.printf("Error: No Checksum with line number, Last Line:"); 00707 //pc.printf("%d\n",gcode_LastN); 00708 //FlushSerialRequestResend(); 00709 return; 00710 } 00711 00712 gcode_LastN = gcode_N; 00713 //if no errors, continue parsing 00714 } else { // if we don't receive 'N' but still see '*' 00715 if (code_seen('*')) { 00716 //pc.printf("Error: No Line Number with checksum, Last Line:"); 00717 //pc.printf("%d\n",gcode_LastN); 00718 return; 00719 } 00720 } 00721 00722 //continues parsing only if we don't receive any 'N' or '*' or no errors if we do. :) 00723 00724 if (code_seen('G')) { 00725 switch ((int)code_value()) { 00726 case 0: // G0 -> G1 00727 case 1: // G1 00728 reset_timers();//avoid timer overflow after 30 seconds 00729 get_coordinates(); // For X Y Z E F 00730 x_steps_to_take = abs(destination_x - current_x)*x_steps_per_unit; 00731 y_steps_to_take = abs(destination_y - current_y)*y_steps_per_unit; 00732 z_steps_to_take = abs(destination_z - current_z)*z_steps_per_unit; 00733 e_steps_to_take = abs(destination_e - current_e)*e_steps_per_unit; 00734 //printf(" x_steps_to_take:%d\n", x_steps_to_take); 00735 00736 00737 time_for_move = max(X_TIME_FOR_MOVE,Y_TIME_FOR_MOVE); 00738 time_for_move = max(time_for_move,Z_TIME_FOR_MOVE); 00739 time_for_move = max(time_for_move,E_TIME_FOR_MOVE); 00740 00741 if (x_steps_to_take) x_interval = time_for_move/x_steps_to_take; 00742 if (y_steps_to_take) y_interval = time_for_move/y_steps_to_take; 00743 if (z_steps_to_take) z_interval = time_for_move/z_steps_to_take; 00744 if (e_steps_to_take) e_interval = time_for_move/e_steps_to_take; 00745 00746 00747 x_steps_remaining = x_steps_to_take; 00748 y_steps_remaining = y_steps_to_take; 00749 z_steps_remaining = z_steps_to_take; 00750 e_steps_remaining = e_steps_to_take; 00751 00752 00753 if (DEBUGGING) { 00754 pc.printf("destination_x: %f\n",destination_x); 00755 pc.printf("current_x: %f\n",current_x); 00756 pc.printf("x_steps_to_take: %d\n",x_steps_to_take); 00757 pc.printf("X_TIME_FOR_MOVE: %f\n",X_TIME_FOR_MOVE); 00758 pc.printf("x_interval: %f\n\n",x_interval); 00759 00760 pc.printf("destination_y: %f\n",destination_y); 00761 pc.printf("current_y: %f\n",current_y); 00762 pc.printf("y_steps_to_take: %d\n",y_steps_to_take); 00763 pc.printf("Y_TIME_FOR_MOVE: %f\n",Y_TIME_FOR_MOVE); 00764 pc.printf("y_interval: %f\n\n",y_interval); 00765 00766 pc.printf("destination_z: %f\n",destination_z); 00767 pc.printf("current_z: %f\n",current_z); 00768 pc.printf("z_steps_to_take: %d\n",z_steps_to_take); 00769 pc.printf("Z_TIME_FOR_MOVE: %f\n",Z_TIME_FOR_MOVE); 00770 pc.printf("z_interval: %f\n\n",z_interval); 00771 00772 pc.printf("destination_e: %f\n",destination_e); 00773 pc.printf("current_e: %f\n",current_e); 00774 pc.printf("e_steps_to_take: %d\n",e_steps_to_take); 00775 pc.printf("E_TIME_FOR_MOVE: %f\n",E_TIME_FOR_MOVE); 00776 pc.printf("e_interval: %f\n\n",e_interval); 00777 } 00778 00779 linear_move(); // make the move 00780 ClearToSend(); 00781 return; 00782 case 4: // G4 dwell 00783 codenum = 0; 00784 if (code_seen('P')) codenum = code_value(); // milliseconds to wait 00785 if (code_seen('S')) codenum = code_value()*1000; // seconds to wait 00786 previous_millis_heater = millis(); // keep track of when we started waiting 00787 while ((millis() - previous_millis_heater) < codenum ) manage_heater(); //manage heater until time is up 00788 break; 00789 case 90: // G90 00790 relative_mode = false; 00791 break; 00792 case 91: // G91 00793 relative_mode = true; 00794 break; 00795 case 92: // G92 00796 if (code_seen('X')) current_x = code_value(); 00797 if (code_seen('Y')) current_y = code_value(); 00798 if (code_seen('Z')) current_z = code_value(); 00799 if (code_seen('E')) current_e = code_value(); 00800 break; 00801 case 93: // G93 00802 pc.printf("previous_micros:%d\n", previous_micros); 00803 pc.printf("previous_micros_x:%d\n", previous_micros_x); 00804 pc.printf("previous_micros_y:%d\n", previous_micros_y); 00805 pc.printf("previous_micros_z:%d\n", previous_micros_z); 00806 break; 00807 00808 } 00809 } 00810 00811 if (code_seen('M')) { 00812 00813 switch ( (int)code_value() ) { 00814 case 104: // M104 - set hot-end temp 00815 00816 if (code_seen('S')) 00817 { 00818 00819 target_raw = temp2analog(code_value()); 00820 //pc.printf("target_raw: %d\n ", target_raw); 00821 } 00822 break; 00823 case 140: // M140 - set heated-printbed temp 00824 if (code_seen('S')) 00825 { 00826 00827 target_raw1 = temp2analog(code_value()); 00828 //pc.printf("target_raw1: %d\n ", target_raw); 00829 } 00830 break; 00831 00832 case 105: // M105 00833 pc.printf("ok T:"); 00834 if (TEMP_0_PIN != NC) { 00835 pc.printf("%f\n", analog2temp( (p_temp0.read_u16()) )); 00836 } else { 00837 pc.printf("0.0\n"); 00838 } 00839 if (!code_seen('N')) return; // If M105 is sent from generated gcode, then it needs a response. 00840 break; 00841 case 109: // M109 - Wait for heater to reach target. 00842 if (code_seen('S')) target_raw = temp2analog(code_value()); 00843 previous_millis_heater = millis(); 00844 while (current_raw < target_raw) { 00845 if ( (millis()-previous_millis_heater) > 1000 ) { //Print Temp Reading every 1 second while heating up. 00846 pc.printf("ok T:"); 00847 if (TEMP_0_PIN != NC) { 00848 pc.printf("%f\n", analog2temp(p_temp0.read_u16())); 00849 } else { 00850 pc.printf("0.0\n"); 00851 } 00852 previous_millis_heater = millis(); 00853 } 00854 manage_heater(); 00855 } 00856 break; 00857 case 106: //M106 Fan On 00858 p_fan = 1; 00859 break; 00860 case 107: //M107 Fan Off 00861 p_fan = 0; 00862 break; 00863 case 80: // M81 - ATX Power On 00864 //if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,OUTPUT); //GND 00865 break; 00866 case 81: // M81 - ATX Power Off 00867 //if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT); //Floating 00868 break; 00869 case 82: 00870 relative_mode_e = false; 00871 break; 00872 case 83: 00873 relative_mode_e = true; 00874 break; 00875 case 84: 00876 disable_x(); 00877 disable_y(); 00878 disable_z(); 00879 disable_e(); 00880 break; 00881 case 85: // M85 00882 code_seen('S'); 00883 max_inactive_time = code_value()*1000; 00884 break; 00885 case 86: // M86 If Endstop is Not Activated then Abort Print 00886 if (code_seen('X')) { 00887 if (X_MIN_PIN != NC) { 00888 if ( p_X_min == ENDSTOPS_INVERTING ) { 00889 kill(3); 00890 } 00891 } 00892 } 00893 if (code_seen('Y')) { 00894 if (Y_MIN_PIN != NC) { 00895 if ( p_Y_min == ENDSTOPS_INVERTING ) { 00896 kill(4); 00897 } 00898 } 00899 } 00900 break; 00901 case 92: // M92 00902 if (code_seen('X')) x_steps_per_unit = code_value(); 00903 if (code_seen('Y')) y_steps_per_unit = code_value(); 00904 if (code_seen('Z')) z_steps_per_unit = code_value(); 00905 if (code_seen('E')) e_steps_per_unit = code_value(); 00906 break; 00907 } 00908 00909 } 00910 00911 ClearToSend(); 00912 } 00913 00914 00915 void get_command() { 00916 if ( pc.readable() ) { 00917 serial_char = pc.getc(); 00918 00919 if (serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) ) { 00920 if (!serial_count) { 00921 return; //empty line 00922 } 00923 cmdbuffer[serial_count] = 0; //terminate string 00924 00925 process_commands(); 00926 00927 comment_mode = false; //for new command 00928 serial_count = 0; //clear buffer 00929 //Serial.println("ok"); 00930 } else { 00931 if (serial_char == ';') { 00932 comment_mode = true; 00933 } 00934 if (!comment_mode) { 00935 cmdbuffer[serial_count++] = serial_char; 00936 } 00937 } 00938 } 00939 00940 00941 } 00942 00943 00944 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00945 void setup() { 00946 pc.baud(BAUDRATE); 00947 pc.printf("start\n");//RepRap 00948 //pc.printf("A:\n");//HYDRA 00949 } 00950 00951 void loop() { 00952 get_command(); 00953 00954 manage_heater(); 00955 00956 manage_inactivity(1); //shutdown if not receiving any new commands 00957 } 00958 00959 int main() { 00960 timer.start(); 00961 setup(); 00962 00963 while (1) { 00964 loop(); 00965 } 00966 }
Generated on Tue Jul 19 2022 15:21:02 by
