Quellcode durchsuchen

Merge pull request #6353 from thinkyhead/rc_endstop_coolness

Clean up endstop triggering code
Scott Lahteine vor 7 Jahren
Ursprung
Commit
51021bc7b8
1 geänderte Dateien mit 123 neuen und 131 gelöschten Zeilen
  1. 123
    131
      Marlin/endstops.cpp

+ 123
- 131
Marlin/endstops.cpp Datei anzeigen

@@ -278,12 +278,16 @@ void Endstops::update() {
278 278
     }
279 279
   #endif
280 280
 
281
-    #if ENABLED(COREXY) || ENABLED(COREXZ)
282
-      #define CORE_X_CMP ==
283
-    #elif ENABLED(COREYX) || ENABLED(COREZX)
284
-      #define CORE_X_CMP !=
285
-    #endif
281
+  /**
282
+   * Define conditions for checking endstops
283
+   */
284
+
285
+  #if IS_CORE
286
+    #define S_(N) stepper.current_block->steps[CORE_AXIS_##N]
287
+    #define D_(N) stepper.motor_direction(CORE_AXIS_##N)
288
+  #endif
286 289
 
290
+  #if CORE_IS_XY || CORE_IS_XZ
287 291
     /**
288 292
      * Head direction in -X axis for CoreXY and CoreXZ bots.
289 293
      *
@@ -291,52 +295,16 @@ void Endstops::update() {
291 295
      * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z, handled below)
292 296
      * If DeltaA ==  DeltaB, the movement is only in the 1st axis (X)
293 297
      */
294
-    #if CORE_IS_XY || CORE_IS_XZ
295
-      if (stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]
296
-        || (    stepper.current_block->steps[CORE_AXIS_1] > 0
297
-             && stepper.motor_direction(CORE_AXIS_1) CORE_X_CMP stepper.motor_direction(CORE_AXIS_2)
298
-           )
299
-      ) {
300
-        if (stepper.motor_direction(X_HEAD))
298
+    #if ENABLED(COREXY) || ENABLED(COREXZ)
299
+      #define X_CMP ==
301 300
     #else
302
-      if (stepper.current_block->steps[X_AXIS] > 0)
303
-        if (stepper.motor_direction(X_AXIS))   // stepping along -X axis (regular Cartesian bot)
301
+      #define X_CMP !=
304 302
     #endif
305
-      { // -direction
306
-        #if ENABLED(DUAL_X_CARRIAGE)
307
-          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
308
-          if ( (stepper.current_block->active_extruder == 0 && X_HOME_DIR < 0)
309
-            || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR < 0)
310
-          )
311
-        #endif
312
-          {
313
-            #if HAS_X_MIN
314
-              UPDATE_ENDSTOP(X, MIN);
315
-            #endif
316
-          }
317
-      }
318
-      else { // +direction
319
-        #if ENABLED(DUAL_X_CARRIAGE)
320
-          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
321
-          if ( (stepper.current_block->active_extruder == 0 && X_HOME_DIR > 0)
322
-            || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR > 0)
323
-          )
324
-        #endif
325
-          {
326
-            #if HAS_X_MAX
327
-              UPDATE_ENDSTOP(X, MAX);
328
-            #endif
329
-          }
330
-      }
331
-  #if CORE_IS_XY || CORE_IS_XZ
332
-    }
333
-  #endif
334
-
335
-  // Handle swapped vs. typical Core axis order
336
-  #if ENABLED(COREYX) || ENABLED(COREYZ)
337
-    #define CORE_YZ_CMP ==
338
-  #elif ENABLED(COREXY) || ENABLED(COREZY)
339
-    #define CORE_YZ_CMP !=
303
+    #define X_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) X_CMP D_(2)) )
304
+    #define X_AXIS_HEAD X_HEAD
305
+  #else
306
+    #define X_MOVE_TEST stepper.current_block->steps[X_AXIS] > 0
307
+    #define X_AXIS_HEAD X_AXIS
340 308
   #endif
341 309
 
342 310
   #if CORE_IS_XY || CORE_IS_YZ
@@ -347,35 +315,16 @@ void Endstops::update() {
347 315
      * If DeltaA ==  DeltaB, the movement is only in the 1st axis (X or Y)
348 316
      * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z)
349 317
      */
350
-    if (stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]
351
-      || (    stepper.current_block->steps[CORE_AXIS_1] > 0
352
-           && stepper.motor_direction(CORE_AXIS_1) CORE_YZ_CMP stepper.motor_direction(CORE_AXIS_2)
353
-         )
354
-    ) {
355
-      if (stepper.motor_direction(Y_HEAD))
318
+    #if ENABLED(COREYX) || ENABLED(COREYZ)
319
+      #define Y_CMP ==
320
+    #else
321
+      #define Y_CMP !=
322
+    #endif
323
+    #define Y_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Y_CMP D_(2)) )
324
+    #define Y_AXIS_HEAD Y_HEAD
356 325
   #else
357
-    if (stepper.current_block->steps[Y_AXIS] > 0)
358
-      if (stepper.motor_direction(Y_AXIS))   // -direction
359
-  #endif
360
-      { // -direction
361
-        #if HAS_Y_MIN
362
-          UPDATE_ENDSTOP(Y, MIN);
363
-        #endif
364
-      }
365
-      else { // +direction
366
-        #if HAS_Y_MAX
367
-          UPDATE_ENDSTOP(Y, MAX);
368
-        #endif
369
-      }
370
-  #if CORE_IS_XY || CORE_IS_YZ
371
-    }
372
-  #endif
373
-
374
-
375
-  #if ENABLED(COREZX) || ENABLED(COREZY)
376
-    #define CORE_YZ_CMP ==
377
-  #elif ENABLED(COREXZ) || ENABLED(COREYZ)
378
-    #define CORE_YZ_CMP !=
326
+    #define Y_MOVE_TEST stepper.current_block->steps[Y_AXIS] > 0
327
+    #define Y_AXIS_HEAD Y_AXIS
379 328
   #endif
380 329
 
381 330
   #if CORE_IS_XZ || CORE_IS_YZ
@@ -386,76 +335,119 @@ void Endstops::update() {
386 335
      * If DeltaA ==  DeltaB, the movement is only in the 1st axis (X or Y, already handled above)
387 336
      * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Z)
388 337
      */
389
-    if (stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]
390
-      || (    stepper.current_block->steps[CORE_AXIS_1] > 0
391
-           && stepper.motor_direction(CORE_AXIS_1) CORE_YZ_CMP stepper.motor_direction(CORE_AXIS_2)
392
-         )
393
-    ) {
394
-      if (stepper.motor_direction(Z_HEAD))
338
+    #if ENABLED(COREZX) || ENABLED(COREZY)
339
+      #define Z_CMP ==
340
+    #else
341
+      #define Z_CMP !=
342
+    #endif
343
+    #define Z_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Z_CMP D_(2)) )
344
+    #define Z_AXIS_HEAD Z_HEAD
395 345
   #else
396
-    if (stepper.current_block->steps[Z_AXIS] > 0)
397
-      if (stepper.motor_direction(Z_AXIS))
346
+    #define Z_MOVE_TEST stepper.current_block->steps[Z_AXIS] > 0
347
+    #define Z_AXIS_HEAD Z_AXIS
398 348
   #endif
399
-      { // Z -direction. Gantry down, bed up.
400
-        #if HAS_Z_MIN
401
-          #if ENABLED(Z_DUAL_ENDSTOPS)
402 349
 
403
-            UPDATE_ENDSTOP_BIT(Z, MIN);
404
-            #if HAS_Z2_MIN
405
-              UPDATE_ENDSTOP_BIT(Z2, MIN);
406
-            #else
407
-              COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
408
-            #endif
350
+  // With Dual X, endstops are only checked in the homing direction for the active extruder
351
+  #if ENABLED(DUAL_X_CARRIAGE)
352
+    #define E0_ACTIVE stepper.current_block->active_extruder == 0
353
+    #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
354
+    #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
355
+  #else
356
+    #define X_MIN_TEST true
357
+    #define X_MAX_TEST true
358
+  #endif
409 359
 
410
-            test_dual_z_endstops(Z_MIN, Z2_MIN);
360
+  /**
361
+   * Check and update endstops according to conditions
362
+   */
411 363
 
412
-          #else // !Z_DUAL_ENDSTOPS
364
+  if (X_MOVE_TEST) {
365
+    if (stepper.motor_direction(X_AXIS_HEAD)) {
366
+      if (X_MIN_TEST) { // -direction
367
+        #if HAS_X_MIN
368
+          UPDATE_ENDSTOP(X, MIN);
369
+        #endif
370
+      }
371
+    }
372
+    else if (X_MAX_TEST) { // +direction
373
+      #if HAS_X_MAX
374
+        UPDATE_ENDSTOP(X, MAX);
375
+      #endif
376
+    }
377
+  }
413 378
 
414
-            #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
415
-              if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
416
-            #else
417
-              UPDATE_ENDSTOP(Z, MIN);
418
-            #endif
379
+  if (Y_MOVE_TEST) {
380
+    if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
381
+      #if HAS_Y_MIN
382
+        UPDATE_ENDSTOP(Y, MIN);
383
+      #endif
384
+    }
385
+    else { // +direction
386
+      #if HAS_Y_MAX
387
+        UPDATE_ENDSTOP(Y, MAX);
388
+      #endif
389
+    }
390
+  }
419 391
 
420
-          #endif // !Z_DUAL_ENDSTOPS
392
+  if (Z_MOVE_TEST) {
393
+    if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
394
+      #if HAS_Z_MIN
395
+        #if ENABLED(Z_DUAL_ENDSTOPS)
421 396
 
422
-        #endif // HAS_Z_MIN
397
+          UPDATE_ENDSTOP_BIT(Z, MIN);
398
+          #if HAS_Z2_MIN
399
+            UPDATE_ENDSTOP_BIT(Z2, MIN);
400
+          #else
401
+            COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
402
+          #endif
423 403
 
424
-        // When closing the gap check the enabled probe
425
-        #if ENABLED(Z_MIN_PROBE_ENDSTOP)
426
-          if (z_probe_enabled) {
427
-            UPDATE_ENDSTOP(Z, MIN_PROBE);
428
-            if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
429
-          }
430
-        #endif
431
-      }
432
-      else { // Z +direction. Gantry up, bed down.
433
-        #if HAS_Z_MAX
404
+          test_dual_z_endstops(Z_MIN, Z2_MIN);
405
+
406
+        #else // !Z_DUAL_ENDSTOPS
434 407
 
435
-          // Check both Z dual endstops
436
-          #if ENABLED(Z_DUAL_ENDSTOPS)
408
+          #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
409
+            if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
410
+          #else
411
+            UPDATE_ENDSTOP(Z, MIN);
412
+          #endif
437 413
 
438
-            UPDATE_ENDSTOP_BIT(Z, MAX);
439
-            #if HAS_Z2_MAX
440
-              UPDATE_ENDSTOP_BIT(Z2, MAX);
441
-            #else
442
-              COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
443
-            #endif
414
+        #endif // !Z_DUAL_ENDSTOPS
444 415
 
445
-            test_dual_z_endstops(Z_MAX, Z2_MAX);
416
+      #endif // HAS_Z_MIN
446 417
 
447
-          // If this pin is not hijacked for the bed probe
448
-          // then it belongs to the Z endstop
449
-          #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
418
+      // When closing the gap check the enabled probe
419
+      #if ENABLED(Z_MIN_PROBE_ENDSTOP)
420
+        if (z_probe_enabled) {
421
+          UPDATE_ENDSTOP(Z, MIN_PROBE);
422
+          if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
423
+        }
424
+      #endif
425
+    }
426
+    else { // Z +direction. Gantry up, bed down.
427
+      #if HAS_Z_MAX
450 428
 
451
-            UPDATE_ENDSTOP(Z, MAX);
429
+        // Check both Z dual endstops
430
+        #if ENABLED(Z_DUAL_ENDSTOPS)
452 431
 
453
-          #endif // !Z_MIN_PROBE_PIN...
454
-        #endif // Z_MAX_PIN
455
-      }
456
-  #if CORE_IS_XZ || CORE_IS_YZ
432
+          UPDATE_ENDSTOP_BIT(Z, MAX);
433
+          #if HAS_Z2_MAX
434
+            UPDATE_ENDSTOP_BIT(Z2, MAX);
435
+          #else
436
+            COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
437
+          #endif
438
+
439
+          test_dual_z_endstops(Z_MAX, Z2_MAX);
440
+
441
+        // If this pin is not hijacked for the bed probe
442
+        // then it belongs to the Z endstop
443
+        #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
444
+
445
+          UPDATE_ENDSTOP(Z, MAX);
446
+
447
+        #endif // !Z_MIN_PROBE_PIN...
448
+      #endif // Z_MAX_PIN
457 449
     }
458
-  #endif
450
+  }
459 451
 
460 452
   old_endstop_bits = current_endstop_bits;
461 453
 

Laden…
Abbrechen
Speichern