浏览代码

SPINDLE/LASER implementation

Scott Lahteine 8 年前
父节点
当前提交
55a87da036
共有 1 个文件被更改,包括 141 次插入0 次删除
  1. 141
    0
      Marlin/Marlin_main.cpp

+ 141
- 0
Marlin/Marlin_main.cpp 查看文件

73
  *
73
  *
74
  * M0   - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
74
  * M0   - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
75
  * M1   - Same as M0
75
  * M1   - Same as M0
76
+ * M3   - Turn laser/spindle on, set spindle/laser speed/power, set rotation to clockwise
77
+ * M4   - Turn laser/spindle on, set spindle/laser speed/power, set rotation to counter-clockwise
78
+ * M5   - Turn laser/spindle off
76
  * M17  - Enable/Power all stepper motors
79
  * M17  - Enable/Power all stepper motors
77
  * M18  - Disable all stepper motors; same as M84
80
  * M18  - Disable all stepper motors; same as M84
78
  * M20  - List SD card. (Requires SDSUPPORT)
81
  * M20  - List SD card. (Requires SDSUPPORT)
5611
 
5614
 
5612
 #endif // HAS_RESUME_CONTINUE
5615
 #endif // HAS_RESUME_CONTINUE
5613
 
5616
 
5617
+#if ENABLED(SPINDLE_LASER_ENABLE)
5618
+  /**
5619
+   * M3: Spindle Clockwise
5620
+   * M4: Spindle Counter-clockwise
5621
+   *
5622
+   *  S0 turns off spindle.
5623
+   *
5624
+   *  If no speed PWM output is defined then M3/M4 just turns it on.
5625
+   *
5626
+   *  At least 12.8KHz (50Hz * 256) is needed for spindle PWM.
5627
+   *  Hardware PWM is required. ISRs are too slow.
5628
+   *
5629
+   * NOTE: WGM for timers 3, 4, and 5 must be either Mode 1 or Mode 5.
5630
+   *       No other settings give a PWM signal that goes from 0 to 5 volts.
5631
+   *
5632
+   *       The system automatically sets WGM to Mode 1, so no special
5633
+   *       initialization is needed.
5634
+   *
5635
+   *       WGM bits for timer 2 are automatically set by the system to
5636
+   *       Mode 1. This produces an acceptable 0 to 5 volt signal.
5637
+   *       No special initialization is needed.
5638
+   *
5639
+   * NOTE: A minimum PWM frequency of 50 Hz is needed. All prescaler
5640
+   *       factors for timers 2, 3, 4, and 5 are acceptable.
5641
+   *
5642
+   *  SPINDLE_LASER_ENABLE_PIN needs an external pullup or it may power on
5643
+   *  the spindle/laser during power-up or when connecting to the host
5644
+   *  (usually goes through a reset which sets all I/O pins to tri-state)
5645
+   *
5646
+   *  PWM duty cycle goes from 0 (off) to 255 (always on).
5647
+   */
5648
+
5649
+  // Wait for spindle to come up to speed
5650
+  inline void delay_for_power_up() {
5651
+    refresh_cmd_timeout();
5652
+    while (PENDING(millis(), SPINDLE_LASER_POWERUP_DELAY + previous_cmd_ms)) idle();
5653
+  }
5654
+
5655
+  // Wait for spindle to stop turning
5656
+  inline void delay_for_power_down() {
5657
+    refresh_cmd_timeout();
5658
+    while (PENDING(millis(), SPINDLE_LASER_POWERDOWN_DELAY + previous_cmd_ms + 1)) idle();
5659
+  }
5660
+
5661
+  /**
5662
+   * ocr_val_mode() is used for debugging and to get the points needed to compute the RPM vs ocr_val line
5663
+   *
5664
+   * it accepts inputs of 0-255
5665
+   */
5666
+
5667
+  inline void ocr_val_mode() {
5668
+    uint8_t spindle_laser_power = code_value_byte();
5669
+    WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low)
5670
+    if (SPINDLE_LASER_PWM_INVERT) spindle_laser_power = 255 - spindle_laser_power;
5671
+    analogWrite(SPINDLE_LASER_PWM_PIN, spindle_laser_power);
5672
+  }
5673
+
5674
+  inline void gcode_M3_M4(bool is_M3) {
5675
+
5676
+    stepper.synchronize();   // wait until previous movement commands (G0/G0/G2/G3) have completed before playing with the spindle
5677
+    #if SPINDLE_DIR_CHANGE
5678
+      const bool rotation_dir = (is_M3 && !SPINDLE_INVERT_DIR || !is_M3 && SPINDLE_INVERT_DIR) ? HIGH : LOW;
5679
+      if (SPINDLE_STOP_ON_DIR_CHANGE \
5680
+         && READ(SPINDLE_LASER_ENABLE_PIN) == SPINDLE_LASER_ENABLE_INVERT \
5681
+         && READ(SPINDLE_DIR_PIN) != rotation_dir
5682
+      ) {
5683
+        WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT);  // turn spindle off
5684
+        delay_for_power_down();
5685
+      }
5686
+      digitalWrite(SPINDLE_DIR_PIN, rotation_dir);
5687
+    #endif
5688
+
5689
+    /**
5690
+     * Our final value for ocr_val is an unsigned 8 bit value between 0 and 255 which usually means uint8_t.
5691
+     * Went to uint16_t because some of the uint8_t calculations would sometimes give 1000 0000 rather than 1111 1111.
5692
+     * Then needed to AND the uint16_t result with 0x00FF to make sure we only wrote the byte of interest.
5693
+     */
5694
+    #if ENABLED(SPINDLE_LASER_PWM)
5695
+      if (code_seen('O')) ocr_val_mode();
5696
+      else {
5697
+        const float spindle_laser_power = code_seen('S') ? code_value_float() : 0;
5698
+        if (spindle_laser_power == 0) {
5699
+          WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT);                                    // turn spindle off (active low)
5700
+          delay_for_power_down();
5701
+        }
5702
+        else {
5703
+          int16_t ocr_val = (spindle_laser_power - (SPEED_POWER_INTERCEPT)) * (1.0 / (SPEED_POWER_SLOPE));  // convert RPM to PWM duty cycle
5704
+          NOMORE(ocr_val, 255);                                                                             // limit to max the Atmel PWM will support
5705
+          if (spindle_laser_power <= SPEED_POWER_MIN)
5706
+            ocr_val = (SPEED_POWER_MIN - (SPEED_POWER_INTERCEPT)) * (1.0 / (SPEED_POWER_SLOPE));            // minimum setting
5707
+          if (spindle_laser_power >= SPEED_POWER_MAX)
5708
+            ocr_val = (SPEED_POWER_MAX - (SPEED_POWER_INTERCEPT)) * (1.0 / (SPEED_POWER_SLOPE));            // limit to max RPM
5709
+          if (SPINDLE_LASER_PWM_INVERT) ocr_val = 255 - ocr_val;
5710
+          WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT);                                     // turn spindle on (active low)
5711
+          analogWrite(SPINDLE_LASER_PWM_PIN, ocr_val & 0xFF);                                               // only write low byte
5712
+          delay_for_power_up();
5713
+        }
5714
+      }
5715
+    #else
5716
+      WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low) if spindle speed option not enabled
5717
+      delay_for_power_up();
5718
+    #endif
5719
+  }
5720
+
5721
+ /**
5722
+  * M5 turn off spindle
5723
+  */
5724
+  inline void gcode_M5() {
5725
+    stepper.synchronize();
5726
+    WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT);
5727
+    delay_for_power_down();
5728
+  }
5729
+
5730
+#endif // SPINDLE_LASER_ENABLE
5731
+
5614
 /**
5732
 /**
5615
  * M17: Enable power on all stepper motors
5733
  * M17: Enable power on all stepper motors
5616
  */
5734
  */
5626
 #endif
5744
 #endif
5627
 
5745
 
5628
 #if ENABLED(PARK_HEAD_ON_PAUSE)
5746
 #if ENABLED(PARK_HEAD_ON_PAUSE)
5747
+
5629
   float resume_position[XYZE];
5748
   float resume_position[XYZE];
5630
   bool move_away_flag = false;
5749
   bool move_away_flag = false;
5631
 
5750
 
9946
           break;
10065
           break;
9947
       #endif // ULTIPANEL
10066
       #endif // ULTIPANEL
9948
 
10067
 
10068
+      #if ENABLED(SPINDLE_LASER_ENABLE)
10069
+        case 3:
10070
+          gcode_M3_M4(true);   // M3: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CW
10071
+          break;               // synchronizes with movement commands
10072
+        case 4:
10073
+          gcode_M3_M4(false);  // M4: turn spindle/laser on, set laser/spindle power/speed, set rotation direction CCW
10074
+          break;               // synchronizes with movement commands
10075
+        case 5:
10076
+          gcode_M5();     // M5 - turn spindle/laser off
10077
+          break;          // synchronizes with movement commands
10078
+      #endif
9949
       case 17: // M17: Enable all stepper motors
10079
       case 17: // M17: Enable all stepper motors
9950
         gcode_M17();
10080
         gcode_M17();
9951
         break;
10081
         break;
12262
     update_case_light();
12392
     update_case_light();
12263
   #endif
12393
   #endif
12264
 
12394
 
12395
+  #if ENABLED(SPINDLE_LASER_ENABLE)
12396
+    OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT);  // init spindle to off
12397
+    #if SPINDLE_DIR_CHANGE
12398
+      OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0);  // init rotation to clockwise (M3)
12399
+    #endif
12400
+    #if ENABLED(SPINDLE_LASER_PWM)
12401
+      SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
12402
+      analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0);  // set to lowest speed
12403
+    #endif
12404
+  #endif
12405
+
12265
   #if HAS_BED_PROBE
12406
   #if HAS_BED_PROBE
12266
     endstops.enable_z_probe(false);
12407
     endstops.enable_z_probe(false);
12267
   #endif
12408
   #endif

正在加载...
取消
保存