浏览代码

Merge pull request #2135 from Wurstnase/new_endstop_bits

new endstop bits
Scott Lahteine 10 年前
父节点
当前提交
834747deb7
共有 2 个文件被更改,包括 73 次插入100 次删除
  1. 2
    1
      Marlin/Marlin.h
  2. 71
    99
      Marlin/stepper.cpp

+ 2
- 1
Marlin/Marlin.h 查看文件

33
 
33
 
34
 #define BIT(b) (1<<(b))
34
 #define BIT(b) (1<<(b))
35
 #define TEST(n,b) (((n)&BIT(b))!=0)
35
 #define TEST(n,b) (((n)&BIT(b))!=0)
36
+#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
36
 #define RADIANS(d) ((d)*M_PI/180.0)
37
 #define RADIANS(d) ((d)*M_PI/180.0)
37
 #define DEGREES(r) ((d)*180.0/M_PI)
38
 #define DEGREES(r) ((d)*180.0/M_PI)
38
 #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
39
 #define NOLESS(v,n) do{ if (v < n) v = n; }while(0)
198
  */
199
  */
199
 enum AxisEnum {X_AXIS=0, Y_AXIS=1, A_AXIS=0, B_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5};
200
 enum AxisEnum {X_AXIS=0, Y_AXIS=1, A_AXIS=0, B_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5};
200
 
201
 
201
-enum EndstopEnum {X_MIN=0, Y_MIN=1, Z_MIN=2, Z_PROBE=3, X_MAX=4, Y_MAX=5, Z_MAX=6};
202
+enum EndstopEnum {X_MIN=0, Y_MIN=1, Z_MIN=2, Z_PROBE=3, X_MAX=4, Y_MAX=5, Z_MAX=6, Z2_MIN=7, Z2_MAX=8};
202
 
203
 
203
 void enable_all_steppers();
204
 void enable_all_steppers();
204
 void disable_all_steppers();
205
 void disable_all_steppers();

+ 71
- 99
Marlin/stepper.cpp 查看文件

76
 volatile long endstops_stepsTotal, endstops_stepsDone;
76
 volatile long endstops_stepsTotal, endstops_stepsDone;
77
 static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_PROBE as BIT value
77
 static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_PROBE as BIT value
78
 
78
 
79
+#ifndef Z_DUAL_ENDSTOPS
80
+  static byte
81
+#else
82
+  static uint16_t
83
+#endif
84
+  old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_PROBE, Z2_MIN, Z2_MAX
85
+
79
 #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
86
 #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
80
   bool abort_on_endstop_hit = false;
87
   bool abort_on_endstop_hit = false;
81
 #endif
88
 #endif
84
   int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
91
   int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
85
 #endif
92
 #endif
86
 
93
 
87
-#if HAS_X_MIN
88
-  static bool old_x_min_endstop = false;
89
-#endif
90
-#if HAS_X_MAX
91
-  static bool old_x_max_endstop = false;
92
-#endif
93
-#if HAS_Y_MIN
94
-  static bool old_y_min_endstop = false;
95
-#endif
96
-#if HAS_Y_MAX
97
-  static bool old_y_max_endstop = false;
98
-#endif
99
-
100
-static bool old_z_min_endstop = false;
101
-static bool old_z_max_endstop = false;
102
-
103
-#ifdef Z_DUAL_ENDSTOPS
104
-  static bool old_z2_min_endstop = false;
105
-  static bool old_z2_max_endstop = false;
106
-#endif
107
-
108
-#ifdef Z_PROBE_ENDSTOP // No need to check for valid pin, SanityCheck.h already does this.
109
-  static bool old_z_probe_endstop = false;
110
-#endif
111
-
112
 static bool check_endstops = true;
94
 static bool check_endstops = true;
113
 
95
 
114
 volatile long count_position[NUM_AXIS] = { 0 };
96
 volatile long count_position[NUM_AXIS] = { 0 };
155
     #define Z_APPLY_STEP(v,Q) \
137
     #define Z_APPLY_STEP(v,Q) \
156
     if (performing_homing) { \
138
     if (performing_homing) { \
157
       if (Z_HOME_DIR > 0) {\
139
       if (Z_HOME_DIR > 0) {\
158
-        if (!(old_z_max_endstop && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
159
-        if (!(old_z2_max_endstop && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
140
+        if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
141
+        if (!(TEST(old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
160
       } else {\
142
       } else {\
161
-        if (!(old_z_min_endstop && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
162
-        if (!(old_z2_min_endstop && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
143
+        if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
144
+        if (!(TEST(old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
163
       } \
145
       } \
164
     } else { \
146
     } else { \
165
       Z_STEP_WRITE(v); \
147
       Z_STEP_WRITE(v); \
266
 }
248
 }
267
 
249
 
268
 void checkHitEndstops() {
250
 void checkHitEndstops() {
269
-  if (endstop_hit_bits) { // #ifdef || endstop_z_probe_hit to save space if needed.
251
+  if (endstop_hit_bits) {
270
     SERIAL_ECHO_START;
252
     SERIAL_ECHO_START;
271
     SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
253
     SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
272
     if (endstop_hit_bits & BIT(X_MIN)) {
254
     if (endstop_hit_bits & BIT(X_MIN)) {
447
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
429
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
448
 ISR(TIMER1_COMPA_vect) {
430
 ISR(TIMER1_COMPA_vect) {
449
 
431
 
450
-  if(cleaning_buffer_counter)
432
+  if (cleaning_buffer_counter)
451
   {
433
   {
452
     current_block = NULL;
434
     current_block = NULL;
453
     plan_discard_current_block();
435
     plan_discard_current_block();
492
     // Check endstops
474
     // Check endstops
493
     if (check_endstops) {
475
     if (check_endstops) {
494
       
476
       
495
-      #define _ENDSTOP(axis, minmax) axis ##_## minmax ##_endstop
477
+      #ifdef Z_DUAL_ENDSTOPS
478
+        uint16_t
479
+      #else
480
+        byte
481
+      #endif
482
+      current_endstop_bits;
483
+
496
       #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
484
       #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
497
       #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
485
       #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
498
-      #define _OLD_ENDSTOP(axis, minmax) old_## axis ##_## minmax ##_endstop
499
       #define _AXIS(AXIS) AXIS ##_AXIS
486
       #define _AXIS(AXIS) AXIS ##_AXIS
500
-      #define _HIT_BIT(AXIS) AXIS ##_MIN
501
-      #define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_HIT_BIT(AXIS))
502
-
503
-      #define UPDATE_ENDSTOP(axis,AXIS,minmax,MINMAX) \
504
-        bool _ENDSTOP(axis, minmax) = (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)); \
505
-        if (_ENDSTOP(axis, minmax) && _OLD_ENDSTOP(axis, minmax) && (current_block->steps[_AXIS(AXIS)] > 0)) { \
487
+      #define _ENDSTOP_HIT(AXIS) endstop_hit_bits |= BIT(_ENDSTOP(AXIS, MIN))
488
+      #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
489
+
490
+      // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
491
+      #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
492
+      // COPY_BIT: copy the value of COPY_BIT to BIT in bits
493
+      #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
494
+      // TEST_ENDSTOP: test the old and the current status of an endstop
495
+      #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
496
+
497
+      #define UPDATE_ENDSTOP(AXIS,MINMAX) \
498
+        SET_ENDSTOP_BIT(AXIS, MINMAX); \
499
+        if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))  && (current_block->steps[_AXIS(AXIS)] > 0)) { \
506
           endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \
500
           endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]; \
507
           _ENDSTOP_HIT(AXIS); \
501
           _ENDSTOP_HIT(AXIS); \
508
           step_events_completed = current_block->step_event_count; \
502
           step_events_completed = current_block->step_event_count; \
509
-        } \
510
-        _OLD_ENDSTOP(axis, minmax) = _ENDSTOP(axis, minmax);
503
+        }
511
       
504
       
512
       #ifdef COREXY
505
       #ifdef COREXY
513
         // Head direction in -X axis for CoreXY bots.
506
         // Head direction in -X axis for CoreXY bots.
524
             #endif
517
             #endif
525
               {
518
               {
526
                 #if HAS_X_MIN
519
                 #if HAS_X_MIN
527
-                  UPDATE_ENDSTOP(x, X, min, MIN);
520
+                  UPDATE_ENDSTOP(X, MIN);
528
                 #endif
521
                 #endif
529
               }
522
               }
530
           }
523
           }
535
             #endif
528
             #endif
536
               {
529
               {
537
                 #if HAS_X_MAX
530
                 #if HAS_X_MAX
538
-                  UPDATE_ENDSTOP(x, X, max, MAX);
531
+                  UPDATE_ENDSTOP(X, MAX);
539
                 #endif
532
                 #endif
540
               }
533
               }
541
           }
534
           }
550
       #endif
543
       #endif
551
           { // -direction
544
           { // -direction
552
             #if HAS_Y_MIN
545
             #if HAS_Y_MIN
553
-              UPDATE_ENDSTOP(y, Y, min, MIN);
546
+              UPDATE_ENDSTOP(Y, MIN);
554
             #endif
547
             #endif
555
           }
548
           }
556
           else { // +direction
549
           else { // +direction
557
             #if HAS_Y_MAX
550
             #if HAS_Y_MAX
558
-              UPDATE_ENDSTOP(y, Y, max, MAX);
551
+              UPDATE_ENDSTOP(Y, MAX);
559
             #endif
552
             #endif
560
           }
553
           }
561
       #ifdef COREXY
554
       #ifdef COREXY
565
         #if HAS_Z_MIN
558
         #if HAS_Z_MIN
566
 
559
 
567
           #ifdef Z_DUAL_ENDSTOPS
560
           #ifdef Z_DUAL_ENDSTOPS
561
+            SET_ENDSTOP_BIT(Z, MIN);
562
+              #if HAS_Z2_MIN
563
+                SET_ENDSTOP_BIT(Z2, MIN);
564
+              #else
565
+                COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN)
566
+              #endif
567
+
568
+            byte z_test = TEST_ENDSTOP(Z_MIN) << 0 + TEST_ENDSTOP(Z2_MIN) << 1; // bit 0 for Z, bit 1 for Z2
568
 
569
 
569
-            bool z_min_endstop = READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING,
570
-                z2_min_endstop =
571
-                  #if HAS_Z2_MIN
572
-                    READ(Z2_MIN_PIN) != Z2_MIN_ENDSTOP_INVERTING
573
-                  #else
574
-                    z_min_endstop
575
-                  #endif
576
-                ;
577
-
578
-            bool z_min_both = z_min_endstop && old_z_min_endstop,
579
-                z2_min_both = z2_min_endstop && old_z2_min_endstop;
580
-            if ((z_min_both || z2_min_both) && current_block->steps[Z_AXIS] > 0) {
570
+            if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
581
               endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
571
               endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
582
               endstop_hit_bits |= BIT(Z_MIN);
572
               endstop_hit_bits |= BIT(Z_MIN);
583
-              if (!performing_homing || (performing_homing && z_min_both && z2_min_both)) //if not performing home or if both endstops were trigged during homing...
584
-                step_events_completed = current_block->step_event_count;
573
+              if (!performing_homing || (performing_homing && !((~z_test) & 0x3)))  //if not performing home or if both endstops were trigged during homing...
574
+                step_events_completed = current_block->step_event_count;            //!((~z_test) & 0x3) = Z_MIN && Z2_MIN
585
             }
575
             }
586
-            old_z_min_endstop = z_min_endstop;
587
-            old_z2_min_endstop = z2_min_endstop;
588
-
589
           #else // !Z_DUAL_ENDSTOPS
576
           #else // !Z_DUAL_ENDSTOPS
590
 
577
 
591
-            UPDATE_ENDSTOP(z, Z, min, MIN);
592
-
578
+            UPDATE_ENDSTOP(Z, MIN);
593
           #endif // !Z_DUAL_ENDSTOPS
579
           #endif // !Z_DUAL_ENDSTOPS
594
-
595
         #endif // Z_MIN_PIN
580
         #endif // Z_MIN_PIN
596
 
581
 
597
         #ifdef Z_PROBE_ENDSTOP
582
         #ifdef Z_PROBE_ENDSTOP
598
-          UPDATE_ENDSTOP(z, Z, probe, PROBE);
599
-          z_probe_endstop=(READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
600
-          if(z_probe_endstop && old_z_probe_endstop)
583
+          UPDATE_ENDSTOP(Z, PROBE);
584
+          SET_ENDSTOP_BIT(Z, PROBE);
585
+
586
+          if (TEST_ENDSTOP(Z_PROBE))
601
           {
587
           {
602
             endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
588
             endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
603
             endstop_hit_bits |= BIT(Z_PROBE);
589
             endstop_hit_bits |= BIT(Z_PROBE);
604
-  //        if (z_probe_endstop && old_z_probe_endstop) SERIAL_ECHOLN("z_probe_endstop = true");
605
           }
590
           }
606
-          old_z_probe_endstop = z_probe_endstop;
607
         #endif
591
         #endif
608
       }
592
       }
609
       else { // z +direction
593
       else { // z +direction
611
 
595
 
612
           #ifdef Z_DUAL_ENDSTOPS
596
           #ifdef Z_DUAL_ENDSTOPS
613
 
597
 
614
-            bool z_max_endstop = READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING,
615
-                z2_max_endstop =
616
-                  #if HAS_Z2_MAX
617
-                    READ(Z2_MAX_PIN) != Z2_MAX_ENDSTOP_INVERTING
618
-                  #else
619
-                    z_max_endstop
620
-                  #endif
621
-                ;
622
-
623
-            bool z_max_both = z_max_endstop && old_z_max_endstop,
624
-                z2_max_both = z2_max_endstop && old_z2_max_endstop;
625
-            if ((z_max_both || z2_max_both) && current_block->steps[Z_AXIS] > 0) {
626
-              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
627
-              endstop_hit_bits |= BIT(Z_MIN);
598
+            SET_ENDSTOP_BIT(Z, MAX);
599
+              #if HAS_Z2_MAX
600
+                SET_ENDSTOP_BIT(Z2, MAX);
601
+              #else
602
+                COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX)
603
+              #endif
628
 
604
 
629
-             // if (z_max_both) SERIAL_ECHOLN("z_max_endstop = true");
630
-             // if (z2_max_both) SERIAL_ECHOLN("z2_max_endstop = true");
605
+            byte z_test = TEST_ENDSTOP(Z_MAX) << 0 + TEST_ENDSTOP(Z2_MAX) << 1; // bit 0 for Z, bit 1 for Z2
631
 
606
 
632
-              if (!performing_homing || (performing_homing && z_max_both && z2_max_both)) //if not performing home or if both endstops were trigged during homing...
633
-                step_events_completed = current_block->step_event_count;
607
+            if (z_test && current_block->steps[Z_AXIS] > 0) {  // t_test = Z_MAX || Z2_MAX
608
+              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
609
+              endstop_hit_bits |= BIT(Z_MIN);
610
+              if (!performing_homing || (performing_homing && !((~z_test) & 0x3)))  //if not performing home or if both endstops were trigged during homing...
611
+                step_events_completed = current_block->step_event_count;            //!((~z_test) & 0x3) = Z_MAX && Z2_MAX
634
             }
612
             }
635
-            old_z_max_endstop = z_max_endstop;
636
-            old_z2_max_endstop = z2_max_endstop;
637
 
613
 
638
           #else // !Z_DUAL_ENDSTOPS
614
           #else // !Z_DUAL_ENDSTOPS
639
 
615
 
640
-            UPDATE_ENDSTOP(z, Z, max, MAX);
616
+            UPDATE_ENDSTOP(Z, MAX);
641
 
617
 
642
           #endif // !Z_DUAL_ENDSTOPS
618
           #endif // !Z_DUAL_ENDSTOPS
643
-
644
         #endif // Z_MAX_PIN
619
         #endif // Z_MAX_PIN
645
         
620
         
646
         #ifdef Z_PROBE_ENDSTOP
621
         #ifdef Z_PROBE_ENDSTOP
647
-          UPDATE_ENDSTOP(z, Z, probe, PROBE);
648
-          z_probe_endstop=(READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
649
-          if(z_probe_endstop && old_z_probe_endstop)
622
+          UPDATE_ENDSTOP(Z, PROBE);
623
+          SET_ENDSTOP_BIT(Z, PROBE);
624
+          if (TEST_ENDSTOP(Z_PROBE))
650
           {
625
           {
651
             endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
626
             endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
652
             endstop_hit_bits |= BIT(Z_PROBE);
627
             endstop_hit_bits |= BIT(Z_PROBE);
653
-//          if (z_probe_endstop && old_z_probe_endstop) SERIAL_ECHOLN("z_probe_endstop = true");
654
           }
628
           }
655
-          old_z_probe_endstop = z_probe_endstop;
656
         #endif
629
         #endif
657
       }
630
       }
658
-
631
+      old_endstop_bits = current_endstop_bits;
659
     }
632
     }
660
 
633
 
661
 
634
 
662
-
663
     // Take multiple steps per interrupt (For high speed moves)
635
     // Take multiple steps per interrupt (For high speed moves)
664
     for (int8_t i = 0; i < step_loops; i++) {
636
     for (int8_t i = 0; i < step_loops; i++) {
665
       #ifndef AT90USB
637
       #ifndef AT90USB

正在加载...
取消
保存