-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodometer.c
More file actions
276 lines (211 loc) · 7.8 KB
/
odometer.c
File metadata and controls
276 lines (211 loc) · 7.8 KB
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
/*
odometer.c - axis odometers including run time + spindle run time
Part of grblHAL
Copyright (c) 2020-2025 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 ODOMETER_ENABLE
#include <string.h>
#include <stdio.h>
#include "grbl/system.h"
#include "grbl/protocol.h"
#include "grbl/nvs_buffer.h"
#include "grbl/task.h"
typedef struct {
uint64_t motors;
uint64_t spindle;
float distance[N_AXIS];
} odometer_data_t;
static uint32_t steps[N_AXIS] = {0};
static bool odometer_changed = false;
static uint32_t odometers_address, odometers_address_prv;
static odometer_data_t odometers, odometers_prv;
static nvs_io_t nvs;
static stepper_pulse_start_ptr stepper_pulse_start;
static on_state_change_ptr on_state_change;
static on_spindle_selected_ptr on_spindle_selected;
static spindle_set_state_ptr spindle_set_state_;
static settings_changed_ptr settings_changed;
static on_report_options_ptr on_report_options;
static void stepperPulseStart (stepper_t *stepper)
{
odometer_changed = true;
if(stepper->step_out.x)
steps[X_AXIS]++;
if(stepper->step_out.y)
steps[Y_AXIS]++;
if(stepper->step_out.z)
steps[Z_AXIS]++;
#ifdef A_AXIS
if(stepper->step_out.a)
steps[A_AXIS]++;
#endif
#ifdef B_AXIS
if(stepper->step_out.b)
steps[B_AXIS]++;
#endif
#ifdef C_AXIS
if(stepper->step_out.c)
steps[C_AXIS]++;
#endif
stepper_pulse_start(stepper);
}
void onStateChanged (sys_state_t state)
{
static uint32_t ms = 0;
if(state & (STATE_CYCLE|STATE_JOG|STATE_HOMING|STATE_SAFETY_DOOR))
ms = hal.get_elapsed_ticks();
else if(odometer_changed) {
uint_fast8_t idx = N_AXIS;
odometer_changed = false;
odometers.motors += (hal.get_elapsed_ticks() - ms);
do {
if(steps[--idx]) {
odometers.distance[idx] += (float)steps[idx] / settings.axis[idx].steps_per_mm;
steps[idx] = 0;
}
} while(idx);
nvs.memcpy_to_nvs(odometers_address, (uint8_t *)&odometers, sizeof(odometer_data_t), true);
}
if(on_state_change)
on_state_change(state);
}
// Called by foreground process.
static void odometers_write (void *data)
{
nvs.memcpy_to_nvs(odometers_address, (uint8_t *)&odometers, sizeof(odometer_data_t), true);
}
ISR_CODE static void ISR_FUNC(onSpindleSetState)(spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
{
static uint32_t ms = 0;
spindle_set_state_(spindle, state, rpm);
if(state.on)
ms = hal.get_elapsed_ticks();
else if(ms) {
odometers.spindle += (hal.get_elapsed_ticks() - ms);
ms = 0;
// Write odometer data in foreground process.
task_add_immediate(odometers_write, NULL);
}
}
static void onSpindleSelected (spindle_ptrs_t *spindle)
{
if(spindle->id == 0 && spindle->set_state != onSpindleSetState) {
spindle_set_state_ = spindle->set_state;
spindle->set_state = onSpindleSetState;
}
if(on_spindle_selected)
on_spindle_selected(spindle);
}
// Reclaim entry points that may have been changed on settings change.
static void onSettingsChanged (settings_t *settings, settings_changed_flags_t changed)
{
settings_changed(settings, changed);
if(hal.stepper.pulse_start != stepperPulseStart) {
stepper_pulse_start = hal.stepper.pulse_start;
hal.stepper.pulse_start = stepperPulseStart;
}
}
static void odometer_data_reset (bool backup)
{
if(backup) {
memcpy(&odometers_prv, &odometers, sizeof(odometer_data_t));
nvs.memcpy_to_nvs(odometers_address_prv, (uint8_t *)&odometers_prv, sizeof(odometer_data_t), true);
}
memset(&odometers, 0, sizeof(odometer_data_t));
nvs.memcpy_to_nvs(odometers_address, (uint8_t *)&odometers, sizeof(odometer_data_t), true);
}
static void odometers_report (odometer_data_t *odometers)
{
char buf[40];
uint_fast8_t idx;
uint32_t hr = odometers->spindle / 3600000, min = (odometers->spindle / 60000) % 60;
sprintf(buf, "SPINDLEHRS %ld:%.2ld", hr, min);
report_message(buf, Message_Plain);
hr = odometers->motors / 3600000;
min = (odometers->motors / 60000) % 60;
sprintf(buf, "MOTORHRS %ld:%.2ld", hr, min);
report_message(buf, Message_Plain);
for(idx = 0 ; idx < N_AXIS ; idx++) {
sprintf(buf, "ODOMETER%s %s", axis_letter[idx], ftoa(odometers->distance[idx] / 1000.0f, 1)); // meters
report_message(buf, Message_Plain);
}
}
static status_code_t odometer_command (sys_state_t state, char *args)
{
status_code_t retval = Status_Unhandled;
if(args == NULL) {
odometers_report(&odometers);
retval = Status_OK;
} else {
strcaps(args);
if(!strcmp(args, "PREV")) {
if(nvs.memcpy_from_nvs((uint8_t *)&odometers_prv, odometers_address_prv, sizeof(odometer_data_t), true) == NVS_TransferResult_OK)
odometers_report(&odometers_prv);
else
report_message("Previous odometer values not available", Message_Warning);
retval = Status_OK;
}
if(!strcmp(args, "RST")) {
odometer_data_reset(true);
retval = Status_OK;
}
}
return retval;
}
const sys_command_t odometer_command_list[] = {
{"ODOMETERS", odometer_command, {}, {
.str = "$ODOMETERS - list odometer log"
ASCII_EOL "$ODOMETERS=PREV - list previous odometer log when available"
ASCII_EOL "$ODOMETERS=RST - copy current log to previous and clear current"
} }
};
static sys_commands_t odometer_commands = {
.n_commands = sizeof(odometer_command_list) / sizeof(sys_command_t),
.commands = odometer_command_list
};
static void onReportOptions (bool newopt)
{
on_report_options(newopt);
if(newopt)
hal.stream.write(",ODO");
else
report_plugin("Odometers", "0.08");
}
void odometer_init()
{
memcpy(&nvs, nvs_buffer_get_physical(), sizeof(nvs_io_t));
if(!(nvs.type == NVS_EEPROM || nvs.type == NVS_FRAM))
task_run_on_startup(report_warning, "EEPROM or FRAM is required for odometers!");
else if(NVS_SIZE - GRBL_NVS_SIZE - hal.nvs.driver_area.size < ((sizeof(odometer_data_t) + NVS_CRC_BYTES) * 2))
task_run_on_startup(report_warning, "Not enough NVS storage for odometers!");
else {
odometers_address = NVS_SIZE - (sizeof(odometer_data_t) + NVS_CRC_BYTES);
odometers_address_prv = odometers_address - (sizeof(odometer_data_t) + NVS_CRC_BYTES);
if(nvs.memcpy_from_nvs((uint8_t *)&odometers, odometers_address, sizeof(odometer_data_t), true) != NVS_TransferResult_OK)
odometer_data_reset(false);
hal.driver_cap.odometers = On;
on_state_change = grbl.on_state_change;
grbl.on_state_change = onStateChanged;
on_report_options = grbl.on_report_options;
grbl.on_report_options = onReportOptions;
settings_changed = hal.settings_changed;
hal.settings_changed = onSettingsChanged;
on_spindle_selected = grbl.on_spindle_selected;
grbl.on_spindle_selected = onSpindleSelected;
stepper_pulse_start = hal.stepper.pulse_start;
hal.stepper.pulse_start = stepperPulseStart;
system_register_commands(&odometer_commands);
}
}
#endif // ODOMETER_ENABLE