-
Notifications
You must be signed in to change notification settings - Fork 1
/
ppi.c
257 lines (198 loc) · 6.77 KB
/
ppi.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
ppi.c - plugin for for laser PPI (Pulses Per Inch) mode
Part of grblHAL
Copyright (c) 2020-2023 Terje Io
Grbl 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.
Grbl 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 Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if PPI_ENABLE
#include <math.h>
#include <string.h>
#include "grbl/hal.h"
typedef struct {
uint_fast16_t ppi;
float ppi_distance;
float ppi_pos;
float next_pos;
uint_fast16_t pulse_length; // uS
bool on;
} laser_ppi_t;
static laser_ppi_t laser = {
.ppi = 600.0f,
.ppi_distance = 25.4f / 600.0f,
.pulse_length = 1500,
.on = false
};
static user_mcode_ptrs_t user_mcode;
static on_report_options_ptr on_report_options;
static void (*stepper_wake_up)(void);
static void (*stepper_pulse_start)(stepper_t *stepper);
static spindle_pulse_on_ptr pulse_on;
static on_spindle_selected_ptr on_spindle_selected;
static spindle_update_pwm_ptr spindle_update_pwm;
static spindle_update_rpm_ptr spindle_update_rpm;
static void stepperWakeUp (void)
{
laser.ppi_pos = laser.next_pos = 0.0f;
stepper_wake_up();
}
static void stepperPulseStartPPI (stepper_t *stepper)
{
static float mm_per_step;
if(laser.on) {
if(stepper->new_block)
mm_per_step = 1.0f / stepper->exec_block->steps_per_mm;
if(stepper->step_outbits.mask) {
laser.ppi_pos += mm_per_step;
if(laser.ppi_pos >= laser.next_pos) {
laser.next_pos += laser.ppi_distance;
pulse_on(laser.pulse_length);
}
}
}
stepper_pulse_start(stepper);
}
static void ppiUpdatePWM (spindle_ptrs_t *spindle, uint_fast16_t pwm)
{
if(!laser.on && pwm > 0)
laser.ppi_pos = laser.next_pos = 0.0f;
laser.on = pwm > 0;
spindle_update_pwm(spindle, pwm);
pulse_on(laser.pulse_length);
}
static void ppiUpdateRPM (spindle_ptrs_t *spindle, float rpm)
{
if(!laser.on && rpm > 0.0f)
laser.ppi_pos = laser.next_pos = 0.0f;
laser.on = rpm > 0.0f;
spindle_update_rpm(spindle, rpm);
}
static bool enable_ppi (bool on)
{
if(!gc_laser_ppi_enable(on ? laser.ppi : 0, laser.pulse_length)) {
if(on && stepper_wake_up == NULL) {
stepper_wake_up = hal.stepper.wake_up;
hal.stepper.wake_up = stepperWakeUp;
stepper_pulse_start = hal.stepper.pulse_start;
hal.stepper.pulse_start = stepperPulseStartPPI;
}
if(!on && stepper_wake_up != NULL) {
hal.stepper.wake_up = stepper_wake_up;
stepper_wake_up = NULL;
hal.stepper.pulse_start = stepper_pulse_start;
stepper_pulse_start = NULL;
}
}
return on;
}
static user_mcode_type_t userMCodeCheck (user_mcode_t mcode)
{
return mcode == LaserPPI_Enable || mcode == LaserPPI_Rate || mcode == LaserPPI_PulseLength
? UserMCode_Normal
: (user_mcode.check ? user_mcode.check(mcode) : UserMCode_Unsupported);
}
static status_code_t userMCodeValidate (parser_block_t *gc_block)
{
status_code_t state = Status_GcodeValueWordMissing;
switch(gc_block->user_mcode) {
case LaserPPI_Enable:
if(!hal.driver_cap.laser_ppi_mode)
state = Status_GcodeUnsupportedCommand;
else if(gc_block->words.p) {
state = Status_OK;
gc_block->words.p = Off;
}
break;
case LaserPPI_Rate:
if(!hal.driver_cap.laser_ppi_mode)
state = Status_GcodeUnsupportedCommand;
else if(gc_block->words.p) {
state = Status_OK;
gc_block->user_mcode_sync = true;
gc_block->words.p = Off;
}
break;
case LaserPPI_PulseLength:
if(!hal.driver_cap.laser_ppi_mode)
state = Status_GcodeUnsupportedCommand;
else if(gc_block->words.p) {
state = Status_OK;
gc_block->user_mcode_sync = true;
gc_block->words.p = Off;
}
break;
default:
state = Status_Unhandled;
break;
}
return state == Status_Unhandled && user_mcode.validate ? user_mcode.validate(gc_block) : state;
}
static void userMCodeExecute (uint_fast16_t state, parser_block_t *gc_block)
{
static bool ppi_on = false;
bool handled = true;
if (state != STATE_CHECK_MODE)
switch(gc_block->user_mcode) {
case LaserPPI_Enable:
ppi_on = gc_block->values.p != 0.0f;
enable_ppi(ppi_on && laser.ppi > 0 && laser.pulse_length > 0);
break;
case LaserPPI_Rate:
if((laser.ppi = (uint_fast16_t)gc_block->values.p) != 0)
laser.ppi_distance = 25.4f / (float)laser.ppi;
enable_ppi(ppi_on && laser.ppi > 0 && laser.pulse_length > 0);
break;
case LaserPPI_PulseLength:
laser.pulse_length = (uint16_t)gc_block->values.p;
enable_ppi(ppi_on && laser.ppi > 0 && laser.pulse_length > 0);
break;
default:
handled = false;
break;
}
if(!handled && user_mcode.execute)
user_mcode.execute(state, gc_block);
}
static void onSpindleSelected (spindle_ptrs_t *spindle)
{
if((hal.driver_cap.laser_ppi_mode = spindle->cap.laser && spindle->pulse_on != NULL)) {
pulse_on = spindle->pulse_on;
if(spindle->update_pwm) {
spindle_update_pwm = spindle->update_pwm;
spindle->update_pwm = ppiUpdatePWM;
}
if(spindle->update_rpm) {
spindle_update_rpm = spindle->update_rpm;
spindle->update_rpm = ppiUpdateRPM;
}
}
if(on_spindle_selected)
on_spindle_selected(spindle);
}
static void onReportOptions (bool newopt)
{
on_report_options(newopt);
if(!newopt)
report_plugin("Laser PPI", "0.07");
}
void ppi_init (void)
{
memcpy(&user_mcode, &grbl.user_mcode, sizeof(user_mcode_ptrs_t));
grbl.user_mcode.check = userMCodeCheck;
grbl.user_mcode.validate = userMCodeValidate;
grbl.user_mcode.execute = userMCodeExecute;
on_spindle_selected = grbl.on_spindle_selected;
grbl.on_spindle_selected = onSpindleSelected;
on_report_options = grbl.on_report_options;
grbl.on_report_options = onReportOptions;
}
#endif