grblHAL core  20261015
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;
51  uint32_t step_event_count;
52 #if PLANNER_ADD_MOTION_MODE
53  motion_mode_t motion_mode;
54 #endif
55  float steps_per_mm;
56  float millimeters;
58  char *message;
63  bool dynamic_rpm;
67 
68 typedef struct st_segment {
69  uint_fast8_t id;
70  struct st_segment *next;
72  uint32_t cycles_per_tick;
73  float current_rate;
75  uint_fast16_t n_step;
76  uint_fast16_t spindle_pwm;
77  float spindle_rpm;
79  bool spindle_sync;
80  bool cruising;
81  uint_fast8_t amass_level;
85 
87 typedef struct stepper {
88  bool new_block;
92  uint_fast8_t amass_level;
93  uint_fast16_t step_count;
94  uint32_t step_event_count;
97 // uint_fast16_t spindle_pwm;
102 
103 #if ENABLE_JERK_ACCELERATION
104 
105 //#define JERK_LOG 400
106 
107 #ifdef JERK_LOG
108 
109 typedef struct {
110  uint32_t idx, rd, d, ru;
111  float accel, max_accel, jerk;
112  struct {
113  float s, s0, v, v0, a0, da, t, mm_remaining, time_var;
114  uint32_t time;
115  uint32_t n_step;
116  uint32_t acc_step;
117  bool accel; // accelerating
118  bool ramp_down; // accel ramp down
119  } data[400];
120 
121 } jlog_t;
122 
123 #endif
124 
125 #endif
126 
127 // Initialize and setup the stepper motor subsystem
128 void stepper_init (void);
129 
130 // Enable steppers, but cycle does not start unless called by motion control or realtime command.
131 void st_wake_up (void);
132 
133 // Immediately disables steppers
134 void st_go_idle (void);
135 
136 // Returns true if motion is ongoing
137 bool st_is_stepping (void);
138 
139 // Reset the stepper subsystem variables
140 void st_reset (void);
141 
142 // Called by spindle_set_state() to inform about RPM changes.
143 void st_rpm_changed (float rpm);
144 
145 // Changes the run state of the step segment buffer to execute the special parking motion.
146 void st_parking_setup_buffer (void);
147 
148 // Restores the step segment buffer to the normal run state after a parking motion.
149 void st_parking_restore_buffer (void);
150 
151 // Reloads step segment buffer. Called continuously by realtime execution system.
152 void st_prep_buffer (void);
153 
154 // Called by planner_recalculate() when the executing block is updated by the new plan.
155 void st_update_plan_block_parameters (bool fast_hold);
156 
157 // Called by realtime status reporting if realtime rate reporting is enabled in config.h.
158 float st_get_realtime_rate (void);
159 
161 
165 
166 #endif
int8_t offset_id_t
Definition: gcode.h:38
motion_mode_t
Definition: gcode.h:97
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:186
void(* spindle_update_rpm_ptr)(struct spindle_ptrs *spindle, float rpm)
Pointer to function for updating spindle RPM.
Definition: spindle_control.h:191
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:812
float st_get_realtime_rate(void)
Definition: stepper.c:1460
void st_parking_restore_buffer(void)
Definition: stepper.c:859
void st_prep_buffer(void)
Definition: stepper.c:890
void stepper_init(void)
void st_rpm_changed(float rpm)
Definition: stepper.c:806
void st_wake_up(void)
Definition: stepper.c:235
struct st_segment segment_t
axes_signals_t st_get_enable_out(void)
Definition: stepper.c:1494
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:841
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:255
bool st_is_stepping(void)
Definition: stepper.c:295
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
motion_mode_t st_get_motion_mode(void)
Definition: stepper.c:1471
void st_reset(void)
Definition: stepper.c:733
offset_id_t st_get_offset_id(void)
Definition: stepper.c:1480
Data for M62, M63 and M67 commands when executed synchronized with motion.
Definition: gcode.h:309
Handlers and data for spindle support.
Definition: spindle_control.h:318
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:58
struct st_block * next
Pointer to next element in cirular list of blocks.
Definition: stepper.h:49
spindle_ptrs_t * spindle
Pointer to current spindle for motions that require dynamic RPM adjustment.
Definition: stepper.h:65
gc_override_flags_t overrides
Block bitfield variable for overrides.
Definition: stepper.h:61
bool backlash_motion
Definition: stepper.h:62
volatile output_command_t * output_commands
Output commands (linked list) to be performed when block is executed.
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:63
steps_t steps
Definition: stepper.h:50
axes_signals_t direction
Definition: stepper.h:60
float programmed_rate
Definition: stepper.h:57
offset_id_t offset_id
Definition: stepper.h:64
float millimeters
Definition: stepper.h:56
float steps_per_mm
Definition: stepper.h:55
Definition: stepper.h:68
float spindle_rpm
Spindle RPM to be set at the start of the segment execution.
Definition: stepper.h:77
uint_fast16_t spindle_pwm
Spindle PWM to be set at the start of segment execution.
Definition: stepper.h:76
ramp_type_t ramp_type
Segment ramp type.
Definition: stepper.h:78
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:83
float current_rate
Definition: stepper.h:73
uint32_t cycles_per_tick
Step distance traveled per ISR tick, aka step rate.
Definition: stepper.h:72
struct st_segment * next
Pointer to next element in cirular list of segments.
Definition: stepper.h:70
uint_fast8_t amass_level
Indicates AMASS level for the ISR to execute this segment.
Definition: stepper.h:81
uint_fast8_t id
Id may be used by driver to track changes.
Definition: stepper.h:69
st_block_t * exec_block
Pointer to the block data for the segment.
Definition: stepper.h:71
uint_fast16_t n_step
Number of step events to be executed for this segment.
Definition: stepper.h:75
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:82
bool cruising
True when in cruising part of profile, only set for spindle synced moves.
Definition: stepper.h:80
float target_position
Target position of segment relative to block start, used by spindle sync code.
Definition: stepper.h:74
bool spindle_sync
True if block is spindle synchronized.
Definition: stepper.h:79
Stepper ISR data struct. Contains the running data for the main stepper ISR.
Definition: stepper.h:87
axes_signals_t dir_changed
Per axis bits set to true on direction changes, might be referenced by driver for advanced functional...
Definition: stepper.h:89
bool new_block
Set to true when a new block is started, might be referenced by driver code for advanced functionalit...
Definition: stepper.h:88
steps_t counter
Counter variables for the Bresenham line tracer.
Definition: stepper.h:96
uint_fast8_t amass_level
AMASS level for this segment.
Definition: stepper.h:92
uint32_t step_event_count
Number of step pulse events to be output by this segment.
Definition: stepper.h:94
st_block_t * exec_block
Pointer to the block data for the segment being executed.
Definition: stepper.h:98
st_block_t * resume_block
Block that was executing when the segment buffer last ran empty.
Definition: stepper.h:99
steps_t steps
Number of step pulse event events per axis step pulse generated.
Definition: stepper.h:95
uint_fast16_t step_count
Steps remaining in line segment motion.
Definition: stepper.h:93
axes_signals_t dir_out
The direction signals to be output. The direction signals may be output only when stepper....
Definition: stepper.h:91
segment_t * exec_segment
Pointer to the segment being executed.
Definition: stepper.h:100
axes_signals_t step_out
The stepping signals to be output.
Definition: stepper.h:90
Definition: nuts_bolts.h:254
Override flags.
Definition: gcode.h:400
Definition: planner.h:52