Spindle Types - bdring/Grbl_Esp32 GitHub Wiki

Overview

Spindles in Grbl_ESP32 are coded a lot differently in Grbl_ESP from Grbl (Arduino). There are classes defined for each spindle type. This allows a simple method of creating new spindles and a standard interface for Grbl to work with. There are a lot of #defines and $$ settings for spindles. The spindle you use may only use a few of them. For example, if you are using a relay to control spindle, the RPM related settings are ignored. See below for details on each type

You can change between spindles types dynamically as long as all of the required I/O is defined in your machine definition. For example: You could have a spindle and a laser on the machine and switch between without recompiling or even rebooting.

What is a Spindle?

The word spindle in Grbl can mean many things. Standard gcode does not support things like lasers, so Grbl_ESP32 uses the M3 (spindle on clockwise) , M4 (spindle on counter clockwise), M5 (spindle off) and Snnnnn (set rpm to nnnnn) to control a lot of tools. These can be On/Off relay based spindles, PWM speed controlled spindles, RC type brushless speed controllers, RS485 VFDs, lasers and more. We could easily add additional things like hot wire and other things.

Note: Currently when you see RPM anywhere, it is referring to the gcode Snnnnn value. When used for lasers, etc. it might mean something different. Just think of it as the appropriate unit for the tool you are using.

Spindles are defined in your machine definition file. The I/O pins you need to define depends on the spindle type you choose. The spindle type is selected by the $Spindle/Type You can set a default in your machine definition file with #define SPINDLE_TYPE SpindleType::XXXXX` statement. If you don't define anything, it will default to a None spindle. Here are the types currently available.

#define SPINDLE_TYPE     SpindleType::NONE 
#define SPINDLE_TYPE     SpindleType::PWM
#define SPINDLE_TYPE     SpindleType::RELAY
#define SPINDLE_TYPE     SpindleType::LASER 
#define SPINDLE_TYPE     SpindleType::DAC   
#define SPINDLE_TYPE     SpindleType::HUANYANG // RS485
#define SPINDLE_TYPE     SpindleType::BESC
#define SPINDLE_TYPE     SpindleType::_10V
#define SPINDLE_TYPE     SpindleType::H2A // RS485

When Grbl_ESP32 boots, it will send some messages to the USB/Serial port. It will tell you what spindle type is active. If there are any pin definitions missing from from your machine definition file, it will send error messages about that too.

Below are the details for each spindle type.

Settings

There are many settings for spindles. See below what settings are used for each spindle type. Settings that are not used by your spindle type will be show and can be changed, but are ignored. The spindle must be off (M5) to change any spindle setting. After changing the setting it will re-initialize your spindle.

SpindleType::NONE

If your machine does not require a spindle, like a pen plotter, choose this type. #define SPINDLE_TYPE SpindleType::NONE It will not use any I/O. It will default to this type if no I/O is defined.

SpindleType::PWM

Many speed control circuits for spindles use a PWM signal to set the speed. Here are the things you need in your machine definition file.

  • Required #define SPINDLE_TYPE SpindleType::PWM
  • Required #define SPINDLE_OUTPUT_PIN GPIO_NUM_nn where nn is the pin number. This is the pin connected to the PWM input on the speed controller.
  • Optional #define SPINDLE _ENABLE_PIN GPIO_NUM_nn If you want a spindle enable signal.
  • Optional #define SPINDLE_DIRECTION_PIN GPIO_NUM_nn if you want a direction signal.
  • $GCode/MaxS Maximum spindle speed. This value is used to match a PWM duty to the RPM you want. If you set this to 1000 and send S500, it will set the duty to 50%. If you send S1500, it will change clip your value to 1000.
  • $GCode/MinS This is the the minimum RPM. If your spindle does not work well below 200 RPM, set $31=200. If you send an S value below 200, it will set the speed to 200.
  • $Spindle/PWM/Frequency Spindle PWM Freq. This sets the frequency of the PWM signal.
  • $Spindle/PWM/Off Spindle PWM Off Value. Typically set to 0. The PWM values are set in percentage of duty cycle.
  • $Spindle/PWM/Min Spindle PWM Min Value Typically set to 0.
  • $Spindle/PWM/Max Spindle PWM Max Value. Typically set to 100.

SpindleType::RELAY

If you are using a relay to turn on your spindle, you only want an on/off signal. an accidental PWM signal could destroy the relay. The ESP32 does not have enough current to directly drive a relay. You should use a driver circuit for that. Here are the settings you need in your machine definition file.

  • Required #define SPINDLE_TYPE SpindleType::RELAY
  • Required #define SPINDLE_OUTPUT_PIN GPIO_NUM_nn where nn is the pin number. This is the pin connected to the relay circuit.
  • Optional #define SPINDLE _ENABLE_PIN GPIO_NUM_nn If you want an enable signal.
  • Optional #define SPINDLE_DIRECTION_PIN GPIO_NUM_nn if you want a direction signal.

Because it is an on/off signal most of the $$ spindle settings will be ignored.

SpindleType::LASER

A laser is basically a PWM spindle with a few extra features. You want it to turn off when the machine is doing a rapid move or is paused. It can also do a speed compensation feature. If you are engraving you want the laser to proportionally reduce the power when it is accelerating or decelerating. Use the M4 command, normally used for counter clockwise rotation, to enable this feature.

  • Optional #define SPINDLE_TYPE SpindleType::LASER
  • Required #define LASER_OUTPUT_PIN GPIO_NUM_nn where nn is the pin number.
  • Optional #define LASER_ENABLE_PIN GPIO_NUM_nn If you want an enable signal.
  • Most PWM Settings also apply
  • $GCode/LaserMode Set $GCode/LaserMode=1 for laser mode
  • $Laser/FullPower=nnnn. This sets the full power number used for the PWM. $Laser/FullPower=100 means your power range is 0-100.

SpindleType::DAC

Some spindle speed controller have an analog input. This mode uses the DAC (digital to analog) on the ESP32 to output an analog signal. You will probably need to run the output through an opamp to amplify the ESP32's 0-3.3V range to the 0-5V or 0-10V range the speed controller needs.

  • Required #define SPINDLE_TYPE SpindleType::DAC
  • Required #define SPINDLE_OUTPUT_PIN GPIO_NUM_nn where nn is the pin number. This is the pin connected to the relay circuit.
  • Optional #define SPINDLE _ENABLE_PIN GPIO_NUM_nn If you want an enable signal.
  • Optional #define SPINDLE_DIRECTION_PIN GPIO_NUM_nn if you want a direction signal.
  • $GCode/MaxS, $GCode/MinS See PWM mode
  • $Spindle/PWM/Frequency, $Spindle/PWM/Off and $Spindle/PWM/Min, Spindle/PWM/Max Do not apply.

Here an a typical opamp circuit that can boost the 3.3v to a higher voltage. The gain on this circuit is 3.2. There are a lot of opamp resources including calculators that can be fond on the Internet.

Note: The DAC is only has 8 bits of resolution. If you want higher resolution you might consider the _10V spindle

SpindleType::_10V

0-10V is a common industrial control signal that is often used by VFD and speed controllers. This uses a 3.3V PWM signal, so you need to boost the voltage with a circuit similar to the one above. For full control, the VFDs often require the 0-10V signal as well as separate forward and reverse signals. This type supports that.

  • Required #define SPINDLE_TYPE SpindleType::_10V
  • Required #define SPINDLE_OUTPUT_PIN GPIO_NUM_nn where nn is the pin number. This is the pin connected to the PWM input on the speed controller.
  • Optional #define SPINDLE _ENABLE_PIN GPIO_NUM_nn If you want a spindle enable signal.
  • Optional #define SPINDLE_FORWARD_PIN GPIO_NUM_nn if you want a forward direction signal.
  • Optional #define SPINDLE_REVERSE_PIN GPIO_NUM_nn if you want a reverse direction signal.

SpindleType::HUANYANG

This spindle mode talks to a Huanyang VFD (a very popular Chinese VFD) using an RS485 serial connection. It can control the speed and which direction the spindle should turn. The ESP32 can create the signals, but not the differential voltages required for the RS485. A 3.3V TTL to RS485 needs to be used. The MAX485CSA works well for this. Type **HUANYANG ** is their original protocol and HY2 is Huanyang's latest protocol.

  • Required #define SPINDLE_TYPE SpindleType::HUANYANG
  • Required #define HUANYANG_TXD_PIN GPIO_NUM_nn This is the transmit data pin.
  • Optional #define SPINDLE_RXD_PIN GPIO_NUM_nn This is the receive data pin.
  • Optional #define HUANYANG_RTS_PIN GPIO_NUM_nn This pin controls whether data is being transmit or received.

You also need to make sure the VFD is setup correctly. Set the registers as follows.

PD004   400  Base frequency as rated on my spindle (default was 50)
PD005   400  Maximum frequency Hz (Typical for spindles)
PD011   120  Min Speed (Recommend Aircooled=120 Water=100)
PD014   10   Acceleration time (Test to optimize)
PD015   10   Deceleration time (Test to optimize)
PD023   1    Reverse run enabled
PD141   220  Spindle max rated voltage
PD142   3.7  Max current Amps (0.8kw=3.7 1.5kw=7.0, 2.2kw=??)
PD143   2    Poles most are 2 (I think this is only used for RPM calc from Hz)
PD144   3000 Max rated motor revolution at 50 Hz => 24000@400Hz = 3000@50HZ

PD001   2    RS485 Control of run commands
PD002   2    RS485 Control of operating frequency
PD163   1    RS485 Address: 1 (Typical. OK to change...see below)
PD164   1    RS485 Baud rate: 9600 (Typical. OK to change...see below)
PD165   3    RS485 Mode: RTU, 8N1

SpindleType::HY2

SpindleType::YL620

SpindleType::BESC

Photo Credit

This is a RC type brushless DC speed controller. It uses a special PWM signal to control the speed of a motor. The motors are very strong, and low cost. The actual speed might not be very accurate though.

See this page for more details

Example machine definition

#define SPINDLE_TYPE            SpindleType::BESC
#define SPINDLE_OUTPUT_PIN      GPIO_NUM_26