forked from grblHAL/Plugins_spindle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepper.c
229 lines (173 loc) · 5.95 KB
/
stepper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
stepper.c - stepper motor spindle driver
!!! EXPERIMENTAL !!!
Part of grblHAL
Copyright (c) 2023-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "shared.h"
#if SPINDLE_ENABLE & (1<<SPINDLE_STEPPER)
#if N_AXIS < 4
//#error Stepper spindle can only bind to an axis > Z axis!
#endif
#include "grbl/stepper2.h"
#include "grbl/protocol.h"
#include "grbl/state_machine.h"
static spindle_id_t spindle_id = -1;
static uint8_t axis_idx = N_AXIS - 1;
static bool stopping = false, running = false;
static st2_motor_t *motor;
static spindle_data_t spindle_data = {0};
static axes_signals_t steppers_enabled = {0};
static on_spindle_selected_ptr on_spindle_selected;
static on_execute_realtime_ptr on_execute_realtime = NULL, on_execute_delay;
static stepper_enable_ptr stepper_enable;
static void stepperEnable (axes_signals_t enable)
{
steppers_enabled = enable;
if(running)
enable.mask |= 1 << axis_idx;
stepper_enable(enable);
}
static void onExecuteRealtime (uint_fast16_t state)
{
if(!st2_motor_run(motor)) {
if(stopping) {
stopping = running = false;
stepperEnable(steppers_enabled);
}
}
on_execute_realtime(state);
}
static void spindleUpdateRPM (spindle_ptrs_t *spindle, float rpm)
{
UNUSED(spindle);
spindle_data.rpm = rpm;
st2_motor_set_speed(motor, rpm);
}
// Start or stop spindle
static void spindleSetState (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
{
UNUSED(spindle);
if(state.on) {
running = true;
stopping = false;
stepperEnable(steppers_enabled);
if(st2_motor_running(motor)) {
if(state.ccw != spindle_data.state_programmed.ccw) {
st2_motor_stop(motor);
while(st2_motor_running(motor))
onExecuteRealtime(state_get());
st2_motor_move(motor, state.ccw ? -1.0f : 1.0f, rpm, Stepper2_InfiniteSteps);
} else
st2_motor_set_speed(motor, rpm);
} else
st2_motor_move(motor, state.ccw ? -1.0f : 1.0f, rpm, Stepper2_InfiniteSteps);
} else
stopping = st2_motor_stop(motor);
if(settings.spindle.at_speed_tolerance > 0.0f) {
float tolerance = rpm * settings.spindle.at_speed_tolerance / 100.0f;
spindle_data.rpm_low_limit = rpm - tolerance;
spindle_data.rpm_high_limit = rpm + tolerance;
}
spindle_data.state_programmed.on = state.on;
spindle_data.state_programmed.ccw = state.ccw;
spindle_data.rpm_programmed = spindle_data.rpm = rpm;
}
static bool spindleConfig (spindle_ptrs_t *spindle)
{
if(spindle == NULL)
return false;
return st2_motor_bind_spindle(axis_idx);
}
#if SPINDLE_SYNC_ENABLE
int64_t offset = 0;
static spindle_data_t *spindleGetData (spindle_data_request_t request)
{
int64_t position = st2_get_position(motor) - offset;
switch(request) {
case SpindleData_Counters:
spindle_data.index_count = (uint32_t)(position / settings.axis[0].steps_per_mm);
spindle_data.pulse_count = position;
break;
case SpindleData_RPM:
//if(!stopped)
// spindle_data.rpm = ;
break;
case SpindleData_AngularPosition:
spindle_data.angular_position = (float)position / (float)settings.axis[0].steps_per_mm;
break;
}
return &spindle_data;
}
static void spindleDataReset (void)
{
offset = st2_get_position(motor);
}
#endif // SPINDLE_SYNC_ENABLE
// Returns spindle state in a spindle_state_t variable
static spindle_state_t spindleGetState (spindle_ptrs_t *spindle)
{
spindle_state_t state = {0};
UNUSED(spindle);
state.on = spindle_data.state_programmed.on;
state.ccw = spindle_data.state_programmed.ccw;
state.at_speed = running ? st2_motor_cruising(motor) : !running;
return state;
}
static void onExecuteDelay (uint_fast16_t state)
{
st2_motor_run(motor);
on_execute_delay(state);
}
static void stepper_spindle_selected (spindle_ptrs_t *spindle)
{
if(on_spindle_selected)
on_spindle_selected(spindle);
#if SPINDLE_SYNC_ENABLE
hal.spindle_data.get = spindleGetData;
hal.spindle_data.reset = spindleDataReset;
#endif
}
/*
static void raise_alarm (sys_state_t state)
{
system_raise_alarm(Alarm_Spindle);
}
*/
void stepper_spindle_init (void)
{
static const spindle_ptrs_t spindle = {
.type = SpindleType_Stepper,
.cap.variable = On,
.cap.at_speed = On,
.cap.direction = On,
.cap.gpio_controlled = On,
.config = spindleConfig,
.set_state = spindleSetState,
.get_state = spindleGetState,
.update_rpm = spindleUpdateRPM
};
if(hal.get_micros && hal.stepper.output_step && (motor = st2_motor_init(axis_idx, true)) && (spindle_id = spindle_register(&spindle, "Stepper")) != -1) {
on_spindle_selected = grbl.on_spindle_selected;
grbl.on_spindle_selected = stepper_spindle_selected;
on_execute_realtime = grbl.on_execute_realtime;
grbl.on_execute_realtime = onExecuteRealtime;
on_execute_delay = grbl.on_execute_delay;
grbl.on_execute_delay = onExecuteDelay;
stepper_enable = hal.stepper.enable;
hal.stepper.enable = stepperEnable;
} else
protocol_enqueue_foreground_task(report_warning, "Stepper spindle has been disabled!");
}
#endif