Bläddra i källkod

Keep Stepper encapsulation, use static data and methods

Scott Lahteine 8 år sedan
förälder
incheckning
668d50f68e
2 ändrade filer med 83 tillägg och 32 borttagningar
  1. 52
    2
      Marlin/stepper.cpp
  2. 31
    30
      Marlin/stepper.h

+ 52
- 2
Marlin/stepper.cpp Visa fil

60
 
60
 
61
 Stepper stepper; // Singleton
61
 Stepper stepper; // Singleton
62
 
62
 
63
+// public:
64
+
65
+block_t* Stepper::current_block = NULL;  // A pointer to the block currently being traced
66
+
67
+#if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
68
+  bool Stepper::abort_on_endstop_hit = false;
69
+#endif
70
+
71
+#if ENABLED(Z_DUAL_ENDSTOPS)
72
+  bool Stepper::performing_homing = false;
73
+#endif
74
+
75
+// private:
76
+
77
+unsigned char Stepper::last_direction_bits = 0;        // The next stepping-bits to be output
78
+unsigned int Stepper::cleaning_buffer_counter = 0;
79
+
80
+#if ENABLED(Z_DUAL_ENDSTOPS)
81
+  bool Stepper::locked_z_motor = false;
82
+  bool Stepper::locked_z2_motor = false;
83
+#endif
84
+
85
+long  Stepper::counter_X = 0,
86
+      Stepper::counter_Y = 0,
87
+      Stepper::counter_Z = 0,
88
+      Stepper::counter_E = 0;
89
+
90
+volatile unsigned long Stepper::step_events_completed = 0; // The number of step events executed in the current block
91
+
92
+#if ENABLED(ADVANCE)
93
+  unsigned char Stepper::old_OCR0A;
94
+  long  Stepper::final_advance = 0,
95
+        Stepper::old_advance = 0,
96
+        Stepper::e_steps[4],
97
+        Stepper::advance_rate,
98
+        Stepper::advance;
99
+#endif
100
+
101
+long Stepper::acceleration_time, Stepper::deceleration_time;
102
+
103
+volatile long Stepper::count_position[NUM_AXIS] = { 0 };
104
+volatile signed char Stepper::count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
105
+
106
+unsigned short Stepper::acc_step_rate; // needed for deceleration start point
107
+uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
108
+unsigned short Stepper::OCR1A_nominal;
109
+
110
+volatile long Stepper::endstops_trigsteps[3];
111
+
63
 #if ENABLED(DUAL_X_CARRIAGE)
112
 #if ENABLED(DUAL_X_CARRIAGE)
64
   #define X_APPLY_DIR(v,ALWAYS) \
113
   #define X_APPLY_DIR(v,ALWAYS) \
65
     if (extruder_duplication_enabled || ALWAYS) { \
114
     if (extruder_duplication_enabled || ALWAYS) { \
238
 
287
 
239
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
288
 // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
240
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
289
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
241
-ISR(TIMER1_COMPA_vect) { stepper.isr(); }
290
+ISR(TIMER1_COMPA_vect) { Stepper::isr(); }
242
 
291
 
243
 void Stepper::isr() {
292
 void Stepper::isr() {
244
   if (cleaning_buffer_counter) {
293
   if (cleaning_buffer_counter) {
405
 #if ENABLED(ADVANCE)
454
 #if ENABLED(ADVANCE)
406
   // Timer interrupt for E. e_steps is set in the main routine;
455
   // Timer interrupt for E. e_steps is set in the main routine;
407
   // Timer 0 is shared with millies
456
   // Timer 0 is shared with millies
408
-  ISR(TIMER0_COMPA_vect) { stepper.advance_isr(); }
457
+  ISR(TIMER0_COMPA_vect) { Stepper::advance_isr(); }
409
 
458
 
410
   void Stepper::advance_isr() {
459
   void Stepper::advance_isr() {
411
     old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
460
     old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
443
 #endif // ADVANCE
492
 #endif // ADVANCE
444
 
493
 
445
 void Stepper::init() {
494
 void Stepper::init() {
495
+
446
   digipot_init(); //Initialize Digipot Motor Current
496
   digipot_init(); //Initialize Digipot Motor Current
447
   microstep_init(); //Initialize Microstepping Pins
497
   microstep_init(); //Initialize Microstepping Pins
448
 
498
 

+ 31
- 30
Marlin/stepper.h Visa fil

80
 
80
 
81
   public:
81
   public:
82
 
82
 
83
-    block_t* current_block = NULL;  // A pointer to the block currently being traced
83
+    static block_t* current_block;  // A pointer to the block currently being traced
84
 
84
 
85
     #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
85
     #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
86
-      bool abort_on_endstop_hit = false;
86
+      static bool abort_on_endstop_hit;
87
     #endif
87
     #endif
88
 
88
 
89
     #if ENABLED(Z_DUAL_ENDSTOPS)
89
     #if ENABLED(Z_DUAL_ENDSTOPS)
90
-      bool performing_homing = false;
90
+      static bool performing_homing;
91
     #endif
91
     #endif
92
 
92
 
93
     #if ENABLED(ADVANCE)
93
     #if ENABLED(ADVANCE)
94
-      long e_steps[4];
94
+      static long e_steps[4];
95
     #endif
95
     #endif
96
 
96
 
97
   private:
97
   private:
98
 
98
 
99
-    unsigned char last_direction_bits = 0;        // The next stepping-bits to be output
100
-    unsigned int cleaning_buffer_counter = 0;
99
+    static unsigned char last_direction_bits;        // The next stepping-bits to be output
100
+    static unsigned int cleaning_buffer_counter;
101
 
101
 
102
     #if ENABLED(Z_DUAL_ENDSTOPS)
102
     #if ENABLED(Z_DUAL_ENDSTOPS)
103
-      bool locked_z_motor = false,
104
-           locked_z2_motor = false;
103
+      static bool locked_z_motor, locked_z2_motor;
105
     #endif
104
     #endif
106
 
105
 
107
     // Counter variables for the Bresenham line tracer
106
     // 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
107
+    static long counter_X, counter_Y, counter_Z, counter_E;
108
+    static volatile unsigned long step_events_completed; // The number of step events executed in the current block
110
 
109
 
111
     #if ENABLED(ADVANCE)
110
     #if ENABLED(ADVANCE)
112
-      unsigned char old_OCR0A;
113
-      long advance_rate, advance, final_advance = 0;
114
-      long old_advance = 0;
111
+      static unsigned char old_OCR0A;
112
+      static long advance_rate, advance, old_advance, final_advance;
115
     #endif
113
     #endif
116
 
114
 
117
-    long acceleration_time, deceleration_time;
115
+    static long acceleration_time, deceleration_time;
118
     //unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
116
     //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;
117
+    static unsigned short acc_step_rate; // needed for deceleration start point
118
+    static uint8_t step_loops, step_loops_nominal;
119
+    static unsigned short OCR1A_nominal;
123
 
120
 
124
-    volatile long endstops_trigsteps[3];
125
-    volatile long endstops_stepsTotal, endstops_stepsDone;
121
+    static volatile long endstops_trigsteps[3];
122
+    static volatile long endstops_stepsTotal, endstops_stepsDone;
126
 
123
 
127
     #if HAS_MOTOR_CURRENT_PWM
124
     #if HAS_MOTOR_CURRENT_PWM
128
       #ifndef PWM_MOTOR_CURRENT
125
       #ifndef PWM_MOTOR_CURRENT
134
     //
131
     //
135
     // Positions of stepper motors, in step units
132
     // Positions of stepper motors, in step units
136
     //
133
     //
137
-    volatile long count_position[NUM_AXIS] = { 0 };
134
+    static volatile long count_position[NUM_AXIS];
138
 
135
 
139
     //
136
     //
140
     // Current direction of stepper motors (+1 or -1)
137
     // Current direction of stepper motors (+1 or -1)
141
     //
138
     //
142
-    volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
139
+    static volatile signed char count_direction[NUM_AXIS];
143
 
140
 
144
   public:
141
   public:
145
 
142
 
146
     //
143
     //
147
     // Constructor / initializer
144
     // Constructor / initializer
148
     //
145
     //
149
-    Stepper() {};
146
+    Stepper() { };
150
 
147
 
151
     //
148
     //
152
     // Initialize stepper hardware
149
     // Initialize stepper hardware
157
     // Interrupt Service Routines
154
     // Interrupt Service Routines
158
     //
155
     //
159
 
156
 
160
-    void isr();
157
+    static void isr();
161
 
158
 
162
     #if ENABLED(ADVANCE)
159
     #if ENABLED(ADVANCE)
163
-      void advance_isr();
160
+      static void advance_isr();
164
     #endif
161
     #endif
165
 
162
 
166
     //
163
     //
177
     //
174
     //
178
     // Set direction bits for all steppers
175
     // Set direction bits for all steppers
179
     //
176
     //
180
-    void set_directions();
177
+    static void set_directions();
181
 
178
 
182
     //
179
     //
183
     // Get the position of a stepper, in steps
180
     // Get the position of a stepper, in steps
213
     //
210
     //
214
     // The direction of a single motor
211
     // The direction of a single motor
215
     //
212
     //
216
-    FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
213
+    static FORCE_INLINE bool motor_direction(AxisEnum axis) { return TEST(last_direction_bits, axis); }
217
 
214
 
218
     #if HAS_DIGIPOTSS
215
     #if HAS_DIGIPOTSS
219
       void digitalPotWrite(int address, int value);
216
       void digitalPotWrite(int address, int value);
251
 
248
 
252
   private:
249
   private:
253
 
250
 
254
-    FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
251
+    static FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
255
       unsigned short timer;
252
       unsigned short timer;
256
 
253
 
257
       NOMORE(step_rate, MAX_STEP_FREQUENCY);
254
       NOMORE(step_rate, MAX_STEP_FREQUENCY);
283
         timer = (unsigned short)pgm_read_word_near(table_address);
280
         timer = (unsigned short)pgm_read_word_near(table_address);
284
         timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
281
         timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
285
       }
282
       }
286
-      if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
283
+      if (timer < 100) { // (20kHz - this should never happen)
284
+        timer = 100;
285
+        MYSERIAL.print(MSG_STEPPER_TOO_HIGH);
286
+        MYSERIAL.println(step_rate);
287
+      }
287
       return timer;
288
       return timer;
288
     }
289
     }
289
 
290
 
290
     // Initializes the trapezoid generator from the current block. Called whenever a new
291
     // Initializes the trapezoid generator from the current block. Called whenever a new
291
     // block begins.
292
     // block begins.
292
-    FORCE_INLINE void trapezoid_generator_reset() {
293
+    static FORCE_INLINE void trapezoid_generator_reset() {
293
 
294
 
294
       static int8_t last_extruder = -1;
295
       static int8_t last_extruder = -1;
295
 
296
 

Laddar…
Avbryt
Spara