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,9 +52,9 @@ cutter_power_t SpindleLaser::menuPower,                               // Power s
52 52
 #endif
53 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 58
 void SpindleLaser::init() {
59 59
   #if ENABLED(SPINDLE_SERVO)
60 60
     MOVE_SERVO(SPINDLE_SERVO_NR, SPINDLE_SERVO_MIN);
@@ -86,6 +86,8 @@ void SpindleLaser::init() {
86 86
 #if ENABLED(SPINDLE_LASER_PWM)
87 87
   /**
88 88
    * Set the cutter PWM directly to the given ocr value
89
+   *
90
+   * @param ocr Power value
89 91
    */
90 92
   void SpindleLaser::_set_ocr(const uint8_t ocr) {
91 93
     #if NEEDS_HARDWARE_PWM && SPINDLE_LASER_FREQUENCY
@@ -105,11 +107,15 @@ void SpindleLaser::init() {
105 107
     WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
106 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 119
 void SpindleLaser::apply_power(const uint8_t opwr) {
114 120
   static uint8_t last_power_applied = 0;
115 121
   if (opwr == last_power_applied) return;
@@ -137,10 +143,10 @@ void SpindleLaser::apply_power(const uint8_t opwr) {
137 143
 }
138 144
 
139 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 150
   void SpindleLaser::set_reverse(const bool reverse) {
145 151
     const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
146 152
     if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
@@ -149,25 +155,17 @@ void SpindleLaser::apply_power(const uint8_t opwr) {
149 155
 #endif
150 156
 
151 157
 #if ENABLED(AIR_EVACUATION)
152
-
153 158
   // Enable / disable Cutter Vacuum or Laser Blower motor
154 159
   void SpindleLaser::air_evac_enable()  { WRITE(AIR_EVACUATION_PIN,  AIR_EVACUATION_ACTIVE); } // Turn ON
155
-
156 160
   void SpindleLaser::air_evac_disable() { WRITE(AIR_EVACUATION_PIN, !AIR_EVACUATION_ACTIVE); } // Turn OFF
157
-
158 161
   void SpindleLaser::air_evac_toggle()  { TOGGLE(AIR_EVACUATION_PIN); } // Toggle state
159
-
160
-#endif // AIR_EVACUATION
162
+#endif
161 163
 
162 164
 #if ENABLED(AIR_ASSIST)
163
-
164 165
   // Enable / disable air assist
165 166
   void SpindleLaser::air_assist_enable()  { WRITE(AIR_ASSIST_PIN,  AIR_ASSIST_PIN); } // Turn ON
166
-
167 167
   void SpindleLaser::air_assist_disable() { WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_PIN); } // Turn OFF
168
-
169 168
   void SpindleLaser::air_assist_toggle()  { TOGGLE(AIR_ASSIST_PIN); } // Toggle state
170
-
171
-#endif // AIR_ASSIST
169
+#endif
172 170
 
173 171
 #endif // HAS_CUTTER

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

@@ -132,54 +132,50 @@ public:
132 132
     public:
133 133
 
134 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 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 141
     static inline uint8_t upower_to_ocr(const cutter_power_t upwr) {
139
-      return (
142
+      return uint8_t(
140 143
         #if CUTTER_UNIT_IS(PWM255)
141
-          uint8_t(upwr)
144
+          upwr
142 145
         #elif CUTTER_UNIT_IS(PERCENT)
143 146
           pct_to_ocr(upwr)
144 147
         #else
145
-          uint8_t(pct_to_ocr(cpwr_to_pct(upwr)))
148
+          pct_to_ocr(cpwr_to_pct(upwr))
146 149
         #endif
147 150
       );
148 151
     }
149 152
 
150
-    // Correct power to configured range
153
+    /**
154
+     * Correct power to configured range
155
+     */
151 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 160
     static inline cutter_power_t power_to_range(const cutter_power_t pwr, const uint8_t pwrUnit) {
165 161
       if (pwr <= 0) return 0;
166 162
       cutter_power_t upwr;
167 163
       switch (pwrUnit) {
168
-        case 0:                                                 // PWM
164
+        case _CUTTER_POWER_PWM255:
169 165
           upwr = cutter_power_t(
170 166
               (pwr < pct_to_ocr(min_pct)) ? pct_to_ocr(min_pct) // Use minimum if set below
171 167
             : (pwr > pct_to_ocr(max_pct)) ? pct_to_ocr(max_pct) // Use maximum if set above
172 168
             :  pwr
173 169
           );
174 170
           break;
175
-        case 1:                                                 // PERCENT
171
+        case _CUTTER_POWER_PERCENT:
176 172
           upwr = cutter_power_t(
177 173
               (pwr < min_pct) ? min_pct                         // Use minimum if set below
178 174
             : (pwr > max_pct) ? max_pct                         // Use maximum if set above
179 175
             :  pwr                                              // PCT
180 176
           );
181 177
           break;
182
-        case 2:                                                 // RPM
178
+        case _CUTTER_POWER_RPM:
183 179
           upwr = cutter_power_t(
184 180
               (pwr < SPEED_POWER_MIN) ? SPEED_POWER_MIN         // Use minimum if set below
185 181
             : (pwr > SPEED_POWER_MAX) ? SPEED_POWER_MAX         // Use maximum if set above
@@ -190,14 +186,34 @@ public:
190 186
       }
191 187
       return upwr;
192 188
     }
193
-
194 189
   #endif // SPINDLE_LASER_PWM
195 190
 
191
+  /**
192
+   * Enable/Disable spindle/laser
193
+   * @param enable true = enable; false = disable
194
+   */
196 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 217
   static inline void power_delay(const bool on) {
202 218
     #if DISABLED(LASER_POWER_INLINE)
203 219
       safe_delay(on ? SPINDLE_LASER_POWERUP_DELAY : SPINDLE_LASER_POWERDOWN_DELAY);
@@ -230,8 +246,6 @@ public:
230 246
     }
231 247
   #endif
232 248
 
233
-  static inline void disable() { isReady = false; set_enabled(false); }
234
-
235 249
   #if HAS_LCD_MENU
236 250
     static inline void enable_with_dir(const bool reverse) {
237 251
       isReady = true;
@@ -325,7 +339,7 @@ public:
325 339
         planner.laser_inline.power = ocrpwr;
326 340
       }
327 341
     #endif
328
-  #endif  // LASER_POWER_INLINE
342
+  #endif // LASER_POWER_INLINE
329 343
 
330 344
   static inline void kill() {
331 345
     TERN_(LASER_POWER_INLINE, inline_disable());

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

@@ -108,7 +108,7 @@ void GcodeSuite::M3_M4(const bool is_M4) {
108 108
   #if ENABLED(SPINDLE_LASER_PWM)
109 109
     if (parser.seenval('O')) {
110 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 113
     else
114 114
       cutter.set_power(cutter.upower_to_ocr(get_s_power()));

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

@@ -1914,7 +1914,7 @@ uint32_t Stepper::block_phase_isr() {
1914 1914
                     laser_trap.acc_step_count += current_block->laser.entry_per;
1915 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 1920
             #else
@@ -1923,7 +1923,7 @@ uint32_t Stepper::block_phase_isr() {
1923 1923
               else {
1924 1924
                 laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER;
1925 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 1928
             #endif
1929 1929
           }
@@ -1991,7 +1991,7 @@ uint32_t Stepper::block_phase_isr() {
1991 1991
                     laser_trap.acc_step_count += current_block->laser.exit_per;
1992 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 1997
             #else
@@ -2000,7 +2000,7 @@ uint32_t Stepper::block_phase_isr() {
2000 2000
               else {
2001 2001
                 laser_trap.till_update = LASER_POWER_INLINE_TRAPEZOID_CONT_PER;
2002 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 2005
             #endif
2006 2006
           }
@@ -2028,7 +2028,7 @@ uint32_t Stepper::block_phase_isr() {
2028 2028
           if (laser_trap.enabled) {
2029 2029
             if (!laser_trap.cruise_set) {
2030 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 2032
               laser_trap.cruise_set = true;
2033 2033
             }
2034 2034
             #if ENABLED(LASER_POWER_INLINE_TRAPEZOID_CONT)
@@ -2249,14 +2249,14 @@ uint32_t Stepper::block_phase_isr() {
2249 2249
           #endif
2250 2250
           // Always have PWM in this case
2251 2251
           if (stat.isPlanned) {                        // Planner controls the laser
2252
-            cutter.set_ocr_power(
2252
+            cutter.ocr_set_power(
2253 2253
               stat.isEnabled ? laser_trap.cur_power : 0 // ON with power or OFF
2254 2254
             );
2255 2255
           }
2256 2256
         #else
2257 2257
           if (stat.isPlanned) {                        // Planner controls the laser
2258 2258
             #if ENABLED(SPINDLE_LASER_PWM)
2259
-              cutter.set_ocr_power(
2259
+              cutter.ocr_set_power(
2260 2260
                 stat.isEnabled ? current_block->laser.power : 0 // ON with power or OFF
2261 2261
               );
2262 2262
             #else
@@ -2304,7 +2304,7 @@ uint32_t Stepper::block_phase_isr() {
2304 2304
         const power_status_t stat = planner.laser_inline.status;
2305 2305
         if (stat.isPlanned) {             // Planner controls the laser
2306 2306
           #if ENABLED(SPINDLE_LASER_PWM)
2307
-            cutter.set_ocr_power(
2307
+            cutter.ocr_set_power(
2308 2308
               stat.isEnabled ? planner.laser_inline.power : 0 // ON with power or OFF
2309 2309
             );
2310 2310
           #else

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

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

Loading…
Peruuta
Tallenna