grblHAL core  20250320
stepper.h
Go to the documentation of this file.
1 /*
2  stepper.h - stepper motor driver: executes motion plans of planner.c using the stepper motors
3 
4  Part of grblHAL
5 
6  Copyright (c) 2019-2025 Terje Io
7  Copyright (c) 2011-2016 Sungeun K. Jeon for Gnea Research LLC
8  Copyright (c) 2009-2011 Simen Svale Skogsrud
9 
10  grblHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  grblHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "planner.h"
25 
26 #ifndef _STEPPER_H_
27 #define _STEPPER_H_
28 
29 typedef enum {
34 
35 typedef enum {
41 
47 typedef struct st_block {
48  uint_fast8_t id;
49  struct st_block *next;
50  uint32_t steps[N_AXIS];
51  uint32_t step_event_count;
52  float steps_per_mm;
53  float millimeters;
55  char *message;
60  bool dynamic_rpm;
64 
65 typedef struct st_segment {
66  uint_fast8_t id;
67  struct st_segment *next;
69  uint32_t cycles_per_tick;
70  float current_rate;
72  uint_fast16_t n_step;
73  uint_fast16_t spindle_pwm;
74  float spindle_rpm;
76  bool spindle_sync;
77  bool cruising;
78  uint_fast8_t amass_level;
82 
84 typedef struct stepper {
85  uint32_t counter_x,
88  #ifdef A_AXIS
89  , counter_a
90  #endif
91  #ifdef B_AXIS
92  , counter_b
93  #endif
94  #ifdef C_AXIS
95  , counter_c
96  #endif
97  #ifdef U_AXIS
98  , counter_u
99  #endif
100  #ifdef V_AXIS
101  , counter_v
102  #endif
103 ;
104  bool new_block;
105  bool dir_change;
108  uint32_t steps[N_AXIS];
109  uint_fast8_t amass_level;
110 // uint_fast16_t spindle_pwm;
111  uint_fast16_t step_count;
112  uint32_t step_event_count;
116 
117 // Initialize and setup the stepper motor subsystem
118 void stepper_init (void);
119 
120 // Enable steppers, but cycle does not start unless called by motion control or realtime command.
121 void st_wake_up (void);
122 
123 // Immediately disables steppers
124 void st_go_idle (void);
125 
126 // Returns true if motion is ongoing
127 bool st_is_stepping (void);
128 
129 // Reset the stepper subsystem variables
130 void st_reset (void);
131 
132 // Called by spindle_set_state() to inform about RPM changes.
133 void st_rpm_changed (float rpm);
134 
135 // Changes the run state of the step segment buffer to execute the special parking motion.
136 void st_parking_setup_buffer (void);
137 
138 // Restores the step segment buffer to the normal run state after a parking motion.
139 void st_parking_restore_buffer (void);
140 
141 // Reloads step segment buffer. Called continuously by realtime execution system.
142 void st_prep_buffer (void);
143 
144 // Called by planner_recalculate() when the executing block is updated by the new plan.
145 void st_update_plan_block_parameters (bool fast_hold);
146 
147 // Called by realtime status reporting if realtime rate reporting is enabled in config.h.
148 float st_get_realtime_rate (void);
149 
151 
153 
154 #endif
#define N_AXIS
Defines number of axes supported - minimum 3, maximum 8. If more than 3 axes are configured a complia...
Definition: config.h:42
int8_t offset_id_t
Definition: gcode.h:36
void(* spindle_update_pwm_ptr)(struct spindle_ptrs *spindle, uint_fast16_t pwm)
Pointer to function for updating spindle speed on the fly.
Definition: spindle_control.h:170
void(* spindle_update_rpm_ptr)(struct spindle_ptrs *spindle, float rpm)
Pointer to function for updating spindle RPM.
Definition: spindle_control.h:175
void stepper_driver_interrupt_handler(void)
squaring_mode_t
Definition: stepper.h:29
@ SquaringMode_A
1
Definition: stepper.h:31
@ SquaringMode_B
2
Definition: stepper.h:32
@ SquaringMode_Both
0
Definition: stepper.h:30
void st_update_plan_block_parameters(bool fast_hold)
Definition: stepper.c:587
float st_get_realtime_rate(void)
Definition: stepper.c:1150
void st_parking_restore_buffer(void)
Definition: stepper.c:634
void st_prep_buffer(void)
Definition: stepper.c:665
void stepper_init(void)
void st_rpm_changed(float rpm)
Definition: stepper.c:581
void st_wake_up(void)
Definition: stepper.c:192
struct st_segment segment_t
struct st_block st_block_t
Holds the planner block Bresenham algorithm execution data for the segments in the segment buffer.
void st_parking_setup_buffer(void)
Definition: stepper.c:616
struct stepper stepper_t
Stepper ISR data struct. Contains the running data for the main stepper ISR.
void st_go_idle(void)
Definition: stepper.c:203
bool st_is_stepping(void)
Definition: stepper.c:226
ramp_type_t
Definition: stepper.h:35
@ Ramp_DecelOverride
Definition: stepper.h:39
@ Ramp_Cruise
Definition: stepper.h:37
@ Ramp_Decel
Definition: stepper.h:38
@ Ramp_Accel
Definition: stepper.h:36
void st_reset(void)
Definition: stepper.c:532
offset_id_t st_get_offset_id(void)
Definition: stepper.c:1161
Data for M62, M63 and M67 commands when executed synchronized with motion.
Definition: gcode.h:278
Handlers and data for spindle support.
Definition: spindle_control.h:284
Holds the planner block Bresenham algorithm execution data for the segments in the segment buffer.
Definition: stepper.h:47
char * message
Message to be displayed when block is executed.
Definition: stepper.h:55
struct st_block * next
Pointer to next element in cirular list of blocks.
Definition: stepper.h:49
axes_signals_t direction_bits
Definition: stepper.h:57
spindle_ptrs_t * spindle
Pointer to current spindle for motions that require dynamic RPM adjustment.
Definition: stepper.h:62
gc_override_flags_t overrides
Block bitfield variable for overrides.
Definition: stepper.h:58
bool backlash_motion
Definition: stepper.h:59
uint_fast8_t id
Id may be used by driver to track changes.
Definition: stepper.h:48
uint32_t step_event_count
Definition: stepper.h:51
bool dynamic_rpm
Tracks motions that require dynamic RPM adjustment.
Definition: stepper.h:60
uint32_t steps[N_AXIS]
Definition: stepper.h:50
float programmed_rate
Definition: stepper.h:54
offset_id_t offset_id
Definition: stepper.h:61
float millimeters
Definition: stepper.h:53
float steps_per_mm
Definition: stepper.h:52
output_command_t * output_commands
Output commands (linked list) to be performed when block is executed.
Definition: stepper.h:56
Definition: stepper.h:65
float spindle_rpm
Spindle RPM to be set at the start of the segment execution.
Definition: stepper.h:74
uint_fast16_t spindle_pwm
Spindle PWM to be set at the start of segment execution.
Definition: stepper.h:73
ramp_type_t ramp_type
Segment ramp type.
Definition: stepper.h:75
spindle_update_rpm_ptr update_rpm
Valid pointer to spindle.update_rpm() if set spindle speed at the start of the segment execution.
Definition: stepper.h:80
float current_rate
Definition: stepper.h:70
uint32_t cycles_per_tick
Step distance traveled per ISR tick, aka step rate.
Definition: stepper.h:69
struct st_segment * next
Pointer to next element in cirular list of segments.
Definition: stepper.h:67
uint_fast8_t amass_level
Indicates AMASS level for the ISR to execute this segment.
Definition: stepper.h:78
uint_fast8_t id
Id may be used by driver to track changes.
Definition: stepper.h:66
st_block_t * exec_block
Pointer to the block data for the segment.
Definition: stepper.h:68
uint_fast16_t n_step
Number of step events to be executed for this segment.
Definition: stepper.h:72
spindle_update_pwm_ptr update_pwm
Valid pointer to spindle.update_pwm() if set spindle speed at the start of the segment execution.
Definition: stepper.h:79
bool cruising
True when in cruising part of profile, only set for spindle synced moves.
Definition: stepper.h:77
float target_position
Target position of segment relative to block start, used by spindle sync code.
Definition: stepper.h:71
bool spindle_sync
True if block is spindle synchronized.
Definition: stepper.h:76
Stepper ISR data struct. Contains the running data for the main stepper ISR.
Definition: stepper.h:84
bool new_block
Set to true when a new block is started, might be referenced by driver code for advanced functionalit...
Definition: stepper.h:104
axes_signals_t dir_outbits
The direction signals to be output. The direction signals may be output only when stepper::dir_change...
Definition: stepper.h:107
uint_fast8_t amass_level
AMASS level for this segment.
Definition: stepper.h:109
uint32_t step_event_count
Number of step pulse events to be output by this segment.
Definition: stepper.h:112
st_block_t * exec_block
Pointer to the block data for the segment being executed.
Definition: stepper.h:113
bool dir_change
Set to true on direction changes, might be referenced by driver for advanced functionality.
Definition: stepper.h:105
uint32_t counter_x
Counter variable for the Bresenham line tracer, X-axis.
Definition: stepper.h:85
uint32_t counter_y
Counter variable for the Bresenham line tracer, Y-axis.
Definition: stepper.h:86
uint_fast16_t step_count
Steps remaining in line segment motion.
Definition: stepper.h:111
uint32_t steps[N_AXIS]
Number of step pulse event events per axis step pulse generated.
Definition: stepper.h:108
segment_t * exec_segment
Pointer to the segment being executed.
Definition: stepper.h:114
axes_signals_t step_outbits
The stepping signals to be output.
Definition: stepper.h:106
uint32_t counter_z
< Counter variable for the Bresenham line tracer, Z-axis
Definition: stepper.h:103
Definition: nuts_bolts.h:125
Override flags.
Definition: gcode.h:337