Skip to content

Commit

Permalink
Check probes only when deployed
Browse files Browse the repository at this point in the history
Aim: Test probes in update_endstops only when activated

Changes:
Configurations
 Add define for FIX_MOUNTED_PROBE to handle the situation where formerly ENDSTOPS_ONLY_FOR_HOMING had to be set, or lowering the nozzle below Z_PROBE_OFFSET_FROM_EXTRUDER could give an "endstop hit" message.
 Add define for Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN to indicate a common situation, that we have a probe but it is connected to an endstop pin
 Add some comments
 Shift some entries to have related things together.

Conditionals.h
 We have a probe (HAS_Z_MIN_PROBE) if one of the pins is defined AND one of the probes is defined.

SanityCheck.h
 Add some tests if the probe is connected and if we have defined a probe.

stepper.cpp
 Changes to test the probe only when it is deployed (z_probe_is_active).
 Test update_endstops() when the probe is deployed.

MarlinMain.cpp
 a. set and reset z_probe_is_active in deploy_z_probe(), stow_z_probe() and dock_sled()
 b. set and reset z_probe_is_active in the case a z-servo is moved to a defined position. The only remaining unhandled servo move is in M280 where we do not end in a defined position. If you want to handle a probe use M401/402
 c. skip deploying/stowing when already deployed/stowed in the dedicated deploy/stow functions.
 d. Handle the new FIX_MOUNTED_PROBE in parallel to a servo driven probe/endstop.

 To do: In another PR. handle all probes in deploy/stow_z_probe.
 Sort out SERVO_LEVELING vs. HAS_SERVO_ENDSTOPS.
  • Loading branch information
AnHardt authored and thinkyhead committed Mar 19, 2016
1 parent 4634fea commit 3f45a1a
Show file tree
Hide file tree
Showing 23 changed files with 1,199 additions and 642 deletions.
5 changes: 5 additions & 0 deletions Marlin/Conditionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,10 @@
#endif
#endif

#if ( (HAS_Z_MIN && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)) || HAS_Z_PROBE ) && \
( ENABLED(FIX_MOUNTED_PROBE) || defined(Z_ENDSTOP_SERVO_NR) || ENABLED(Z_PROBE_ALLEN_KEY) || ENABLED(Z_PROBE_SLED) )
#define HAS_Z_MIN_PROBE
#endif

#endif //CONFIGURATION_LCD
#endif //CONDITIONALS_H
103 changes: 63 additions & 40 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,52 @@ const bool Z_MIN_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the l
//#define DISABLE_MAX_ENDSTOPS
//#define DISABLE_MIN_ENDSTOPS

// If you want to enable the Z probe pin, but disable its use, uncomment the line below.
// This only affects a Z probe endstop if you have separate Z min endstop as well and have
// activated Z_MIN_PROBE_ENDSTOP below. If you are using the Z Min endstop on your Z probe,
// this has no effect.
//===========================================================================
//============================= Z Probe Options =============================
//===========================================================================

// Enable Z_MIN_PROBE_ENDSTOP to use _both_ a Z-probe and a Z-min-endstop on the same machine.
// With this option the Z_MIN_PROBE_PIN will only be used for probing, never for homing.
//
// *** PLEASE READ ALL INSTRUCTIONS BELOW FOR SAFETY! ***
//
// To continue using the Z-min-endstop for homing, be sure to disable Z_SAFE_HOMING.
// Example: To park the head outside the bed area when homing with G28.
//
// To use a separate Z probe, your board must define a Z_MIN_PROBE_PIN.
//
// For a servo-based Z probe, you must set up servo support below, including
// NUM_SERVOS, Z_ENDSTOP_SERVO_NR and SERVO_ENDSTOP_ANGLES.
//
// - RAMPS 1.3/1.4 boards may be able to use the 5V, GND, and Aux4->D32 pin.
// - Use 5V for powered (usu. inductive) sensors.
// - Otherwise connect:
// - normally-closed switches to GND and D32.
// - normally-open switches to 5V and D32.
//
// Normally-closed switches are advised and are the default.
//
// The Z_MIN_PROBE_PIN sets the Arduino pin to use. (See your board's pins file.)
// Since the RAMPS Aux4->D32 pin maps directly to the Arduino D32 pin, D32 is the
// default pin for all RAMPS-based boards. Some other boards map differently.
// To set or change the pin for your board, edit the appropriate pins_XXXXX.h file.
//
// WARNING:
// Setting the wrong pin may have unexpected and potentially disastrous consequences.
// Use with caution and do your homework.
//
//#define Z_MIN_PROBE_ENDSTOP

// Enable Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN to use the Z_MIN_PIN for your Z_MIN_PROBE.
// The Z_MIN_PIN will then be used for both Z-homing and probing.
#define Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN

// To use a probe you must enable one of the two options above!

// This option disables the use of the Z_MIN_PROBE_PIN
// To enable the Z probe pin but disable its use, uncomment the line below. This only affects a
// Z probe switch if you have a separate Z min endstop also and have activated Z_MIN_PROBE_ENDSTOP above.
// If you're using the Z MIN endstop connector for your Z probe, this has no effect.
//#define DISABLE_Z_MIN_PROBE_ENDSTOP

// For Inverting Stepper Enable Pins (Active Low) use 0, Non Inverting (Active High) use 1
Expand Down Expand Up @@ -521,14 +563,26 @@ const bool Z_MIN_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the l
//#define Z_PROBE_END_SCRIPT "G1 Z10 F12000\nG1 X15 Y330\nG1 Z0.5\nG1 Z10" // These commands will be executed in the end of G29 routine.
// Useful to retract a deployable Z probe.

//#define Z_PROBE_SLED // Turn on if you have a Z probe mounted on a sled like those designed by Charles Bell.
//#define SLED_DOCKING_OFFSET 5 // The extra distance the X axis must travel to pickup the sled. 0 should be fine but you can push it further if you'd like.
// Probes are sensors/switches that need to be activated before they can be used
// and deactivated after the use.
// Allen Key Probes, Servo Probes, Z-Sled Probes, FIX_MOUNTED_PROBE, ... . You have to activate one of these for the AUTO_BED_LEVELING_FEATURE

// A fix mounted probe, like the normal inductive probe, must be deactivated to go below Z_PROBE_OFFSET_FROM_EXTRUDER
// when the hardware endstops are active.
//#define FIX_MOUNTED_PROBE

// A Servo Probe can be defined in the servo section below.

// An Allen Key Probe is currently predefined only in the delta example configurations.

//#define Z_PROBE_SLED // Enable if you have a Z probe mounted on a sled like those designed by Charles Bell.
//#define SLED_DOCKING_OFFSET 5 // The extra distance the X axis must travel to pickup the sled. 0 should be fine but you can push it further if you'd like.

//If you have enabled the Bed Auto Leveling and are using the same Z Probe for Z Homing,
//it is highly recommended you let this Z_SAFE_HOMING enabled!!!
// If you've enabled AUTO_BED_LEVELING_FEATURE and are using the Z Probe for Z Homing,
// it is highly recommended you leave Z_SAFE_HOMING enabled!

#define Z_SAFE_HOMING // This feature is meant to avoid Z homing with Z probe outside the bed area.
#define Z_SAFE_HOMING // Use the z-min-probe for homing to z-min - not the z-min-endstop.
// This feature is meant to avoid Z homing with Z probe outside the bed area.
// When defined, it will:
// - Allow Z homing only after X and Y homing AND stepper drivers still enabled.
// - If stepper drivers timeout, it will need X and Y homing again before Z homing.
Expand All @@ -542,37 +596,6 @@ const bool Z_MIN_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the l

#endif

// Support for a dedicated Z probe endstop separate from the Z min endstop.
// If you would like to use both a Z probe and a Z min endstop together,
// uncomment #define Z_MIN_PROBE_ENDSTOP and read the instructions below.
// If you still want to use the Z min endstop for homing, disable Z_SAFE_HOMING above.
// Example: To park the head outside the bed area when homing with G28.
//
// WARNING:
// The Z min endstop will need to set properly as it would without a Z probe
// to prevent head crashes and premature stopping during a print.
//
// To use a separate Z probe endstop, you must have a Z_MIN_PROBE_PIN
// defined in the pins_XXXXX.h file for your control board.
// If you are using a servo based Z probe, you will need to enable NUM_SERVOS,
// Z_ENDSTOP_SERVO_NR and SERVO_ENDSTOP_ANGLES in the R/C SERVO support below.
// RAMPS 1.3/1.4 boards may be able to use the 5V, Ground and the D32 pin
// in the Aux 4 section of the RAMPS board. Use 5V for powered sensors,
// otherwise connect to ground and D32 for normally closed configuration
// and 5V and D32 for normally open configurations.
// Normally closed configuration is advised and assumed.
// The D32 pin in Aux 4 on RAMPS maps to the Arduino D32 pin.
// Z_MIN_PROBE_PIN is setting the pin to use on the Arduino.
// Since the D32 pin on the RAMPS maps to D32 on Arduino, this works.
// D32 is currently selected in the RAMPS 1.3/1.4 pin file.
// All other boards will need changes to the respective pins_XXXXX.h file.
//
// WARNING:
// Setting the wrong pin may have unexpected and potentially disastrous outcomes.
// Use with caution and do your homework.
//
//#define Z_MIN_PROBE_ENDSTOP

#endif // AUTO_BED_LEVELING_FEATURE


Expand Down
103 changes: 73 additions & 30 deletions Marlin/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ void serial_echopair_P(const char* s_P, unsigned long v) { serialprintPGM(s_P);
float extrude_min_temp = EXTRUDE_MINTEMP;
#endif

#if ENABLED(HAS_Z_MIN_PROBE)
extern volatile bool z_probe_is_active;
#endif

#if ENABLED(SDSUPPORT)
#include "SdFatUtil.h"
int freeMemory() { return SdFatUtil::FreeRam(); }
Expand Down Expand Up @@ -619,12 +623,26 @@ void servo_init() {
servo[3].detach();
#endif

// Set position of Servo Endstops that are defined
#if HAS_SERVO_ENDSTOPS
#if HAS_SERVO_ENDSTOPS

z_probe_is_active = false;

/**
* Set position of all defined Servo Endstops
*
* ** UNSAFE! - NEEDS UPDATE! **
*
* The servo might be deployed and positioned too low to stow
* when starting up the machine or rebooting the board.
* There's no way to know where the nozzle is positioned until
* homing has been done - no homing with z-probe without init!
*
*/
for (int i = 0; i < 3; i++)
if (servo_endstop_id[i] >= 0)
servo[servo_endstop_id[i]].move(servo_endstop_angle[i][1]);
#endif

#endif // HAS_SERVO_ENDSTOPS

}

Expand Down Expand Up @@ -1458,6 +1476,8 @@ static void setup_for_endstop_move() {
refresh_cmd_timeout();
}

#if ENABLED(HAS_Z_MIN_PROBE)

static void deploy_z_probe() {

#if ENABLED(DEBUG_LEVELING_FEATURE)
Expand All @@ -1466,6 +1486,8 @@ static void setup_for_endstop_move() {
}
#endif

if (z_probe_is_active) return;

#if HAS_SERVO_ENDSTOPS

// Engage Z Servo endstop if enabled
Expand Down Expand Up @@ -1505,20 +1527,19 @@ static void setup_for_endstop_move() {
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE != Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE)
feedrate = Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE;

// Move to trigger deployment
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE != Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE)
feedrate = Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE;
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_X != Z_PROBE_ALLEN_KEY_DEPLOY_2_X)
destination[X_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_3_X;
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_Y != Z_PROBE_ALLEN_KEY_DEPLOY_2_Y)
destination[Y_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_3_Y;
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_Z != Z_PROBE_ALLEN_KEY_DEPLOY_2_Z)
destination[Z_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_3_Z;

prepare_move_raw();

#endif
}
// Move to trigger deployment
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE != Z_PROBE_ALLEN_KEY_DEPLOY_2_FEEDRATE)
feedrate = Z_PROBE_ALLEN_KEY_DEPLOY_3_FEEDRATE;
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_X != Z_PROBE_ALLEN_KEY_DEPLOY_2_X)
destination[X_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_3_X;
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_Y != Z_PROBE_ALLEN_KEY_DEPLOY_2_Y)
destination[Y_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_3_Y;
if (Z_PROBE_ALLEN_KEY_DEPLOY_3_Z != Z_PROBE_ALLEN_KEY_DEPLOY_2_Z)
destination[Z_AXIS] = Z_PROBE_ALLEN_KEY_DEPLOY_3_Z;

prepare_move_raw();
#endif
}

// Partially Home X,Y for safety
destination[X_AXIS] = destination[X_AXIS] * 0.75;
Expand All @@ -1545,6 +1566,12 @@ static void setup_for_endstop_move() {

#endif // Z_PROBE_ALLEN_KEY

#if ENABLED(FIX_MOUNTED_PROBE)
// Noting to be done. Just set z_probe_is_active
#endif

z_probe_is_active = true;

}

static void stow_z_probe(bool doRaise = true) {
Expand All @@ -1554,6 +1581,8 @@ static void setup_for_endstop_move() {
}
#endif

if (!z_probe_is_active) return;

#if HAS_SERVO_ENDSTOPS

// Retract Z Servo endstop if enabled
Expand Down Expand Up @@ -1638,7 +1667,14 @@ static void setup_for_endstop_move() {
Stop();
}
#endif // Z_PROBE_ALLEN_KEY

#if ENABLED(FIX_MOUNTED_PROBE)
// Noting to be done. Just set z_probe_is_active
#endif

z_probe_is_active = false;
}
#endif // HAS_Z_MIN_PROBE

enum ProbeAction {
ProbeStay = 0,
Expand Down Expand Up @@ -1825,6 +1861,9 @@ static void setup_for_endstop_move() {
SERIAL_EOL;
}
#endif

if (z_probe_is_active == dock) return;

if (!axis_known_position[X_AXIS] || !axis_known_position[Y_AXIS]) {
LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
SERIAL_ECHO_START;
Expand All @@ -1847,6 +1886,8 @@ static void setup_for_endstop_move() {
digitalWrite(SLED_PIN, HIGH); // turn on magnet
}
do_blocking_move_to_x(oldXpos); // return to position before docking

z_probe_is_active = dock;
}

#endif // Z_PROBE_SLED
Expand Down Expand Up @@ -1887,9 +1928,7 @@ static void homeaxis(AxisEnum axis) {
if (axis == Z_AXIS) {
if (axis_home_dir < 0) dock_sled(false);
}
#endif

#if SERVO_LEVELING && DISABLED(Z_PROBE_SLED)
#elif SERVO_LEVELING || ENABLED(FIX_MOUNTED_PROBE)

// Deploy a Z probe if there is one, and homing towards the bed
if (axis == Z_AXIS) {
Expand All @@ -1900,8 +1939,10 @@ static void homeaxis(AxisEnum axis) {

#if HAS_SERVO_ENDSTOPS
// Engage Servo endstop if enabled
if (axis != Z_AXIS && servo_endstop_id[axis] >= 0)
if (axis != Z_AXIS && servo_endstop_id[axis] >= 0) {
servo[servo_endstop_id[axis]].move(servo_endstop_angle[axis][0]);
z_probe_is_active = true;
}
#endif

// Set a flag for Z motor locking
Expand Down Expand Up @@ -2034,9 +2075,7 @@ static void homeaxis(AxisEnum axis) {
if (axis == Z_AXIS) {
if (axis_home_dir < 0) dock_sled(true);
}
#endif

#if SERVO_LEVELING && DISABLED(Z_PROBE_SLED)
#elif SERVO_LEVELING || ENABLED(FIX_MOUNTED_PROBE)

// Deploy a Z probe if there is one, and homing towards the bed
if (axis == Z_AXIS) {
Expand All @@ -2063,6 +2102,7 @@ static void homeaxis(AxisEnum axis) {
}
#endif
servo[servo_endstop_id[axis]].move(servo_endstop_angle[axis][1]);
z_probe_is_active = false;
}
#endif
}
Expand Down Expand Up @@ -3195,7 +3235,7 @@ inline void gcode_G28() {
#if ENABLED(Z_PROBE_ALLEN_KEY)
stow_z_probe();
#elif Z_RAISE_AFTER_PROBING > 0
raise_z_after_probing();
raise_z_after_probing(); // ???
#endif
#else // !DELTA
if (verbose_level > 0)
Expand Down Expand Up @@ -3279,6 +3319,9 @@ inline void gcode_G28() {
}
#endif
enqueue_and_echo_commands_P(PSTR(Z_PROBE_END_SCRIPT));
#if ENABLED(HAS_Z_MIN_PROBE)
z_probe_is_active = false;
#endif
st_synchronize();
#endif

Expand All @@ -3292,7 +3335,7 @@ inline void gcode_G28() {

}

#if DISABLED(Z_PROBE_SLED)
#if DISABLED(Z_PROBE_SLED) // could be avoided

/**
* G30: Do a single Z probe at the current XY
Expand All @@ -3301,11 +3344,11 @@ inline void gcode_G28() {
#if HAS_SERVO_ENDSTOPS
raise_z_for_servo();
#endif
deploy_z_probe(); // Engage Z Servo endstop if available
deploy_z_probe(); // Engage Z Servo endstop if available. Z_PROBE_SLED is missed her.

st_synchronize();
// TODO: clear the leveling matrix or the planner will be set incorrectly
setup_for_endstop_move();
setup_for_endstop_move(); // to late. Must be done before deploying.

feedrate = homing_feedrate[Z_AXIS];

Expand All @@ -3318,12 +3361,12 @@ inline void gcode_G28() {
SERIAL_PROTOCOL(current_position[Z_AXIS] + 0.0001);
SERIAL_EOL;

clean_up_after_endstop_move();
clean_up_after_endstop_move(); // to early. must be done after the stowing.

#if HAS_SERVO_ENDSTOPS
raise_z_for_servo();
#endif
stow_z_probe(false); // Retract Z Servo endstop if available
stow_z_probe(false); // Retract Z Servo endstop if available. Z_PROBE_SLED is missed her.
}

#endif //!Z_PROBE_SLED
Expand Down
Loading

0 comments on commit 3f45a1a

Please sign in to comment.