@ -25,6 +25,7 @@
http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
*/
#include <EEPROM.h>
#include "EEPROMwrite.h"
#include "fastio.h"
#include "Configuration.h"
@ -37,9 +38,6 @@
#include "temperature.h"
#include "motion_control.h"
#ifdef SIMPLE_LCD
#include "Simplelcd.h"
#endif
char version_string[] = "1.0.0 Alpha 1";
@ -109,12 +107,9 @@ char version_string[] = "1.0.0 Alpha 1";
//Stepper Movement Variables
char axis_codes[NUM_AXIS] = {
'X', 'Y', 'Z', 'E'};
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};
const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
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};
float offset[3] = {0.0, 0.0, 0.0};
bool home_all_axis = true;
float feedrate = 1500.0, next_feedrate, saved_feedrate;
@ -131,6 +126,7 @@ uint8_t fanpwm=0;
volatile int feedmultiply=100; //100->1 200->2
int saved_feedmultiply;
volatile bool feedmultiplychanged=false;
// comm variables
#define MAX_CMD_SIZE 96
#define BUFSIZE 4
@ -146,13 +142,10 @@ boolean comment_mode = false;
char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
extern float HeaterPower;
#include "EEPROM.h"
const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
float tt = 0, bt = 0;
//Inactivity shutdown variables
unsigned long previous_millis_cmd = 0;
unsigned long max_inactive_time = 0;
@ -173,6 +166,9 @@ bool savetosd = false;
int16_t n;
unsigned long autostart_atmillis=0;
bool autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
void initsd()
{
sdactive = false;
@ -200,34 +196,39 @@ void initsd()
#endif //SDSS
}
void quickinitsd(){
void quickinitsd()
{
sdactive=false;
autostart_atmillis=millis()+5000;
}
inline void write_command(char *buf){
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){
if((npos = strchr(buf, 'N')) != NULL)
{
begin = strchr(npos, ' ') + 1;
end = strchr(npos, '*') - 1;
}
end[1] = '\r';
end[2] = '\n';
end[3] = '\0';
//Serial.println(begin);
file.write(begin);
if (file.writeError){
if (file.writeError)
{
SERIAL_ERRORLN("error writing to file");
}
}
#endif //SDSUPPORT
///adds an command to the main command buffer
//adds an command to the main command buffer
//thats really done in a non-safe way.
//needs overworking someday
void enquecommand(const char *cmd)
{
if(buflen < BUFSIZE)
@ -242,21 +243,18 @@ void enquecommand(const char *cmd)
void setup()
{
Serial.begin(BAUDRATE);
SERIAL_ECHOLN("Marlin "<<version_string);
Serial.println("start");
#if defined FANCY_LCD || defined SIMPLE_LCD
lcd_init();
#endif
for(int i = 0; i < BUFSIZE; i++){
for(int i = 0; i < BUFSIZE; i++)
{
fromsd[i] = false;
}
RetrieveSettings(); // loads data from EEPROM if available
for(int i=0; i < NUM_AXIS; i++) {
for(int i=0; i < NUM_AXIS; i++)
{
axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
}
@ -267,17 +265,14 @@ void setup()
WRITE(SDPOWER,HIGH);
#endif //SDPOWER
quickinitsd();
#endif //SDSUPPORT
plan_init(); // Initialize planner;
st_init(); // Initialize stepper;
tp_init(); // Initialize temperature loop
//checkautostart();
}
#ifdef SDSUPPORT
bool autostart_stilltocheck=true;
void checkautostart(bool force)
{
@ -304,8 +299,6 @@ void checkautostart(bool force)
dir_t p;
root.rewind();
//char filename[11];
//int cnt=0;
bool found=false;
while (root.readDir(p) > 0)
@ -328,20 +321,15 @@ void checkautostart(bool force)
enquecommand(cmd);
enquecommand("M24");
found=true;
}
}
if(!found)
lastnr=-1;
else
lastnr++;
}
#else
inline void checkautostart(bool x)
{
}
#else //NO SD SUPORT
inline void checkautostart(bool x){}
#endif
@ -353,19 +341,23 @@ void loop()
if(buflen)
{
#ifdef SDSUPPORT
if(savetosd){
if(strstr(cmdbuffer[bufindr],"M29") == NULL){
if(savetosd)
{
if(strstr(cmdbuffer[bufindr],"M29") == NULL)
{
write_command(cmdbuffer[bufindr]);
Serial.println("ok");
}
else{
else
{
file.sync();
file.close();
savetosd = false;
Serial.println("Done saving file.");
}
}
else{
else
{
process_commands();
}
#else
@ -497,7 +489,8 @@ inline void get_command()
LCD_MESSAGE(time);
checkautostart(true);
}
if(!serial_count) return; //if empty line
if(!serial_count)
return; //if empty line
cmdbuffer[bufindw][serial_count] = 0; //terminate string
if(!comment_mode){
fromsd[bufindw] = true;
@ -518,15 +511,18 @@ inline void get_command()
}
inline float code_value() {
inline float code_value()
{
return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
}
inline long code_value_long() {
inline long code_value_long()
{
return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
}
inline bool code_seen(char code_string[]) {
inline bool code_seen(char code_string[]) //Return True if the string was found
{
return (strstr(cmdbuffer[bufindr], code_string) != NULL);
} //Return True if the string was found
}
inline bool code_seen(char code)
{
@ -579,10 +575,10 @@ inline void process_commands()
destination[i] = current_position[i];
}
feedrate = 0.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[X_AXIS]))) {
if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
{
if ((X_MIN_PIN > -1 && X_HOME_DIR==-1) || (X_MAX_PIN > -1 && X_HOME_DIR==1)){
// st_synchronize();
current_position[X_AXIS] = 0;
@ -781,6 +777,8 @@ inline void process_commands()
//processed in write to file routine above
//savetosd = false;
break;
#endif //SDSUPPORT
case 30: //M30 take time since the start of the SD print or an M109 command
{
stoptime=millis();
@ -794,7 +792,6 @@ inline void process_commands()
LCD_MESSAGE(time);
}
break;
#endif //SDSUPPORT
case 42: //M42 -Change pin status via gcode
if (code_seen('S'))
{
@ -837,8 +834,6 @@ inline void process_commands()
#if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
Serial.print("ok T:");
Serial.print(tt);
// Serial.print(", raw:");
// Serial.print(current_raw);
#if TEMP_1_PIN > -1
#ifdef PIDTEMP
Serial.print(" B:");
@ -847,18 +842,19 @@ inline void process_commands()
#else
Serial.println(HeaterPower);
#endif
#else //not PIDTEMP
Serial.println();
#endif //PIDTEMP
#else
Serial.println();
#endif
#else
Serial.println();
#endif
#endif //TEMP_1_PIN
#else
Serial.println("echo: No thermistors - no temp");
#endif
return;
//break;
case 109: {// M109 - Wait for extruder heater to reach target.
break;
case 109:
{// M109 - Wait for extruder heater to reach target.
LCD_MESSAGE("Heating...");
if (code_seen('S')) setTargetHotend0(code_value());
@ -878,7 +874,8 @@ inline void process_commands()
#else
while ( target_direction ? (isHeatingHotend0()) : (isCoolingHotend0()) ) {
#endif //TEMP_RESIDENCY_TIME
if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up/cooling down
if( (millis() - codenum) > 1000 )
{ //Print Temp Reading every 1 second while heating up/cooling down
Serial.print("T:");
Serial.println( degHotend0() );
codenum = millis();
@ -890,7 +887,8 @@ inline void process_commands()
or when current temp falls outside the hysteresis after target temp was reached */
if ((residencyStart == -1 && target_direction && !isHeatingHotend0()) ||
(residencyStart == -1 && !target_direction && !isCoolingHotend0()) ||
(residencyStart > -1 && labs(degHotend0() - degTargetHotend0()) > TEMP_HYSTERESIS) ) {
(residencyStart > -1 && labs(degHotend0() - degTargetHotend0()) > TEMP_HYSTERESIS) )
{
residencyStart = millis();
}
#endif //TEMP_RESIDENCY_TIME
@ -920,6 +918,7 @@ inline void process_commands()
}
#endif
break;
#if FAN_PIN > -1
case 106: //M106 Fan On
if (code_seen('S')){
@ -937,7 +936,8 @@ inline void process_commands()
WRITE(FAN_PIN,LOW);
analogWrite(FAN_PIN, 0);
break;
#endif
#endif //FAN_PIN
#if (PS_ON_PIN > -1)
case 80: // M80 - ATX Power On
SET_OUTPUT(PS_ON_PIN); //GND
@ -952,12 +952,13 @@ inline void process_commands()
case 83:
axis_relative_modes[3] = true;
break;
case 18:
case 18: //compatibility
case 84:
if(code_seen('S')){
stepper_inactive_time = code_value() * 1000;
}
else{
else
{
st_synchronize();
disable_x();
disable_y();
@ -970,13 +971,14 @@ inline void process_commands()
max_inactive_time = code_value() * 1000;
break;
case 92: // M92
for(int i=0; i < NUM_AXIS; i++) {
if(code_seen(axis_codes[i])) axis_steps_per_unit[i] = code_value();
for(int i=0; i < NUM_AXIS; i++)
{
if(code_seen(axis_codes[i]))
axis_steps_per_unit[i] = code_value();
}
break;
case 115: // M115
Serial.println("FIRMWARE_NAME:Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
Serial.println("FIRMWARE_NAME:Marlin; Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
break;
case 114: // M114
Serial.print("X:");
@ -1026,7 +1028,8 @@ inline void process_commands()
break;
//TODO: update for all axis, use for loop
case 201: // M201
for(int i=0; i < NUM_AXIS; i++) {
for(int i=0; i < NUM_AXIS; i++)
{
if(code_seen(axis_codes[i])) axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
}
break;
@ -1066,20 +1069,12 @@ inline void process_commands()
}
}
break;
#ifdef PIDTEMP
case 301: // M301
if(code_seen('P')) Kp = code_value();
if(code_seen('I')) Ki = code_value()*PID_dT;
if(code_seen('D')) Kd = code_value()/PID_dT;
// SERIAL_ECHOLN("Kp "<<_FLOAT(Kp,2));
// SERIAL_ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2));
// SERIAL_ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2));
// temp_iState_min = 0.0;
// if (Ki!=0) {
// temp_iState_max = PID_INTEGRAL_DRIVE_MAX / (Ki/100.0);
// }
// else temp_iState_max = 1.0e10;
break;
#endif //PIDTEMP
case 500: // Store settings in EEPROM
@ -1100,7 +1095,8 @@ inline void process_commands()
}
}
else{
else
{
Serial.print("echo: Unknown command:\"");
Serial.print(cmdbuffer[bufindr]);
Serial.println("\"");
@ -1276,9 +1272,14 @@ void prepare_arc_move(char isclockwise) {
void manage_inactivity(byte debug) {
if( (millis()-previous_millis_cmd) > max_inactive_time ) if(max_inactive_time) kill();
if( (millis()-previous_millis_cmd) > stepper_inactive_time ) if(stepper_inactive_time) {
void manage_inactivity(byte debug)
{
if( (millis()-previous_millis_cmd) > max_inactive_time )
if(max_inactive_time)
kill();
if( (millis()-previous_millis_cmd) > stepper_inactive_time )
if(stepper_inactive_time)
{
disable_x();
disable_y();
disable_z();