-
Notifications
You must be signed in to change notification settings - Fork 1
/
coolant.c
298 lines (229 loc) · 9.83 KB
/
coolant.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
coolant.c - plugin for for handling laser coolant
Part of grblHAL
Copyright (c) 2020-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 "driver.h"
#if LASER_COOLANT_ENABLE
#include <string.h>
#include <math.h>
#ifdef ARDUINO
#include "../grbl/hal.h"
#include "../grbl/protocol.h"
#include "../grbl/nvs_buffer.h"
#else
#include "grbl/hal.h"
#include "grbl/protocol.h"
#include "grbl/nvs_buffer.h"
#endif
typedef union {
uint8_t value;
struct {
uint8_t enable : 1;
};
} coolant_options_t;
typedef struct {
coolant_options_t options;
float min_temp;
float max_temp;
float on_delay;
float off_delay;
uint8_t coolant_ok_port;
uint8_t coolant_temp_port;
} laser_coolant_settings_t;
static uint8_t coolant_ok_port, coolant_temp_port;
static bool coolant_on = false, monitor_on = false, can_monitor = false, coolant_off_pending = false;
static on_report_options_ptr on_report_options;
static on_realtime_report_ptr on_realtime_report;
static coolant_ptrs_t on_coolant_changed;
static nvs_address_t nvs_address;
static laser_coolant_settings_t coolant_settings;
static uint8_t n_ain, n_din;
static char max_aport[4], max_dport[4];
static void coolant_lost_handler (uint8_t port, bool state)
{
if(coolant_on && !coolant_off_pending)
system_set_exec_alarm(Alarm_AbortCycle);
}
static void coolant_flood_off (void *data)
{
coolant_state_t mode = hal.coolant.get_state();
mode.flood = Off;
on_coolant_changed.set_state(mode);
coolant_off_pending = coolant_on = false;
sys.report.coolant = On; // Set to report change immediately
}
// Start/stop tube coolant, wait for ok signal on start if delay is configured.
static void coolantSetState (coolant_state_t mode)
{
static bool irq_checked = false;
bool changed = mode.flood != hal.coolant.get_state().flood || (mode.flood && coolant_off_pending);
if(changed && !mode.flood) {
if(coolant_settings.off_delay > 0.0f && !sys.reset_pending) {
mode.flood = On;
coolant_off_pending = task_add_delayed(coolant_flood_off, NULL, (uint32_t)(coolant_settings.off_delay * 60.0f * 1000.0f));
on_coolant_changed.set_state(mode);
return;
}
coolant_on = false;
}
on_coolant_changed.set_state(mode);
if(changed && mode.flood) {
task_delete(coolant_flood_off, NULL);
coolant_off_pending = false;
if(coolant_settings.on_delay > 0.0f && hal.port.wait_on_input(Port_Digital, coolant_ok_port, WaitMode_High, coolant_settings.on_delay) != 1) {
mode.flood = Off;
coolant_on = false;
on_coolant_changed.set_state(mode);
system_set_exec_alarm(Alarm_AbortCycle);
} else
coolant_on = true;
}
if(!irq_checked) {
irq_checked = true;
if(hal.port.get_pin_info) {
xbar_t *port = hal.port.get_pin_info(Port_Digital, Port_Input, coolant_ok_port);
if(port && (port->cap.irq_mode & IRQ_Mode_Falling))
hal.port.register_interrupt_handler(coolant_ok_port, IRQ_Mode_Falling, coolant_lost_handler);
}
}
monitor_on = mode.flood && (coolant_settings.min_temp + coolant_settings.max_temp) > 0.0f;
}
static void onRealtimeReport (stream_write_ptr stream_write, report_tracking_flags_t report)
{
static float coolant_temp_prev = 0.0f;
char buf[20] = "";
if(can_monitor) {
float coolant_temp = (float)hal.port.wait_on_input(Port_Analog, coolant_temp_port, WaitMode_Immediate, 0.0f) / 10.0f;
if(coolant_temp_prev != coolant_temp || report.all) {
strcat(buf, "|TCT:");
strcat(buf, ftoa(coolant_temp, 1));
coolant_temp_prev = coolant_temp;
}
if(monitor_on && coolant_temp > coolant_settings.max_temp)
system_set_exec_alarm(Alarm_AbortCycle);
}
if(*buf != '\0')
stream_write(buf);
if(on_realtime_report)
on_realtime_report(stream_write, report);
}
static bool is_setting_available (const setting_detail_t *setting)
{
return (setting->id == Setting_CoolantMaxTemp || setting->id == Setting_CoolantTempPort) && n_ain > 0;
}
static const setting_detail_t plugin_settings[] = {
{ Setting_CoolantOnDelay, Group_Coolant, "Laser coolant on delay", "seconds", Format_Decimal, "#0.0", "0.0", "30.0", Setting_NonCore, &coolant_settings.on_delay, NULL, NULL },
{ Setting_CoolantOffDelay, Group_Coolant, "Laser coolant off delay", "minutes", Format_Decimal, "#0.0", "0.0", "30.0", Setting_NonCore, &coolant_settings.off_delay, NULL, NULL },
// { Setting_CoolantMinTemp, Group_Coolant, "Laser coolant min temp", "deg", Format_Decimal, "#0.0", "0.0", "30.0", Setting_NonCore, &coolant_settings.min_temp, NULL, NULL, false },
{ Setting_CoolantMaxTemp, Group_Coolant, "Laser coolant max temp", "deg", Format_Decimal, "#0.0", "0.0", "30.0", Setting_NonCore, &coolant_settings.max_temp, NULL, is_setting_available },
{ Setting_CoolantTempPort, Group_AuxPorts, "Coolant temperature port", NULL, Format_Int8, "#0", "0", max_aport, Setting_NonCore, &coolant_settings.coolant_temp_port, NULL, is_setting_available, { .reboot_required = On } },
{ Setting_CoolantOkPort, Group_AuxPorts, "Coolant ok port", NULL, Format_Int8, "#0", "0", max_dport, Setting_NonCore, &coolant_settings.coolant_ok_port, NULL, NULL, { .reboot_required = On } }
};
#ifndef NO_SETTINGS_DESCRIPTIONS
static const setting_descr_t plugin_settings_descr[] = {
{ Setting_CoolantOnDelay, "" },
{ Setting_CoolantOffDelay, "" },
{ Setting_CoolantMaxTemp, "" },
{ Setting_CoolantTempPort, "Aux port number to use for coolant temperature monitoring." },
{ Setting_CoolantOkPort, "Aux port number to use for coolant ok signal." },
};
#endif
static void coolant_settings_save (void)
{
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&coolant_settings, sizeof(laser_coolant_settings_t), true);
}
static void coolant_settings_restore (void)
{
coolant_settings.min_temp =
coolant_settings.max_temp =
coolant_settings.on_delay =
coolant_settings.off_delay = 0.0f;
if(ioport_can_claim_explicit()) {
coolant_settings.coolant_temp_port = n_ain ? n_ain - 1 : 0;
coolant_settings.coolant_ok_port= n_din - 1;
}
coolant_settings_save();
}
static void coolant_settings_load (void)
{
bool ok = true;
if(hal.nvs.memcpy_from_nvs((uint8_t *)&coolant_settings, nvs_address, sizeof(laser_coolant_settings_t), true) != NVS_TransferResult_OK)
coolant_settings_restore();
if(ioport_can_claim_explicit()) {
// Sanity checks
if(coolant_settings.coolant_temp_port > n_ain)
coolant_settings.coolant_temp_port = n_ain ? n_ain - 1 : 0;
if(coolant_settings.coolant_ok_port > n_din)
coolant_settings.coolant_ok_port = n_din - 1;
coolant_temp_port = coolant_settings.coolant_temp_port;
coolant_ok_port = coolant_settings.coolant_ok_port;
if(n_ain > 0)
ok = (can_monitor = ioport_claim(Port_Analog, Port_Input, &coolant_temp_port, "Coolant temperature"));
ok &= ioport_claim(Port_Digital, Port_Input, &coolant_ok_port, "Coolant ok");
}
if(ok) {
on_realtime_report = grbl.on_realtime_report;
grbl.on_realtime_report = onRealtimeReport;
memcpy(&on_coolant_changed, &hal.coolant, sizeof(coolant_ptrs_t));
hal.coolant.set_state = coolantSetState;
}
}
static void report_options (bool newopt)
{
on_report_options(newopt);
if(!newopt)
report_plugin("Laser coolant", "0.06");
}
void laser_coolant_init (void)
{
static setting_details_t setting_details = {
.settings = plugin_settings,
.n_settings = sizeof(plugin_settings) / sizeof(setting_detail_t),
#ifndef NO_SETTINGS_DESCRIPTIONS
.descriptions = plugin_settings_descr,
.n_descriptions = sizeof(plugin_settings_descr) / sizeof(setting_descr_t),
#endif
.save = coolant_settings_save,
.load = coolant_settings_load,
.restore = coolant_settings_restore,
};
bool ok;
n_ain = ioports_available(Port_Analog, Port_Input);
n_din = ioports_available(Port_Digital, Port_Input);
ok = n_din >= 1;
if(ok) {
if(!ioport_can_claim_explicit()) {
// Driver does not support explicit port claiming, claim the highest numbered ports instead.
if((ok = (nvs_address = nvs_alloc(sizeof(laser_coolant_settings_t))))) {
if(hal.port.num_analog_in > 0) {
coolant_temp_port = hal.port.num_analog_in - 1;
can_monitor = ioport_claim(Port_Analog, Port_Input, &coolant_temp_port, "Coolant temperature");
}
coolant_ok_port = hal.port.num_digital_in - 1;
ioport_claim(Port_Digital, Port_Input, &coolant_ok_port, "Coolant ok");
}
} else
ok = (nvs_address = nvs_alloc(sizeof(laser_coolant_settings_t)));
}
if(ok) {
if(n_ain)
strcpy(max_aport, uitoa(n_ain - 1));
strcpy(max_dport, uitoa(n_din - 1));
on_report_options = grbl.on_report_options;
grbl.on_report_options = report_options;
settings_register(&setting_details);
} else
protocol_enqueue_foreground_task(report_warning, "Laser coolant plugin failed to initialize!");
}
#endif