Commit ee98a976 authored by MagoKimbra's avatar MagoKimbra

New Pid endstop and fix M303 error

parent 7f4d4221
......@@ -21,7 +21,6 @@
// startup. Implementation of an idea by Prof Braino to inform user that any changes made to this
// build by the user have been successfully uploaded into firmware.
#define STRING_VERSION "4.1.3"
#define STRING_URL "reprap.org"
#define STRING_VERSION_CONFIG_H __DATE__ " " __TIME__ // build date and time
#define STRING_CONFIG_H_AUTHOR "(none, default config)" // Who made the changes.
#define STRING_SPLASH_LINE1 "v" STRING_VERSION // will be shown during bootup in line 1
......@@ -222,6 +221,7 @@
#define PID_FUNCTIONAL_RANGE 10 // degC
#define PID_INTEGRAL_DRIVE_MAX PID_MAX // Limit for the integral term
#define K1 0.95 // Smoothing factor within the PID
#define MAX_OVERSHOOT_PID_AUTOTUNE 20 // Max valor for overshoot autotune
// HotEnd{HE0,HE1,HE2,HE3}
#define DEFAULT_Kp {40, 40, 40, 40} // Kp for E0, E1, E2, E3
......
......@@ -31,7 +31,7 @@
/**
* Whenever an M104 or M109 increases the target temperature the firmware will wait for the
* WATCH_TEMP_PERIOD to transpire, and if the temperature hasn't increased by WATCH_TEMP_INCREASE
* WATCH_TEMP_PERIOD to expire, and if the temperature hasn't increased by WATCH_TEMP_INCREASE
* degrees, the machine is halted, requiring a hard reset. This test restarts with any M104/M109,
* but only if the current temperature is far enough below the target for a reliable test.
*/
......
......@@ -36,6 +36,7 @@
#define BIT(b) (1<<(b))
#define TEST(n,b) (((n)&BIT(b))!=0)
#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
#define RADIANS(d) ((d)*M_PI/180.0)
#define DEGREES(r) ((d)*180.0/M_PI)
#define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
......
......@@ -3667,11 +3667,13 @@ inline void gcode_G92() {
didXYZ = true;
}
}
if (didXYZ) {
#if defined(DELTA) || defined(SCARA)
if (didXYZ) sync_plan_position_delta();
sync_plan_position_delta();
#else
if (didXYZ) sync_plan_position();
sync_plan_position();
#endif
}
}
#ifdef ULTIPANEL
......@@ -5961,7 +5963,7 @@ inline void gcode_T() {
#else // !DUAL_X_CARRIAGE
// Offset hotend (only by XY)
#if HOTENDS > 1
for (int i=X_AXIS; i<=Y_AXIS; i++)
for (int i = X_AXIS; i <= Y_AXIS; i++)
current_position[i] += hotend_offset[i][target_extruder] - hotend_offset[i][active_extruder];
#endif // HOTENDS > 1
......@@ -6409,7 +6411,10 @@ void process_next_command() {
#endif // PREVENT_DANGEROUS_EXTRUDE
case 303: // M303 PID autotune
gcode_M303(); break;
gcode_M303();
gcode_LastN += 1;
FlushSerialRequestResend();
break;
#ifdef PIDTEMPBED
case 304: // M304
......
......@@ -41,10 +41,10 @@
#define START "start" //start for host
#define OK "ok" //ok answer for host
#define ER "Error:" //error for host
#define ER "Error: " //error for host
#define WT "wait" //wait for host
#define DB "<MK4>: " //message for user
#define RS "Resend:" //resend for host
#define DB "MK4: " //message for user
#define RS "Resend: " //resend for host
#define PAUSE "//action:pause" //command for host that support action
#define RESUME "//action:resume" //command for host that support action
#define DISCONNECT "//action:disconnect" //command for host that support action
......
......@@ -76,6 +76,11 @@ volatile long endstops_trigsteps[3] = { 0 };
volatile long endstops_stepsTotal, endstops_stepsDone;
static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_PROBE as BIT value
static char old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_PROBE
#ifdef Z_DUAL_ENDSTOPS
static char old_dual_endstop_bits = 0; // actually only implemented for Z
#endif
#ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
bool abort_on_endstop_hit = false;
#endif
......@@ -84,35 +89,6 @@ static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_PROB
int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
#endif
#if HAS_X_MIN
static bool old_x_min_endstop = false;
#endif
#if HAS_X_MAX
static bool old_x_max_endstop = false;
#endif
#if HAS_Y_MIN
static bool old_y_min_endstop = false;
#endif
#if HAS_Y_MAX
static bool old_y_max_endstop = false;
#endif
static bool old_z_min_endstop = false;
static bool old_z_max_endstop = false;
#ifdef Z_DUAL_ENDSTOPS
static bool old_z2_min_endstop = false;
static bool old_z2_max_endstop = false;
#endif
#ifdef Z_PROBE_ENDSTOP // No need to check for valid pin, SanityCheck.h already does this.
static bool old_z_probe_endstop = false;
#endif
#ifdef NPR2
static bool old_e_min_endstop = false;
#endif
static bool check_endstops = true;
volatile long count_position[NUM_AXIS] = { 0 };
......@@ -159,11 +135,11 @@ volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
#define Z_APPLY_STEP(v,Q) \
if (performing_homing) { \
if (Z_HOME_DIR > 0) {\
if (!(old_z_max_endstop && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
if (!(old_z2_max_endstop && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
if (!(TEST(old_dual_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
} else {\
if (!(old_z_min_endstop && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
if (!(old_z2_min_endstop && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
if (!(TEST(old_dual_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
} \
} else { \
Z_STEP_WRITE(v); \
......@@ -490,22 +466,31 @@ ISR(TIMER1_COMPA_vect) {
// Check endstops
if (check_endstops) {
#define _ENDSTOP(axis, minmax) axis ##_## minmax ##_endstop
char current_endstop_bits;
#ifdef Z_DUAL_ENDSTOPS
char current_dual_endstop_bits;
#endif
#define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
#define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
#define _OLD_ENDSTOP(axis, minmax) old_## axis ##_## minmax ##_endstop
#define _AXIS(AXIS) AXIS ##_AXIS
#define _HIT_BIT(AXIS) AXIS ##_MIN
#define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_HIT_BIT(AXIS))
#define UPDATE_ENDSTOP(axis,AXIS,minmax,MINMAX) \
bool _ENDSTOP(axis, minmax) = (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)); \
if (_ENDSTOP(axis, minmax) && _OLD_ENDSTOP(axis, minmax) && (current_block->steps[_AXIS(AXIS)] > 0)) { \
#define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_ENDSTOP(AXIS, MIN))
#define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
// GET_ENDSTOP_STATUS: set the current endstop bits for an endstop to its status
#define GET_ENDSTOP_STATUS(endstop, AXIS, MINMAX) SET_BIT(endstop, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
// TEST_ENDSTOP: test the old and the current status of an endstop
#define TEST_ENDSTOPS(AXIS, MINMAX) (TEST(current_endstop_bits, _ENDSTOP(AXIS, MINMAX)) && TEST(old_endstop_bits, _ENDSTOP(AXIS, MINMAX)))
// TEST_DUAL_ENDSTOP: same like TEST_ENDSTOP for dual endstops
#define TEST_DUAL_ENDSTOPS(AXIS, MINMAX) (TEST(current_dual_endstop_bits, _ENDSTOP(AXIS, MINMAX)) && TEST(old_dual_endstop_bits, _ENDSTOP(AXIS, MINMAX)))
#define UPDATE_ENDSTOP(AXIS,MINMAX) \
GET_ENDSTOP_STATUS(current_endstop_bits, AXIS, MINMAX); \
if (TEST_ENDSTOPS(AXIS, MINMAX) && (current_block->steps[_AXIS(AXIS)] > 0)) { \
endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \
_ENDSTOP_HIT(AXIS); \
step_events_completed = current_block->step_event_count; \
} \
_OLD_ENDSTOP(axis, minmax) = _ENDSTOP(axis, minmax);
}
#ifdef COREXY
// Head direction in -X axis for CoreXY bots.
......@@ -522,7 +507,7 @@ ISR(TIMER1_COMPA_vect) {
#endif
{
#if HAS_X_MIN
UPDATE_ENDSTOP(x, X, min, MIN);
UPDATE_ENDSTOP(X, MIN);
#endif
}
}
......@@ -533,7 +518,7 @@ ISR(TIMER1_COMPA_vect) {
#endif
{
#if HAS_X_MAX
UPDATE_ENDSTOP(x, X, max, MAX);
UPDATE_ENDSTOP(X, MAX);
#endif
}
}
......@@ -548,12 +533,12 @@ ISR(TIMER1_COMPA_vect) {
#endif
{ // -direction
#if HAS_Y_MIN
UPDATE_ENDSTOP(y, Y, min, MIN);
UPDATE_ENDSTOP(Y, MIN);
#endif
}
else { // +direction
#if HAS_Y_MAX
UPDATE_ENDSTOP(y, Y, max, MAX);
UPDATE_ENDSTOP(Y, MAX);
#endif
}
#ifdef COREXY
......@@ -563,43 +548,38 @@ ISR(TIMER1_COMPA_vect) {
#if HAS_Z_MIN
#ifdef Z_DUAL_ENDSTOPS
GET_ENDSTOP_STATUS(current_endstop_bits, Z, MIN);
#if HAS_Z2_MIN
GET_ENDSTOP_STATUS(current_dual_endstop_bits, Z, MIN);
#endif
bool z_min_endstop = READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING,
z2_min_endstop =
bool z_test = TEST_ENDSTOPS(Z, MIN)
#if HAS_Z2_MIN
READ(Z2_MIN_PIN) != Z2_MIN_ENDSTOP_INVERTING
#else
z_min_endstop
&& TEST_DUAL_ENDSTOPS(Z, MIN)
#endif
;
bool z_min_both = z_min_endstop && old_z_min_endstop,
z2_min_both = z2_min_endstop && old_z2_min_endstop;
if ((z_min_both || z2_min_both) && current_block->steps[Z_AXIS] > 0) {
if (z_test && current_block->steps[Z_AXIS] > 0) {
endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
endstop_hit_bits |= BIT(Z_MIN);
if (!performing_homing || (performing_homing && z_min_both && z2_min_both)) //if not performing home or if both endstops were trigged during homing...
if (!performing_homing || (performing_homing && !z_test)) //if not performing home or if both endstops were trigged during homing...
step_events_completed = current_block->step_event_count;
}
old_z_min_endstop = z_min_endstop;
old_z2_min_endstop = z2_min_endstop;
#else // !Z_DUAL_ENDSTOPS
UPDATE_ENDSTOP(z, Z, min, MIN);
UPDATE_ENDSTOP(Z, MIN);
#endif // !Z_DUAL_ENDSTOPS
#endif // Z_MIN_PIN
#ifdef Z_PROBE_ENDSTOP
UPDATE_ENDSTOP(z, Z, probe, PROBE);
z_probe_endstop = (READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
if (z_probe_endstop && old_z_probe_endstop) {
UPDATE_ENDSTOP(Z, PROBE);
GET_ENDSTOP_STATUS(current_endstop_bits, Z, PROBE);
if(TEST_ENDSTOPS(Z, PROBE))
{
endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
endstop_hit_bits |= BIT(Z_PROBE);
}
old_z_probe_endstop = z_probe_endstop;
#endif
}
else { // z +direction
......@@ -607,40 +587,39 @@ ISR(TIMER1_COMPA_vect) {
#ifdef Z_DUAL_ENDSTOPS
bool z_max_endstop = READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING,
z2_max_endstop =
GET_ENDSTOP_STATUS(current_endstop_bits, Z, MAX);
#if HAS_Z2_MAX
READ(Z2_MAX_PIN) != Z2_MAX_ENDSTOP_INVERTING
#else
z_max_endstop
GET_ENDSTOP_STATUS(current_dual_endstop_bits, Z, MAX);
#endif
bool z_test = TEST_ENDSTOPS(Z, MAX)
#if HAS_Z2_MAX
&& TEST_DUAL_ENDSTOPS(Z, MAX)
#endif
;
bool z_max_both = z_max_endstop && old_z_max_endstop,
z2_max_both = z2_max_endstop && old_z2_max_endstop;
if ((z_max_both || z2_max_both) && current_block->steps[Z_AXIS] > 0) {
if (z_test && current_block->steps[Z_AXIS] > 0) {
endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
endstop_hit_bits |= BIT(Z_MIN);
// if (z_max_both) ECHO_EV("z_max_endstop = true");
// if (z2_max_both) ECHO_EV("z2_max_endstop = true");
if (!performing_homing || (performing_homing && z_max_both && z2_max_both)) //if not performing home or if both endstops were trigged during homing...
if (!performing_homing || (performing_homing && !z_test)) //if not performing home or if both endstops were trigged during homing...
step_events_completed = current_block->step_event_count;
}
old_z_max_endstop = z_max_endstop;
old_z2_max_endstop = z2_max_endstop;
#else // !Z_DUAL_ENDSTOPS
UPDATE_ENDSTOP(z, Z, max, MAX);
UPDATE_ENDSTOP(Z, MAX);
#endif // !Z_DUAL_ENDSTOPS
#endif // Z_MAX_PIN
}
old_endstop_bits = current_endstop_bits;
#ifdef Z_DUAL_ENDSTOPS
old_dual_endstop_bits = current_dual_endstop_bits;
#endif
}
// Take multiple steps per interrupt (For high speed moves)
for (int8_t i = 0; i < step_loops; i++) {
#ifndef AT90USB
......
......@@ -55,9 +55,9 @@ float current_temperature_bed = 0.0;
#endif
#ifdef PIDTEMPBED
float bedKp=DEFAULT_bedKp;
float bedKi=(DEFAULT_bedKi*PID_dT);
float bedKd=(DEFAULT_bedKd/PID_dT);
float bedKp = DEFAULT_bedKp;
float bedKi = (DEFAULT_bedKi*PID_dT);
float bedKd = (DEFAULT_bedKd/PID_dT);
#endif //PIDTEMPBED
#ifdef FAN_SOFT_PWM
......@@ -181,8 +181,7 @@ static void updateTemperaturesFromRawValues();
//================================ Functions ================================
//===========================================================================
void PID_autotune(float temp, int hotend, int ncycles)
{
void PID_autotune(float temp, int hotend, int ncycles) {
float input = 0.0;
int cycles = 0;
bool heating = true;
......@@ -300,7 +299,7 @@ void PID_autotune(float temp, int hotend, int ncycles)
}
}
}
if (input > temp + 20) {
if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
ECHO_LM(ER, MSG_PID_TEMP_TOO_HIGH);
return;
}
......@@ -315,7 +314,7 @@ void PID_autotune(float temp, int hotend, int ncycles)
}
else {
p = soft_pwm[hotend];
ECHO_SMV(OK, MSG_T, input);
ECHO_SMV(OK, MSG_T, input, 1);
ECHO_EMV(MSG_AT, p);
}
......
......@@ -66,7 +66,7 @@ extern float current_temperature_bed;
#ifdef PIDTEMP
extern float Kp[HOTENDS], Ki[HOTENDS], Kd[HOTENDS];
#define PID_PARAM(param,e) param[e] // use macro to point to array value
#define PID_PARAM(param, e) param[e] // use macro to point to array value
float scalePID_i(float i);
float scalePID_d(float d);
float unscalePID_i(float i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment