Skip to content

Commit f5d720e

Browse files
committed
new motion control events
new motion control events to allow control of the amount of line segments to be sent to the planner and pre-modify the segment information
1 parent 0138972 commit f5d720e

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

uCNC/src/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ __NOTE__: Not all event hooks might be listed here. To find all available event
6666
| mc_home_axis_start | homing_status_t* | ENABLE_MOTION_CONTROL_MODULES | Fires once per axis when homing motion is starting. Pointer to homing_status_t struct with homming information |
6767
| mc_home_axis_finish | homing_status_t* | ENABLE_MOTION_CONTROL_MODULES | Fires once per axis when homing motion is finnished |
6868
| mc_line_segment | motion_data_t* | ENABLE_MOTION_CONTROL_MODULES | Fires when a line segment is about to be sent from the motion control to the planner. Arg is a pointer to a motion_data_t struct with the current motion block data |
69+
| mc_line_calc_segments | uint32_t* | ENABLE_MOTION_CONTROL_MODULES & MOTION_SEGMENTED | Allows to modify the amount of segments to be passed to the planner on a mc_line command. Arg is a pointer to an integer that defines the amount of segments to break the motion into. |
70+
| mc_line_segment_pre | mc_line_segment_pre_args_t* | ENABLE_MOTION_CONTROL_MODULES | Fired before a calling mc_line_segment with the data to be sent to the planner. Arg is a pointer to a mc_line_segment_pre_args_t struct with the current target coordinates, target step position and motion block data |
6971
| planner_pre_output | planner_block_t* | ENABLE_PLANNER_MODULES | **WARNING: This event must not run any code that requires to know the code context (like printing) to prevent deadlocks**Fires right before a section of the current planner block is sent to the step generation (interpolator) ISR. It's possible to perform changes and modifications to the steps being generated with a very small delay. The delay is defined by the size of the interpolator buffer `INTERPOLATOR_BUFFER_SIZE` and the stepping sample frequency `INTERPOLATOR_FREQ` |
7072

7173
Each of these events exposes a delegate and and event that have the following naming convention:

uCNC/src/core/motion_control.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727

2828
// segmented motions
2929
#if (defined(KINEMATICS_MOTION_BY_SEGMENTS) || defined(BRESENHAM_16BIT) || defined(ENABLE_G39_H_MAPPING))
30+
#ifndef MOTION_SEGMENTED
3031
#define MOTION_SEGMENTED
3132
#endif
33+
#endif
3234

3335
// non uniform segmented motions
3436
#if (defined(KINEMATICS_MOTION_BY_SEGMENTS) || defined(ENABLE_G39_H_MAPPING))
@@ -84,6 +86,18 @@ FORCEINLINE static float mc_apply_hmap(float *target);
8486
#endif
8587

8688
#ifdef ENABLE_MOTION_CONTROL_MODULES
89+
// event_mc_line_calc_segments_handler
90+
WEAK_EVENT_HANDLER(mc_line_calc_segments)
91+
{
92+
DEFAULT_EVENT_HANDLER(mc_line_calc_segments);
93+
}
94+
95+
// event_mc_line_segment_pre_handler
96+
WEAK_EVENT_HANDLER(mc_line_segment_pre)
97+
{
98+
DEFAULT_EVENT_HANDLER(mc_line_segment_pre);
99+
}
100+
87101
// event_mc_line_segment_handler
88102
WEAK_EVENT_HANDLER(mc_line_segment)
89103
{
@@ -561,6 +575,13 @@ uint8_t mc_line(float *target, motion_data_t *block_data)
561575
}
562576
#endif
563577

578+
// final modifier is a hookable event
579+
// this allows to complete control and override over the amount of segments to be produced.
580+
#ifdef ENABLE_MOTION_CONTROL_MODULES
581+
// event_mc_line_segment_handler
582+
EVENT_INVOKE(mc_line_calc_segments, &line_segments);
583+
#endif
584+
564585
if (line_segments > 1)
565586
{
566587
float m_inv = 1.0f / (float)line_segments;
@@ -591,6 +612,11 @@ uint8_t mc_line(float *target, motion_data_t *block_data)
591612
prev_target[AXIS_TOOL] += h_offset;
592613
#endif
593614
kinematics_coordinates_to_steps(prev_target, step_new_pos);
615+
#ifdef ENABLE_MOTION_CONTROL_MODULES
616+
// event_mc_line_segment_handler
617+
mc_line_segment_pre_args_t args = {.target = prev_target, .target_steps = step_new_pos, .block_data = block_data};
618+
EVENT_INVOKE(mc_line_segment_pre, &args);
619+
#endif
594620
error = mc_line_segment(step_new_pos, block_data);
595621
#ifdef ENABLE_G39_H_MAPPING
596622
// unmodify target
@@ -610,6 +636,11 @@ uint8_t mc_line(float *target, motion_data_t *block_data)
610636
{
611637
kinematics_coordinates_to_steps(target, step_new_pos);
612638
}
639+
#endif
640+
#ifdef ENABLE_MOTION_CONTROL_MODULES
641+
// event_mc_line_segment_handler
642+
mc_line_segment_pre_args_t args = {.target = target, .target_steps = step_new_pos, .block_data = block_data};
643+
EVENT_INVOKE(mc_line_segment_pre, &args);
613644
#endif
614645
error = mc_line_segment(step_new_pos, block_data);
615646

@@ -847,7 +878,7 @@ bool mc_home_motion(uint8_t axis_mask, bool is_origin_search, bool fast_mode)
847878
{
848879
return false;
849880
}
850-
881+
851882
mc_line(target, &block_data);
852883
itp_sync();
853884
if (cnc_get_exec_state(EXEC_HOMING_HIT) != EXEC_HOMING_HIT)
@@ -991,8 +1022,8 @@ uint8_t mc_home_axis(uint8_t axis_mask, uint8_t axis_limit)
9911022
}
9921023

9931024
cnc_dwell_ms(g_settings.debounce_ms); // adds a delay before reading io pin (debounce)
994-
io_enable_limits(); // temporary limits disable
995-
limits_flags = io_get_raw_limits(); // get the raw (unfiltered values)
1025+
io_enable_limits(); // temporary limits disable
1026+
limits_flags = io_get_raw_limits(); // get the raw (unfiltered values)
9961027

9971028
// all limits should be cleared
9981029
if (limits_flags)

uCNC/src/core/motion_control.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ extern "C"
136136
#endif
137137

138138
#ifdef ENABLE_MOTION_CONTROL_MODULES
139+
// event_mc_line_calc_segments_handler
140+
DECL_EVENT_HANDLER(mc_line_calc_segments);
141+
typedef struct mc_line_segment_pre_args_
142+
{
143+
float *target;
144+
uint32_t *target_steps;
145+
motion_data_t *block_data;
146+
} mc_line_segment_pre_args_t;
147+
// event_mc_line_segment_pre_handler
148+
DECL_EVENT_HANDLER(mc_line_segment_pre);
139149
// event_mc_line_segment_handler
140150
DECL_EVENT_HANDLER(mc_line_segment);
141151
// event_mc_home_start

0 commit comments

Comments
 (0)