Skip to content

Commit

Permalink
Merge pull request #592 from Paciente8159/fix-tool-speed-report
Browse files Browse the repository at this point in the history
fixed tool speed report functions
  • Loading branch information
Paciente8159 committed Jan 19, 2024
2 parents 67de21d + a64ab51 commit 9883870
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 38 deletions.
2 changes: 1 addition & 1 deletion uCNC/src/core/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ int16_t planner_get_spindle_speed(float scale)
scaled_spindle = 0.01f * (float)g_planner_state.spindle_speed_override * scaled_spindle;
}
scaled_spindle = CLAMP(g_settings.spindle_min_rpm, scaled_spindle, g_settings.spindle_max_rpm);
int16_t output = tool_range_speed(scaled_spindle);
int16_t output = tool_range_speed(scaled_spindle, 0);

return (!neg) ? output : -output;
}
Expand Down
5 changes: 4 additions & 1 deletion uCNC/src/hal/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void dummy_tool_set_speed(int16_t value)
// with this value some operation must be done in order to update the tool
}
static int16_t dummy_tool_range_speed(int16_t value)
static int16_t dummy_tool_range_speed(int16_t value, uint8_t conv)
{
// in this function you do all your calculations to convert from GCode S speed to tool IO control speed (to PWM value or other for example)
Expand All @@ -86,6 +86,9 @@ static int16_t dummy_tool_range_speed(int16_t value)
// do something to value and return the converted value, that can be either a convertion formula or a lookup table
// conv = 0 (to convert from S to tool IO control speed)
// conv = 1 (to convert from tool IO control speed to S)
return value;
}
Expand Down
8 changes: 4 additions & 4 deletions uCNC/src/hal/tools/tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ uint16_t tool_get_speed()
{
return tool_current.get_speed();
}
return tool_current_speed;
return tool_get_setpoint();
#endif
return 0;
}
Expand All @@ -224,19 +224,19 @@ int16_t tool_get_setpoint(void)
{
// input value will always be positive
#if TOOL_COUNT > 0
return tool_current_speed;
return tool_range_speed(tool_current_speed, 1);
#endif
return 0;
}

int16_t tool_range_speed(int16_t value)
int16_t tool_range_speed(int16_t value, uint8_t conv)
{
// input value will always be positive
#if TOOL_COUNT > 0
if (tool_current.range_speed)
{
value = ABS(value);
return tool_current.range_speed(value);
return tool_current.range_speed(value, conv);
}
#endif
return value;
Expand Down
20 changes: 10 additions & 10 deletions uCNC/src/hal/tools/tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C"
#include <stdint.h>

typedef void (*tool_func)(void);
typedef int16_t (*tool_range_speed_func)(int16_t);
typedef int16_t (*tool_range_speed_func)(int16_t, uint8_t);
typedef uint16_t (*tool_get_speed_func)(void);
typedef void (*tool_set_speed_func)(int16_t);
typedef void (*tool_coolant_func)(uint8_t);
Expand All @@ -47,15 +47,15 @@ extern "C"
tool_coolant_func set_coolant; /*enables/disables the coolant*/
} tool_t;

void tool_init(void); // initializes tool inside µCNC
void tool_change(uint8_t tool); // executes a tool change µCNC. This runs the shutdown code of the current tool and then runs the startup code of the next tool
void tool_pid_update(void); // if tool PID option is enabled this function is called in the main loop to update the tool or make some adjustment
int16_t tool_get_setpoint(void); // return the current tool setpoint. That is the value set with S parameter in gcode
int16_t tool_range_speed(int16_t value); // this function is always called when you send an M3, M4 or S command. It runs before tool_set_speed function and converts from S<value> to whatever tool value needed (for example if using PWM will convert to a PWM value 0-255)
uint16_t tool_get_speed(void); // this function returns the current tool speed. Always returns the setpoint value, unless the tool has a custom get_speed function (for example to return the true speed of a feedback sensor)
void tool_set_speed(int16_t value); // this sets the tool speed. The value passed to this function is the actual IO value needed (for example a PWM value). On M5 or tool shutdown, this value is always 0.
void tool_set_coolant(uint8_t value); // this gets a maks with the coolant outputs to enable(1) or disable(0)
void tool_stop(void); // this stops the tool and coolant
void tool_init(void); // initializes tool inside µCNC
void tool_change(uint8_t tool); // executes a tool change µCNC. This runs the shutdown code of the current tool and then runs the startup code of the next tool
void tool_pid_update(void); // if tool PID option is enabled this function is called in the main loop to update the tool or make some adjustment
int16_t tool_get_setpoint(void); // return the current tool setpoint. That is the value set with S parameter in gcode
int16_t tool_range_speed(int16_t value, uint8_t conv); // this function converts from GCode S to tool speed and back. For example if using PWM will convert from tool speed S to a PWM value 0-255 if conv=0 or vice-versa if conv=1
uint16_t tool_get_speed(void); // this function returns the current tool speed. Always returns the setpoint value, unless the tool has a custom get_speed function (for example to return the true speed of a feedback sensor)
void tool_set_speed(int16_t value); // this sets the tool speed. The value passed to this function is the actual IO value needed (for example a PWM value). On M5 or tool shutdown, this value is always 0.
void tool_set_coolant(uint8_t value); // this gets a maks with the coolant outputs to enable(1) or disable(0)
void tool_stop(void); // this stops the tool and coolant

#ifdef __cplusplus
}
Expand Down
28 changes: 15 additions & 13 deletions uCNC/src/hal/tools/tools/laser_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,22 @@ static void set_speed(int16_t value)
#endif
}

static int16_t range_speed(int16_t value)
static int16_t range_speed(int16_t value, uint8_t conv)
{
if (value == 0)
{
return 0;
}

// converts core tool speed to laser power (PWM)
value = (int16_t)(LASER_PWM_MIN_VALUE + ((255.0f - LASER_PWM_MIN_VALUE) * (((float)value) / g_settings.spindle_max_rpm)));
if (!conv)
{
value = (int16_t)(LASER_PWM_MIN_VALUE + ((255.0f - LASER_PWM_MIN_VALUE) * (((float)value) / g_settings.spindle_max_rpm)));
}
else
{
value = (int16_t)roundf((1.0f / (float)(255.0f - LASER_PWM_MIN_VALUE)) * (value - LASER_PWM_MIN_VALUE) * g_settings.spindle_max_rpm);
}
return value;
}

Expand All @@ -94,21 +106,11 @@ static void set_coolant(uint8_t value)
#endif
}

static uint16_t get_speed(void)
{
#if ASSERT_PIN(LASER_PWM)
float laser = (float)io_get_pwm(LASER_PWM) * g_settings.spindle_max_rpm * UINT8_MAX_INV;
return (uint16_t)lroundf(laser);
#else
return 0;
#endif
}

const tool_t laser_pwm = {
.startup_code = &startup_code,
.shutdown_code = &shutdown_code,
.pid_update = NULL,
.range_speed = &range_speed,
.get_speed = &get_speed,
.get_speed = NULL,
.set_speed = &set_speed,
.set_coolant = &set_coolant};
11 changes: 9 additions & 2 deletions uCNC/src/hal/tools/tools/pen_servo.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ static void shutdown_code(void)
#endif
}

static int16_t range_speed(int16_t value)
static int16_t range_speed(int16_t value, uint8_t conv)
{
if (value == 0)
{
return 0;
}

value = (int16_t)((PEN_SERVO_RANGE) * (((float)value) / g_settings.spindle_max_rpm) + PEN_SERVO_LOW);
if (!conv)
{
value = (int16_t)((PEN_SERVO_RANGE) * (((float)value) / g_settings.spindle_max_rpm) + PEN_SERVO_LOW);
}
else{
value = (int16_t)roundf((1.0f / (float)PEN_SERVO_RANGE) * (value - PEN_SERVO_LOW) * g_settings.spindle_max_rpm);
}

return value;
}

Expand Down
11 changes: 9 additions & 2 deletions uCNC/src/hal/tools/tools/spindle_besc.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ static void shutdown_code(void)
#endif
}

static int16_t range_speed(int16_t value)
static int16_t range_speed(int16_t value, uint8_t conv)
{
if (value == 0)
{
return 0;
}

value = (int16_t)((SPINDLE_BESC_RANGE) * (((float)value) / g_settings.spindle_max_rpm) + SPINDLE_BESC_LOW);
if (!conv)
{
value = (int16_t)((SPINDLE_BESC_RANGE) * (((float)value) / g_settings.spindle_max_rpm) + SPINDLE_BESC_LOW);
}
else
{
value = (int16_t)roundf((1.0f / (float)SPINDLE_BESC_RANGE) * (value - SPINDLE_BESC_LOW) * g_settings.spindle_max_rpm);
}
return value;
}

Expand Down
12 changes: 10 additions & 2 deletions uCNC/src/hal/tools/tools/spindle_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,17 @@ static void set_coolant(uint8_t value)
#endif
}

static int16_t range_speed(int16_t value)
static int16_t range_speed(int16_t value, uint8_t conv)
{
value = (int16_t)((255.0f) * (((float)value) / g_settings.spindle_max_rpm));
// converts core tool speed to laser power (PWM)
if (!conv)
{
value = (int16_t)((255.0f) * (((float)value) / g_settings.spindle_max_rpm));
}
else
{
value = (int16_t)roundf((1.0f / 255.0f) * value * g_settings.spindle_max_rpm);
}
return value;
}

Expand Down
14 changes: 11 additions & 3 deletions uCNC/src/hal/tools/tools/vfd_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void startup_code(void)

#if defined(ENABLE_TOOL_PID_CONTROLLER) && !defined(DISABLE_VFD_PWM_PID)
EXTENDED_SETTING_INIT(VFD_PWM_PID_SETTING_ID, vfd_pwm_pid.k);
settings_load(EXTENDED_SETTING_ADDRESS(VFD_PWM_PID_SETTING_ID), (uint8_t*)vfd_pwm_pid.k, sizeof(vfd_pwm_pid.k));
settings_load(EXTENDED_SETTING_ADDRESS(VFD_PWM_PID_SETTING_ID), (uint8_t *)vfd_pwm_pid.k, sizeof(vfd_pwm_pid.k));
vfd_pwm_pid.max = g_settings.spindle_max_rpm;
vfd_pwm_pid.min = g_settings.spindle_min_rpm;
#endif
Expand Down Expand Up @@ -102,9 +102,17 @@ static void set_coolant(uint8_t value)
#endif
}

static int16_t range_speed(int16_t value)
static int16_t range_speed(int16_t value, uint8_t conv)
{
value = (int16_t)((255.0f) * (((float)value) / g_settings.spindle_max_rpm));
// converts core tool speed to laser power (PWM)
if (!conv)
{
value = (int16_t)((255.0f) * (((float)value) / g_settings.spindle_max_rpm));
}
else
{
value = (int16_t)roundf((1.0f / 255.0f) * value * g_settings.spindle_max_rpm);
}
return value;
}

Expand Down

0 comments on commit 9883870

Please sign in to comment.