Skip to content

Commit

Permalink
Merge pull request #34 from Paciente8159/debouncing
Browse files Browse the repository at this point in the history
Added limit switch software debouncing option to µCNC (on AVR is built around RTC functions)
  • Loading branch information
Paciente8159 authored Jun 17, 2021
2 parents 91e4599 + ae830f5 commit b0d8816
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions uCNC/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@
#define DEFAULT_HARD_LIMITS_ENABLED 0
#define DEFAULT_SOFT_LIMITS_ENABLED 0

#define DEFAULT_DEBOUNCE_MS 250

#endif
6 changes: 5 additions & 1 deletion uCNC/mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ void mcu_change_step_ISR(uint16_t ticks, uint16_t prescaller);
void mcu_step_stop_ISR(void);

//Custom delay function
//void mcu_delay_ms(uint16_t miliseconds);
#ifdef RTC_ENABLE
//gets the mcu running time in ms
uint32_t mcu_millis();
#endif

#ifndef mcu_delay_ms
//Custom delay function
void mcu_delay_ms(uint32_t miliseconds);
#endif

//Non volatile memory
uint8_t mcu_eeprom_getc(uint16_t address);
void mcu_eeprom_putc(uint16_t address, uint8_t value);
Expand Down
12 changes: 11 additions & 1 deletion uCNC/mcus/avr/mcu_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ static __attribute__((always_inline)) void mcu_delay_1ms(void)
}while(--loop);
}*/

/*void mcu_delay_ms(uint16_t miliseconds)
/*void mcu_delay_ms(uint32_t miliseconds)
{
do{
_delay_ms(1);
Expand All @@ -959,6 +959,16 @@ uint32_t mcu_millis()
return val;
}

void mcu_delay_ms(uint32_t miliseconds)
{
uint32_t t_start = mcu_runtime_ms;
uint32_t t_end = mcu_runtime_ms;
while (t_end - t_start < miliseconds)
{
t_end = mcu_runtime_ms;
}
}

void mcu_start_rtc()
{
#if (F_CPU <= 16000000UL)
Expand Down
2 changes: 1 addition & 1 deletion uCNC/mcus/stm32f10x/mcu_stm32f10x.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ void mcu_step_stop_ISR(void)
}

//Custom delay function
//void mcu_delay_ms(uint16_t miliseconds);
//void mcu_delay_ms(uint32_t miliseconds);

#ifdef RTC_ENABLE
//gets the mcu running time in ms
Expand Down
4 changes: 4 additions & 0 deletions uCNC/mcus/stm32f10x/mcumap_stm32f10x.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#include "stm32f1xx_hal.h"

//defines the frequency of the mcu
#ifndef F_CPU
#define F_CPU 72000000UL
#endif
//defines the maximum and minimum step rates
#define F_STEP_MAX 30000
#define F_STEP_MIN 4
Expand Down Expand Up @@ -3029,4 +3031,6 @@
#define mcu_enable_interrupts __enable_irq
#define mcu_disable_interrupts __disable_irq

#define mcu_delay_ms(x) HAL_Delay(x)

#endif
6 changes: 3 additions & 3 deletions uCNC/mcus/virtual/mcu_virtual.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ void *comsimul(void)
{
uart_char = c;
serial_rx_isr(c);
if(c == '\n' | c=='\r')
if (c == '\n' | c == '\r')
{
while(!serial_rx_is_empty())
while (!serial_rx_is_empty())
{
usleep(1);
}
Expand Down Expand Up @@ -397,7 +397,7 @@ void mcu_step_stop_ISR(void)
pulse_enabled = false;
}

void mcu_delay_ms(uint16_t miliseconds)
void mcu_delay_ms(uint32_t miliseconds)
{
}

Expand Down
2 changes: 2 additions & 0 deletions uCNC/mcus/virtual/mcumap_virtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,6 @@ extern virtports_t virtualports;
#define COM_INREG virtualports->uart
#define COM_OUTREG virtualports->uart

#define mcu_dealy_ms(x) delay(x)

#endif
20 changes: 12 additions & 8 deletions uCNC/motion_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool mc_toogle_checkmode(void)
// 2. applies all kinematic transformations to the target
// 3. converts the target in actuator position
// 4. calculates motion change from the previous line
uint8_t mc_line(float *target, motion_data_t* block_data)
uint8_t mc_line(float *target, motion_data_t *block_data)
{
uint32_t step_new_pos[STEPPER_COUNT];
float feed = block_data->feed;
Expand Down Expand Up @@ -212,7 +212,7 @@ uint8_t mc_line(float *target, motion_data_t* block_data)
}

//applies an algorithm similar to grbl with slight changes
uint8_t mc_arc(float *target, float center_offset_a, float center_offset_b, float radius, uint8_t axis_0, uint8_t axis_1, bool isclockwise, motion_data_t* block_data)
uint8_t mc_arc(float *target, float center_offset_a, float center_offset_b, float radius, uint8_t axis_0, uint8_t axis_1, bool isclockwise, motion_data_t *block_data)
{
float mc_position[AXIS_COUNT];

Expand Down Expand Up @@ -253,7 +253,7 @@ uint8_t mc_arc(float *target, float center_offset_a, float center_offset_b, floa
radiusangle = fast_flt_div2(radiusangle);
float diameter = fast_flt_mul2(radius);
uint16_t segment_count = floor(fabs(radiusangle) / sqrt(g_settings.arc_tolerance * (diameter - g_settings.arc_tolerance)));
float arc_per_sgm = (segment_count != 0) ? arc_angle/segment_count : arc_angle;
float arc_per_sgm = (segment_count != 0) ? arc_angle / segment_count : arc_angle;
float dist_sgm = 0;

//for all other axis finds the linear motion distance
Expand Down Expand Up @@ -343,7 +343,7 @@ uint8_t mc_arc(float *target, float center_offset_a, float center_offset_b, floa
return mc_line(target, block_data);
}

uint8_t mc_dwell(motion_data_t* block_data)
uint8_t mc_dwell(motion_data_t *block_data)
{
if (mc_checkmode) // check mode (gcode simulation) doesn't send code to planner
{
Expand Down Expand Up @@ -433,6 +433,7 @@ uint8_t mc_home_axis(uint8_t axis, uint8_t axis_limit)
return EXEC_ALARM_HOMING_FAIL_RESET;
}

mcu_delay_ms(g_settings.debounce_ms); //adds a delay before reading io pin (debounce)
limits_flags = io_get_limits();

//the wrong switch was activated bails
Expand Down Expand Up @@ -473,6 +474,7 @@ uint8_t mc_home_axis(uint8_t axis, uint8_t axis_limit)
}
} while (cnc_get_exec_state(EXEC_RUN));

mcu_delay_ms(g_settings.debounce_ms); //adds a delay before reading io pin (debounce)
//resets limit mask
g_settings.limits_invert_mask ^= axis_limit;
//stops, flushes buffers and clears the hold if active
Expand All @@ -486,6 +488,7 @@ uint8_t mc_home_axis(uint8_t axis, uint8_t axis_limit)
return EXEC_ALARM_HOMING_FAIL_RESET;
}

mcu_delay_ms(g_settings.debounce_ms); //adds a delay before reading io pin (debounce)
limits_flags = io_get_limits();

if (CHECKFLAG(limits_flags, axis_limit))
Expand All @@ -496,7 +499,7 @@ uint8_t mc_home_axis(uint8_t axis, uint8_t axis_limit)
return STATUS_OK;
}

uint8_t mc_update_tools(motion_data_t* block_data)
uint8_t mc_update_tools(motion_data_t *block_data)
{
if (mc_checkmode) // check mode (gcode simulation) doesn't send code to planner
{
Expand All @@ -516,7 +519,7 @@ uint8_t mc_update_tools(motion_data_t* block_data)
return STATUS_OK;
}

uint8_t mc_probe(float *target, bool invert_probe, motion_data_t* block_data)
uint8_t mc_probe(float *target, bool invert_probe, motion_data_t *block_data)
{
#ifdef PROBE
uint8_t prev_state = cnc_get_exec_state(EXEC_HOLD);
Expand All @@ -531,8 +534,8 @@ uint8_t mc_probe(float *target, bool invert_probe, motion_data_t* block_data)
return STATUS_CRITICAL_FAIL;
}

#if(defined(FORCE_SOFT_POLLING) || (PROBEEN_MASK!=PROBEISR_MASK))
if(io_get_probe())
#if (defined(FORCE_SOFT_POLLING) || (PROBEEN_MASK != PROBEISR_MASK))
if (io_get_probe())
{
io_probe_isr();
break;
Expand All @@ -545,6 +548,7 @@ uint8_t mc_probe(float *target, bool invert_probe, motion_data_t* block_data)
itp_clear();
planner_clear();
cnc_clear_exec_state(~prev_state & EXEC_HOLD); //restores HOLD previous state
mcu_delay_ms(g_settings.debounce_ms); //adds a delay before reading io pin (debounce)
bool probe_notok = (!invert_probe) ? io_get_probe() : !io_get_probe();
if (probe_notok)
{
Expand Down
1 change: 1 addition & 0 deletions uCNC/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ void protocol_send_ucnc_settings(void)
protocol_send_gcode_setting_line_int(23, g_settings.homing_dir_invert_mask);
protocol_send_gcode_setting_line_flt(24, g_settings.homing_slow_feed_rate);
protocol_send_gcode_setting_line_flt(25, g_settings.homing_fast_feed_rate);
protocol_send_gcode_setting_line_int(26, g_settings.debounce_ms);
protocol_send_gcode_setting_line_flt(27, g_settings.homing_offset);
protocol_send_gcode_setting_line_flt(30, g_settings.spindle_max_rpm);
protocol_send_gcode_setting_line_flt(31, g_settings.spindle_min_rpm);
Expand Down
10 changes: 7 additions & 3 deletions uCNC/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include "parser.h"
#include "cnc.h"

//if settings struct is changed this version has to change too
#define SETTINGS_VERSION "V02"
//if settings struct is changed this version should change too
#define SETTINGS_VERSION "V03"

settings_t g_settings;

Expand Down Expand Up @@ -189,7 +189,8 @@ const settings_t __rom__ default_settings =
.hard_limits_enabled = DEFAULT_HARD_LIMITS_ENABLED,
.homing_enabled = DEFAULT_HOMING_ENABLED,
.spindle_max_rpm = DEFAULT_SPINDLE_MAX_RPM,
.spindle_min_rpm = DEFAULT_SPINDLE_MIN_RPM};
.spindle_min_rpm = DEFAULT_SPINDLE_MIN_RPM,
.debounce_ms = DEFAULT_DEBOUNCE_MS};

//static uint8_t settings_crc;

Expand Down Expand Up @@ -357,6 +358,9 @@ uint8_t settings_change(uint8_t setting, float value)
case 25:
g_settings.homing_fast_feed_rate = value;
break;
case 26:
g_settings.debounce_ms = value16;
break;
case 27:
g_settings.homing_offset = value;
break;
Expand Down
1 change: 1 addition & 0 deletions uCNC/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef struct
bool soft_limits_enabled;
bool hard_limits_enabled;
bool homing_enabled;
uint16_t debounce_ms;
uint8_t homing_dir_invert_mask;
float homing_fast_feed_rate;
float homing_slow_feed_rate;
Expand Down

0 comments on commit b0d8816

Please sign in to comment.