/
solo-estop.c
618 lines (521 loc) · 20.1 KB
/
solo-estop.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/********************************************************************
* Description: solo-estop
* This file, 'solo-estop.c', is a HAL component that
* has inputs for various E-Stop conditions and will
* output signals to iocontrol and halui to control
* E-Stop state.
*
* Author: John Allwine <john@pocketnc.com>
* License: GPL Version 2
*
* Copyright (c) 2020 Pocket NC Company All rights reserved.
*
********************************************************************/
#include "rtapi.h" /* RTAPI realtime OS API */
#include "rtapi_app.h" /* RTAPI realtime module decls */
#include "rtapi_errno.h" /* EINVAL etc */
#include "rtapi_math.h"
#include "hal.h" /* HAL public API decls */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
MODULE_AUTHOR("John Allwine");
MODULE_DESCRIPTION("E-Stop conditions on Penta Machine Solo.");
MODULE_LICENSE("GPL");
// helper macro for easily creating HAL pins
#define PIN(type,inOrOut,dataName,pinName) \
retval = hal_pin_##type##_newf((inOrOut), &(data->dataName), comp_id, "%s." #pinName, modname); \
if(retval < 0) { \
rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: could not create pin %s." #pinName, modname, modname); \
hal_exit(comp_id); \
return -1; \
}
typedef struct {
// Latched button state variables.
// We capture when the physical E-Stop button
// was pushed or released for timing or for
// making assumptions about motor/spindle faults
// (the motors and VFD will report faults when
// they aren't powered, which they won't be if
// the physical E-Stop button is pressed).
hal_bit_t buttonPushed;
hal_bit_t buttonReleased;
// Latched fault variables.
// When a fault is detected these variables will stay high
// until reset by the user by clicking the E-Stop button in
// the UI.
hal_bit_t xFaulted;
hal_bit_t yFaulted;
hal_bit_t zFaulted;
hal_bit_t bFaulted;
hal_bit_t cFaulted;
hal_bit_t tFaulted;
hal_bit_t xFErrored;
hal_bit_t yFErrored;
hal_bit_t zFErrored;
hal_bit_t bFErrored;
hal_bit_t cFErrored;
hal_bit_t tFErrored;
hal_bit_t estop;
hal_bit_t estopped;
hal_s32_t spindleErroredWithCode;
hal_bit_t spindleModbusNotOk;
// Fault variables.
// These variables show the current state of various
// conditions that trigger a fault.
hal_bit_t *xFault; // X fault as reported from the ClearPath motors. Connect to torque.fault.x.
hal_bit_t *yFault; // Y fault as reported from the ClearPath motors. Connect to torque.fault.y.
hal_bit_t *zFault; // Z fault as reported from the ClearPath motors. Connect to torque.fault.z.
hal_bit_t *bFault; // B fault as reported from the ClearPath motors. Connect to torque.fault.b.
hal_bit_t *cFault; // C fault as reported from the ClearPath motors. Connect to torque.fault.c.
hal_bit_t *tFault; // T fault as reported from the ClearPath motors. Connect to torque.fault.t.
hal_bit_t *button; // Reports the state of the physical E-Stop button that disconnects power to the motors via a relay.
hal_s32_t *spindleErrorCode; // Reports an error code from the VFD that controls the spindle. Connect to spindle-vfd.error-code.
hal_bit_t *spindleModbusOk; // Reports whether the ModBus connection to the VFD that controls the spindle is ok. Connect to spindle-vfd.modbus-ok.
// Following error flags for each joint
hal_bit_t *xFError;
hal_bit_t *yFError;
hal_bit_t *zFError;
hal_bit_t *bFError;
hal_bit_t *cFError;
hal_bit_t *tFError;
hal_bit_t *ignoreComErrors;
// The user-request-enable pin should be connected
// to iocontrol.0.user-request-enable and indicates
// that the user wants to reset E-Stop (they clicked
// a button in the UI).
hal_bit_t *userRequestEnable;
// The latched version of userRequestEnable. This will
// stay high after the user clicks a button to reset
// E-Stop until the motors have a chance to disable then
// re-enable.
// This is also an output that must be connected to
// halui.estop.reset in order to clear the software
// E-Stop condition in the case that the user releases
// the physical E-Stop (the software E-Stop is cleared
// via the python interface when clicking the E-Stop
// button in the UI).
hal_bit_t *userRequestedEnable;
// Timer that restarts when the user clicks a button to
// reset E-Stop. Is used to time various pulse times,
// such as disabling/re-enabling the motors and turning on
// the machine.
hal_u32_t timeSinceEnable;
hal_u32_t timeSinceEStop;
// Timer that starts at initialization. Used to prevent
// motor faults at start up, when they haven't been powered
// on yet.
hal_u32_t timeSinceStartUp;
// Timer that starts once the physical E-Stop button has been
// released. Used to make assumptions about motor and spindle
// faults and to allow enough time to pass before attempting
// to re-enable motors after powering them up.
hal_u32_t timeSinceButtonRelease;
// emcEnable is False when in E-Stop and True when not in E-stop.
// Connect to iocontrol.0.emc-enable-in.
hal_bit_t *emcEnable;
// Connect to iocontrol.0.user-enable-out to trigger
// faults internal to EMC.
hal_bit_t *userEnable;
// Output pin that controls whether the machine is on.
// Connect to halui.machine.on
hal_bit_t *machineOn;
// Power is connected to a pin that can trigger the same relay as
// the physical E-Stop button in order to cut power to the spindle
// and axis motors and solenoids.
hal_bit_t *power;
// Individual enable pins for each motor. Necessary to be able to disable/re-enable
// motors after a motor fault. This will need to be revisited if we implement hard
// stop homing as the motors will also need to be controlled by the homing routine
// (see SOFT-455).
hal_bit_t *xMotorEnable;
hal_bit_t *yMotorEnable;
hal_bit_t *zMotorEnable;
hal_bit_t *bMotorEnable;
hal_bit_t *cMotorEnable;
hal_bit_t *tMotorEnable;
hal_bit_t *unhome;
} data_t;
// Max time of the timer. Since we don't need the timer for very long
// this ensures we never overflow the timer.
#define MAX_TIME 6000
#define UNHOME_TIME 100
// Time when the machine-on pin should go high after a reset.
#define MACHINE_ON_TIME 3100
// How much time to give all the motors to get out of a fault state at start up or
// after releasing the physical E-Stop button.
// The motors report a fault when powered off, so we use various conditions to
// avoid reporting a fault in that condition, such as at startup and when the
// physical E-Stop button is pushed.
#define STARTUP_TIME 3000
// How long to disable the motors for after a reset. Will be re-enabled after
// this time elapses.
#define DISABLE_MOTOR_TIME 100
// Time that must elapse after the user clicks the E-Stop reset in the UI
// before we actually reset software E-Stop (this helps prevent reporting
// motor faults when the motors are still resetting).
#define RESET_TIME 3000
static data_t *data;
static const char *modname = "solo-estop";
static int comp_id;
static void update(void *arg, long period) {
const hal_bit_t ignoreComErrors = *(data->ignoreComErrors);
const hal_bit_t notIgnoreComErrors = !ignoreComErrors;
const hal_bit_t xFault = *(data->xFault) && notIgnoreComErrors;
const hal_bit_t yFault = *(data->yFault) && notIgnoreComErrors;
const hal_bit_t zFault = *(data->zFault) && notIgnoreComErrors;
const hal_bit_t bFault = *(data->bFault) && notIgnoreComErrors;
const hal_bit_t cFault = *(data->cFault) && notIgnoreComErrors;
const hal_bit_t tFault = *(data->tFault) && notIgnoreComErrors;
const hal_bit_t xFError = *(data->xFError);
const hal_bit_t yFError = *(data->yFError);
const hal_bit_t zFError = *(data->zFError);
const hal_bit_t bFError = *(data->bFError);
const hal_bit_t cFError = *(data->cFError);
const hal_bit_t tFError = *(data->cFError);
const hal_bit_t power = *(data->power);
const hal_bit_t button = *(data->button);
const hal_s32_t spindleErrorCode = *(data->spindleErrorCode) && notIgnoreComErrors;
const hal_bit_t spindleModbusOk = *(data->spindleModbusOk) || ignoreComErrors;
const hal_u32_t timeSinceEnable = data->timeSinceEnable;
const hal_bit_t userRequestEnable = *(data->userRequestEnable);
const hal_bit_t userRequestedEnable = *(data->userRequestedEnable);
const hal_bit_t xFaulted = data->xFaulted;
const hal_bit_t yFaulted = data->yFaulted;
const hal_bit_t zFaulted = data->zFaulted;
const hal_bit_t bFaulted = data->bFaulted;
const hal_bit_t cFaulted = data->cFaulted;
const hal_bit_t tFaulted = data->tFaulted;
const hal_bit_t xFErrored = data->xFErrored;
const hal_bit_t yFErrored = data->yFErrored;
const hal_bit_t zFErrored = data->zFErrored;
const hal_bit_t bFErrored = data->bFErrored;
const hal_bit_t cFErrored = data->cFErrored;
const hal_bit_t tFErrored = data->tFErrored;
const hal_bit_t spindleErroredWithCode = data->spindleErroredWithCode;
const hal_bit_t spindleModbusNotOk = data->spindleModbusNotOk;
const hal_bit_t buttonPushed = data->buttonPushed;
const hal_bit_t buttonReleased = data->buttonReleased;
// The motors and spindle VFD report errors when power is cut due to the physical E-Stop button being pressed.
// To avoid reporting that there is a spindle or motor fault when the button is pressed, we use the following
// conditions.
// Check that the button isn't pressed or was latched.
// Check that enough time after start up has elapsed.
// Check that enough time after a user initiated E-Stop reset has elapsed.
const hal_bit_t preventFaultsFromButtonPushAndStartup = !button &&
!buttonPushed &&
power &&
data->timeSinceStartUp > STARTUP_TIME &&
data->timeSinceEnable > RESET_TIME &&
data->timeSinceButtonRelease > STARTUP_TIME;
if(xFault && preventFaultsFromButtonPushAndStartup) {
// Only report the fault when it first happens
if(!xFaulted) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Motor X fault.");
}
data->xFaulted = true;
}
if(yFault && preventFaultsFromButtonPushAndStartup) {
// Only report the fault when it first happens
if(!yFaulted) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Motor Y fault.");
}
data->yFaulted = true;
}
if(zFault && preventFaultsFromButtonPushAndStartup) {
if(!zFaulted) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Motor Z fault.");
}
data->zFaulted = true;
}
if(bFault && preventFaultsFromButtonPushAndStartup) {
if(!bFaulted) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Motor B fault.");
}
data->bFaulted = true;
}
if(cFault && preventFaultsFromButtonPushAndStartup) {
if(!cFaulted) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Motor C fault.");
}
data->cFaulted = true;
}
if(tFault && preventFaultsFromButtonPushAndStartup) {
if(!tFaulted) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Coolant motor fault.");
}
data->tFaulted = true;
}
if(xFError) {
if(!xFErrored) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: X following error.");
}
data->xFErrored = true;
}
if(yFError) {
if(!yFErrored) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Y following error.");
}
data->yFErrored = true;
}
if(zFError) {
if(!zFErrored) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Z following error.");
}
data->zFErrored = true;
}
if(bFError) {
if(!bFErrored) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: B following error.");
}
data->bFErrored = true;
}
if(cFError) {
if(!cFErrored) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: C following error.");
}
data->cFErrored = true;
}
if(tFError) {
if(!tFErrored) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Coolant motor following error.");
}
data->tFErrored = true;
}
if(spindleErrorCode != 0 && preventFaultsFromButtonPushAndStartup) {
if(spindleErroredWithCode == 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Spindle error: code %d", spindleErrorCode);
}
data->spindleErroredWithCode = spindleErrorCode;
}
if(!spindleModbusOk && preventFaultsFromButtonPushAndStartup) {
if(!spindleModbusNotOk) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop: Spindle communication error.");
}
data->spindleModbusNotOk = true;
}
if(button) {
if(!buttonPushed) {
rtapi_print_msg(RTAPI_MSG_ERR, "E-Stop button pressed.");
}
data->buttonPushed = true;
}
if(buttonPushed && !button) {
if(!buttonReleased) {
data->timeSinceButtonRelease = 0;
}
data->buttonReleased = true;
*(data->power) = 1;
}
if(userRequestEnable) {
*(data->power) = 1;
}
*(data->unhome) = data->estopped && data->timeSinceEStop > UNHOME_TIME;
// current fault state
hal_bit_t fault = xFault ||
yFault ||
zFault ||
bFault ||
cFault ||
tFault ||
xFError ||
yFError ||
zFError ||
bFError ||
cFError ||
tFError ||
!spindleModbusOk ||
spindleErrorCode != 0 ||
button;
// latched fault value
hal_bit_t faulted = xFaulted ||
yFaulted ||
zFaulted ||
bFaulted ||
cFaulted ||
tFaulted ||
xFErrored ||
yFErrored ||
zFErrored ||
bFErrored ||
cFErrored ||
tFErrored ||
spindleModbusNotOk ||
spindleErroredWithCode != 0 ||
buttonPushed;
// When user initiates an E-Stop reset
if(!(*(data->userRequestedEnable)) && (userRequestEnable || (buttonReleased && data->timeSinceButtonRelease > STARTUP_TIME))) {
// Latch the userRequestedEnable variable and reset our timer
*(data->userRequestedEnable) = 1;
data->timeSinceEnable = 0;
}
hal_bit_t reset = false;
if(*(data->userRequestedEnable)) {
// Once the user has requested to reset E-Stop, we disable
// the motors and re-enable them to clear any fault conditions.
if(data->timeSinceEnable < DISABLE_MOTOR_TIME) {
*(data->xMotorEnable) = false;
*(data->yMotorEnable) = false;
*(data->zMotorEnable) = false;
*(data->bMotorEnable) = false;
*(data->cMotorEnable) = false;
*(data->tMotorEnable) = false;
} else {
*(data->xMotorEnable) = true;
*(data->yMotorEnable) = true;
*(data->zMotorEnable) = true;
*(data->bMotorEnable) = true;
*(data->cMotorEnable) = true;
*(data->tMotorEnable) = true;
}
// Once enough time has elapsed, actually reset the E-Stop.
if(data->timeSinceEnable > RESET_TIME) {
// unlatch faults, they'll relatch if the fault continues
// so the errors will be reported again in response to the
// user attempting to reset e-stop
data->xFaulted = false;
data->yFaulted = false;
data->zFaulted = false;
data->bFaulted = false;
data->cFaulted = false;
data->tFaulted = false;
data->xFErrored = false;
data->yFErrored = false;
data->zFErrored = false;
data->bFErrored = false;
data->cFErrored = false;
data->tFErrored = false;
data->spindleErroredWithCode = 0;
data->spindleModbusNotOk = false;
data->buttonPushed = false;
data->buttonReleased = false;
data->estopped = false;
*(data->userRequestedEnable) = false;
*(data->power) = true;
reset = true;
}
}
// prevent potentially overflowing our timer variable
if(data->timeSinceButtonRelease <= MAX_TIME) {
data->timeSinceButtonRelease += 1;
}
// prevent potentially overflowing our timer variable
if(data->timeSinceEnable <= MAX_TIME) {
data->timeSinceEnable += 1;
}
// prevent potentially overflowing our timer variable
if(data->timeSinceStartUp <= MAX_TIME) {
data->timeSinceStartUp += 1;
}
if(data->timeSinceEStop <= MAX_TIME) {
data->timeSinceEStop += 1;
}
data->estop = !(!fault && *(data->userEnable) && (!faulted || (faulted && reset)));
if(data->estop && !data->estopped) {
data->timeSinceEStop = 0;
data->estopped = true;
*(data->power) = false;
}
*(data->emcEnable) = !data->estop;
// Delay turning the machine on for a short period of time after resetting the software E-Stop.
// machineOn should be connected to halui.machine.on, which will drive halui.machine.is-on high,
// which is required in order to home or do anything CNC-wise. It doesn't seem to always take if
// it's toggled on at the same time as emcEnable, so we add a small delay to ensure we properly
// set the machine state to on.
*(data->machineOn) = *(data->emcEnable) && data->timeSinceEnable > MACHINE_ON_TIME;
}
int rtapi_app_main(void) {
int retval;
comp_id = hal_init(modname);
if(comp_id < 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: hal_init() failed\n", modname);
return -1;
}
data = hal_malloc(sizeof(data_t));
PIN(bit, HAL_IN, xFault, x-fault);
PIN(bit, HAL_IN, yFault, y-fault);
PIN(bit, HAL_IN, zFault, z-fault);
PIN(bit, HAL_IN, bFault, b-fault);
PIN(bit, HAL_IN, cFault, c-fault);
PIN(bit, HAL_IN, tFault, t-fault);
PIN(bit, HAL_IN, xFError, x-f-error);
PIN(bit, HAL_IN, yFError, y-f-error);
PIN(bit, HAL_IN, zFError, z-f-error);
PIN(bit, HAL_IN, bFError, b-f-error);
PIN(bit, HAL_IN, cFError, c-f-error);
PIN(bit, HAL_IN, tFError, t-f-error);
PIN(bit, HAL_IN, ignoreComErrors, ignore-com-errors);
PIN(bit, HAL_IN, button, button);
PIN(s32, HAL_IN, spindleErrorCode, spindle-error-code);
PIN(bit, HAL_IN, spindleModbusOk, spindle-modbus-ok);
PIN(bit, HAL_IN, userRequestEnable, user-request-enable);
PIN(bit, HAL_OUT, userRequestedEnable, user-requested-enable);
PIN(bit, HAL_OUT, emcEnable, emc-enable);
PIN(bit, HAL_IN, userEnable, user-enable);
PIN(bit, HAL_OUT, power, power);
PIN(bit, HAL_OUT, machineOn, machine-on);
PIN(bit, HAL_OUT, xMotorEnable, x-motor-enable);
PIN(bit, HAL_OUT, yMotorEnable, y-motor-enable);
PIN(bit, HAL_OUT, zMotorEnable, z-motor-enable);
PIN(bit, HAL_OUT, bMotorEnable, b-motor-enable);
PIN(bit, HAL_OUT, cMotorEnable, c-motor-enable);
PIN(bit, HAL_OUT, tMotorEnable, t-motor-enable);
PIN(bit, HAL_OUT, unhome, unhome);
*(data->xFault) = 0;
*(data->yFault) = 0;
*(data->zFault) = 0;
*(data->bFault) = 0;
*(data->cFault) = 0;
*(data->tFault) = 0;
*(data->xFError) = 0;
*(data->yFError) = 0;
*(data->zFError) = 0;
*(data->bFError) = 0;
*(data->cFError) = 0;
*(data->tFError) = 0;
*(data->button) = 0;
*(data->spindleErrorCode) = 0;
*(data->spindleModbusOk) = 1;
*(data->emcEnable) = 0;
*(data->power) = 1;
*(data->xMotorEnable) = 1;
*(data->yMotorEnable) = 1;
*(data->zMotorEnable) = 1;
*(data->bMotorEnable) = 1;
*(data->cMotorEnable) = 1;
*(data->tMotorEnable) = 1;
*(data->machineOn) = 0;
*(data->ignoreComErrors) = 0;
data->xFaulted = 0;
data->yFaulted = 0;
data->zFaulted = 0;
data->bFaulted = 0;
data->cFaulted = 0;
data->tFaulted = 0;
data->xFErrored = 0;
data->yFErrored = 0;
data->zFErrored = 0;
data->bFErrored = 0;
data->cFErrored = 0;
data->tFErrored = 0;
data->estop = 0;
data->estopped = 0;
data->timeSinceEStop = 0;
data->timeSinceEnable = 0;
data->timeSinceStartUp = 0;
char name[20];
rtapi_snprintf(name, sizeof(name), "%s.funct", modname);
retval = hal_export_funct(name, update, NULL, 0, 0, comp_id);
if(retval < 0) {
rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: exporting funct failed", modname);
hal_exit(comp_id);
return -1;
}
rtapi_print_msg(RTAPI_MSG_INFO, "%s: installed\n", modname);
hal_ready(comp_id);
return 0;
}
void rtapi_app_exit(void) {
hal_exit(comp_id);
}