Selaa lähdekoodia

🎨 Misc. Spindle/Laser (etc.) cleanup

Scott Lahteine 2 vuotta sitten
vanhempi
commit
845d42ef40

+ 19
- 21
Marlin/src/feature/spindle_laser.cpp Näytä tiedosto

52
 #endif
52
 #endif
53
 #define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0)
53
 #define SPINDLE_LASER_PWM_OFF TERN(SPINDLE_LASER_PWM_INVERT, 255, 0)
54
 
54
 
55
-//
56
-// Init the cutter to a safe OFF state
57
-//
55
+/**
56
+ * Init the cutter to a safe OFF state
57
+ */
58
 void SpindleLaser::init() {
58
 void SpindleLaser::init() {
59
   #if ENABLED(SPINDLE_SERVO)
59
   #if ENABLED(SPINDLE_SERVO)
60
     MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN);
60
     MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN);
86
 #if ENABLED(SPINDLE_LASER_PWM)
86
 #if ENABLED(SPINDLE_LASER_PWM)
87
   /**
87
   /**
88
    * Set the cutter PWM directly to the given ocr value
88
    * Set the cutter PWM directly to the given ocr value
89
+   *
90
+   * @param ocr Power value
89
    */
91
    */
90
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
92
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
91
     #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
93
     #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
105
     WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
107
     WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
106
     _set_ocr(0);
108
     _set_ocr(0);
107
   }
109
   }
108
-#endif
110
+#endif // SPINDLE_LASER_PWM
109
 
111
 
110
-//
111
-// Set cutter ON/OFF state (and PWM) to the given cutter power value
112
-//
112
+/**
113
+ * Apply power for laser/spindle
114
+ *
115
+ * Apply cutter power value for PWM, Servo, and on/off pin.
116
+ *
117
+ * @param opwr Power value. Range 0 to MAX. When 0 disable spindle/laser.
118
+ */
113
 void SpindleLaser::apply_power(const uint8_t opwr) {
119
 void SpindleLaser::apply_power(const uint8_t opwr) {
114
   static uint8_t last_power_applied = 0;
120
   static uint8_t last_power_applied = 0;
115
   if (opwr == last_power_applied) return;
121
   if (opwr == last_power_applied) return;
137
 }
143
 }
138
 
144
 
139
 #if ENABLED(SPINDLE_CHANGE_DIR)
145
 #if ENABLED(SPINDLE_CHANGE_DIR)
140
-  //
141
-  // Set the spindle direction and apply immediately
142
-  // Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
143
-  //
146
+  /**
147
+   * Set the spindle direction and apply immediately
148
+   * Stop on direction change if SPINDLE_STOP_ON_DIR_CHANGE is enabled
149
+   */
144
   void SpindleLaser::set_reverse(const bool reverse) {
150
   void SpindleLaser::set_reverse(const bool reverse) {
145
     const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
151
     const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
146
     if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
152
     if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
149
 #endif
155
 #endif
150
 
156
 
151
 #if ENABLED(AIR_EVACUATION)
157
 #if ENABLED(AIR_EVACUATION)
152
-
153
   // Enable / disable Cutter Vacuum or Laser Blower motor
158
   // Enable / disable Cutter Vacuum or Laser Blower motor
154
   void SpindleLaser::air_evac_enable()  { WRITE(AIR_EVACUATION_PIN,  AIR_EVACUATION_ACTIVE); } // Turn ON
159
   void SpindleLaser::air_evac_enable()  { WRITE(AIR_EVACUATION_PIN,  AIR_EVACUATION_ACTIVE); } // Turn ON
155
-
156
   void SpindleLaser::air_evac_disable() { WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); } // Turn OFF
160
   void SpindleLaser::air_evac_disable() { WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); } // Turn OFF
157
-
158
   void SpindleLaser::air_evac_toggle()  { TOGGLE(AIR_EVACUATION_PIN); } // Toggle state
161
   void SpindleLaser::air_evac_toggle()  { TOGGLE(AIR_EVACUATION_PIN); } // Toggle state
159
-
160
-#endif // AIR_EVACUATION
162
+#endif
161
 
163
 
162
 #if ENABLED(AIR_ASSIST)
164
 #if ENABLED(AIR_ASSIST)
163
-
164
   // Enable / disable air assist
165
   // Enable / disable air assist
165
   void SpindleLaser::air_assist_enable()  { WRITE(AIR_ASSIST_PIN,  AIR_ASSIST_PIN); } // Turn ON
166
   void SpindleLaser::air_assist_enable()  { WRITE(AIR_ASSIST_PIN,  AIR_ASSIST_PIN); } // Turn ON
166
-
167
   void SpindleLaser::air_assist_disable() { WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_PIN); } // Turn OFF
167
   void SpindleLaser::air_assist_disable() { WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_PIN); } // Turn OFF
168
-
169
   void SpindleLaser::air_assist_toggle()  { TOGGLE(AIR_ASSIST_PIN); } // Toggle state
168
   void SpindleLaser::air_assist_toggle()  { TOGGLE(AIR_ASSIST_PIN); } // Toggle state
170
-
171
-#endif // AIR_ASSIST
169
+#endif
172
 
170
 
173
 #endif // HAS_CUTTER
171
 #endif // HAS_CUTTER

+ 40
- 26
Marlin/src/feature/spindle_laser.h Näytä tiedosto

132
     public:
132
     public:
133
 
133
 
134
     static void set_ocr(const uint8_t ocr);
134
     static void set_ocr(const uint8_t ocr);
135
-    static inline void set_ocr_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
135
+    static inline void ocr_set_power(const uint8_t ocr) { power = ocr; set_ocr(ocr); }
136
     static void ocr_off();
136
     static void ocr_off();
137
-    // Used to update output for power->OCR translation
137
+
138
+    /**
139
+     * Update output for power->OCR translation
140
+     */
138
     static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
141
     static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
139
-      return (
142
+      return uint8_t(
140
         #if CUTTER_UNIT_IS(PWM255)
143
         #if CUTTER_UNIT_IS(PWM255)
141
-          uint8_t(upwr)
144
+          upwr
142
         #elif CUTTER_UNIT_IS(PERCENT)
145
         #elif CUTTER_UNIT_IS(PERCENT)
143
           pct_to_ocr(upwr)
146
           pct_to_ocr(upwr)
144
         #else
147
         #else
145
-          uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
148
+          pct_to_ocr(cpwr_to_pct(upwr))
146
         #endif
149
         #endif
147
       );
150
       );
148
     }
151
     }
149
 
152
 
150
-    // Correct power to configured range
153
+    /**
154
+     * Correct power to configured range
155
+     */
151
     static inline cutter_power_t power_to_range(const cutter_power_t pwr) {
156
     static inline cutter_power_t power_to_range(const cutter_power_t pwr) {
152
-      return power_to_range(pwr, (
153
-        #if CUTTER_UNIT_IS(PWM255)
154
-          0
155
-        #elif CUTTER_UNIT_IS(PERCENT)
156
-          1
157
-        #elif CUTTER_UNIT_IS(RPM)
158
-          2
159
-        #else
160
-          #error "CUTTER_UNIT_IS(unknown)"
161
-        #endif
162
-      ));
157
+      return power_to_range(pwr, _CUTTER_POWER(CUTTER_POWER_UNIT));
163
     }
158
     }
159
+
164
     static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
160
     static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
165
       if (pwr <= 0) return 0;
161
       if (pwr <= 0) return 0;
166
       cutter_power_t upwr;
162
       cutter_power_t upwr;
167
       switch (pwrUnit) {
163
       switch (pwrUnit) {
168
-        case 0:                                                 // PWM
164
+        case _CUTTER_POWER_PWM255:
169
           upwr = cutter_power_t(
165
           upwr = cutter_power_t(
170
               (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below
166
               (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below
171
             : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above
167
             : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above
172
             :  pwr
168
             :  pwr
173
           );
169
           );
174
           break;
170
           break;
175
-        case 1:                                                 // PERCENT
171
+        case _CUTTER_POWER_PERCENT:
176
           upwr = cutter_power_t(
172
           upwr = cutter_power_t(
177
               (pwr < min_pct) ? min_pct                         // Use minimum if set below
173
               (pwr < min_pct) ? min_pct                         // Use minimum if set below
178
             : (pwr > max_pct) ? max_pct                         // Use maximum if set above
174
             : (pwr > max_pct) ? max_pct                         // Use maximum if set above
179
             :  pwr                                              // PCT
175
             :  pwr                                              // PCT
180
           );
176
           );
181
           break;
177
           break;
182
-        case 2:                                                 // RPM
178
+        case _CUTTER_POWER_RPM:
183
           upwr = cutter_power_t(
179
           upwr = cutter_power_t(
184
               (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN         // Use minimum if set below
180
               (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN         // Use minimum if set below
185
             : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX         // Use maximum if set above
181
             : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX         // Use maximum if set above
190
       }
186
       }
191
       return upwr;
187
       return upwr;
192
     }
188
     }
193
-
194
   #endif // SPINDLE_LASER_PWM
189
   #endif // SPINDLE_LASER_PWM
195
 
190
 
191
+  /**
192
+   * Enable/Disable spindle/laser
193
+   * @param enable true = enable; false = disable
194
+   */
196
   static inline void set_enabled(const bool enable) {
195
   static inline void set_enabled(const bool enable) {
197
-    set_power(enable ? TERN(SPINDLE_LASER_PWM, (power ?: (unitPower ? upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP)) : 0)), 255) : 0);
196
+    uint8_t value = 0;
197
+    if (enable) {
198
+      #if ENABLED(SPINDLE_LASER_PWM)
199
+        if (power)
200
+          value = power;
201
+        else if (unitPower)
202
+          value = upower_to_ocr(cpwr_to_upwr(SPEED_POWER_STARTUP));
203
+      #else
204
+        value = 255;
205
+      #endif
206
+    }
207
+    set_power(value);
198
   }
208
   }
199
 
209
 
200
-  // Wait for spindle to spin up or spin down
210
+  static inline void disable() { isReady = false; set_enabled(false); }
211
+
212
+  /**
213
+   * Wait for spindle to spin up or spin down
214
+   *
215
+   * @param on true = state to on; false = state to off.
216
+   */
201
   static inline void power_delay(const bool on) {
217
   static inline void power_delay(const bool on) {
202
     #if DISABLED(LASER_POWER_INLINE)
218
     #if DISABLED(LASER_POWER_INLINE)
203
       safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
219
       safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
230
     }
246
     }
231
   #endif
247
   #endif
232
 
248
 
233
-  static inline void disable() { isReady = false; set_enabled(false); }
234
-
235
   #if HAS_LCD_MENU
249
   #if HAS_LCD_MENU
236
     static inline void enable_with_dir(const bool reverse) {
250
     static inline void enable_with_dir(const bool reverse) {
237
       isReady = true;
251
       isReady = true;
325
         planner.laser_inline.power = ocrpwr;
339
         planner.laser_inline.power = ocrpwr;
326
       }
340
       }
327
     #endif
341
     #endif
328
-  #endif  // LASER_POWER_INLINE
342
+  #endif // LASER_POWER_INLINE
329
 
343
 
330
   static inline void kill() {
344
   static inline void kill() {
331
     TERN_(LASER_POWER_INLINE, inline_disable());
345
     TERN_(LASER_POWER_INLINE, inline_disable());

+ 1
- 1
Marlin/src/gcode/control/M3-M5.cpp Näytä tiedosto

108
   #if ENABLED(SPINDLE_LASER_PWM)
108
   #if ENABLED(SPINDLE_LASER_PWM)
109
     if (parser.seenval('O')) {
109
     if (parser.seenval('O')) {
110
       cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
110
       cutter.unitPower = cutter.power_to_range(parser.value_byte(), 0);
111
-      cutter.set_ocr_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
111
+      cutter.ocr_set_power(cutter.unitPower); // The OCR is a value from 0 to 255 (uint8_t)
112
     }
112
     }
113
     else
113
     else
114
       cutter.set_power(cutter.upower_to_ocr(get_s_power()));
114
       cutter.set_power(cutter.upower_to_ocr(get_s_power()));

+ 8
- 8
Marlin/src/module/stepper.cpp Näytä tiedosto

1914
                     laser_trap.acc_step_count += current_block->laser.entry_per;
1914
                     laser_trap.acc_step_count += current_block->laser.entry_per;
1915
                     if (laser_trap.cur_power < current_block->laser.power) laser_trap.cur_power++;
1915
                     if (laser_trap.cur_power < current_block->laser.power) laser_trap.cur_power++;
1916
                   }
1916
                   }
1917
-                  cutter.set_ocr_power(laser_trap.cur_power);
1917
+                  cutter.ocr_set_power(laser_trap.cur_power);
1918
                 }
1918
                 }
1919
               }
1919
               }
1920
             #else
1920
             #else
1923
               else {
1923
               else {
1924
                 laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER;
1924
                 laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER;
1925
                 laser_trap.cur_power = (current_block->laser.power * acc_step_rate) / current_block->nominal_rate;
1925
                 laser_trap.cur_power = (current_block->laser.power * acc_step_rate) / current_block->nominal_rate;
1926
-                cutter.set_ocr_power(laser_trap.cur_power); // Cycle efficiency is irrelevant it the last line was many cycles
1926
+                cutter.ocr_set_power(laser_trap.cur_power); // Cycle efficiency is irrelevant it the last line was many cycles
1927
               }
1927
               }
1928
             #endif
1928
             #endif
1929
           }
1929
           }
1991
                     laser_trap.acc_step_count += current_block->laser.exit_per;
1991
                     laser_trap.acc_step_count += current_block->laser.exit_per;
1992
                     if (laser_trap.cur_power > current_block->laser.power_exit) laser_trap.cur_power--;
1992
                     if (laser_trap.cur_power > current_block->laser.power_exit) laser_trap.cur_power--;
1993
                   }
1993
                   }
1994
-                  cutter.set_ocr_power(laser_trap.cur_power);
1994
+                  cutter.ocr_set_power(laser_trap.cur_power);
1995
                 }
1995
                 }
1996
               }
1996
               }
1997
             #else
1997
             #else
2000
               else {
2000
               else {
2001
                 laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER;
2001
                 laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER;
2002
                 laser_trap.cur_power = (current_block->laser.power * step_rate) / current_block->nominal_rate;
2002
                 laser_trap.cur_power = (current_block->laser.power * step_rate) / current_block->nominal_rate;
2003
-                cutter.set_ocr_power(laser_trap.cur_power); // Cycle efficiency isn't relevant when the last line was many cycles
2003
+                cutter.ocr_set_power(laser_trap.cur_power); // Cycle efficiency isn't relevant when the last line was many cycles
2004
               }
2004
               }
2005
             #endif
2005
             #endif
2006
           }
2006
           }
2028
           if (laser_trap.enabled) {
2028
           if (laser_trap.enabled) {
2029
             if (!laser_trap.cruise_set) {
2029
             if (!laser_trap.cruise_set) {
2030
               laser_trap.cur_power = current_block->laser.power;
2030
               laser_trap.cur_power = current_block->laser.power;
2031
-              cutter.set_ocr_power(laser_trap.cur_power);
2031
+              cutter.ocr_set_power(laser_trap.cur_power);
2032
               laser_trap.cruise_set = true;
2032
               laser_trap.cruise_set = true;
2033
             }
2033
             }
2034
             #if ENABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
2034
             #if ENABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
2249
           #endif
2249
           #endif
2250
           // Always have PWM in this case
2250
           // Always have PWM in this case
2251
           if (stat.isPlanned) {                        // Planner controls the laser
2251
           if (stat.isPlanned) {                        // Planner controls the laser
2252
-            cutter.set_ocr_power(
2252
+            cutter.ocr_set_power(
2253
               stat.isEnabled ? laser_trap.cur_power : 0 // ON with power or OFF
2253
               stat.isEnabled ? laser_trap.cur_power : 0 // ON with power or OFF
2254
             );
2254
             );
2255
           }
2255
           }
2256
         #else
2256
         #else
2257
           if (stat.isPlanned) {                        // Planner controls the laser
2257
           if (stat.isPlanned) {                        // Planner controls the laser
2258
             #if ENABLED(SPINDLE_LASER_PWM)
2258
             #if ENABLED(SPINDLE_LASER_PWM)
2259
-              cutter.set_ocr_power(
2259
+              cutter.ocr_set_power(
2260
                 stat.isEnabled ? current_block->laser.power : 0 // ON with power or OFF
2260
                 stat.isEnabled ? current_block->laser.power : 0 // ON with power or OFF
2261
               );
2261
               );
2262
             #else
2262
             #else
2304
         const power_status_t stat = planner.laser_inline.status;
2304
         const power_status_t stat = planner.laser_inline.status;
2305
         if (stat.isPlanned) {             // Planner controls the laser
2305
         if (stat.isPlanned) {             // Planner controls the laser
2306
           #if ENABLED(SPINDLE_LASER_PWM)
2306
           #if ENABLED(SPINDLE_LASER_PWM)
2307
-            cutter.set_ocr_power(
2307
+            cutter.ocr_set_power(
2308
               stat.isEnabled ? planner.laser_inline.power : 0 // ON with power or OFF
2308
               stat.isEnabled ? planner.laser_inline.power : 0 // ON with power or OFF
2309
             );
2309
             );
2310
           #else
2310
           #else

+ 2
- 2
Marlin/src/pins/mega/pins_PROTONEER_CNC_SHIELD_V3.h Näytä tiedosto

23
 
23
 
24
 /**
24
 /**
25
  * Protoneer v3.00 pin assignments
25
  * Protoneer v3.00 pin assignments
26
- * 
26
+ *
27
  * This CNC shield has an UNO pinout and fits all Arduino-compatibles.
27
  * This CNC shield has an UNO pinout and fits all Arduino-compatibles.
28
- * 
28
+ *
29
  * Referenced docs:
29
  * Referenced docs:
30
  * - https://blog.protoneer.co.nz/arduino-cnc-shield-v3-00-assembly-guide/
30
  * - https://blog.protoneer.co.nz/arduino-cnc-shield-v3-00-assembly-guide/
31
  * - https://blog.protoneer.co.nz/arduino-cnc-shield/
31
  * - https://blog.protoneer.co.nz/arduino-cnc-shield/

Loading…
Peruuta
Tallenna