grblHAL core  20241107
spindle_control.c File Reference
#include <math.h>
#include <string.h>
#include "hal.h"
#include "protocol.h"
#include "state_machine.h"
#include "settings.h"

Data Structures

struct  spindle_reg_t
 Structure for holding spindle registration data. More...
 
struct  spindle_sys_t
 Structure for holding data about an enabled spindle. More...
 

Functions

spindle_id_t spindle_register (const spindle_ptrs_t *spindle, const char *name)
 Register a spindle with the core. More...
 
spindle_num_t spindle_enable (spindle_id_t spindle_id)
 Enable a spindle and make it available for use by gcode. More...
 
bool spindle_select (spindle_id_t spindle_id)
 Enables a spindle and sets it as default spindle (spindle number 0). More...
 
spindle_ptrs_tspindle_get_hal (spindle_id_t spindle_id, spindle_hal_t hal)
 Get the handlers (function pointers) etc. associated with the spindle. More...
 
spindle_id_t spindle_get_default (void)
 Get the spindle id of the default spindle (spindle number 0). More...
 
spindle_cap_t spindle_get_caps (bool active)
 Get the merged spindle capabilities of all registered spindles. More...
 
const char * spindle_get_name (spindle_id_t spindle_id)
 Get the registered name of a spindle. More...
 
void spindle_update_caps (spindle_ptrs_t *spindle, spindle_pwm_t *pwm_caps)
 Update the capabilities of a registered PWM spindle. May be used by the driver on spindle initialization or when spindle settings has been changed. More...
 
uint8_t spindle_get_count (void)
 Get number of registered spindles. More...
 
void spindle_bind_encoder (const spindle_data_ptrs_t *encoder_data)
 
bool spindle_set_at_speed_range (spindle_ptrs_t *spindle, spindle_data_t *spindle_data, float rpm)
 
bool spindle_enumerate_spindles (spindle_enumerate_callback_ptr callback, void *data)
 Enumerate registered spindles by calling a callback function for each of them. More...
 
bool spindle_is_enabled (spindle_num_t spindle_num)
 Check if a spindle is enabled and available or not. More...
 
spindle_ptrs_tspindle_get (spindle_num_t spindle_num)
 Get the handlers (function pointers) etc. associated with an enabled spindle. More...
 
spindle_id_t spindle_add_null (void)
 Register a null spindle that has no connection to the outside world. This is done automatically on startup if no spindle can be succesfully enabled. More...
 
void spindle_set_override (spindle_ptrs_t *spindle, override_t speed_override)
 Set spindle speed override. More...
 
bool spindle_set_state (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
 Immediately sets spindle running state with direction and spindle rpm, if enabled. Called by g-code parser spindle_sync(), parking retract and restore, g-code program end, sleep, and spindle stop override. More...
 
bool spindle_sync (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
 G-code parser entry-point for setting spindle state. Forces a planner buffer sync and bails if an abort or check-mode is active. If the spindle supports at speed functionality it will wait for it to reach the speed and raise an alarm if the speed is not reached within the timeout period. More...
 
bool spindle_restore (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
 Restore spindle running state with direction, enable, spindle RPM and appropriate delay. More...
 
float spindle_set_rpm (spindle_ptrs_t *spindle, float rpm, override_t override_pct)
 Calculate and set programmed RPM according to override and max/min limits. More...
 
void spindle_all_off (void)
 Turn off all enabled spindles. More...
 
bool spindle_is_on (void)
 Check if any of the enabled spindles is running. More...
 
bool spindle_precompute_pwm_values (spindle_ptrs_t *spindle, spindle_pwm_t *pwm_data, spindle_settings_t *settings, uint32_t clock_hz)
 Precompute PWM values for faster conversion. More...
 

Function Documentation

◆ spindle_add_null()

spindle_id_t spindle_add_null ( void  )

Register a null spindle that has no connection to the outside world. This is done automatically on startup if no spindle can be succesfully enabled.

Returns
assigned spindle id as a spindle_id_t if successful, -1 if not.

◆ spindle_all_off()

void spindle_all_off ( void  )

Turn off all enabled spindles.

◆ spindle_bind_encoder()

void spindle_bind_encoder ( const spindle_data_ptrs_t encoder_data)

◆ spindle_enable()

spindle_num_t spindle_enable ( spindle_id_t  spindle_id)

Enable a spindle and make it available for use by gcode.

Parameters
spindle_idspindle id as a spindle_id_t.
Returns
assigned spindle number as a spindle_num_t if successful -1 if not.

NOTE: up to N_SYS_SPINDLE spindles can be enabled at a time.

◆ spindle_enumerate_spindles()

bool spindle_enumerate_spindles ( spindle_enumerate_callback_ptr  callback,
void *  data 
)

Enumerate registered spindles by calling a callback function for each of them.

Parameters
callbackpointer to a spindle_enumerate_callback_ptr type function.
datapointer to optional data to pass to the callback function.
Returns
true if spindles are registered and a callback function was provided, false otherwise.

◆ spindle_get()

spindle_ptrs_t* spindle_get ( spindle_num_t  spindle_num)

Get the handlers (function pointers) etc. associated with an enabled spindle.

Parameters
spindle_numspindle number as a spindle_num_t.
Returns
pointer to a spindle_ptrs_t structure if successful, NULL if not.

NOTE: do not modify the returned structure!

◆ spindle_get_caps()

spindle_cap_t spindle_get_caps ( bool  active)

Get the merged spindle capabilities of all registered spindles.

Parameters
activetrue to return active capabilities, false to return default capabilities.
Returns
capabilities in a spindle_cap_t structure.

◆ spindle_get_count()

uint8_t spindle_get_count ( void  )

Get number of registered spindles.

Returns
number of registered spindles.

◆ spindle_get_default()

spindle_id_t spindle_get_default ( void  )

Get the spindle id of the default spindle (spindle number 0).

Returns
spindle id as a spindle_id_t if successful, -2 if not (no spindle available).

◆ spindle_get_hal()

spindle_ptrs_t* spindle_get_hal ( spindle_id_t  spindle_id,
spindle_hal_t  hal 
)

Get the handlers (function pointers) etc. associated with the spindle.

Parameters
spindle_idspindle id as a spindle_id_t.
hala spindle_hal_t enum value:
SpindleHAL_Raw - get the read only version as supplied at registration
SpindleHAL_Configured - get the version with run-time modifications applied by the spindle driver.
SpindleHAL_Active - get the enabled version available from gcode. Can be overriden by event handlers prior to activation.
Returns
pointer to a spindle_ptrs_t structure if successful, NULL if not.

NOTE: do not modify the returned structure!

◆ spindle_get_name()

const char* spindle_get_name ( spindle_id_t  spindle_id)

Get the registered name of a spindle.

Parameters
spindle_idspindle id as a spindle_id_t.
Returns
pointer to a null terminated string if succesful, NULL if not.

◆ spindle_is_enabled()

bool spindle_is_enabled ( spindle_num_t  spindle_num)

Check if a spindle is enabled and available or not.

Parameters
spindle_numspindle number as a spindle_num_t.
Returns
true if the spindle is enabled, false otherwise.

◆ spindle_is_on()

bool spindle_is_on ( void  )

Check if any of the enabled spindles is running.

Returns
true if a spindle is running, false otherwise.

◆ spindle_precompute_pwm_values()

bool spindle_precompute_pwm_values ( spindle_ptrs_t spindle,
spindle_pwm_t pwm_data,
spindle_settings_t settings,
uint32_t  clock_hz 
)

Precompute PWM values for faster conversion.

Parameters
spindlepointer to a spindle_ptrs_t structure.
pwm_datapointer to a spindle_pwm_t structure, to hold the precomputed values.
clock_hztimer clock frequency used for PWM generation.
Returns
true if successful, false if no PWM range possible - driver should then revert to simple on/off spindle control.

◆ spindle_register()

spindle_id_t spindle_register ( const spindle_ptrs_t spindle,
const char *  name 
)

Register a spindle with the core.

Parameters
spindlepointer to a spindle_ptrs_t structure.
namepointer to a null terminated string.
Returns
assigned spindle id as a spindle_id_t if successful, -1 if not.

NOTE: The first spindle registered will become the default active spindle. NOTE: up to N_SPINDLE spindles can be registered at a time.

◆ spindle_restore()

bool spindle_restore ( spindle_ptrs_t spindle,
spindle_state_t  state,
float  rpm 
)

Restore spindle running state with direction, enable, spindle RPM and appropriate delay.

Parameters
spindlepointer to a spindle_ptrs_t structure.
statea spindle_state_t structure.
rpmthe spindle RPM to set.
Returns
true if successful, false if the current controller state is ABORTED.

◆ spindle_select()

bool spindle_select ( spindle_id_t  spindle_id)

Enables a spindle and sets it as default spindle (spindle number 0).

Parameters
spindle_idspindle id as a spindle_id_t.
Returns
true if succsesful, false if not.

◆ spindle_set_at_speed_range()

bool spindle_set_at_speed_range ( spindle_ptrs_t spindle,
spindle_data_t spindle_data,
float  rpm 
)

◆ spindle_set_override()

void spindle_set_override ( spindle_ptrs_t spindle,
override_t  speed_override 
)

Set spindle speed override.

Parameters
spindlepointer to a spindle_ptrs_t structure.
speed_overrideoverride as a percentage of the programmed RPM.

NOTE: Unlike motion overrides, spindle overrides do not require a planner reinitialization.

◆ spindle_set_rpm()

float spindle_set_rpm ( spindle_ptrs_t spindle,
float  rpm,
override_t  override_pct 
)

Calculate and set programmed RPM according to override and max/min limits.

Parameters
spindlepointer to a spindle_ptrs_t structure.
rpmthe programmed RPM.
override_pctoverride value in percent.
Returns
the calulated RPM.

◆ spindle_set_state()

bool spindle_set_state ( spindle_ptrs_t spindle,
spindle_state_t  state,
float  rpm 
)

Immediately sets spindle running state with direction and spindle rpm, if enabled. Called by g-code parser spindle_sync(), parking retract and restore, g-code program end, sleep, and spindle stop override.

Parameters
spindlepointer to a spindle_ptrs_t structure.
statea spindle_state_t structure.
rpmthe spindle RPM to set.
Returns
true if successful, false if the current controller state is ABORTED.

◆ spindle_sync()

bool spindle_sync ( spindle_ptrs_t spindle,
spindle_state_t  state,
float  rpm 
)

G-code parser entry-point for setting spindle state. Forces a planner buffer sync and bails if an abort or check-mode is active. If the spindle supports at speed functionality it will wait for it to reach the speed and raise an alarm if the speed is not reached within the timeout period.

Parameters
spindlepointer to a spindle_ptrs_t structure.
statea spindle_state_t structure.
rpmthe spindle RPM to set.
Returns
true if successful, false if the current controller state is ABORTED.

◆ spindle_update_caps()

void spindle_update_caps ( spindle_ptrs_t spindle,
spindle_pwm_t pwm_caps 
)

Update the capabilities of a registered PWM spindle. May be used by the driver on spindle initialization or when spindle settings has been changed.

Parameters
spindlepointer to a spindle_ptrs_t structure.
pwm_capspointer to a spindle_pwm_t structure.