The image below shows a basic schematic of how to run a spindle in closed loop mode in both position (controlled by M19) and velocity modes
The basic idea is to have two PID controllers, one for position and one for velocity. In the diagram the top PID handles position and is enabled by the motion.spindle-orient pin and the lower one handles velocity and is enabled by motion.spindle-on.
A mux2 controlled by motion.spindle-orient sends the output of one or the other PID to the motor control outputs. The example uses a 0-10V VFD with separate Forward and Reverse outputs.
There are many values and settings not shown for clarity. Clearly both PID components need tuning. Typically the velocity PID will require I and FF0 and the position PID will need P and I.
On most web browsers Ctrl-+ will increase the image size to view the SVG.