Browse Source

Merge pull request #3631 from thinkyhead/rc_singletons

Encapsulate Stepper, Planner, Endstops in singleton classes
Scott Lahteine 8 years ago
parent
commit
c2145566c7
13 changed files with 1601 additions and 1302 deletions
  1. 7
    1
      Marlin/Marlin.h
  2. 197
    259
      Marlin/Marlin_main.cpp
  3. 1
    1
      Marlin/cardreader.cpp
  4. 73
    73
      Marlin/configuration_store.cpp
  5. 356
    0
      Marlin/endstops.cpp
  6. 105
    0
      Marlin/endstops.h
  7. 151
    207
      Marlin/planner.cpp
  8. 228
    92
      Marlin/planner.h
  9. 151
    566
      Marlin/stepper.cpp
  10. 285
    62
      Marlin/stepper.h
  11. 2
    2
      Marlin/temperature.cpp
  12. 7
    3
      Marlin/temperature.h
  13. 38
    36
      Marlin/ultralcd.cpp

+ 7
- 1
Marlin/Marlin.h View File

@@ -216,7 +216,7 @@ void manage_inactivity(bool ignore_stepper_queue = false);
216 216
  */
217 217
 enum AxisEnum {X_AXIS = 0, A_AXIS = 0, Y_AXIS = 1, B_AXIS = 1, Z_AXIS = 2, C_AXIS = 2, E_AXIS = 3, X_HEAD = 4, Y_HEAD = 5, Z_HEAD = 5};
218 218
 
219
-enum EndstopEnum {X_MIN = 0, Y_MIN = 1, Z_MIN = 2, Z_MIN_PROBE = 3, X_MAX = 4, Y_MAX = 5, Z_MAX = 6, Z2_MIN = 7, Z2_MAX = 8};
219
+#define _AXIS(AXIS) AXIS ##_AXIS
220 220
 
221 221
 void enable_all_steppers();
222 222
 void disable_all_steppers();
@@ -283,6 +283,12 @@ extern float sw_endstop_max[3]; // axis[n].sw_endstop_max
283 283
 extern bool axis_known_position[3]; // axis[n].is_known
284 284
 extern bool axis_homed[3]; // axis[n].is_homed
285 285
 
286
+// GCode support for external objects
287
+extern bool code_seen(char);
288
+extern float code_value();
289
+extern long code_value_long();
290
+extern int16_t code_value_short();
291
+
286 292
 #if ENABLED(DELTA)
287 293
   #ifndef DELTA_RADIUS_TRIM_TOWER_1
288 294
     #define DELTA_RADIUS_TRIM_TOWER_1 0.0

+ 197
- 259
Marlin/Marlin_main.cpp
File diff suppressed because it is too large
View File


+ 1
- 1
Marlin/cardreader.cpp View File

@@ -596,7 +596,7 @@ void CardReader::updir() {
596 596
 }
597 597
 
598 598
 void CardReader::printingHasFinished() {
599
-  st_synchronize();
599
+  stepper.synchronize();
600 600
   if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
601 601
     file.close();
602 602
     file_subcall_ctr--;

+ 73
- 73
Marlin/configuration_store.cpp View File

@@ -43,18 +43,18 @@
43 43
  *
44 44
  *  100  Version (char x4)
45 45
  *
46
- *  104  M92 XYZE  axis_steps_per_unit (float x4)
47
- *  120  M203 XYZE max_feedrate (float x4)
48
- *  136  M201 XYZE max_acceleration_units_per_sq_second (uint32_t x4)
49
- *  152  M204 P    acceleration (float)
50
- *  156  M204 R    retract_acceleration (float)
51
- *  160  M204 T    travel_acceleration (float)
52
- *  164  M205 S    minimumfeedrate (float)
53
- *  168  M205 T    mintravelfeedrate (float)
54
- *  172  M205 B    minsegmenttime (ulong)
55
- *  176  M205 X    max_xy_jerk (float)
56
- *  180  M205 Z    max_z_jerk (float)
57
- *  184  M205 E    max_e_jerk (float)
46
+ *  104  M92 XYZE  planner.axis_steps_per_unit (float x4)
47
+ *  120  M203 XYZE planner.max_feedrate (float x4)
48
+ *  136  M201 XYZE planner.max_acceleration_units_per_sq_second (uint32_t x4)
49
+ *  152  M204 P    planner.acceleration (float)
50
+ *  156  M204 R    planner.retract_acceleration (float)
51
+ *  160  M204 T    planner.travel_acceleration (float)
52
+ *  164  M205 S    planner.min_feedrate (float)
53
+ *  168  M205 T    planner.min_travel_feedrate (float)
54
+ *  172  M205 B    planner.min_segment_time (ulong)
55
+ *  176  M205 X    planner.max_xy_jerk (float)
56
+ *  180  M205 Z    planner.max_z_jerk (float)
57
+ *  184  M205 E    planner.max_e_jerk (float)
58 58
  *  188  M206 XYZ  home_offset (float x3)
59 59
  *
60 60
  * Mesh bed leveling:
@@ -173,18 +173,18 @@ void Config_StoreSettings()  {
173 173
   char ver[4] = "000";
174 174
   int i = EEPROM_OFFSET;
175 175
   EEPROM_WRITE_VAR(i, ver); // invalidate data first
176
-  EEPROM_WRITE_VAR(i, axis_steps_per_unit);
177
-  EEPROM_WRITE_VAR(i, max_feedrate);
178
-  EEPROM_WRITE_VAR(i, max_acceleration_units_per_sq_second);
179
-  EEPROM_WRITE_VAR(i, acceleration);
180
-  EEPROM_WRITE_VAR(i, retract_acceleration);
181
-  EEPROM_WRITE_VAR(i, travel_acceleration);
182
-  EEPROM_WRITE_VAR(i, minimumfeedrate);
183
-  EEPROM_WRITE_VAR(i, mintravelfeedrate);
184
-  EEPROM_WRITE_VAR(i, minsegmenttime);
185
-  EEPROM_WRITE_VAR(i, max_xy_jerk);
186
-  EEPROM_WRITE_VAR(i, max_z_jerk);
187
-  EEPROM_WRITE_VAR(i, max_e_jerk);
176
+  EEPROM_WRITE_VAR(i, planner.axis_steps_per_unit);
177
+  EEPROM_WRITE_VAR(i, planner.max_feedrate);
178
+  EEPROM_WRITE_VAR(i, planner.max_acceleration_units_per_sq_second);
179
+  EEPROM_WRITE_VAR(i, planner.acceleration);
180
+  EEPROM_WRITE_VAR(i, planner.retract_acceleration);
181
+  EEPROM_WRITE_VAR(i, planner.travel_acceleration);
182
+  EEPROM_WRITE_VAR(i, planner.min_feedrate);
183
+  EEPROM_WRITE_VAR(i, planner.min_travel_feedrate);
184
+  EEPROM_WRITE_VAR(i, planner.min_segment_time);
185
+  EEPROM_WRITE_VAR(i, planner.max_xy_jerk);
186
+  EEPROM_WRITE_VAR(i, planner.max_z_jerk);
187
+  EEPROM_WRITE_VAR(i, planner.max_e_jerk);
188 188
   EEPROM_WRITE_VAR(i, home_offset);
189 189
 
190 190
   uint8_t mesh_num_x = 3;
@@ -351,22 +351,22 @@ void Config_RetrieveSettings() {
351 351
     float dummy = 0;
352 352
 
353 353
     // version number match
354
-    EEPROM_READ_VAR(i, axis_steps_per_unit);
355
-    EEPROM_READ_VAR(i, max_feedrate);
356
-    EEPROM_READ_VAR(i, max_acceleration_units_per_sq_second);
354
+    EEPROM_READ_VAR(i, planner.axis_steps_per_unit);
355
+    EEPROM_READ_VAR(i, planner.max_feedrate);
356
+    EEPROM_READ_VAR(i, planner.max_acceleration_units_per_sq_second);
357 357
 
358 358
     // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
359
-    reset_acceleration_rates();
360
-
361
-    EEPROM_READ_VAR(i, acceleration);
362
-    EEPROM_READ_VAR(i, retract_acceleration);
363
-    EEPROM_READ_VAR(i, travel_acceleration);
364
-    EEPROM_READ_VAR(i, minimumfeedrate);
365
-    EEPROM_READ_VAR(i, mintravelfeedrate);
366
-    EEPROM_READ_VAR(i, minsegmenttime);
367
-    EEPROM_READ_VAR(i, max_xy_jerk);
368
-    EEPROM_READ_VAR(i, max_z_jerk);
369
-    EEPROM_READ_VAR(i, max_e_jerk);
359
+    planner.reset_acceleration_rates();
360
+
361
+    EEPROM_READ_VAR(i, planner.acceleration);
362
+    EEPROM_READ_VAR(i, planner.retract_acceleration);
363
+    EEPROM_READ_VAR(i, planner.travel_acceleration);
364
+    EEPROM_READ_VAR(i, planner.min_feedrate);
365
+    EEPROM_READ_VAR(i, planner.min_travel_feedrate);
366
+    EEPROM_READ_VAR(i, planner.min_segment_time);
367
+    EEPROM_READ_VAR(i, planner.max_xy_jerk);
368
+    EEPROM_READ_VAR(i, planner.max_z_jerk);
369
+    EEPROM_READ_VAR(i, planner.max_e_jerk);
370 370
     EEPROM_READ_VAR(i, home_offset);
371 371
 
372 372
     uint8_t dummy_uint8 = 0, mesh_num_x = 0, mesh_num_y = 0;
@@ -528,9 +528,9 @@ void Config_ResetDefault() {
528 528
   float tmp2[] = DEFAULT_MAX_FEEDRATE;
529 529
   long tmp3[] = DEFAULT_MAX_ACCELERATION;
530 530
   for (uint8_t i = 0; i < NUM_AXIS; i++) {
531
-    axis_steps_per_unit[i] = tmp1[i];
532
-    max_feedrate[i] = tmp2[i];
533
-    max_acceleration_units_per_sq_second[i] = tmp3[i];
531
+    planner.axis_steps_per_unit[i] = tmp1[i];
532
+    planner.max_feedrate[i] = tmp2[i];
533
+    planner.max_acceleration_units_per_sq_second[i] = tmp3[i];
534 534
     #if ENABLED(SCARA)
535 535
       if (i < COUNT(axis_scaling))
536 536
         axis_scaling[i] = 1;
@@ -538,17 +538,17 @@ void Config_ResetDefault() {
538 538
   }
539 539
 
540 540
   // steps per sq second need to be updated to agree with the units per sq second
541
-  reset_acceleration_rates();
542
-
543
-  acceleration = DEFAULT_ACCELERATION;
544
-  retract_acceleration = DEFAULT_RETRACT_ACCELERATION;
545
-  travel_acceleration = DEFAULT_TRAVEL_ACCELERATION;
546
-  minimumfeedrate = DEFAULT_MINIMUMFEEDRATE;
547
-  minsegmenttime = DEFAULT_MINSEGMENTTIME;
548
-  mintravelfeedrate = DEFAULT_MINTRAVELFEEDRATE;
549
-  max_xy_jerk = DEFAULT_XYJERK;
550
-  max_z_jerk = DEFAULT_ZJERK;
551
-  max_e_jerk = DEFAULT_EJERK;
541
+  planner.reset_acceleration_rates();
542
+
543
+  planner.acceleration = DEFAULT_ACCELERATION;
544
+  planner.retract_acceleration = DEFAULT_RETRACT_ACCELERATION;
545
+  planner.travel_acceleration = DEFAULT_TRAVEL_ACCELERATION;
546
+  planner.min_feedrate = DEFAULT_MINIMUMFEEDRATE;
547
+  planner.min_segment_time = DEFAULT_MINSEGMENTTIME;
548
+  planner.min_travel_feedrate = DEFAULT_MINTRAVELFEEDRATE;
549
+  planner.max_xy_jerk = DEFAULT_XYJERK;
550
+  planner.max_z_jerk = DEFAULT_ZJERK;
551
+  planner.max_e_jerk = DEFAULT_EJERK;
552 552
   home_offset[X_AXIS] = home_offset[Y_AXIS] = home_offset[Z_AXIS] = 0;
553 553
 
554 554
   #if ENABLED(MESH_BED_LEVELING)
@@ -653,10 +653,10 @@ void Config_PrintSettings(bool forReplay) {
653 653
     SERIAL_ECHOLNPGM("Steps per unit:");
654 654
     CONFIG_ECHO_START;
655 655
   }
656
-  SERIAL_ECHOPAIR("  M92 X", axis_steps_per_unit[X_AXIS]);
657
-  SERIAL_ECHOPAIR(" Y", axis_steps_per_unit[Y_AXIS]);
658
-  SERIAL_ECHOPAIR(" Z", axis_steps_per_unit[Z_AXIS]);
659
-  SERIAL_ECHOPAIR(" E", axis_steps_per_unit[E_AXIS]);
656
+  SERIAL_ECHOPAIR("  M92 X", planner.axis_steps_per_unit[X_AXIS]);
657
+  SERIAL_ECHOPAIR(" Y", planner.axis_steps_per_unit[Y_AXIS]);
658
+  SERIAL_ECHOPAIR(" Z", planner.axis_steps_per_unit[Z_AXIS]);
659
+  SERIAL_ECHOPAIR(" E", planner.axis_steps_per_unit[E_AXIS]);
660 660
   SERIAL_EOL;
661 661
 
662 662
   CONFIG_ECHO_START;
@@ -677,10 +677,10 @@ void Config_PrintSettings(bool forReplay) {
677 677
     SERIAL_ECHOLNPGM("Maximum feedrates (mm/s):");
678 678
     CONFIG_ECHO_START;
679 679
   }
680
-  SERIAL_ECHOPAIR("  M203 X", max_feedrate[X_AXIS]);
681
-  SERIAL_ECHOPAIR(" Y", max_feedrate[Y_AXIS]);
682
-  SERIAL_ECHOPAIR(" Z", max_feedrate[Z_AXIS]);
683
-  SERIAL_ECHOPAIR(" E", max_feedrate[E_AXIS]);
680
+  SERIAL_ECHOPAIR("  M203 X", planner.max_feedrate[X_AXIS]);
681
+  SERIAL_ECHOPAIR(" Y", planner.max_feedrate[Y_AXIS]);
682
+  SERIAL_ECHOPAIR(" Z", planner.max_feedrate[Z_AXIS]);
683
+  SERIAL_ECHOPAIR(" E", planner.max_feedrate[E_AXIS]);
684 684
   SERIAL_EOL;
685 685
 
686 686
   CONFIG_ECHO_START;
@@ -688,19 +688,19 @@ void Config_PrintSettings(bool forReplay) {
688 688
     SERIAL_ECHOLNPGM("Maximum Acceleration (mm/s2):");
689 689
     CONFIG_ECHO_START;
690 690
   }
691
-  SERIAL_ECHOPAIR("  M201 X", max_acceleration_units_per_sq_second[X_AXIS]);
692
-  SERIAL_ECHOPAIR(" Y", max_acceleration_units_per_sq_second[Y_AXIS]);
693
-  SERIAL_ECHOPAIR(" Z", max_acceleration_units_per_sq_second[Z_AXIS]);
694
-  SERIAL_ECHOPAIR(" E", max_acceleration_units_per_sq_second[E_AXIS]);
691
+  SERIAL_ECHOPAIR("  M201 X", planner.max_acceleration_units_per_sq_second[X_AXIS]);
692
+  SERIAL_ECHOPAIR(" Y", planner.max_acceleration_units_per_sq_second[Y_AXIS]);
693
+  SERIAL_ECHOPAIR(" Z", planner.max_acceleration_units_per_sq_second[Z_AXIS]);
694
+  SERIAL_ECHOPAIR(" E", planner.max_acceleration_units_per_sq_second[E_AXIS]);
695 695
   SERIAL_EOL;
696 696
   CONFIG_ECHO_START;
697 697
   if (!forReplay) {
698 698
     SERIAL_ECHOLNPGM("Accelerations: P=printing, R=retract and T=travel");
699 699
     CONFIG_ECHO_START;
700 700
   }
701
-  SERIAL_ECHOPAIR("  M204 P", acceleration);
702
-  SERIAL_ECHOPAIR(" R", retract_acceleration);
703
-  SERIAL_ECHOPAIR(" T", travel_acceleration);
701
+  SERIAL_ECHOPAIR("  M204 P", planner.acceleration);
702
+  SERIAL_ECHOPAIR(" R", planner.retract_acceleration);
703
+  SERIAL_ECHOPAIR(" T", planner.travel_acceleration);
704 704
   SERIAL_EOL;
705 705
 
706 706
   CONFIG_ECHO_START;
@@ -708,12 +708,12 @@ void Config_PrintSettings(bool forReplay) {
708 708
     SERIAL_ECHOLNPGM("Advanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s),  Z=maximum Z jerk (mm/s),  E=maximum E jerk (mm/s)");
709 709
     CONFIG_ECHO_START;
710 710
   }
711
-  SERIAL_ECHOPAIR("  M205 S", minimumfeedrate);
712
-  SERIAL_ECHOPAIR(" T", mintravelfeedrate);
713
-  SERIAL_ECHOPAIR(" B", minsegmenttime);
714
-  SERIAL_ECHOPAIR(" X", max_xy_jerk);
715
-  SERIAL_ECHOPAIR(" Z", max_z_jerk);
716
-  SERIAL_ECHOPAIR(" E", max_e_jerk);
711
+  SERIAL_ECHOPAIR("  M205 S", planner.min_feedrate);
712
+  SERIAL_ECHOPAIR(" T", planner.min_travel_feedrate);
713
+  SERIAL_ECHOPAIR(" B", planner.min_segment_time);
714
+  SERIAL_ECHOPAIR(" X", planner.max_xy_jerk);
715
+  SERIAL_ECHOPAIR(" Z", planner.max_z_jerk);
716
+  SERIAL_ECHOPAIR(" E", planner.max_e_jerk);
717 717
   SERIAL_EOL;
718 718
 
719 719
   CONFIG_ECHO_START;

+ 356
- 0
Marlin/endstops.cpp View File

@@ -0,0 +1,356 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ * endstops.cpp - A singleton object to manage endstops
25
+ */
26
+
27
+#include "Marlin.h"
28
+#include "endstops.h"
29
+#include "stepper.h"
30
+#include "ultralcd.h"
31
+
32
+// TEST_ENDSTOP: test the old and the current status of an endstop
33
+#define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits & old_endstop_bits, ENDSTOP))
34
+
35
+Endstops endstops;
36
+
37
+Endstops::Endstops() {
38
+  enable_globally(ENABLED(ENDSTOPS_ONLY_FOR_HOMING));
39
+  enable(true);
40
+  #if ENABLED(HAS_Z_MIN_PROBE)
41
+    enable_z_probe(false);
42
+  #endif
43
+} // Endstops::Endstops
44
+
45
+void Endstops::init() {
46
+
47
+  #if HAS_X_MIN
48
+    SET_INPUT(X_MIN_PIN);
49
+    #if ENABLED(ENDSTOPPULLUP_XMIN)
50
+      WRITE(X_MIN_PIN,HIGH);
51
+    #endif
52
+  #endif
53
+
54
+  #if HAS_Y_MIN
55
+    SET_INPUT(Y_MIN_PIN);
56
+    #if ENABLED(ENDSTOPPULLUP_YMIN)
57
+      WRITE(Y_MIN_PIN,HIGH);
58
+    #endif
59
+  #endif
60
+
61
+  #if HAS_Z_MIN
62
+    SET_INPUT(Z_MIN_PIN);
63
+    #if ENABLED(ENDSTOPPULLUP_ZMIN)
64
+      WRITE(Z_MIN_PIN,HIGH);
65
+    #endif
66
+  #endif
67
+
68
+  #if HAS_Z2_MIN
69
+    SET_INPUT(Z2_MIN_PIN);
70
+    #if ENABLED(ENDSTOPPULLUP_ZMIN)
71
+      WRITE(Z2_MIN_PIN,HIGH);
72
+    #endif
73
+  #endif
74
+
75
+  #if HAS_X_MAX
76
+    SET_INPUT(X_MAX_PIN);
77
+    #if ENABLED(ENDSTOPPULLUP_XMAX)
78
+      WRITE(X_MAX_PIN,HIGH);
79
+    #endif
80
+  #endif
81
+
82
+  #if HAS_Y_MAX
83
+    SET_INPUT(Y_MAX_PIN);
84
+    #if ENABLED(ENDSTOPPULLUP_YMAX)
85
+      WRITE(Y_MAX_PIN,HIGH);
86
+    #endif
87
+  #endif
88
+
89
+  #if HAS_Z_MAX
90
+    SET_INPUT(Z_MAX_PIN);
91
+    #if ENABLED(ENDSTOPPULLUP_ZMAX)
92
+      WRITE(Z_MAX_PIN,HIGH);
93
+    #endif
94
+  #endif
95
+
96
+  #if HAS_Z2_MAX
97
+    SET_INPUT(Z2_MAX_PIN);
98
+    #if ENABLED(ENDSTOPPULLUP_ZMAX)
99
+      WRITE(Z2_MAX_PIN,HIGH);
100
+    #endif
101
+  #endif
102
+
103
+  #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
104
+    SET_INPUT(Z_MIN_PROBE_PIN);
105
+    #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
106
+      WRITE(Z_MIN_PROBE_PIN,HIGH);
107
+    #endif
108
+  #endif
109
+
110
+} // Endstops::init
111
+
112
+void Endstops::report_state() {
113
+  if (endstop_hit_bits) {
114
+    #if ENABLED(ULTRA_LCD)
115
+      char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
116
+      #define _SET_STOP_CHAR(A,C) (chr## A = C)
117
+    #else
118
+      #define _SET_STOP_CHAR(A,C) ;
119
+    #endif
120
+
121
+    #define _ENDSTOP_HIT_ECHO(A,C) do{ \
122
+      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
123
+      _SET_STOP_CHAR(A,C); }while(0)
124
+
125
+    #define _ENDSTOP_HIT_TEST(A,C) \
126
+      if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
127
+        _ENDSTOP_HIT_ECHO(A,C)
128
+
129
+    SERIAL_ECHO_START;
130
+    SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
131
+    _ENDSTOP_HIT_TEST(X, 'X');
132
+    _ENDSTOP_HIT_TEST(Y, 'Y');
133
+    _ENDSTOP_HIT_TEST(Z, 'Z');
134
+
135
+    #if ENABLED(Z_MIN_PROBE_ENDSTOP)
136
+      #define P_AXIS Z_AXIS
137
+      if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
138
+    #endif
139
+    SERIAL_EOL;
140
+
141
+    #if ENABLED(ULTRA_LCD)
142
+      char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
143
+      sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
144
+      lcd_setstatus(msg);
145
+    #endif
146
+
147
+    hit_on_purpose();
148
+
149
+    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
150
+      if (abort_on_endstop_hit) {
151
+        card.sdprinting = false;
152
+        card.closefile();
153
+        stepper.quick_stop();
154
+        disable_all_heaters(); // switch off all heaters.
155
+      }
156
+    #endif
157
+  }
158
+} // Endstops::report_state
159
+
160
+void Endstops::M119() {
161
+  SERIAL_PROTOCOLLN(MSG_M119_REPORT);
162
+  #if HAS_X_MIN
163
+    SERIAL_PROTOCOLPGM(MSG_X_MIN);
164
+    SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
165
+  #endif
166
+  #if HAS_X_MAX
167
+    SERIAL_PROTOCOLPGM(MSG_X_MAX);
168
+    SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
169
+  #endif
170
+  #if HAS_Y_MIN
171
+    SERIAL_PROTOCOLPGM(MSG_Y_MIN);
172
+    SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
173
+  #endif
174
+  #if HAS_Y_MAX
175
+    SERIAL_PROTOCOLPGM(MSG_Y_MAX);
176
+    SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
177
+  #endif
178
+  #if HAS_Z_MIN
179
+    SERIAL_PROTOCOLPGM(MSG_Z_MIN);
180
+    SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
181
+  #endif
182
+  #if HAS_Z_MAX
183
+    SERIAL_PROTOCOLPGM(MSG_Z_MAX);
184
+    SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
185
+  #endif
186
+  #if HAS_Z2_MAX
187
+    SERIAL_PROTOCOLPGM(MSG_Z2_MAX);
188
+    SERIAL_PROTOCOLLN(((READ(Z2_MAX_PIN)^Z2_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
189
+  #endif
190
+  #if HAS_Z_PROBE
191
+    SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
192
+    SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
193
+  #endif
194
+} // Endstops::M119
195
+
196
+#if ENABLED(Z_DUAL_ENDSTOPS)
197
+
198
+  // Pass the result of the endstop test
199
+  void Endstops::test_dual_z_endstops(EndstopEnum es1, EndstopEnum es2) {
200
+    byte z_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Z, bit 1 for Z2
201
+    if (stepper.current_block->steps[Z_AXIS] > 0) {
202
+      stepper.endstop_triggered(Z_AXIS);
203
+      SBI(endstop_hit_bits, Z_MIN);
204
+      if (!stepper.performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
205
+        stepper.kill_current_block();
206
+    }
207
+  }
208
+
209
+#endif
210
+
211
+// Check endstops - Called from ISR!
212
+void Endstops::update() {
213
+
214
+  #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
215
+  #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
216
+  #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
217
+  #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
218
+
219
+  // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
220
+  #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
221
+  // COPY_BIT: copy the value of COPY_BIT to BIT in bits
222
+  #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
223
+
224
+  #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
225
+      UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
226
+      if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && stepper.current_block->steps[_AXIS(AXIS)] > 0) { \
227
+        _ENDSTOP_HIT(AXIS); \
228
+        stepper.endstop_triggered(_AXIS(AXIS)); \
229
+      } \
230
+    } while(0)
231
+
232
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
233
+    // Head direction in -X axis for CoreXY and CoreXZ bots.
234
+    // If Delta1 == -Delta2, the movement is only in Y or Z axis
235
+    if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[CORE_AXIS_2]) || (stepper.motor_direction(A_AXIS) == stepper.motor_direction(CORE_AXIS_2))) {
236
+      if (stepper.motor_direction(X_HEAD))
237
+  #else
238
+    if (stepper.motor_direction(X_AXIS))   // stepping along -X axis (regular Cartesian bot)
239
+  #endif
240
+      { // -direction
241
+        #if ENABLED(DUAL_X_CARRIAGE)
242
+          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
243
+          if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == -1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == -1))
244
+        #endif
245
+          {
246
+            #if HAS_X_MIN
247
+              UPDATE_ENDSTOP(X, MIN);
248
+            #endif
249
+          }
250
+      }
251
+      else { // +direction
252
+        #if ENABLED(DUAL_X_CARRIAGE)
253
+          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
254
+          if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == 1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == 1))
255
+        #endif
256
+          {
257
+            #if HAS_X_MAX
258
+              UPDATE_ENDSTOP(X, MAX);
259
+            #endif
260
+          }
261
+      }
262
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
263
+    }
264
+  #endif
265
+
266
+  #if ENABLED(COREXY)
267
+    // Head direction in -Y axis for CoreXY bots.
268
+    // If DeltaX == DeltaY, the movement is only in X axis
269
+    if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[B_AXIS]) || (stepper.motor_direction(A_AXIS) != stepper.motor_direction(B_AXIS))) {
270
+      if (stepper.motor_direction(Y_HEAD))
271
+  #else
272
+      if (stepper.motor_direction(Y_AXIS))   // -direction
273
+  #endif
274
+      { // -direction
275
+        #if HAS_Y_MIN
276
+          UPDATE_ENDSTOP(Y, MIN);
277
+        #endif
278
+      }
279
+      else { // +direction
280
+        #if HAS_Y_MAX
281
+          UPDATE_ENDSTOP(Y, MAX);
282
+        #endif
283
+      }
284
+  #if ENABLED(COREXY)
285
+    }
286
+  #endif
287
+
288
+  #if ENABLED(COREXZ)
289
+    // Head direction in -Z axis for CoreXZ bots.
290
+    // If DeltaX == DeltaZ, the movement is only in X axis
291
+    if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[C_AXIS]) || (stepper.motor_direction(A_AXIS) != stepper.motor_direction(C_AXIS))) {
292
+      if (stepper.motor_direction(Z_HEAD))
293
+  #else
294
+      if (stepper.motor_direction(Z_AXIS))
295
+  #endif
296
+      { // z -direction
297
+        #if HAS_Z_MIN
298
+
299
+          #if ENABLED(Z_DUAL_ENDSTOPS)
300
+
301
+            UPDATE_ENDSTOP_BIT(Z, MIN);
302
+            #if HAS_Z2_MIN
303
+              UPDATE_ENDSTOP_BIT(Z2, MIN);
304
+            #else
305
+              COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
306
+            #endif
307
+
308
+            test_dual_z_endstops(Z_MIN, Z2_MIN);
309
+
310
+          #else // !Z_DUAL_ENDSTOPS
311
+
312
+            #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
313
+              if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
314
+            #else
315
+              UPDATE_ENDSTOP(Z, MIN);
316
+            #endif
317
+
318
+          #endif // !Z_DUAL_ENDSTOPS
319
+
320
+        #endif // HAS_Z_MIN
321
+
322
+        #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
323
+          if (z_probe_enabled) {
324
+            UPDATE_ENDSTOP(Z, MIN_PROBE);
325
+            if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
326
+          }
327
+        #endif
328
+      }
329
+      else { // z +direction
330
+        #if HAS_Z_MAX
331
+
332
+          #if ENABLED(Z_DUAL_ENDSTOPS)
333
+
334
+            UPDATE_ENDSTOP_BIT(Z, MAX);
335
+            #if HAS_Z2_MAX
336
+              UPDATE_ENDSTOP_BIT(Z2, MAX);
337
+            #else
338
+              COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
339
+            #endif
340
+
341
+            test_dual_z_endstops(Z_MAX, Z2_MAX);
342
+
343
+          #else // !Z_DUAL_ENDSTOPS
344
+
345
+            UPDATE_ENDSTOP(Z, MAX);
346
+
347
+          #endif // !Z_DUAL_ENDSTOPS
348
+        #endif // Z_MAX_PIN
349
+      }
350
+  #if ENABLED(COREXZ)
351
+    }
352
+  #endif
353
+
354
+  old_endstop_bits = current_endstop_bits;
355
+
356
+} // Endstops::update()

+ 105
- 0
Marlin/endstops.h View File

@@ -0,0 +1,105 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+/**
24
+ *  endstops.h - manages endstops
25
+ */
26
+
27
+#ifndef ENDSTOPS_H
28
+#define ENDSTOPS_H
29
+
30
+enum EndstopEnum {X_MIN = 0, Y_MIN = 1, Z_MIN = 2, Z_MIN_PROBE = 3, X_MAX = 4, Y_MAX = 5, Z_MAX = 6, Z2_MIN = 7, Z2_MAX = 8};
31
+
32
+class Endstops {
33
+
34
+  public:
35
+
36
+    volatile char endstop_hit_bits; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
37
+
38
+    #if ENABLED(Z_DUAL_ENDSTOPS)
39
+      uint16_t current_endstop_bits = 0,
40
+                   old_endstop_bits = 0;
41
+    #else
42
+      byte current_endstop_bits = 0,
43
+               old_endstop_bits = 0;
44
+    #endif
45
+        
46
+
47
+    bool enabled = true;
48
+    bool enabled_globally =
49
+      #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
50
+        false
51
+      #else
52
+        true
53
+      #endif
54
+    ;
55
+
56
+    Endstops();
57
+
58
+    /**
59
+     * Initialize the endstop pins
60
+     */
61
+    void init();
62
+
63
+    /**
64
+     * Update the endstops bits from the pins
65
+     */
66
+    void update();
67
+
68
+    /**
69
+     * Print an error message reporting the position when the endstops were last hit.
70
+     */
71
+    void report_state(); //call from somewhere to create an serial error message with the locations the endstops where hit, in case they were triggered
72
+
73
+    /**
74
+     * Report endstop positions in response to M119
75
+     */
76
+    void M119();
77
+
78
+    // Enable / disable endstop checking globally
79
+    FORCE_INLINE void enable_globally(bool onoff=true) { enabled_globally = enabled = onoff; }
80
+
81
+    // Enable / disable endstop checking
82
+    FORCE_INLINE void enable(bool onoff=true) { enabled = onoff; }
83
+
84
+    // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
85
+    FORCE_INLINE void not_homing() { enabled = enabled_globally; }
86
+
87
+    // Clear endstops (i.e., they were hit intentionally) to suppress the report
88
+    FORCE_INLINE void hit_on_purpose() { endstop_hit_bits = 0; }
89
+
90
+    // Enable / disable endstop z-probe checking
91
+    #if ENABLED(HAS_Z_MIN_PROBE)
92
+      volatile bool z_probe_enabled = false;
93
+      FORCE_INLINE void enable_z_probe(bool onoff=true) { z_probe_enabled = onoff; }
94
+    #endif
95
+
96
+  private:
97
+
98
+    #if ENABLED(Z_DUAL_ENDSTOPS)
99
+      void test_dual_z_endstops(EndstopEnum es1, EndstopEnum es2);
100
+    #endif
101
+};
102
+
103
+extern Endstops endstops;
104
+
105
+#endif // ENDSTOPS_H

+ 151
- 207
Marlin/planner.cpp View File

@@ -81,105 +81,27 @@
81 81
   #include "mesh_bed_leveling.h"
82 82
 #endif
83 83
 
84
-//===========================================================================
85
-//============================= public variables ============================
86
-//===========================================================================
87
-
88
-millis_t minsegmenttime;
89
-float max_feedrate[NUM_AXIS]; // Max speeds in mm per minute
90
-float axis_steps_per_unit[NUM_AXIS];
91
-unsigned long max_acceleration_units_per_sq_second[NUM_AXIS]; // Use M201 to override by software
92
-float minimumfeedrate;
93
-float acceleration;         // Normal acceleration mm/s^2  DEFAULT ACCELERATION for all printing moves. M204 SXXXX
94
-float retract_acceleration; // Retract acceleration mm/s^2 filament pull-back and push-forward while standing still in the other axes M204 TXXXX
95
-float travel_acceleration;  // Travel acceleration mm/s^2  DEFAULT ACCELERATION for all NON printing moves. M204 MXXXX
96
-float max_xy_jerk;          // The largest speed change requiring no acceleration
97
-float max_z_jerk;
98
-float max_e_jerk;
99
-float mintravelfeedrate;
100
-unsigned long axis_steps_per_sqr_second[NUM_AXIS];
101
-
102
-#if ENABLED(AUTO_BED_LEVELING_FEATURE)
103
-  // Transform required to compensate for bed level
104
-  matrix_3x3 plan_bed_level_matrix = {
105
-    1.0, 0.0, 0.0,
106
-    0.0, 1.0, 0.0,
107
-    0.0, 0.0, 1.0
108
-  };
109
-#endif // AUTO_BED_LEVELING_FEATURE
84
+Planner planner;
110 85
 
111
-#if ENABLED(AUTOTEMP)
112
-  float autotemp_max = 250;
113
-  float autotemp_min = 210;
114
-  float autotemp_factor = 0.1;
115
-  bool autotemp_enabled = false;
116
-#endif
117
-
118
-#if ENABLED(FAN_SOFT_PWM)
119
-  extern unsigned char fanSpeedSoftPwm[FAN_COUNT];
120
-#endif
121
-
122
-//===========================================================================
123
-//============ semi-private variables, used in inline functions =============
124
-//===========================================================================
125
-
126
-block_t block_buffer[BLOCK_BUFFER_SIZE];            // A ring buffer for motion instfructions
127
-volatile unsigned char block_buffer_head;           // Index of the next block to be pushed
128
-volatile unsigned char block_buffer_tail;           // Index of the block to process now
129
-
130
-//===========================================================================
131
-//============================ private variables ============================
132
-//===========================================================================
133
-
134
-// The current position of the tool in absolute steps
135
-long position[NUM_AXIS];               // Rescaled from extern when axis_steps_per_unit are changed by gcode
136
-static float previous_speed[NUM_AXIS]; // Speed of previous path line segment
137
-static float previous_nominal_speed;   // Nominal speed of previous path line segment
138
-
139
-uint8_t g_uc_extruder_last_move[EXTRUDERS] = { 0 };
140
-
141
-#ifdef XY_FREQUENCY_LIMIT
142
-  // Used for the frequency limit
143
-  #define MAX_FREQ_TIME (1000000.0/XY_FREQUENCY_LIMIT)
144
-  // Old direction bits. Used for speed calculations
145
-  static unsigned char old_direction_bits = 0;
146
-  // Segment times (in µs). Used for speed calculations
147
-  static long axis_segment_time[2][3] = { {MAX_FREQ_TIME + 1, 0, 0}, {MAX_FREQ_TIME + 1, 0, 0} };
148
-#endif
149
-
150
-#if ENABLED(DUAL_X_CARRIAGE)
151
-  extern bool extruder_duplication_enabled;
152
-#endif
153
-
154
-//===========================================================================
155
-//================================ functions ================================
156
-//===========================================================================
157
-
158
-// Get the next / previous index of the next block in the ring buffer
159
-// NOTE: Using & here (not %) because BLOCK_BUFFER_SIZE is always a power of 2
160
-FORCE_INLINE int8_t next_block_index(int8_t block_index) { return BLOCK_MOD(block_index + 1); }
161
-FORCE_INLINE int8_t prev_block_index(int8_t block_index) { return BLOCK_MOD(block_index - 1); }
162
-
163
-// Calculates the distance (not time) it takes to accelerate from initial_rate to target_rate using the
164
-// given acceleration:
165
-FORCE_INLINE float estimate_acceleration_distance(float initial_rate, float target_rate, float acceleration) {
166
-  if (acceleration == 0) return 0; // acceleration was 0, set acceleration distance to 0
167
-  return (target_rate * target_rate - initial_rate * initial_rate) / (acceleration * 2);
86
+Planner::Planner() {
87
+  #if ENABLED(AUTO_BED_LEVELING_FEATURE)
88
+    bed_level_matrix.set_to_identity();
89
+  #endif
90
+  init();
168 91
 }
169 92
 
170
-// This function gives you the point at which you must start braking (at the rate of -acceleration) if
171
-// you started at speed initial_rate and accelerated until this point and want to end at the final_rate after
172
-// a total travel of distance. This can be used to compute the intersection point between acceleration and
173
-// deceleration in the cases where the trapezoid has no plateau (i.e. never reaches maximum speed)
174
-
175
-FORCE_INLINE float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance) {
176
-  if (acceleration == 0) return 0; // acceleration was 0, set intersection distance to 0
177
-  return (acceleration * 2 * distance - initial_rate * initial_rate + final_rate * final_rate) / (acceleration * 4);
93
+void Planner::init() {
94
+  block_buffer_head = block_buffer_tail = 0;
95
+  memset(position, 0, sizeof(position)); // clear position
96
+  for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
97
+  previous_nominal_speed = 0.0;
178 98
 }
179 99
 
180
-// Calculates trapezoid parameters so that the entry- and exit-speed is compensated by the provided factors.
181
-
182
-void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor) {
100
+/**
101
+ * Calculate trapezoid parameters, multiplying the entry- and exit-speeds
102
+ * by the provided factors.
103
+ */
104
+void Planner::calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor) {
183 105
   unsigned long initial_rate = ceil(block->nominal_rate * entry_factor),
184 106
                 final_rate = ceil(block->nominal_rate * exit_factor); // (steps per second)
185 107
 
@@ -225,12 +147,6 @@ void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exi
225 147
   CRITICAL_SECTION_END;
226 148
 }
227 149
 
228
-// Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
229
-// acceleration within the allotted distance.
230
-FORCE_INLINE float max_allowable_speed(float acceleration, float target_velocity, float distance) {
231
-  return sqrt(target_velocity * target_velocity - 2 * acceleration * distance);
232
-}
233
-
234 150
 // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.
235 151
 // This method will calculate the junction jerk as the euclidean distance between the nominal
236 152
 // velocities of the respective blocks.
@@ -240,8 +156,8 @@ FORCE_INLINE float max_allowable_speed(float acceleration, float target_velocity
240 156
 //}
241 157
 
242 158
 
243
-// The kernel called by planner_recalculate() when scanning the plan from last to first entry.
244
-void planner_reverse_pass_kernel(block_t* previous, block_t* current, block_t* next) {
159
+// The kernel called by recalculate() when scanning the plan from last to first entry.
160
+void Planner::reverse_pass_kernel(block_t* previous, block_t* current, block_t* next) {
245 161
   if (!current) return;
246 162
   UNUSED(previous);
247 163
 
@@ -267,31 +183,34 @@ void planner_reverse_pass_kernel(block_t* previous, block_t* current, block_t* n
267 183
   } // Skip last block. Already initialized and set for recalculation.
268 184
 }
269 185
 
270
-// planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
271
-// implements the reverse pass.
272
-void planner_reverse_pass() {
273
-  uint8_t block_index = block_buffer_head;
186
+/**
187
+ * recalculate() needs to go over the current plan twice.
188
+ * Once in reverse and once forward. This implements the reverse pass.
189
+ */
190
+void Planner::reverse_pass() {
274 191
 
275
-  //Make a local copy of block_buffer_tail, because the interrupt can alter it
276
-  CRITICAL_SECTION_START;
277
-    unsigned char tail = block_buffer_tail;
278
-  CRITICAL_SECTION_END
192
+  if (movesplanned() > 3) {
279 193
 
280
-  if (BLOCK_MOD(block_buffer_head - tail + BLOCK_BUFFER_SIZE) > 3) { // moves queued
281
-    block_index = BLOCK_MOD(block_buffer_head - 3);
282 194
     block_t* block[3] = { NULL, NULL, NULL };
283
-    while (block_index != tail) {
284
-      block_index = prev_block_index(block_index);
195
+
196
+    // Make a local copy of block_buffer_tail, because the interrupt can alter it
197
+    CRITICAL_SECTION_START;
198
+      uint8_t tail = block_buffer_tail;
199
+    CRITICAL_SECTION_END
200
+
201
+    uint8_t b = BLOCK_MOD(block_buffer_head - 3);
202
+    while (b != tail) {
203
+      b = prev_block_index(b);
285 204
       block[2] = block[1];
286 205
       block[1] = block[0];
287
-      block[0] = &block_buffer[block_index];
288
-      planner_reverse_pass_kernel(block[0], block[1], block[2]);
206
+      block[0] = &block_buffer[b];
207
+      reverse_pass_kernel(block[0], block[1], block[2]);
289 208
     }
290 209
   }
291 210
 }
292 211
 
293
-// The kernel called by planner_recalculate() when scanning the plan from first to last entry.
294
-void planner_forward_pass_kernel(block_t* previous, block_t* current, block_t* next) {
212
+// The kernel called by recalculate() when scanning the plan from first to last entry.
213
+void Planner::forward_pass_kernel(block_t* previous, block_t* current, block_t* next) {
295 214
   if (!previous) return;
296 215
   UNUSED(next);
297 216
 
@@ -312,26 +231,28 @@ void planner_forward_pass_kernel(block_t* previous, block_t* current, block_t* n
312 231
   }
313 232
 }
314 233
 
315
-// planner_recalculate() needs to go over the current plan twice. Once in reverse and once forward. This
316
-// implements the forward pass.
317
-void planner_forward_pass() {
318
-  uint8_t block_index = block_buffer_tail;
234
+/**
235
+ * recalculate() needs to go over the current plan twice.
236
+ * Once in reverse and once forward. This implements the forward pass.
237
+ */
238
+void Planner::forward_pass() {
319 239
   block_t* block[3] = { NULL, NULL, NULL };
320 240
 
321
-  while (block_index != block_buffer_head) {
241
+  for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
322 242
     block[0] = block[1];
323 243
     block[1] = block[2];
324
-    block[2] = &block_buffer[block_index];
325
-    planner_forward_pass_kernel(block[0], block[1], block[2]);
326
-    block_index = next_block_index(block_index);
244
+    block[2] = &block_buffer[b];
245
+    forward_pass_kernel(block[0], block[1], block[2]);
327 246
   }
328
-  planner_forward_pass_kernel(block[1], block[2], NULL);
247
+  forward_pass_kernel(block[1], block[2], NULL);
329 248
 }
330 249
 
331
-// Recalculates the trapezoid speed profiles for all blocks in the plan according to the
332
-// entry_factor for each junction. Must be called by planner_recalculate() after
333
-// updating the blocks.
334
-void planner_recalculate_trapezoids() {
250
+/**
251
+ * Recalculate the trapezoid speed profiles for all blocks in the plan
252
+ * according to the entry_factor for each junction. Must be called by
253
+ * recalculate() after updating the blocks.
254
+ */
255
+void Planner::recalculate_trapezoids() {
335 256
   int8_t block_index = block_buffer_tail;
336 257
   block_t* current;
337 258
   block_t* next = NULL;
@@ -358,54 +279,52 @@ void planner_recalculate_trapezoids() {
358 279
   }
359 280
 }
360 281
 
361
-// Recalculates the motion plan according to the following algorithm:
362
-//
363
-//   1. Go over every block in reverse order and calculate a junction speed reduction (i.e. block_t.entry_factor)
364
-//      so that:
365
-//     a. The junction jerk is within the set limit
366
-//     b. No speed reduction within one block requires faster deceleration than the one, true constant
367
-//        acceleration.
368
-//   2. Go over every block in chronological order and dial down junction speed reduction values if
369
-//     a. The speed increase within one block would require faster acceleration than the one, true
370
-//        constant acceleration.
371
-//
372
-// When these stages are complete all blocks have an entry_factor that will allow all speed changes to
373
-// be performed using only the one, true constant acceleration, and where no junction jerk is jerkier than
374
-// the set limit. Finally it will:
375
-//
376
-//   3. Recalculate trapezoids for all blocks.
377
-
378
-void planner_recalculate() {
379
-  planner_reverse_pass();
380
-  planner_forward_pass();
381
-  planner_recalculate_trapezoids();
382
-}
383
-
384
-void plan_init() {
385
-  block_buffer_head = block_buffer_tail = 0;
386
-  memset(position, 0, sizeof(position)); // clear position
387
-  for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
388
-  previous_nominal_speed = 0.0;
282
+/*
283
+ * Recalculate the motion plan according to the following algorithm:
284
+ *
285
+ *   1. Go over every block in reverse order...
286
+ *
287
+ *      Calculate a junction speed reduction (block_t.entry_factor) so:
288
+ *
289
+ *      a. The junction jerk is within the set limit, and
290
+ *
291
+ *      b. No speed reduction within one block requires faster
292
+ *         deceleration than the one, true constant acceleration.
293
+ *
294
+ *   2. Go over every block in chronological order...
295
+ *
296
+ *      Dial down junction speed reduction values if:
297
+ *      a. The speed increase within one block would require faster
298
+ *         acceleration than the one, true constant acceleration.
299
+ *
300
+ * After that, all blocks will have an entry_factor allowing all speed changes to
301
+ * be performed using only the one, true constant acceleration, and where no junction
302
+ * jerk is jerkier than the set limit, Jerky. Finally it will:
303
+ *
304
+ *   3. Recalculate "trapezoids" for all blocks.
305
+ */
306
+void Planner::recalculate() {
307
+  reverse_pass();
308
+  forward_pass();
309
+  recalculate_trapezoids();
389 310
 }
390 311
 
391 312
 
392 313
 #if ENABLED(AUTOTEMP)
393
-  void getHighESpeed() {
314
+
315
+  void Planner::getHighESpeed() {
394 316
     static float oldt = 0;
395 317
 
396 318
     if (!autotemp_enabled) return;
397 319
     if (degTargetHotend0() + 2 < autotemp_min) return; // probably temperature set to zero.
398 320
 
399 321
     float high = 0.0;
400
-    uint8_t block_index = block_buffer_tail;
401
-
402
-    while (block_index != block_buffer_head) {
403
-      block_t* block = &block_buffer[block_index];
322
+    for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
323
+      block_t* block = &block_buffer[b];
404 324
       if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
405 325
         float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed; // mm/sec;
406 326
         NOLESS(high, se);
407 327
       }
408
-      block_index = next_block_index(block_index);
409 328
     }
410 329
 
411 330
     float t = autotemp_min + high * autotemp_factor;
@@ -417,9 +336,13 @@ void plan_init() {
417 336
     oldt = t;
418 337
     setTargetHotend0(t);
419 338
   }
339
+
420 340
 #endif //AUTOTEMP
421 341
 
422
-void check_axes_activity() {
342
+/**
343
+ * Maintain fans, paste extruder pressure, 
344
+ */
345
+void Planner::check_axes_activity() {
423 346
   unsigned char axis_active[NUM_AXIS] = { 0 },
424 347
                 tail_fan_speed[FAN_COUNT];
425 348
 
@@ -432,26 +355,23 @@ void check_axes_activity() {
432 355
                   tail_e_to_p_pressure = baricuda_e_to_p_pressure;
433 356
   #endif
434 357
 
435
-  block_t* block;
436
-
437 358
   if (blocks_queued()) {
438 359
 
439
-    uint8_t block_index = block_buffer_tail;
440
-
441 360
     #if FAN_COUNT > 0
442
-      for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = block_buffer[block_index].fan_speed[i];
361
+      for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = block_buffer[block_buffer_tail].fan_speed[i];
443 362
     #endif
444 363
 
364
+    block_t* block;
365
+
445 366
     #if ENABLED(BARICUDA)
446
-      block = &block_buffer[block_index];
367
+      block = &block_buffer[block_buffer_tail];
447 368
       tail_valve_pressure = block->valve_pressure;
448 369
       tail_e_to_p_pressure = block->e_to_p_pressure;
449 370
     #endif
450 371
 
451
-    while (block_index != block_buffer_head) {
452
-      block = &block_buffer[block_index];
372
+    for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
373
+      block = &block_buffer[b];
453 374
       for (int i = 0; i < NUM_AXIS; i++) if (block->steps[i]) axis_active[i]++;
454
-      block_index = next_block_index(block_index);
455 375
     }
456 376
   }
457 377
   #if ENABLED(DISABLE_X)
@@ -549,15 +469,20 @@ void check_axes_activity() {
549 469
   #endif
550 470
 }
551 471
 
472
+/**
473
+ * Planner::buffer_line
474
+ *
475
+ * Add a new linear movement to the buffer.
476
+ *
477
+ *  x,y,z,e   - target position in mm
478
+ *  feed_rate - (target) speed of the move
479
+ *  extruder  - target extruder
480
+ */
552 481
 
553
-float junction_deviation = 0.1;
554
-// Add a new linear movement to the buffer. steps[X_AXIS], _y and _z is the absolute position in
555
-// mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration
556
-// calculation the caller must also provide the physical length of the line in millimeters.
557 482
 #if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
558
-  void plan_buffer_line(float x, float y, float z, const float& e, float feed_rate, const uint8_t extruder)
483
+  void Planner::buffer_line(float x, float y, float z, const float& e, float feed_rate, const uint8_t extruder)
559 484
 #else
560
-  void plan_buffer_line(const float& x, const float& y, const float& z, const float& e, float feed_rate, const uint8_t extruder)
485
+  void Planner::buffer_line(const float& x, const float& y, const float& z, const float& e, float feed_rate, const uint8_t extruder)
561 486
 #endif  // AUTO_BED_LEVELING_FEATURE
562 487
 {
563 488
   // Calculate the buffer head after we push this byte
@@ -570,7 +495,7 @@ float junction_deviation = 0.1;
570 495
   #if ENABLED(MESH_BED_LEVELING)
571 496
     if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
572 497
   #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
573
-    apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
498
+    apply_rotation_xyz(bed_level_matrix, x, y, z);
574 499
   #endif
575 500
 
576 501
   // The target position of the tool in absolute steps
@@ -703,7 +628,8 @@ float junction_deviation = 0.1;
703 628
 
704 629
   // Enable extruder(s)
705 630
   if (block->steps[E_AXIS]) {
706
-    if (DISABLE_INACTIVE_EXTRUDER) { //enable only selected extruder
631
+
632
+    #if ENABLED(DISABLE_INACTIVE_EXTRUDER) // Enable only the selected extruder
707 633
 
708 634
       for (int i = 0; i < EXTRUDERS; i++)
709 635
         if (g_uc_extruder_last_move[i] > 0) g_uc_extruder_last_move[i]--;
@@ -762,19 +688,18 @@ float junction_deviation = 0.1;
762 688
           #endif // EXTRUDERS > 2
763 689
         #endif // EXTRUDERS > 1
764 690
       }
765
-    }
766
-    else { // enable all
691
+    #else
767 692
       enable_e0();
768 693
       enable_e1();
769 694
       enable_e2();
770 695
       enable_e3();
771
-    }
696
+    #endif
772 697
   }
773 698
 
774 699
   if (block->steps[E_AXIS])
775
-    NOLESS(feed_rate, minimumfeedrate);
700
+    NOLESS(feed_rate, min_feedrate);
776 701
   else
777
-    NOLESS(feed_rate, mintravelfeedrate);
702
+    NOLESS(feed_rate, min_travel_feedrate);
778 703
 
779 704
   /**
780 705
    * This part of the code calculates the total length of the movement.
@@ -837,9 +762,9 @@ float junction_deviation = 0.1;
837 762
       //  segment time im micro seconds
838 763
       unsigned long segment_time = lround(1000000.0/inverse_second);
839 764
       if (mq) {
840
-        if (segment_time < minsegmenttime) {
765
+        if (segment_time < min_segment_time) {
841 766
           // buffer is draining, add extra time.  The amount of time added increases if the buffer is still emptied more.
842
-          inverse_second = 1000000.0 / (segment_time + lround(2 * (minsegmenttime - segment_time) / moves_queued));
767
+          inverse_second = 1000000.0 / (segment_time + lround(2 * (min_segment_time - segment_time) / moves_queued));
843 768
           #ifdef XY_FREQUENCY_LIMIT
844 769
             segment_time = lround(1000000.0 / inverse_second);
845 770
           #endif
@@ -968,6 +893,9 @@ float junction_deviation = 0.1;
968 893
   block->acceleration_rate = (long)(acc_st * 16777216.0 / (F_CPU / 8.0));
969 894
 
970 895
   #if 0  // Use old jerk for now
896
+
897
+    float junction_deviation = 0.1;
898
+
971 899
     // Compute path unit vector
972 900
     double unit_vec[3];
973 901
 
@@ -1083,11 +1011,11 @@ float junction_deviation = 0.1;
1083 1011
   // Update position
1084 1012
   for (int i = 0; i < NUM_AXIS; i++) position[i] = target[i];
1085 1013
 
1086
-  planner_recalculate();
1014
+  recalculate();
1087 1015
 
1088
-  st_wake_up();
1016
+  stepper.wake_up();
1089 1017
 
1090
-} // plan_buffer_line()
1018
+} // buffer_line()
1091 1019
 
1092 1020
 #if ENABLED(AUTO_BED_LEVELING_FEATURE) && DISABLED(DELTA)
1093 1021
 
@@ -1096,13 +1024,15 @@ float junction_deviation = 0.1;
1096 1024
    *
1097 1025
    * On CORE machines XYZ is derived from ABC.
1098 1026
    */
1099
-  vector_3 plan_get_position() {
1100
-    vector_3 position = vector_3(st_get_axis_position_mm(X_AXIS), st_get_axis_position_mm(Y_AXIS), st_get_axis_position_mm(Z_AXIS));
1027
+  vector_3 Planner::adjusted_position() {
1028
+    vector_3 position = vector_3(stepper.get_axis_position_mm(X_AXIS), stepper.get_axis_position_mm(Y_AXIS), stepper.get_axis_position_mm(Z_AXIS));
1029
+
1030
+    //position.debug("in Planner::position");
1031
+    //bed_level_matrix.debug("in Planner::position");
1032
+
1033
+    matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
1034
+    //inverse.debug("in Planner::inverse");
1101 1035
 
1102
-    //position.debug("in plan_get position");
1103
-    //plan_bed_level_matrix.debug("in plan_get_position");
1104
-    matrix_3x3 inverse = matrix_3x3::transpose(plan_bed_level_matrix);
1105
-    //inverse.debug("in plan_get inverse");
1106 1036
     position.apply_rotation(inverse);
1107 1037
     //position.debug("after rotation");
1108 1038
 
@@ -1117,34 +1047,48 @@ float junction_deviation = 0.1;
1117 1047
  * On CORE machines stepper ABC will be translated from the given XYZ.
1118 1048
  */
1119 1049
 #if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
1120
-  void plan_set_position(float x, float y, float z, const float& e)
1050
+  void Planner::set_position(float x, float y, float z, const float& e)
1121 1051
 #else
1122
-  void plan_set_position(const float& x, const float& y, const float& z, const float& e)
1052
+  void Planner::set_position(const float& x, const float& y, const float& z, const float& e)
1123 1053
 #endif // AUTO_BED_LEVELING_FEATURE || MESH_BED_LEVELING
1124 1054
   {
1125 1055
     #if ENABLED(MESH_BED_LEVELING)
1126 1056
       if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
1127 1057
     #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
1128
-      apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
1058
+      apply_rotation_xyz(bed_level_matrix, x, y, z);
1129 1059
     #endif
1130 1060
 
1131 1061
     long nx = position[X_AXIS] = lround(x * axis_steps_per_unit[X_AXIS]),
1132 1062
          ny = position[Y_AXIS] = lround(y * axis_steps_per_unit[Y_AXIS]),
1133 1063
          nz = position[Z_AXIS] = lround(z * axis_steps_per_unit[Z_AXIS]),
1134 1064
          ne = position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
1135
-    st_set_position(nx, ny, nz, ne);
1065
+    stepper.set_position(nx, ny, nz, ne);
1136 1066
     previous_nominal_speed = 0.0; // Resets planner junction speeds. Assumes start from rest.
1137 1067
 
1138 1068
     for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = 0.0;
1139 1069
   }
1140 1070
 
1141
-void plan_set_e_position(const float& e) {
1071
+/**
1072
+ * Directly set the planner E position (hence the stepper E position).
1073
+ */
1074
+void Planner::set_e_position(const float& e) {
1142 1075
   position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
1143
-  st_set_e_position(position[E_AXIS]);
1076
+  stepper.set_e_position(position[E_AXIS]);
1144 1077
 }
1145 1078
 
1146
-// Calculate the steps/s^2 acceleration rates, based on the mm/s^s
1147
-void reset_acceleration_rates() {
1079
+// Recalculate the steps/s^2 acceleration rates, based on the mm/s^2
1080
+void Planner::reset_acceleration_rates() {
1148 1081
   for (int i = 0; i < NUM_AXIS; i++)
1149 1082
     axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
1150 1083
 }
1084
+
1085
+#if ENABLED(AUTOTEMP)
1086
+
1087
+  void Planner::autotemp_M109() {
1088
+    autotemp_enabled = code_seen('F');
1089
+    if (autotemp_enabled) autotemp_factor = code_value();
1090
+    if (code_seen('S')) autotemp_min = code_value();
1091
+    if (code_seen('B')) autotemp_max = code_value();
1092
+  }
1093
+
1094
+#endif

+ 228
- 92
Marlin/planner.h View File

@@ -48,17 +48,36 @@
48 48
 
49 49
 #include "Marlin.h"
50 50
 
51
-// This struct is used when buffering the setup for each linear movement "nominal" values are as specified in
52
-// the source g-code and may never actually be reached if acceleration management is active.
51
+#if ENABLED(AUTO_BED_LEVELING_FEATURE)
52
+  #include "vector_3.h"
53
+#endif
54
+
55
+class Planner;
56
+extern Planner planner;
57
+
58
+/**
59
+ * struct block_t
60
+ *
61
+ * A single entry in the planner buffer.
62
+ * Tracks linear movement over multiple axes.
63
+ *
64
+ * The "nominal" values are as-specified by gcode, and
65
+ * may never actually be reached due to acceleration limits.
66
+ */
53 67
 typedef struct {
68
+
69
+  unsigned char active_extruder;            // The extruder to move (if E move)
70
+
54 71
   // Fields used by the bresenham algorithm for tracing the line
55 72
   long steps[NUM_AXIS];                     // Step count along each axis
56 73
   unsigned long step_event_count;           // The number of step events required to complete this block
74
+
57 75
   long accelerate_until;                    // The index of the step event on which to stop acceleration
58 76
   long decelerate_after;                    // The index of the step event on which to start decelerating
59 77
   long acceleration_rate;                   // The acceleration rate used for acceleration calculation
78
+
60 79
   unsigned char direction_bits;             // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
61
-  unsigned char active_extruder;            // Selects the active extruder
80
+
62 81
   #if ENABLED(ADVANCE)
63 82
     long advance_rate;
64 83
     volatile long initial_advance;
@@ -67,7 +86,6 @@ typedef struct {
67 86
   #endif
68 87
 
69 88
   // Fields used by the motion planner to manage acceleration
70
-  // float speed_x, speed_y, speed_z, speed_e;          // Nominal mm/sec for each axis
71 89
   float nominal_speed;                               // The nominal speed for this block in mm/sec
72 90
   float entry_speed;                                 // Entry speed at previous-current junction in mm/sec
73 91
   float max_entry_speed;                             // Maximum allowable junction entry speed in mm/sec
@@ -97,102 +115,220 @@ typedef struct {
97 115
 
98 116
 #define BLOCK_MOD(n) ((n)&(BLOCK_BUFFER_SIZE-1))
99 117
 
100
-// Initialize the motion plan subsystem
101
-void plan_init();
118
+class Planner {
102 119
 
103
-void check_axes_activity();
120
+  public:
104 121
 
105
-// Get the number of buffered moves
106
-extern volatile unsigned char block_buffer_head;
107
-extern volatile unsigned char block_buffer_tail;
108
-FORCE_INLINE uint8_t movesplanned() { return BLOCK_MOD(block_buffer_head - block_buffer_tail + BLOCK_BUFFER_SIZE); }
122
+    /**
123
+     * A ring buffer of moves described in steps
124
+     */
125
+    block_t block_buffer[BLOCK_BUFFER_SIZE];
126
+    volatile uint8_t block_buffer_head = 0;           // Index of the next block to be pushed
127
+    volatile uint8_t block_buffer_tail = 0;
128
+
129
+    float max_feedrate[NUM_AXIS]; // Max speeds in mm per minute
130
+    float axis_steps_per_unit[NUM_AXIS];
131
+    unsigned long axis_steps_per_sqr_second[NUM_AXIS];
132
+    unsigned long max_acceleration_units_per_sq_second[NUM_AXIS]; // Use M201 to override by software
133
+
134
+    millis_t min_segment_time;
135
+    float min_feedrate;
136
+    float acceleration;         // Normal acceleration mm/s^2  DEFAULT ACCELERATION for all printing moves. M204 SXXXX
137
+    float retract_acceleration; // Retract acceleration mm/s^2 filament pull-back and push-forward while standing still in the other axes M204 TXXXX
138
+    float travel_acceleration;  // Travel acceleration mm/s^2  DEFAULT ACCELERATION for all NON printing moves. M204 MXXXX
139
+    float max_xy_jerk;          // The largest speed change requiring no acceleration
140
+    float max_z_jerk;
141
+    float max_e_jerk;
142
+    float min_travel_feedrate;
143
+
144
+    #if ENABLED(AUTO_BED_LEVELING_FEATURE)
145
+      matrix_3x3 bed_level_matrix; // Transform to compensate for bed level
146
+    #endif
147
+
148
+  private:
109 149
 
110
-#if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
150
+    /**
151
+     * The current position of the tool in absolute steps
152
+     * Reclculated if any axis_steps_per_unit are changed by gcode
153
+     */
154
+    long position[NUM_AXIS] = { 0 };
111 155
 
112
-  #if ENABLED(AUTO_BED_LEVELING_FEATURE)
113
-    #include "vector_3.h"
156
+    /**
157
+     * Speed of previous path line segment
158
+     */
159
+    float previous_speed[NUM_AXIS];
160
+
161
+    /**
162
+     * Nominal speed of previous path line segment
163
+     */
164
+    float previous_nominal_speed;
165
+
166
+    #if ENABLED(DISABLE_INACTIVE_EXTRUDER)
167
+      /**
168
+       * Counters to manage disabling inactive extruders
169
+       */
170
+      uint8_t g_uc_extruder_last_move[EXTRUDERS] = { 0 };
171
+    #endif // DISABLE_INACTIVE_EXTRUDER
172
+
173
+    #ifdef XY_FREQUENCY_LIMIT
174
+      // Used for the frequency limit
175
+      #define MAX_FREQ_TIME (1000000.0/XY_FREQUENCY_LIMIT)
176
+      // Old direction bits. Used for speed calculations
177
+      static unsigned char old_direction_bits = 0;
178
+      // Segment times (in µs). Used for speed calculations
179
+      static long axis_segment_time[2][3] = { {MAX_FREQ_TIME + 1, 0, 0}, {MAX_FREQ_TIME + 1, 0, 0} };
180
+    #endif
181
+
182
+    #if ENABLED(DUAL_X_CARRIAGE)
183
+      extern bool extruder_duplication_enabled;
184
+    #endif
114 185
 
115
-    // Transform required to compensate for bed level
116
-    extern matrix_3x3 plan_bed_level_matrix;
186
+  public:
187
+
188
+    Planner();
189
+
190
+    void init();
191
+
192
+    void reset_acceleration_rates();
193
+
194
+    // Manage fans, paste pressure, etc.
195
+    void check_axes_activity();
117 196
 
118 197
     /**
119
-     * Get the position applying the bed level matrix
198
+     * Number of moves currently in the planner
120 199
      */
121
-    vector_3 plan_get_position();
122
-  #endif  // AUTO_BED_LEVELING_FEATURE
123
-
124
-  /**
125
-   * Add a new linear movement to the buffer. x, y, z are the signed, absolute target position in
126
-   * millimeters. Feed rate specifies the (target) speed of the motion.
127
-   */
128
-  void plan_buffer_line(float x, float y, float z, const float& e, float feed_rate, const uint8_t extruder);
129
-
130
-  /**
131
-   * Set the planner positions. Used for G92 instructions.
132
-   * Multiplies by axis_steps_per_unit[] to set stepper positions.
133
-   * Clears previous speed values.
134
-   */
135
-  void plan_set_position(float x, float y, float z, const float& e);
136
-
137
-#else
138
-
139
-  void plan_buffer_line(const float& x, const float& y, const float& z, const float& e, float feed_rate, const uint8_t extruder);
140
-  void plan_set_position(const float& x, const float& y, const float& z, const float& e);
141
-
142
-#endif // AUTO_BED_LEVELING_FEATURE || MESH_BED_LEVELING
143
-
144
-void plan_set_e_position(const float& e);
145
-
146
-//===========================================================================
147
-//============================= public variables ============================
148
-//===========================================================================
149
-
150
-extern millis_t minsegmenttime;
151
-extern float max_feedrate[NUM_AXIS]; // Max speeds in mm per minute
152
-extern float axis_steps_per_unit[NUM_AXIS];
153
-extern unsigned long max_acceleration_units_per_sq_second[NUM_AXIS]; // Use M201 to override by software
154
-extern float minimumfeedrate;
155
-extern float acceleration;         // Normal acceleration mm/s^2  DEFAULT ACCELERATION for all printing moves. M204 SXXXX
156
-extern float retract_acceleration; // Retract acceleration mm/s^2 filament pull-back and push-forward while standing still in the other axes M204 TXXXX
157
-extern float travel_acceleration;  // Travel acceleration mm/s^2  DEFAULT ACCELERATION for all NON printing moves. M204 MXXXX
158
-extern float max_xy_jerk;          // The largest speed change requiring no acceleration
159
-extern float max_z_jerk;
160
-extern float max_e_jerk;
161
-extern float mintravelfeedrate;
162
-extern unsigned long axis_steps_per_sqr_second[NUM_AXIS];
163
-
164
-#if ENABLED(AUTOTEMP)
165
-  extern bool autotemp_enabled;
166
-  extern float autotemp_max;
167
-  extern float autotemp_min;
168
-  extern float autotemp_factor;
169
-#endif
200
+    FORCE_INLINE uint8_t movesplanned() { return BLOCK_MOD(block_buffer_head - block_buffer_tail + BLOCK_BUFFER_SIZE); }
201
+
202
+    #if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
203
+
204
+      #if ENABLED(AUTO_BED_LEVELING_FEATURE)
205
+        /**
206
+         * The corrected position, applying the bed level matrix
207
+         */
208
+        vector_3 adjusted_position();
209
+      #endif
210
+
211
+      /**
212
+       * Add a new linear movement to the buffer.
213
+       *
214
+       *  x,y,z,e   - target position in mm
215
+       *  feed_rate - (target) speed of the move
216
+       *  extruder  - target extruder
217
+       */
218
+      void buffer_line(float x, float y, float z, const float& e, float feed_rate, const uint8_t extruder);
219
+
220
+      /**
221
+       * Set the planner.position and individual stepper positions.
222
+       * Used by G92, G28, G29, and other procedures.
223
+       *
224
+       * Multiplies by axis_steps_per_unit[] and does necessary conversion
225
+       * for COREXY / COREXZ to set the corresponding stepper positions.
226
+       *
227
+       * Clears previous speed values.
228
+       */
229
+      void set_position(float x, float y, float z, const float& e);
230
+
231
+    #else
232
+
233
+      void buffer_line(const float& x, const float& y, const float& z, const float& e, float feed_rate, const uint8_t extruder);
234
+      void set_position(const float& x, const float& y, const float& z, const float& e);
235
+
236
+    #endif // AUTO_BED_LEVELING_FEATURE || MESH_BED_LEVELING
237
+
238
+    /**
239
+     * Set the E position (mm) of the planner (and the E stepper)
240
+     */
241
+    void set_e_position(const float& e);
242
+
243
+    /**
244
+     * Does the buffer have any blocks queued?
245
+     */
246
+    FORCE_INLINE bool blocks_queued() { return (block_buffer_head != block_buffer_tail); }
247
+
248
+    /**
249
+     * "Discards" the block and "releases" the memory.
250
+     * Called when the current block is no longer needed.
251
+     */
252
+    FORCE_INLINE void discard_current_block() {
253
+      if (blocks_queued())
254
+        block_buffer_tail = BLOCK_MOD(block_buffer_tail + 1);
255
+    }
256
+
257
+    /**
258
+     * The current block. NULL if the buffer is empty.
259
+     * This also marks the block as busy.
260
+     */
261
+    FORCE_INLINE block_t* get_current_block() {
262
+      if (blocks_queued()) {
263
+        block_t* block = &block_buffer[block_buffer_tail];
264
+        block->busy = true;
265
+        return block;
266
+      }
267
+      else
268
+        return NULL;
269
+    }
270
+
271
+    /**
272
+     * Get the index of the next / previous block in the ring buffer
273
+     */
274
+    FORCE_INLINE int8_t next_block_index(int8_t block_index) { return BLOCK_MOD(block_index + 1); }
275
+    FORCE_INLINE int8_t prev_block_index(int8_t block_index) { return BLOCK_MOD(block_index - 1); }
276
+
277
+    /**
278
+     * Calculate the distance (not time) it takes to accelerate
279
+     * from initial_rate to target_rate using the given acceleration:
280
+     */
281
+    FORCE_INLINE float estimate_acceleration_distance(float initial_rate, float target_rate, float acceleration) {
282
+      if (acceleration == 0) return 0; // acceleration was 0, set acceleration distance to 0
283
+      return (target_rate * target_rate - initial_rate * initial_rate) / (acceleration * 2);
284
+    }
285
+
286
+    /**
287
+     * Return the point at which you must start braking (at the rate of -'acceleration') if
288
+     * you start at 'initial_rate', accelerate (until reaching the point), and want to end at
289
+     * 'final_rate' after traveling 'distance'.
290
+     *
291
+     * This is used to compute the intersection point between acceleration and deceleration
292
+     * in cases where the "trapezoid" has no plateau (i.e., never reaches maximum speed)
293
+     */
294
+    FORCE_INLINE float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance) {
295
+      if (acceleration == 0) return 0; // acceleration was 0, set intersection distance to 0
296
+      return (acceleration * 2 * distance - initial_rate * initial_rate + final_rate * final_rate) / (acceleration * 4);
297
+    }
298
+
299
+    /**
300
+     * Calculate the maximum allowable speed at this point, in order
301
+     * to reach 'target_velocity' using 'acceleration' within a given
302
+     * 'distance'.
303
+     */
304
+    FORCE_INLINE float max_allowable_speed(float acceleration, float target_velocity, float distance) {
305
+      return sqrt(target_velocity * target_velocity - 2 * acceleration * distance);
306
+    }
307
+
308
+
309
+    #if ENABLED(AUTOTEMP)
310
+      float autotemp_max = 250;
311
+      float autotemp_min = 210;
312
+      float autotemp_factor = 0.1;
313
+      bool autotemp_enabled = false;
314
+      void getHighESpeed();
315
+      void autotemp_M109();
316
+    #endif
317
+
318
+  private:
319
+
320
+    void calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor);
321
+
322
+    void reverse_pass_kernel(block_t* previous, block_t* current, block_t* next);
323
+    void forward_pass_kernel(block_t* previous, block_t* current, block_t* next);
324
+
325
+    void reverse_pass();
326
+    void forward_pass();
327
+
328
+    void recalculate_trapezoids();
329
+
330
+    void recalculate();
170 331
 
171
-extern block_t block_buffer[BLOCK_BUFFER_SIZE];            // A ring buffer for motion instructions
172
-extern volatile unsigned char block_buffer_head;           // Index of the next block to be pushed
173
-extern volatile unsigned char block_buffer_tail;
174
-
175
-// Returns true if the buffer has a queued block, false otherwise
176
-FORCE_INLINE bool blocks_queued() { return (block_buffer_head != block_buffer_tail); }
177
-
178
-// Called when the current block is no longer needed. Discards
179
-// the block and makes the memory available for new blocks.
180
-FORCE_INLINE void plan_discard_current_block() {
181
-  if (blocks_queued())
182
-    block_buffer_tail = BLOCK_MOD(block_buffer_tail + 1);
183
-}
184
-
185
-// Gets the current block. Returns NULL if buffer empty
186
-FORCE_INLINE block_t* plan_get_current_block() {
187
-  if (blocks_queued()) {
188
-    block_t* block = &block_buffer[block_buffer_tail];
189
-    block->busy = true;
190
-    return block;
191
-  }
192
-  else
193
-    return NULL;
194
-}
195
-
196
-void reset_acceleration_rates();
332
+};
197 333
 
198 334
 #endif // PLANNER_H

+ 151
- 566
Marlin/stepper.cpp View File

@@ -21,7 +21,7 @@
21 21
  */
22 22
 
23 23
 /**
24
- * stepper.cpp - stepper motor driver: executes motion plans using stepper motors
24
+ * stepper.cpp - A singleton object to execute motion plans using stepper motors
25 25
  * Marlin Firmware
26 26
  *
27 27
  * Derived from Grbl
@@ -46,6 +46,7 @@
46 46
 
47 47
 #include "Marlin.h"
48 48
 #include "stepper.h"
49
+#include "endstops.h"
49 50
 #include "planner.h"
50 51
 #include "temperature.h"
51 52
 #include "ultralcd.h"
@@ -57,85 +58,7 @@
57 58
   #include <SPI.h>
58 59
 #endif
59 60
 
60
-//===========================================================================
61
-//============================= public variables ============================
62
-//===========================================================================
63
-block_t* current_block;  // A pointer to the block currently being traced
64
-
65
-#if ENABLED(HAS_Z_MIN_PROBE)
66
-  volatile bool z_probe_is_active = false;
67
-#endif
68
-
69
-//===========================================================================
70
-//============================= private variables ===========================
71
-//===========================================================================
72
-//static makes it impossible to be called from outside of this file by extern.!
73
-
74
-// Variables used by The Stepper Driver Interrupt
75
-static unsigned char out_bits = 0;        // The next stepping-bits to be output
76
-static unsigned int cleaning_buffer_counter;
77
-
78
-#if ENABLED(Z_DUAL_ENDSTOPS)
79
-  static bool performing_homing = false,
80
-              locked_z_motor = false,
81
-              locked_z2_motor = false;
82
-#endif
83
-
84
-// Counter variables for the Bresenham line tracer
85
-static long counter_x, counter_y, counter_z, counter_e;
86
-volatile static unsigned long step_events_completed; // The number of step events executed in the current block
87
-
88
-#if ENABLED(ADVANCE)
89
-  static long advance_rate, advance, final_advance = 0;
90
-  static long old_advance = 0;
91
-  static long e_steps[4];
92
-#endif
93
-
94
-static long acceleration_time, deceleration_time;
95
-//static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
96
-static unsigned short acc_step_rate; // needed for deceleration start point
97
-static uint8_t step_loops;
98
-static uint8_t step_loops_nominal;
99
-static unsigned short OCR1A_nominal;
100
-
101
-volatile long endstops_trigsteps[3] = { 0 };
102
-volatile long endstops_stepsTotal, endstops_stepsDone;
103
-static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
104
-
105
-#if DISABLED(Z_DUAL_ENDSTOPS)
106
-  static byte
107
-#else
108
-  static uint16_t
109
-#endif
110
-    old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX
111
-
112
-#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
113
-  bool abort_on_endstop_hit = false;
114
-#endif
115
-
116
-#if HAS_MOTOR_CURRENT_PWM
117
-  #ifndef PWM_MOTOR_CURRENT
118
-    #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
119
-  #endif
120
-  const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
121
-#endif
122
-
123
-static bool check_endstops = true;
124
-static bool check_endstops_global =
125
-  #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
126
-    false
127
-  #else
128
-    true
129
-  #endif
130
-;
131
-
132
-volatile long count_position[NUM_AXIS] = { 0 }; // Positions of stepper motors, in step units
133
-volatile signed char count_direction[NUM_AXIS] = { 1 };
134
-
135
-
136
-//===========================================================================
137
-//================================ functions ================================
138
-//===========================================================================
61
+Stepper stepper; // Singleton
139 62
 
140 63
 #if ENABLED(DUAL_X_CARRIAGE)
141 64
   #define X_APPLY_DIR(v,ALWAYS) \
@@ -173,12 +96,12 @@ volatile signed char count_direction[NUM_AXIS] = { 1 };
173 96
     #define Z_APPLY_STEP(v,Q) \
174 97
     if (performing_homing) { \
175 98
       if (Z_HOME_DIR > 0) {\
176
-        if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
177
-        if (!(TEST(old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
99
+        if (!(TEST(endstops.old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
100
+        if (!(TEST(endstops.old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
178 101
       } \
179 102
       else { \
180
-        if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
181
-        if (!(TEST(old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
103
+        if (!(TEST(endstops.old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
104
+        if (!(TEST(endstops.old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
182 105
       } \
183 106
     } \
184 107
     else { \
@@ -195,31 +118,6 @@ volatile signed char count_direction[NUM_AXIS] = { 1 };
195 118
 
196 119
 #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
197 120
 
198
-// intRes = intIn1 * intIn2 >> 16
199
-// uses:
200
-// r26 to store 0
201
-// r27 to store the byte 1 of the 24 bit result
202
-#define MultiU16X8toH16(intRes, charIn1, intIn2) \
203
-  asm volatile ( \
204
-                 "clr r26 \n\t" \
205
-                 "mul %A1, %B2 \n\t" \
206
-                 "movw %A0, r0 \n\t" \
207
-                 "mul %A1, %A2 \n\t" \
208
-                 "add %A0, r1 \n\t" \
209
-                 "adc %B0, r26 \n\t" \
210
-                 "lsr r0 \n\t" \
211
-                 "adc %A0, r26 \n\t" \
212
-                 "adc %B0, r26 \n\t" \
213
-                 "clr r1 \n\t" \
214
-                 : \
215
-                 "=&r" (intRes) \
216
-                 : \
217
-                 "d" (charIn1), \
218
-                 "d" (intIn2) \
219
-                 : \
220
-                 "r26" \
221
-               )
222
-
223 121
 // intRes = longIn1 * longIn2 >> 24
224 122
 // uses:
225 123
 // r26 to store 0
@@ -281,312 +179,38 @@ volatile signed char count_direction[NUM_AXIS] = { 1 };
281 179
 #define ENABLE_STEPPER_DRIVER_INTERRUPT()  SBI(TIMSK1, OCIE1A)
282 180
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
283 181
 
284
-void enable_endstops(bool check) { check_endstops = check; }
285
-
286
-void enable_endstops_globally(bool check) { check_endstops_global = check_endstops = check; }
287
-
288
-void endstops_not_homing() { check_endstops = check_endstops_global; }
289
-
290
-void endstops_hit_on_purpose() { endstop_hit_bits = 0; }
291
-
292
-void checkHitEndstops() {
293
-  if (endstop_hit_bits) {
294
-    #if ENABLED(ULTRA_LCD)
295
-      char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
296
-      #define _SET_STOP_CHAR(A,C) (chr## A = C)
297
-    #else
298
-      #define _SET_STOP_CHAR(A,C) ;
299
-    #endif
300
-
301
-    #define _ENDSTOP_HIT_ECHO(A,C) do{ \
302
-      SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", endstops_trigsteps[A ##_AXIS] / axis_steps_per_unit[A ##_AXIS]); \
303
-      _SET_STOP_CHAR(A,C); }while(0)
304
-
305
-    #define _ENDSTOP_HIT_TEST(A,C) \
306
-      if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
307
-        _ENDSTOP_HIT_ECHO(A,C)
308
-
309
-    SERIAL_ECHO_START;
310
-    SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
311
-    _ENDSTOP_HIT_TEST(X, 'X');
312
-    _ENDSTOP_HIT_TEST(Y, 'Y');
313
-    _ENDSTOP_HIT_TEST(Z, 'Z');
314
-
315
-    #if ENABLED(Z_MIN_PROBE_ENDSTOP)
316
-      #define P_AXIS Z_AXIS
317
-      if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
318
-    #endif
319
-    SERIAL_EOL;
320
-
321
-    #if ENABLED(ULTRA_LCD)
322
-      char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
323
-      sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
324
-      lcd_setstatus(msg);
325
-    #endif
326
-
327
-    endstops_hit_on_purpose();
328
-
329
-    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
330
-      if (abort_on_endstop_hit) {
331
-        card.sdprinting = false;
332
-        card.closefile();
333
-        quickStop();
334
-        disable_all_heaters(); // switch off all heaters.
335
-      }
336
-    #endif
337
-  }
338
-}
339
-
340
-// Check endstops - Called from ISR!
341
-inline void update_endstops() {
342
-
343
-  #if ENABLED(Z_DUAL_ENDSTOPS)
344
-    uint16_t
345
-  #else
346
-    byte
347
-  #endif
348
-      current_endstop_bits = 0;
349
-
350
-  #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
351
-  #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
352
-  #define _AXIS(AXIS) AXIS ##_AXIS
353
-  #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
354
-  #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
355
-
356
-  // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
357
-  #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
358
-  // COPY_BIT: copy the value of COPY_BIT to BIT in bits
359
-  #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
360
-  // TEST_ENDSTOP: test the old and the current status of an endstop
361
-  #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
362
-
363
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
364
-
365
-    #define _SET_TRIGSTEPS(AXIS) do { \
366
-        float axis_pos = count_position[_AXIS(AXIS)]; \
367
-        if (_AXIS(AXIS) == A_AXIS) \
368
-          axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2; \
369
-        else if (_AXIS(AXIS) == CORE_AXIS_2) \
370
-          axis_pos = (count_position[A_AXIS] - axis_pos) / 2; \
371
-        endstops_trigsteps[_AXIS(AXIS)] = axis_pos; \
372
-      } while(0)
373
-
374
-  #else
375
-
376
-    #define _SET_TRIGSTEPS(AXIS) endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]
377
-
378
-  #endif // COREXY || COREXZ
379
-
380
-  #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
381
-      SET_ENDSTOP_BIT(AXIS, MINMAX); \
382
-      if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && current_block->steps[_AXIS(AXIS)] > 0) { \
383
-        _SET_TRIGSTEPS(AXIS); \
384
-        _ENDSTOP_HIT(AXIS); \
385
-        step_events_completed = current_block->step_event_count; \
386
-      } \
387
-    } while(0)
388
-
389
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
390
-    // Head direction in -X axis for CoreXY and CoreXZ bots.
391
-    // If Delta1 == -Delta2, the movement is only in Y or Z axis
392
-    if ((current_block->steps[A_AXIS] != current_block->steps[CORE_AXIS_2]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, CORE_AXIS_2))) {
393
-      if (TEST(out_bits, X_HEAD))
394
-  #else
395
-    if (TEST(out_bits, X_AXIS))   // stepping along -X axis (regular Cartesian bot)
396
-  #endif
397
-      { // -direction
398
-        #if ENABLED(DUAL_X_CARRIAGE)
399
-          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
400
-          if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
401
-        #endif
402
-          {
403
-            #if HAS_X_MIN
404
-              UPDATE_ENDSTOP(X, MIN);
405
-            #endif
406
-          }
407
-      }
408
-      else { // +direction
409
-        #if ENABLED(DUAL_X_CARRIAGE)
410
-          // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
411
-          if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
412
-        #endif
413
-          {
414
-            #if HAS_X_MAX
415
-              UPDATE_ENDSTOP(X, MAX);
416
-            #endif
417
-          }
418
-      }
419
-  #if ENABLED(COREXY) || ENABLED(COREXZ)
420
-    }
421
-  #endif
422
-
423
-  #if ENABLED(COREXY)
424
-    // Head direction in -Y axis for CoreXY bots.
425
-    // If DeltaX == DeltaY, the movement is only in X axis
426
-    if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
427
-      if (TEST(out_bits, Y_HEAD))
428
-  #else
429
-      if (TEST(out_bits, Y_AXIS))   // -direction
430
-  #endif
431
-      { // -direction
432
-        #if HAS_Y_MIN
433
-          UPDATE_ENDSTOP(Y, MIN);
434
-        #endif
435
-      }
436
-      else { // +direction
437
-        #if HAS_Y_MAX
438
-          UPDATE_ENDSTOP(Y, MAX);
439
-        #endif
440
-      }
441
-  #if ENABLED(COREXY)
442
-    }
443
-  #endif
444
-
445
-  #if ENABLED(COREXZ)
446
-    // Head direction in -Z axis for CoreXZ bots.
447
-    // If DeltaX == DeltaZ, the movement is only in X axis
448
-    if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
449
-      if (TEST(out_bits, Z_HEAD))
450
-  #else
451
-      if (TEST(out_bits, Z_AXIS))
452
-  #endif
453
-      { // z -direction
454
-        #if HAS_Z_MIN
455
-
456
-          #if ENABLED(Z_DUAL_ENDSTOPS)
457
-            SET_ENDSTOP_BIT(Z, MIN);
458
-            #if HAS_Z2_MIN
459
-              SET_ENDSTOP_BIT(Z2, MIN);
460
-            #else
461
-              COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
462
-            #endif
463
-
464
-            byte z_test = TEST_ENDSTOP(Z_MIN) | (TEST_ENDSTOP(Z2_MIN) << 1); // bit 0 for Z, bit 1 for Z2
465
-
466
-            if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
467
-              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
468
-              SBI(endstop_hit_bits, Z_MIN);
469
-              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
470
-                step_events_completed = current_block->step_event_count;
471
-            }
472
-          #else // !Z_DUAL_ENDSTOPS
473
-
474
-            #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
475
-              if (z_probe_is_active) UPDATE_ENDSTOP(Z, MIN);
476
-            #else
477
-              UPDATE_ENDSTOP(Z, MIN);
478
-            #endif
479
-          #endif // !Z_DUAL_ENDSTOPS
480
-        #endif
481
-
482
-        #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
483
-          if (z_probe_is_active) {
484
-            UPDATE_ENDSTOP(Z, MIN_PROBE);
485
-            if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
486
-          }
487
-        #endif
488
-      }
489
-      else { // z +direction
490
-        #if HAS_Z_MAX
491
-
492
-          #if ENABLED(Z_DUAL_ENDSTOPS)
493
-
494
-            SET_ENDSTOP_BIT(Z, MAX);
495
-            #if HAS_Z2_MAX
496
-              SET_ENDSTOP_BIT(Z2, MAX);
497
-            #else
498
-              COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
499
-            #endif
500
-
501
-            byte z_test = TEST_ENDSTOP(Z_MAX) | (TEST_ENDSTOP(Z2_MAX) << 1); // bit 0 for Z, bit 1 for Z2
502
-
503
-            if (z_test && current_block->steps[Z_AXIS] > 0) {  // t_test = Z_MAX || Z2_MAX
504
-              endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
505
-              SBI(endstop_hit_bits, Z_MIN);
506
-              if (!performing_homing || (z_test == 0x3))  //if not performing home or if both endstops were trigged during homing...
507
-                step_events_completed = current_block->step_event_count;
508
-            }
509
-
510
-          #else // !Z_DUAL_ENDSTOPS
511
-
512
-            UPDATE_ENDSTOP(Z, MAX);
513
-
514
-          #endif // !Z_DUAL_ENDSTOPS
515
-        #endif // Z_MAX_PIN
516
-      }
517
-  #if ENABLED(COREXZ)
518
-    }
519
-  #endif
520
-  old_endstop_bits = current_endstop_bits;
521
-}
522
-
523
-//         __________________________
524
-//        /|                        |\     _________________         ^
525
-//       / |                        | \   /|               |\        |
526
-//      /  |                        |  \ / |               | \       s
527
-//     /   |                        |   |  |               |  \      p
528
-//    /    |                        |   |  |               |   \     e
529
-//   +-----+------------------------+---+--+---------------+----+    e
530
-//   |               BLOCK 1            |      BLOCK 2          |    d
531
-//
532
-//                           time ----->
533
-//
534
-//  The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
535
-//  first block->accelerate_until step_events_completed, then keeps going at constant speed until
536
-//  step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
537
-//  The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
538
-
539
-void st_wake_up() {
182
+/**
183
+ *         __________________________
184
+ *        /|                        |\     _________________         ^
185
+ *       / |                        | \   /|               |\        |
186
+ *      /  |                        |  \ / |               | \       s
187
+ *     /   |                        |   |  |               |  \      p
188
+ *    /    |                        |   |  |               |   \     e
189
+ *   +-----+------------------------+---+--+---------------+----+    e
190
+ *   |               BLOCK 1            |      BLOCK 2          |    d
191
+ *
192
+ *                           time ----->
193
+ *
194
+ *  The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
195
+ *  first block->accelerate_until step_events_completed, then keeps going at constant speed until
196
+ *  step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
197
+ *  The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
198
+ */
199
+void Stepper::wake_up() {
540 200
   //  TCNT1 = 0;
541 201
   ENABLE_STEPPER_DRIVER_INTERRUPT();
542 202
 }
543 203
 
544
-FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
545
-  unsigned short timer;
546
-
547
-  NOMORE(step_rate, MAX_STEP_FREQUENCY);
548
-
549
-  if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
550
-    step_rate = (step_rate >> 2) & 0x3fff;
551
-    step_loops = 4;
552
-  }
553
-  else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
554
-    step_rate = (step_rate >> 1) & 0x7fff;
555
-    step_loops = 2;
556
-  }
557
-  else {
558
-    step_loops = 1;
559
-  }
560
-
561
-  NOLESS(step_rate, F_CPU / 500000);
562
-  step_rate -= F_CPU / 500000; // Correct for minimal speed
563
-  if (step_rate >= (8 * 256)) { // higher step rate
564
-    unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
565
-    unsigned char tmp_step_rate = (step_rate & 0x00ff);
566
-    unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
567
-    MultiU16X8toH16(timer, tmp_step_rate, gain);
568
-    timer = (unsigned short)pgm_read_word_near(table_address) - timer;
569
-  }
570
-  else { // lower step rates
571
-    unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
572
-    table_address += ((step_rate) >> 1) & 0xfffc;
573
-    timer = (unsigned short)pgm_read_word_near(table_address);
574
-    timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
575
-  }
576
-  if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
577
-  return timer;
578
-}
579
-
580 204
 /**
581 205
  * Set the stepper direction of each axis
582 206
  *
583 207
  *   X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
584 208
  *   X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
585 209
  */
586
-void set_stepper_direction() {
210
+void Stepper::set_directions() {
587 211
 
588 212
   #define SET_STEP_DIR(AXIS) \
589
-    if (TEST(out_bits, AXIS ##_AXIS)) { \
213
+    if (motor_direction(AXIS ##_AXIS)) { \
590 214
       AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
591 215
       count_direction[AXIS ##_AXIS] = -1; \
592 216
     } \
@@ -600,7 +224,7 @@ void set_stepper_direction() {
600 224
   SET_STEP_DIR(Z); // C
601 225
 
602 226
   #if DISABLED(ADVANCE)
603
-    if (TEST(out_bits, E_AXIS)) {
227
+    if (motor_direction(E_AXIS)) {
604 228
       REV_E_DIR();
605 229
       count_direction[E_AXIS] = -1;
606 230
     }
@@ -611,52 +235,14 @@ void set_stepper_direction() {
611 235
   #endif //!ADVANCE
612 236
 }
613 237
 
614
-// Initializes the trapezoid generator from the current block. Called whenever a new
615
-// block begins.
616
-FORCE_INLINE void trapezoid_generator_reset() {
617
-
618
-  static int8_t last_extruder = -1;
619
-
620
-  if (current_block->direction_bits != out_bits || current_block->active_extruder != last_extruder) {
621
-    out_bits = current_block->direction_bits;
622
-    last_extruder = current_block->active_extruder;
623
-    set_stepper_direction();
624
-  }
625
-
626
-  #if ENABLED(ADVANCE)
627
-    advance = current_block->initial_advance;
628
-    final_advance = current_block->final_advance;
629
-    // Do E steps + advance steps
630
-    e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
631
-    old_advance = advance >>8;
632
-  #endif
633
-  deceleration_time = 0;
634
-  // step_rate to timer interval
635
-  OCR1A_nominal = calc_timer(current_block->nominal_rate);
636
-  // make a note of the number of step loops required at nominal speed
637
-  step_loops_nominal = step_loops;
638
-  acc_step_rate = current_block->initial_rate;
639
-  acceleration_time = calc_timer(acc_step_rate);
640
-  OCR1A = acceleration_time;
641
-
642
-  // SERIAL_ECHO_START;
643
-  // SERIAL_ECHOPGM("advance :");
644
-  // SERIAL_ECHO(current_block->advance/256.0);
645
-  // SERIAL_ECHOPGM("advance rate :");
646
-  // SERIAL_ECHO(current_block->advance_rate/256.0);
647
-  // SERIAL_ECHOPGM("initial advance :");
648
-  // SERIAL_ECHO(current_block->initial_advance/256.0);
649
-  // SERIAL_ECHOPGM("final advance :");
650
-  // SERIAL_ECHOLN(current_block->final_advance/256.0);
651
-}
652
-
653 238
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
654 239
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
655
-ISR(TIMER1_COMPA_vect) {
240
+ISR(TIMER1_COMPA_vect) { stepper.isr(); }
656 241
 
242
+void Stepper::isr() {
657 243
   if (cleaning_buffer_counter) {
658 244
     current_block = NULL;
659
-    plan_discard_current_block();
245
+    planner.discard_current_block();
660 246
     #ifdef SD_FINISHED_RELEASECOMMAND
661 247
       if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
662 248
     #endif
@@ -668,12 +254,12 @@ ISR(TIMER1_COMPA_vect) {
668 254
   // If there is no current block, attempt to pop one from the buffer
669 255
   if (!current_block) {
670 256
     // Anything in the buffer?
671
-    current_block = plan_get_current_block();
257
+    current_block = planner.get_current_block();
672 258
     if (current_block) {
673 259
       current_block->busy = true;
674 260
       trapezoid_generator_reset();
675
-      counter_x = -(current_block->step_event_count >> 1);
676
-      counter_y = counter_z = counter_e = counter_x;
261
+      counter_X = -(current_block->step_event_count >> 1);
262
+      counter_Y = counter_Z = counter_E = counter_X;
677 263
       step_events_completed = 0;
678 264
 
679 265
       #if ENABLED(Z_LATE_ENABLE)
@@ -697,9 +283,9 @@ ISR(TIMER1_COMPA_vect) {
697 283
 
698 284
     // Update endstops state, if enabled
699 285
     #if ENABLED(HAS_Z_MIN_PROBE)
700
-      if (check_endstops || z_probe_is_active) update_endstops();
286
+      if (endstops.enabled || endstops.z_probe_enabled) endstops.update();
701 287
     #else
702
-      if (check_endstops) update_endstops();
288
+      if (endstops.enabled) endstops.update();
703 289
     #endif
704 290
 
705 291
     // Take multiple steps per interrupt (For high speed moves)
@@ -709,48 +295,47 @@ ISR(TIMER1_COMPA_vect) {
709 295
       #endif
710 296
 
711 297
       #if ENABLED(ADVANCE)
712
-        counter_e += current_block->steps[E_AXIS];
713
-        if (counter_e > 0) {
714
-          counter_e -= current_block->step_event_count;
715
-          e_steps[current_block->active_extruder] += TEST(out_bits, E_AXIS) ? -1 : 1;
298
+        counter_E += current_block->steps[E_AXIS];
299
+        if (counter_E > 0) {
300
+          counter_E -= current_block->step_event_count;
301
+          e_steps[current_block->active_extruder] += motor_direction(E_AXIS) ? -1 : 1;
716 302
         }
717 303
       #endif //ADVANCE
718 304
 
719
-      #define _COUNTER(axis) counter_## axis
305
+      #define _COUNTER(AXIS) counter_## AXIS
720 306
       #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
721 307
       #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
722 308
 
723
-      #define STEP_ADD(axis, AXIS) \
724
-        _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
725
-        if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
309
+      #define STEP_ADD(AXIS) \
310
+        _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
311
+        if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
726 312
 
727
-      STEP_ADD(x,X);
728
-      STEP_ADD(y,Y);
729
-      STEP_ADD(z,Z);
313
+      STEP_ADD(X);
314
+      STEP_ADD(Y);
315
+      STEP_ADD(Z);
730 316
       #if DISABLED(ADVANCE)
731
-        STEP_ADD(e,E);
317
+        STEP_ADD(E);
732 318
       #endif
733 319
 
734
-      #define STEP_IF_COUNTER(axis, AXIS) \
735
-        if (_COUNTER(axis) > 0) { \
736
-          _COUNTER(axis) -= current_block->step_event_count; \
320
+      #define STEP_IF_COUNTER(AXIS) \
321
+        if (_COUNTER(AXIS) > 0) { \
322
+          _COUNTER(AXIS) -= current_block->step_event_count; \
737 323
           count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
738 324
           _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
739 325
         }
740 326
 
741
-      STEP_IF_COUNTER(x, X);
742
-      STEP_IF_COUNTER(y, Y);
743
-      STEP_IF_COUNTER(z, Z);
327
+      STEP_IF_COUNTER(X);
328
+      STEP_IF_COUNTER(Y);
329
+      STEP_IF_COUNTER(Z);
744 330
       #if DISABLED(ADVANCE)
745
-        STEP_IF_COUNTER(e, E);
331
+        STEP_IF_COUNTER(E);
746 332
       #endif
747 333
 
748 334
       step_events_completed++;
749 335
       if (step_events_completed >= current_block->step_event_count) break;
750 336
     }
751 337
     // Calculate new timer value
752
-    unsigned short timer;
753
-    unsigned short step_rate;
338
+    unsigned short timer, step_rate;
754 339
     if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
755 340
 
756 341
       MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
@@ -811,16 +396,17 @@ ISR(TIMER1_COMPA_vect) {
811 396
     // If current block is finished, reset pointer
812 397
     if (step_events_completed >= current_block->step_event_count) {
813 398
       current_block = NULL;
814
-      plan_discard_current_block();
399
+      planner.discard_current_block();
815 400
     }
816 401
   }
817 402
 }
818 403
 
819 404
 #if ENABLED(ADVANCE)
820
-  unsigned char old_OCR0A;
821 405
   // Timer interrupt for E. e_steps is set in the main routine;
822 406
   // Timer 0 is shared with millies
823
-  ISR(TIMER0_COMPA_vect) {
407
+  ISR(TIMER0_COMPA_vect) { stepper.advance_isr(); }
408
+
409
+  void Stepper::advance_isr() {
824 410
     old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
825 411
     OCR0A = old_OCR0A;
826 412
 
@@ -852,9 +438,10 @@ ISR(TIMER1_COMPA_vect) {
852 438
       #endif
853 439
     }
854 440
   }
441
+
855 442
 #endif // ADVANCE
856 443
 
857
-void st_init() {
444
+void Stepper::init() {
858 445
   digipot_init(); //Initialize Digipot Motor Current
859 446
   microstep_init(); //Initialize Microstepping Pins
860 447
 
@@ -944,70 +531,10 @@ void st_init() {
944 531
     if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
945 532
   #endif
946 533
 
947
-  //endstops and pullups
948
-
949
-  #if HAS_X_MIN
950
-    SET_INPUT(X_MIN_PIN);
951
-    #if ENABLED(ENDSTOPPULLUP_XMIN)
952
-      WRITE(X_MIN_PIN,HIGH);
953
-    #endif
954
-  #endif
955
-
956
-  #if HAS_Y_MIN
957
-    SET_INPUT(Y_MIN_PIN);
958
-    #if ENABLED(ENDSTOPPULLUP_YMIN)
959
-      WRITE(Y_MIN_PIN,HIGH);
960
-    #endif
961
-  #endif
962
-
963
-  #if HAS_Z_MIN
964
-    SET_INPUT(Z_MIN_PIN);
965
-    #if ENABLED(ENDSTOPPULLUP_ZMIN)
966
-      WRITE(Z_MIN_PIN,HIGH);
967
-    #endif
968
-  #endif
969
-
970
-  #if HAS_Z2_MIN
971
-    SET_INPUT(Z2_MIN_PIN);
972
-    #if ENABLED(ENDSTOPPULLUP_ZMIN)
973
-      WRITE(Z2_MIN_PIN,HIGH);
974
-    #endif
975
-  #endif
976
-
977
-  #if HAS_X_MAX
978
-    SET_INPUT(X_MAX_PIN);
979
-    #if ENABLED(ENDSTOPPULLUP_XMAX)
980
-      WRITE(X_MAX_PIN,HIGH);
981
-    #endif
982
-  #endif
983
-
984
-  #if HAS_Y_MAX
985
-    SET_INPUT(Y_MAX_PIN);
986
-    #if ENABLED(ENDSTOPPULLUP_YMAX)
987
-      WRITE(Y_MAX_PIN,HIGH);
988
-    #endif
989
-  #endif
990
-
991
-  #if HAS_Z_MAX
992
-    SET_INPUT(Z_MAX_PIN);
993
-    #if ENABLED(ENDSTOPPULLUP_ZMAX)
994
-      WRITE(Z_MAX_PIN,HIGH);
995
-    #endif
996
-  #endif
997
-
998
-  #if HAS_Z2_MAX
999
-    SET_INPUT(Z2_MAX_PIN);
1000
-    #if ENABLED(ENDSTOPPULLUP_ZMAX)
1001
-      WRITE(Z2_MAX_PIN,HIGH);
1002
-    #endif
1003
-  #endif
1004
-
1005
-  #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
1006
-    SET_INPUT(Z_MIN_PROBE_PIN);
1007
-    #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
1008
-      WRITE(Z_MIN_PROBE_PIN,HIGH);
1009
-    #endif
1010
-  #endif
534
+  //
535
+  // Init endstops and pullups here
536
+  //
537
+  endstops.init();
1011 538
 
1012 539
   #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
1013 540
   #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
@@ -1083,17 +610,17 @@ void st_init() {
1083 610
     SBI(TIMSK0, OCIE0A);
1084 611
   #endif //ADVANCE
1085 612
 
1086
-  enable_endstops(true); // Start with endstops active. After homing they can be disabled
613
+  endstops.enable(true); // Start with endstops active. After homing they can be disabled
1087 614
   sei();
1088 615
 
1089
-  set_stepper_direction(); // Init directions to out_bits = 0
616
+  set_directions(); // Init directions to last_direction_bits = 0
1090 617
 }
1091 618
 
1092 619
 
1093 620
 /**
1094 621
  * Block until all buffered steps are executed
1095 622
  */
1096
-void st_synchronize() { while (blocks_queued()) idle(); }
623
+void Stepper::synchronize() { while (planner.blocks_queued()) idle(); }
1097 624
 
1098 625
 /**
1099 626
  * Set the stepper positions directly in steps
@@ -1101,10 +628,10 @@ void st_synchronize() { while (blocks_queued()) idle(); }
1101 628
  * The input is based on the typical per-axis XYZ steps.
1102 629
  * For CORE machines XYZ needs to be translated to ABC.
1103 630
  *
1104
- * This allows st_get_axis_position_mm to correctly
631
+ * This allows get_axis_position_mm to correctly
1105 632
  * derive the current XYZ position later on.
1106 633
  */
1107
-void st_set_position(const long& x, const long& y, const long& z, const long& e) {
634
+void Stepper::set_position(const long& x, const long& y, const long& z, const long& e) {
1108 635
   CRITICAL_SECTION_START;
1109 636
 
1110 637
   #if ENABLED(COREXY)
@@ -1129,7 +656,7 @@ void st_set_position(const long& x, const long& y, const long& z, const long& e)
1129 656
   CRITICAL_SECTION_END;
1130 657
 }
1131 658
 
1132
-void st_set_e_position(const long& e) {
659
+void Stepper::set_e_position(const long& e) {
1133 660
   CRITICAL_SECTION_START;
1134 661
   count_position[E_AXIS] = e;
1135 662
   CRITICAL_SECTION_END;
@@ -1138,7 +665,7 @@ void st_set_e_position(const long& e) {
1138 665
 /**
1139 666
  * Get a stepper's position in steps.
1140 667
  */
1141
-long st_get_position(AxisEnum axis) {
668
+long Stepper::position(AxisEnum axis) {
1142 669
   CRITICAL_SECTION_START;
1143 670
   long count_pos = count_position[axis];
1144 671
   CRITICAL_SECTION_END;
@@ -1149,7 +676,7 @@ long st_get_position(AxisEnum axis) {
1149 676
  * Get an axis position according to stepper position(s)
1150 677
  * For CORE machines apply translation from ABC to XYZ.
1151 678
  */
1152
-float st_get_axis_position_mm(AxisEnum axis) {
679
+float Stepper::get_axis_position_mm(AxisEnum axis) {
1153 680
   float axis_steps;
1154 681
   #if ENABLED(COREXY) | ENABLED(COREXZ)
1155 682
     if (axis == X_AXIS || axis == CORE_AXIS_2) {
@@ -1162,31 +689,82 @@ float st_get_axis_position_mm(AxisEnum axis) {
1162 689
       axis_steps = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
1163 690
     }
1164 691
     else
1165
-      axis_steps = st_get_position(axis);
692
+      axis_steps = position(axis);
1166 693
   #else
1167
-    axis_steps = st_get_position(axis);
694
+    axis_steps = position(axis);
1168 695
   #endif
1169
-  return axis_steps / axis_steps_per_unit[axis];
696
+  return axis_steps / planner.axis_steps_per_unit[axis];
1170 697
 }
1171 698
 
1172
-void finishAndDisableSteppers() {
1173
-  st_synchronize();
699
+void Stepper::finish_and_disable() {
700
+  synchronize();
1174 701
   disable_all_steppers();
1175 702
 }
1176 703
 
1177
-void quickStop() {
704
+void Stepper::quick_stop() {
1178 705
   cleaning_buffer_counter = 5000;
1179 706
   DISABLE_STEPPER_DRIVER_INTERRUPT();
1180
-  while (blocks_queued()) plan_discard_current_block();
707
+  while (planner.blocks_queued()) planner.discard_current_block();
1181 708
   current_block = NULL;
1182 709
   ENABLE_STEPPER_DRIVER_INTERRUPT();
1183 710
 }
1184 711
 
712
+void Stepper::endstop_triggered(AxisEnum axis) {
713
+
714
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
715
+
716
+    float axis_pos = count_position[axis];
717
+    if (axis == A_AXIS)
718
+      axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2;
719
+    else if (axis == CORE_AXIS_2)
720
+      axis_pos = (count_position[A_AXIS] - axis_pos) / 2;
721
+    endstops_trigsteps[axis] = axis_pos;
722
+
723
+  #else // !COREXY && !COREXZ
724
+
725
+    endstops_trigsteps[axis] = count_position[axis];
726
+
727
+  #endif // !COREXY && !COREXZ
728
+
729
+  kill_current_block();
730
+}
731
+
732
+void Stepper::report_positions() {
733
+  CRITICAL_SECTION_START;
734
+  long xpos = count_position[X_AXIS],
735
+       ypos = count_position[Y_AXIS],
736
+       zpos = count_position[Z_AXIS];
737
+  CRITICAL_SECTION_END;
738
+
739
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
740
+    SERIAL_PROTOCOLPGM(MSG_COUNT_A);
741
+  #else
742
+    SERIAL_PROTOCOLPGM(MSG_COUNT_X);
743
+  #endif
744
+  SERIAL_PROTOCOL(xpos);
745
+
746
+  #if ENABLED(COREXY) || ENABLED(COREXZ)
747
+    SERIAL_PROTOCOLPGM(" B:");
748
+  #else
749
+    SERIAL_PROTOCOLPGM(" Y:");
750
+  #endif
751
+  SERIAL_PROTOCOL(ypos);
752
+
753
+  #if ENABLED(COREXZ) || ENABLED(COREXZ)
754
+    SERIAL_PROTOCOLPGM(" C:");
755
+  #else
756
+    SERIAL_PROTOCOLPGM(" Z:");
757
+  #endif
758
+  SERIAL_PROTOCOL(zpos);
759
+
760
+  SERIAL_EOL;
761
+}
762
+
1185 763
 #if ENABLED(BABYSTEPPING)
1186 764
 
1187 765
   // MUST ONLY BE CALLED BY AN ISR,
1188 766
   // No other ISR should ever interrupt this!
1189
-  void babystep(const uint8_t axis, const bool direction) {
767
+  void Stepper::babystep(const uint8_t axis, const bool direction) {
1190 768
 
1191 769
     #define _ENABLE(axis) enable_## axis()
1192 770
     #define _READ_DIR(AXIS) AXIS ##_DIR_READ
@@ -1256,10 +834,14 @@ void quickStop() {
1256 834
 
1257 835
 #endif //BABYSTEPPING
1258 836
 
837
+/**
838
+ * Software-controlled Stepper Motor Current
839
+ */
840
+
1259 841
 #if HAS_DIGIPOTSS
1260 842
 
1261 843
   // From Arduino DigitalPotControl example
1262
-  void digitalPotWrite(int address, int value) {
844
+  void Stepper::digitalPotWrite(int address, int value) {
1263 845
     digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
1264 846
     SPI.transfer(address); //  send in the address and value via SPI:
1265 847
     SPI.transfer(value);
@@ -1269,8 +851,7 @@ void quickStop() {
1269 851
 
1270 852
 #endif //HAS_DIGIPOTSS
1271 853
 
1272
-// Initialize Digipot Motor Current
1273
-void digipot_init() {
854
+void Stepper::digipot_init() {
1274 855
   #if HAS_DIGIPOTSS
1275 856
     const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
1276 857
 
@@ -1299,7 +880,7 @@ void digipot_init() {
1299 880
   #endif
1300 881
 }
1301 882
 
1302
-void digipot_current(uint8_t driver, int current) {
883
+void Stepper::digipot_current(uint8_t driver, int current) {
1303 884
   #if HAS_DIGIPOTSS
1304 885
     const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
1305 886
     digitalPotWrite(digipot_ch[driver], current);
@@ -1322,7 +903,7 @@ void digipot_current(uint8_t driver, int current) {
1322 903
   #endif
1323 904
 }
1324 905
 
1325
-void microstep_init() {
906
+void Stepper::microstep_init() {
1326 907
   #if HAS_MICROSTEPS_E1
1327 908
     pinMode(E1_MS1_PIN, OUTPUT);
1328 909
     pinMode(E1_MS2_PIN, OUTPUT);
@@ -1343,7 +924,11 @@ void microstep_init() {
1343 924
   #endif
1344 925
 }
1345 926
 
1346
-void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
927
+/**
928
+ * Software-controlled Microstepping
929
+ */
930
+
931
+void Stepper::microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
1347 932
   if (ms1 >= 0) switch (driver) {
1348 933
     case 0: digitalWrite(X_MS1_PIN, ms1); break;
1349 934
     case 1: digitalWrite(Y_MS1_PIN, ms1); break;
@@ -1364,7 +949,7 @@ void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
1364 949
   }
1365 950
 }
1366 951
 
1367
-void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
952
+void Stepper::microstep_mode(uint8_t driver, uint8_t stepping_mode) {
1368 953
   switch (stepping_mode) {
1369 954
     case 1: microstep_ms(driver, MICROSTEP1); break;
1370 955
     case 2: microstep_ms(driver, MICROSTEP2); break;
@@ -1374,7 +959,7 @@ void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
1374 959
   }
1375 960
 }
1376 961
 
1377
-void microstep_readings() {
962
+void Stepper::microstep_readings() {
1378 963
   SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
1379 964
   SERIAL_PROTOCOLPGM("X: ");
1380 965
   SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
@@ -1396,7 +981,7 @@ void microstep_readings() {
1396 981
 }
1397 982
 
1398 983
 #if ENABLED(Z_DUAL_ENDSTOPS)
1399
-  void In_Homing_Process(bool state) { performing_homing = state; }
1400
-  void Lock_z_motor(bool state) { locked_z_motor = state; }
1401
-  void Lock_z2_motor(bool state) { locked_z2_motor = state; }
984
+  void Stepper::set_homing_flag(bool state) { performing_homing = state; }
985
+  void Stepper::set_z_lock(bool state) { locked_z_motor = state; }
986
+  void Stepper::set_z2_lock(bool state) { locked_z2_motor = state; }
1402 987
 #endif

+ 285
- 62
Marlin/stepper.h View File

@@ -21,90 +21,313 @@
21 21
  */
22 22
 
23 23
 /**
24
-  stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
25
-  Part of Grbl
24
+ * stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
25
+ * Part of Grbl
26
+ *
27
+ * Copyright (c) 2009-2011 Simen Svale Skogsrud
28
+ *
29
+ * Grbl is free software: you can redistribute it and/or modify
30
+ * it under the terms of the GNU General Public License as published by
31
+ * the Free Software Foundation, either version 3 of the License, or
32
+ * (at your option) any later version.
33
+ *
34
+ * Grbl is distributed in the hope that it will be useful,
35
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
+ * GNU General Public License for more details.
38
+ *
39
+ * You should have received a copy of the GNU General Public License
40
+ * along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
41
+ */
26 42
 
27
-  Copyright (c) 2009-2011 Simen Svale Skogsrud
43
+#ifndef STEPPER_H
44
+#define STEPPER_H
28 45
 
29
-  Grbl is free software: you can redistribute it and/or modify
30
-  it under the terms of the GNU General Public License as published by
31
-  the Free Software Foundation, either version 3 of the License, or
32
-  (at your option) any later version.
46
+#include "planner.h"
47
+#include "speed_lookuptable.h"
48
+#include "stepper_indirection.h"
49
+#include "language.h"
33 50
 
34
-  Grbl is distributed in the hope that it will be useful,
35
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
36
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
-  GNU General Public License for more details.
51
+class Stepper;
52
+extern Stepper stepper;
38 53
 
39
-  You should have received a copy of the GNU General Public License
40
-  along with Grbl.  If not, see <http://www.gnu.org/licenses/>.
41
-*/
54
+// intRes = intIn1 * intIn2 >> 16
55
+// uses:
56
+// r26 to store 0
57
+// r27 to store the byte 1 of the 24 bit result
58
+#define MultiU16X8toH16(intRes, charIn1, intIn2) \
59
+  asm volatile ( \
60
+                 "clr r26 \n\t" \
61
+                 "mul %A1, %B2 \n\t" \
62
+                 "movw %A0, r0 \n\t" \
63
+                 "mul %A1, %A2 \n\t" \
64
+                 "add %A0, r1 \n\t" \
65
+                 "adc %B0, r26 \n\t" \
66
+                 "lsr r0 \n\t" \
67
+                 "adc %A0, r26 \n\t" \
68
+                 "adc %B0, r26 \n\t" \
69
+                 "clr r1 \n\t" \
70
+                 : \
71
+                 "=&r" (intRes) \
72
+                 : \
73
+                 "d" (charIn1), \
74
+                 "d" (intIn2) \
75
+                 : \
76
+                 "r26" \
77
+               )
42 78
 
43
-#ifndef stepper_h
44
-#define stepper_h
79
+class Stepper {
45 80
 
46
-#include "planner.h"
47
-#include "stepper_indirection.h"
81
+  public:
82
+
83
+    block_t* current_block = NULL;  // A pointer to the block currently being traced
84
+
85
+    #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
86
+      bool abort_on_endstop_hit = false;
87
+    #endif
88
+
89
+    #if ENABLED(Z_DUAL_ENDSTOPS)
90
+      bool performing_homing = false;
91
+    #endif
92
+
93
+    #if ENABLED(ADVANCE)
94
+      long e_steps[4];
95
+    #endif
96
+
97
+  private:
98
+
99
+    unsigned char last_direction_bits = 0;        // The next stepping-bits to be output
100
+    unsigned int cleaning_buffer_counter = 0;
101
+
102
+    #if ENABLED(Z_DUAL_ENDSTOPS)
103
+      bool locked_z_motor = false,
104
+           locked_z2_motor = false;
105
+    #endif
106
+
107
+    // Counter variables for the Bresenham line tracer
108
+    long counter_X = 0, counter_Y = 0, counter_Z = 0, counter_E = 0;
109
+    volatile unsigned long step_events_completed = 0; // The number of step events executed in the current block
110
+
111
+    #if ENABLED(ADVANCE)
112
+      unsigned char old_OCR0A;
113
+      long advance_rate, advance, final_advance = 0;
114
+      long old_advance = 0;
115
+    #endif
116
+
117
+    long acceleration_time, deceleration_time;
118
+    //unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
119
+    unsigned short acc_step_rate; // needed for deceleration start point
120
+    uint8_t step_loops;
121
+    uint8_t step_loops_nominal;
122
+    unsigned short OCR1A_nominal;
123
+
124
+    volatile long endstops_trigsteps[3];
125
+    volatile long endstops_stepsTotal, endstops_stepsDone;
126
+
127
+    #if HAS_MOTOR_CURRENT_PWM
128
+      #ifndef PWM_MOTOR_CURRENT
129
+        #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
130
+      #endif
131
+      const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
132
+    #endif
133
+
134
+    //
135
+    // Positions of stepper motors, in step units
136
+    //
137
+    volatile long count_position[NUM_AXIS] = { 0 };
138
+
139
+    //
140
+    // Current direction of stepper motors (+1 or -1)
141
+    //
142
+    volatile signed char count_direction[NUM_AXIS] = { 1 };
143
+
144
+  public:
145
+
146
+    //
147
+    // Constructor / initializer
148
+    //
149
+    Stepper() {};
150
+
151
+    //
152
+    // Initialize stepper hardware
153
+    //
154
+    void init();
155
+
156
+    //
157
+    // Interrupt Service Routines
158
+    //
159
+
160
+    void isr();
161
+
162
+    #if ENABLED(ADVANCE)
163
+      void advance_isr();
164
+    #endif
165
+
166
+    //
167
+    // Block until all buffered steps are executed
168
+    //
169
+    void synchronize();
170
+
171
+    //
172
+    // Set the current position in steps
173
+    //
174
+    void set_position(const long& x, const long& y, const long& z, const long& e);
175
+    void set_e_position(const long& e);
176
+
177
+    //
178
+    // Set direction bits for all steppers
179
+    //
180
+    void set_directions();
181
+
182
+    //
183
+    // Get the position of a stepper, in steps
184
+    //
185
+    long position(AxisEnum axis);
186
+
187
+    //
188
+    // Report the positions of the steppers, in steps
189
+    //
190
+    void report_positions();
191
+
192
+    //
193
+    // Get the position (mm) of an axis based on stepper position(s)
194
+    //
195
+    float get_axis_position_mm(AxisEnum axis);
196
+
197
+    //
198
+    // The stepper subsystem goes to sleep when it runs out of things to execute. Call this
199
+    // to notify the subsystem that it is time to go to work.
200
+    //
201
+    void wake_up();
202
+
203
+    //
204
+    // Wait for moves to finish and disable all steppers
205
+    //
206
+    void finish_and_disable();
207
+
208
+    //
209
+    // Quickly stop all steppers and clear the blocks queue
210
+    //
211
+    void quick_stop();
48 212
 
49
-#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
50
-  extern bool abort_on_endstop_hit;
51
-#endif
213
+    //
214
+    // The direction of a single motor
215
+    //
216
+    FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
52 217
 
53
-// Initialize and start the stepper motor subsystem
54
-void st_init();
218
+    #if HAS_DIGIPOTSS
219
+      void digitalPotWrite(int address, int value);
220
+    #endif
221
+    void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
222
+    void digipot_current(uint8_t driver, int current);
223
+    void microstep_readings();
55 224
 
56
-// Block until all buffered steps are executed
57
-void st_synchronize();
225
+    #if ENABLED(Z_DUAL_ENDSTOPS)
226
+      void set_homing_flag(bool state);
227
+      void set_z_lock(bool state);
228
+      void set_z2_lock(bool state);
229
+    #endif
58 230
 
59
-// Set current position in steps
60
-void st_set_position(const long& x, const long& y, const long& z, const long& e);
61
-void st_set_e_position(const long& e);
231
+    #if ENABLED(BABYSTEPPING)
232
+      void babystep(const uint8_t axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
233
+    #endif
62 234
 
63
-// Get current position in steps
64
-long st_get_position(AxisEnum axis);
235
+    inline void kill_current_block() {
236
+      step_events_completed = current_block->step_event_count;
237
+    }
65 238
 
66
-// Get current axis position in mm
67
-float st_get_axis_position_mm(AxisEnum axis);
239
+    //
240
+    // Handle a triggered endstop
241
+    //
242
+    void endstop_triggered(AxisEnum axis);
68 243
 
69
-// The stepper subsystem goes to sleep when it runs out of things to execute. Call this
70
-// to notify the subsystem that it is time to go to work.
71
-void st_wake_up();
244
+    //
245
+    // Triggered position of an axis in mm (not core-savvy)
246
+    //
247
+    FORCE_INLINE float triggered_position_mm(AxisEnum axis) {
248
+      return endstops_trigsteps[axis] / planner.axis_steps_per_unit[axis];
249
+    }
72 250
 
251
+    FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
252
+      unsigned short timer;
73 253
 
74
-void checkHitEndstops(); //call from somewhere to create an serial error message with the locations the endstops where hit, in case they were triggered
75
-void endstops_hit_on_purpose(); //avoid creation of the message, i.e. after homing and before a routine call of checkHitEndstops();
254
+      NOMORE(step_rate, MAX_STEP_FREQUENCY);
76 255
 
77
-void enable_endstops(bool check); // Enable/disable endstop checking
256
+      if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
257
+        step_rate = (step_rate >> 2) & 0x3fff;
258
+        step_loops = 4;
259
+      }
260
+      else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
261
+        step_rate = (step_rate >> 1) & 0x7fff;
262
+        step_loops = 2;
263
+      }
264
+      else {
265
+        step_loops = 1;
266
+      }
78 267
 
79
-void enable_endstops_globally(bool check);
80
-void endstops_not_homing();
268
+      NOLESS(step_rate, F_CPU / 500000);
269
+      step_rate -= F_CPU / 500000; // Correct for minimal speed
270
+      if (step_rate >= (8 * 256)) { // higher step rate
271
+        unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
272
+        unsigned char tmp_step_rate = (step_rate & 0x00ff);
273
+        unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
274
+        MultiU16X8toH16(timer, tmp_step_rate, gain);
275
+        timer = (unsigned short)pgm_read_word_near(table_address) - timer;
276
+      }
277
+      else { // lower step rates
278
+        unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
279
+        table_address += ((step_rate) >> 1) & 0xfffc;
280
+        timer = (unsigned short)pgm_read_word_near(table_address);
281
+        timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
282
+      }
283
+      if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
284
+      return timer;
285
+    }
81 286
 
82
-void checkStepperErrors(); //Print errors detected by the stepper
287
+    // Initializes the trapezoid generator from the current block. Called whenever a new
288
+    // block begins.
289
+    FORCE_INLINE void trapezoid_generator_reset() {
83 290
 
84
-void finishAndDisableSteppers();
291
+      static int8_t last_extruder = -1;
85 292
 
86
-extern block_t* current_block;  // A pointer to the block currently being traced
293
+      if (current_block->direction_bits != last_direction_bits || current_block->active_extruder != last_extruder) {
294
+        last_direction_bits = current_block->direction_bits;
295
+        last_extruder = current_block->active_extruder;
296
+        set_directions();
297
+      }
87 298
 
88
-void quickStop();
299
+      #if ENABLED(ADVANCE)
300
+        advance = current_block->initial_advance;
301
+        final_advance = current_block->final_advance;
302
+        // Do E steps + advance steps
303
+        e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
304
+        old_advance = advance >>8;
305
+      #endif
306
+      deceleration_time = 0;
307
+      // step_rate to timer interval
308
+      OCR1A_nominal = calc_timer(current_block->nominal_rate);
309
+      // make a note of the number of step loops required at nominal speed
310
+      step_loops_nominal = step_loops;
311
+      acc_step_rate = current_block->initial_rate;
312
+      acceleration_time = calc_timer(acc_step_rate);
313
+      OCR1A = acceleration_time;
89 314
 
90
-#if HAS_DIGIPOTSS
91
-  void digitalPotWrite(int address, int value);
92
-#endif
93
-void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2);
94
-void microstep_mode(uint8_t driver, uint8_t stepping);
95
-void digipot_init();
96
-void digipot_current(uint8_t driver, int current);
97
-void microstep_init();
98
-void microstep_readings();
315
+      // SERIAL_ECHO_START;
316
+      // SERIAL_ECHOPGM("advance :");
317
+      // SERIAL_ECHO(current_block->advance/256.0);
318
+      // SERIAL_ECHOPGM("advance rate :");
319
+      // SERIAL_ECHO(current_block->advance_rate/256.0);
320
+      // SERIAL_ECHOPGM("initial advance :");
321
+      // SERIAL_ECHO(current_block->initial_advance/256.0);
322
+      // SERIAL_ECHOPGM("final advance :");
323
+      // SERIAL_ECHOLN(current_block->final_advance/256.0);
324
+    }
99 325
 
100
-#if ENABLED(Z_DUAL_ENDSTOPS)
101
-  void In_Homing_Process(bool state);
102
-  void Lock_z_motor(bool state);
103
-  void Lock_z2_motor(bool state);
104
-#endif
326
+  private:
327
+    void microstep_mode(uint8_t driver, uint8_t stepping);
328
+    void digipot_init();
329
+    void microstep_init();
105 330
 
106
-#if ENABLED(BABYSTEPPING)
107
-  void babystep(const uint8_t axis, const bool direction); // perform a short step with a single stepper motor, outside of any convention
108
-#endif
331
+};
109 332
 
110
-#endif
333
+#endif // STEPPER_H

+ 2
- 2
Marlin/temperature.cpp View File

@@ -604,7 +604,7 @@ float get_pid_output(int e) {
604 604
         #if ENABLED(PID_ADD_EXTRUSION_RATE)
605 605
           cTerm[e] = 0;
606 606
           if (e == active_extruder) {
607
-            long e_position = st_get_position(E_AXIS);
607
+            long e_position = stepper.position(E_AXIS);
608 608
             if (e_position > last_position[e]) {
609 609
               lpq[lpq_ptr++] = e_position - last_position[e];
610 610
               last_position[e] = e_position;
@@ -613,7 +613,7 @@ float get_pid_output(int e) {
613 613
               lpq[lpq_ptr++] = 0;
614 614
             }
615 615
             if (lpq_ptr >= lpq_len) lpq_ptr = 0;
616
-            cTerm[e] = (lpq[lpq_ptr] / axis_steps_per_unit[E_AXIS]) * PID_PARAM(Kc, e);
616
+            cTerm[e] = (lpq[lpq_ptr] / planner.axis_steps_per_unit[E_AXIS]) * PID_PARAM(Kc, e);
617 617
             pid_output += cTerm[e];
618 618
           }
619 619
         #endif //PID_ADD_EXTRUSION_RATE

+ 7
- 3
Marlin/temperature.h View File

@@ -79,6 +79,10 @@ extern float current_temperature_bed;
79 79
   extern unsigned char soft_pwm_bed;
80 80
 #endif
81 81
 
82
+#if ENABLED(FAN_SOFT_PWM)
83
+  extern unsigned char fanSpeedSoftPwm[FAN_COUNT];
84
+#endif
85
+
82 86
 #if ENABLED(PIDTEMP)
83 87
 
84 88
   #if ENABLED(PID_PARAMS_PER_EXTRUDER)
@@ -178,9 +182,9 @@ void checkExtruderAutoFans();
178 182
 
179 183
 FORCE_INLINE void autotempShutdown() {
180 184
   #if ENABLED(AUTOTEMP)
181
-    if (autotemp_enabled) {
182
-      autotemp_enabled = false;
183
-      if (degTargetHotend(active_extruder) > autotemp_min)
185
+    if (planner.autotemp_enabled) {
186
+      planner.autotemp_enabled = false;
187
+      if (degTargetHotend(active_extruder) > planner.autotemp_min)
184 188
         setTargetHotend(0, active_extruder);
185 189
     }
186 190
   #endif

+ 38
- 36
Marlin/ultralcd.cpp View File

@@ -463,9 +463,9 @@ static void lcd_status_screen() {
463 463
 inline void line_to_current(AxisEnum axis) {
464 464
   #if ENABLED(DELTA)
465 465
     calculate_delta(current_position);
466
-    plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
466
+    planner.buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
467 467
   #else
468
-    plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
468
+    planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], manual_feedrate[axis]/60, active_extruder);
469 469
   #endif
470 470
 }
471 471
 
@@ -476,7 +476,7 @@ inline void line_to_current(AxisEnum axis) {
476 476
   static void lcd_sdcard_resume() { card.startFileprint(); }
477 477
 
478 478
   static void lcd_sdcard_stop() {
479
-    quickStop();
479
+    stepper.quick_stop();
480 480
     card.sdprinting = false;
481 481
     card.closefile();
482 482
     autotempShutdown();
@@ -495,7 +495,7 @@ inline void line_to_current(AxisEnum axis) {
495 495
 static void lcd_main_menu() {
496 496
   START_MENU();
497 497
   MENU_ITEM(back, MSG_WATCH);
498
-  if (movesplanned() || IS_SD_PRINTING) {
498
+  if (planner.movesplanned() || IS_SD_PRINTING) {
499 499
     MENU_ITEM(submenu, MSG_TUNE, lcd_tune_menu);
500 500
   }
501 501
   else {
@@ -911,7 +911,7 @@ void lcd_cooldown() {
911 911
       current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
912 912
       line_to_current(Z_AXIS);
913 913
     #endif
914
-    st_synchronize();
914
+    stepper.synchronize();
915 915
   }
916 916
 
917 917
   static void _lcd_level_goto_next_point();
@@ -934,7 +934,7 @@ void lcd_cooldown() {
934 934
     ENCODER_DIRECTION_NORMAL();
935 935
 
936 936
     // Encoder wheel adjusts the Z position
937
-    if (encoderPosition && movesplanned() <= 3) {
937
+    if (encoderPosition && planner.movesplanned() <= 3) {
938 938
       refresh_cmd_timeout();
939 939
       current_position[Z_AXIS] += float((int32_t)encoderPosition) * (MBL_Z_STEP);
940 940
       NOLESS(current_position[Z_AXIS], 0);
@@ -964,7 +964,7 @@ void lcd_cooldown() {
964 964
             #endif
965 965
           ;
966 966
           line_to_current(Z_AXIS);
967
-          st_synchronize();
967
+          stepper.synchronize();
968 968
 
969 969
           mbl.active = true;
970 970
           enqueue_and_echo_commands_P(PSTR("G28"));
@@ -1037,7 +1037,7 @@ void lcd_cooldown() {
1037 1037
     if (LCD_CLICKED) {
1038 1038
       _lcd_level_bed_position = 0;
1039 1039
       current_position[Z_AXIS] = MESH_HOME_SEARCH_Z;
1040
-      plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1040
+      planner.set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1041 1041
       lcd_goto_menu(_lcd_level_goto_next_point, true);
1042 1042
     }
1043 1043
   }
@@ -1191,7 +1191,7 @@ float move_menu_scale;
1191 1191
 
1192 1192
 static void _lcd_move(const char* name, AxisEnum axis, float min, float max) {
1193 1193
   ENCODER_DIRECTION_NORMAL();
1194
-  if (encoderPosition && movesplanned() <= 3) {
1194
+  if (encoderPosition && planner.movesplanned() <= 3) {
1195 1195
     refresh_cmd_timeout();
1196 1196
     current_position[axis] += float((int32_t)encoderPosition) * move_menu_scale;
1197 1197
     if (min_software_endstops) NOLESS(current_position[axis], min);
@@ -1223,7 +1223,7 @@ static void lcd_move_e(
1223 1223
     unsigned short original_active_extruder = active_extruder;
1224 1224
     active_extruder = e;
1225 1225
   #endif
1226
-  if (encoderPosition && movesplanned() <= 3) {
1226
+  if (encoderPosition && planner.movesplanned() <= 3) {
1227 1227
     current_position[E_AXIS] += float((int32_t)encoderPosition) * move_menu_scale;
1228 1228
     line_to_current(E_AXIS);
1229 1229
     lcdDrawUpdate = LCDVIEW_REDRAW_NOW;
@@ -1511,10 +1511,10 @@ static void lcd_control_temperature_menu() {
1511 1511
   // Autotemp, Min, Max, Fact
1512 1512
   //
1513 1513
   #if ENABLED(AUTOTEMP) && (TEMP_SENSOR_0 != 0)
1514
-    MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &autotemp_enabled);
1515
-    MENU_ITEM_EDIT(float3, MSG_MIN, &autotemp_min, 0, HEATER_0_MAXTEMP - 15);
1516
-    MENU_ITEM_EDIT(float3, MSG_MAX, &autotemp_max, 0, HEATER_0_MAXTEMP - 15);
1517
-    MENU_ITEM_EDIT(float32, MSG_FACTOR, &autotemp_factor, 0.0, 1.0);
1514
+    MENU_ITEM_EDIT(bool, MSG_AUTOTEMP, &planner.autotemp_enabled);
1515
+    MENU_ITEM_EDIT(float3, MSG_MIN, &planner.autotemp_min, 0, HEATER_0_MAXTEMP - 15);
1516
+    MENU_ITEM_EDIT(float3, MSG_MAX, &planner.autotemp_max, 0, HEATER_0_MAXTEMP - 15);
1517
+    MENU_ITEM_EDIT(float32, MSG_FACTOR, &planner.autotemp_factor, 0.0, 1.0);
1518 1518
   #endif
1519 1519
 
1520 1520
   //
@@ -1618,6 +1618,8 @@ static void lcd_control_temperature_preheat_abs_settings_menu() {
1618 1618
   END_MENU();
1619 1619
 }
1620 1620
 
1621
+static void _reset_acceleration_rates() { planner.reset_acceleration_rates(); }
1622
+
1621 1623
 /**
1622 1624
  *
1623 1625
  * "Control" > "Motion" submenu
@@ -1633,34 +1635,34 @@ static void lcd_control_motion_menu() {
1633 1635
   #if ENABLED(MANUAL_BED_LEVELING)
1634 1636
     MENU_ITEM_EDIT(float43, MSG_BED_Z, &mbl.z_offset, -1, 1);
1635 1637
   #endif
1636
-  MENU_ITEM_EDIT(float5, MSG_ACC, &acceleration, 10, 99000);
1637
-  MENU_ITEM_EDIT(float3, MSG_VXY_JERK, &max_xy_jerk, 1, 990);
1638
+  MENU_ITEM_EDIT(float5, MSG_ACC, &planner.acceleration, 10, 99000);
1639
+  MENU_ITEM_EDIT(float3, MSG_VXY_JERK, &planner.max_xy_jerk, 1, 990);
1638 1640
   #if ENABLED(DELTA)
1639
-    MENU_ITEM_EDIT(float3, MSG_VZ_JERK, &max_z_jerk, 1, 990);
1641
+    MENU_ITEM_EDIT(float3, MSG_VZ_JERK, &planner.max_z_jerk, 1, 990);
1640 1642
   #else
1641
-    MENU_ITEM_EDIT(float52, MSG_VZ_JERK, &max_z_jerk, 0.1, 990);
1643
+    MENU_ITEM_EDIT(float52, MSG_VZ_JERK, &planner.max_z_jerk, 0.1, 990);
1642 1644
   #endif
1643
-  MENU_ITEM_EDIT(float3, MSG_VE_JERK, &max_e_jerk, 1, 990);
1644
-  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_X, &max_feedrate[X_AXIS], 1, 999);
1645
-  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Y, &max_feedrate[Y_AXIS], 1, 999);
1646
-  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Z, &max_feedrate[Z_AXIS], 1, 999);
1647
-  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_E, &max_feedrate[E_AXIS], 1, 999);
1648
-  MENU_ITEM_EDIT(float3, MSG_VMIN, &minimumfeedrate, 0, 999);
1649
-  MENU_ITEM_EDIT(float3, MSG_VTRAV_MIN, &mintravelfeedrate, 0, 999);
1650
-  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_X, &max_acceleration_units_per_sq_second[X_AXIS], 100, 99000, reset_acceleration_rates);
1651
-  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Y, &max_acceleration_units_per_sq_second[Y_AXIS], 100, 99000, reset_acceleration_rates);
1652
-  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Z, &max_acceleration_units_per_sq_second[Z_AXIS], 10, 99000, reset_acceleration_rates);
1653
-  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_E, &max_acceleration_units_per_sq_second[E_AXIS], 100, 99000, reset_acceleration_rates);
1654
-  MENU_ITEM_EDIT(float5, MSG_A_RETRACT, &retract_acceleration, 100, 99000);
1655
-  MENU_ITEM_EDIT(float5, MSG_A_TRAVEL, &travel_acceleration, 100, 99000);
1656
-  MENU_ITEM_EDIT(float52, MSG_XSTEPS, &axis_steps_per_unit[X_AXIS], 5, 9999);
1657
-  MENU_ITEM_EDIT(float52, MSG_YSTEPS, &axis_steps_per_unit[Y_AXIS], 5, 9999);
1645
+  MENU_ITEM_EDIT(float3, MSG_VE_JERK, &planner.max_e_jerk, 1, 990);
1646
+  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_X, &planner.max_feedrate[X_AXIS], 1, 999);
1647
+  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Y, &planner.max_feedrate[Y_AXIS], 1, 999);
1648
+  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_Z, &planner.max_feedrate[Z_AXIS], 1, 999);
1649
+  MENU_ITEM_EDIT(float3, MSG_VMAX MSG_E, &planner.max_feedrate[E_AXIS], 1, 999);
1650
+  MENU_ITEM_EDIT(float3, MSG_VMIN, &planner.min_feedrate, 0, 999);
1651
+  MENU_ITEM_EDIT(float3, MSG_VTRAV_MIN, &planner.min_travel_feedrate, 0, 999);
1652
+  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_X, &planner.max_acceleration_units_per_sq_second[X_AXIS], 100, 99000, _reset_acceleration_rates);
1653
+  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Y, &planner.max_acceleration_units_per_sq_second[Y_AXIS], 100, 99000, _reset_acceleration_rates);
1654
+  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_Z, &planner.max_acceleration_units_per_sq_second[Z_AXIS], 10, 99000, _reset_acceleration_rates);
1655
+  MENU_ITEM_EDIT_CALLBACK(long5, MSG_AMAX MSG_E, &planner.max_acceleration_units_per_sq_second[E_AXIS], 100, 99000, _reset_acceleration_rates);
1656
+  MENU_ITEM_EDIT(float5, MSG_A_RETRACT, &planner.retract_acceleration, 100, 99000);
1657
+  MENU_ITEM_EDIT(float5, MSG_A_TRAVEL, &planner.travel_acceleration, 100, 99000);
1658
+  MENU_ITEM_EDIT(float52, MSG_XSTEPS, &planner.axis_steps_per_unit[X_AXIS], 5, 9999);
1659
+  MENU_ITEM_EDIT(float52, MSG_YSTEPS, &planner.axis_steps_per_unit[Y_AXIS], 5, 9999);
1658 1660
   #if ENABLED(DELTA)
1659
-    MENU_ITEM_EDIT(float52, MSG_ZSTEPS, &axis_steps_per_unit[Z_AXIS], 5, 9999);
1661
+    MENU_ITEM_EDIT(float52, MSG_ZSTEPS, &planner.axis_steps_per_unit[Z_AXIS], 5, 9999);
1660 1662
   #else
1661
-    MENU_ITEM_EDIT(float51, MSG_ZSTEPS, &axis_steps_per_unit[Z_AXIS], 5, 9999);
1663
+    MENU_ITEM_EDIT(float51, MSG_ZSTEPS, &planner.axis_steps_per_unit[Z_AXIS], 5, 9999);
1662 1664
   #endif
1663
-  MENU_ITEM_EDIT(float51, MSG_ESTEPS, &axis_steps_per_unit[E_AXIS], 5, 9999);
1665
+  MENU_ITEM_EDIT(float51, MSG_ESTEPS, &planner.axis_steps_per_unit[E_AXIS], 5, 9999);
1664 1666
   #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
1665 1667
     MENU_ITEM_EDIT(bool, MSG_ENDSTOP_ABORT, &abort_on_endstop_hit);
1666 1668
   #endif

Loading…
Cancel
Save