-
Notifications
You must be signed in to change notification settings - Fork 6
/
mh400e_gears.c
617 lines (550 loc) · 19.8 KB
/
mh400e_gears.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
/*
LinuxCNC component for controlling the MAHO MH400E gearbox.
Copyright (C) 2018 Sergey 'Jin' Bostandzhyan <[email protected]>
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Functions related to gear switching. */
#include "mh400e_gears.h"
#include "mh400e_twitch.h"
typedef enum
{
SHAFT_STATE_OFF, /* Initial shaft state */
SHAFT_STATE_ON, /* Shift in process (i.e. shaft motor running) */
SHAFT_STATE_RESTART /* Error condition, we missed our target and reached
an end point, we need to go back */
} shaft_state_t;
/* Group all data that is required to operate on one shaft */
typedef struct
{
shaft_state_t state;
pin_group_t status_pins;
hal_bit_t *motor_on;
hal_bit_t *motor_reverse;
hal_bit_t *motor_slow;
unsigned char current_mask; /* auto updated via global variable */
unsigned char target_mask;
} shaft_data_t;
/* Group all data required for gearshifting */
static struct
{
hal_bit_t *start_shift;
hal_bit_t *do_stop_spindle;
hal_bit_t *is_spindle_stopped;
hal_bit_t *trigger_estop;
hal_bit_t *notify_spindle_at_speed;
bool spindle_on_before_shift;
shaft_data_t backgear;
shaft_data_t midrange;
shaft_data_t input_stage;
long delay;
statefunc next;
} g_gearbox_data;
/* One time setup function to prepare data structures related to gearbox
* switching*/
FUNCTION(gearbox_setup)
{
/* Populate data structures that will be used be the state functions
* when shifting gears */
g_gearbox_data.backgear.state = SHAFT_STATE_OFF;
/* Grabbing the pin pointers in EXTRA_SETUP did not work because the
* component did not seem to be fully initializedt there.
*
* Another issue:
* while output pins are defined as (*__comp_inst->pin_name) by
* halcompile, input pins are turned into (0+*__comp_inst->pin_name)
* which makes it impossible to get the pointers via the defines
* created by halcompile. Accessing the pin variable directly did not
* work due to macro expansion, only workaround I found was to temporarily
* disable the macros.
*/
#pragma push_macro("reducer_left")
#pragma push_macro("reducer_right")
#pragma push_macro("reducer_center")
#pragma push_macro("reducer_left_center")
#undef reducer_left
#undef reducer_right
#undef reducer_center
#undef reducer_left_center
g_gearbox_data.backgear.status_pins = (pin_group_t)
{
__comp_inst->reducer_left,
__comp_inst->reducer_right,
__comp_inst->reducer_center,
__comp_inst->reducer_left_center
};
#pragma pop_macro("reducer_left")
#pragma pop_macro("reducer_right")
#pragma pop_macro("reducer_center")
#pragma pop_macro("reducer_left_center")
g_gearbox_data.backgear.motor_on = &reducer_motor;
g_gearbox_data.backgear.motor_reverse = &reverse_direction;
g_gearbox_data.backgear.motor_slow = &motor_lowspeed;
g_gearbox_data.backgear.current_mask = 0;
g_gearbox_data.backgear.target_mask =
mh400e_gears[MH400E_NEUTRAL_GEAR_INDEX].value; /* neutral */
g_gearbox_data.midrange.state = SHAFT_STATE_OFF;
#pragma push_macro("middle_left")
#pragma push_macro("middle_right")
#pragma push_macro("middle_center")
#pragma push_macro("middle_left_center")
#undef middle_left
#undef middle_right
#undef middle_center
#undef middle_left_center
g_gearbox_data.midrange.status_pins = (pin_group_t)
{
__comp_inst->middle_left,
__comp_inst->middle_right,
__comp_inst->middle_center,
__comp_inst->middle_left_center
};
#pragma pop_macro("middle_left")
#pragma pop_macro("middle_right")
#pragma pop_macro("middle_center")
#pragma pop_macro("middle_left_center")
g_gearbox_data.midrange.motor_on = &midrange_motor;
g_gearbox_data.midrange.motor_reverse = &reverse_direction;
g_gearbox_data.midrange.motor_slow = &motor_lowspeed;
g_gearbox_data.midrange.current_mask = 0;
g_gearbox_data.midrange.target_mask = 0; /* don't care for neutral */
g_gearbox_data.input_stage.state = SHAFT_STATE_OFF;
#pragma push_macro("input_left")
#pragma push_macro("input_right")
#pragma push_macro("input_center")
#pragma push_macro("input_left_center")
#undef input_left
#undef input_right
#undef input_center
#undef input_left_center
g_gearbox_data.input_stage.status_pins = (pin_group_t)
{
__comp_inst->input_left,
__comp_inst->input_right,
__comp_inst->input_center,
__comp_inst->input_left_center
};
#pragma pop_macro("input_left")
#pragma pop_macro("input_right")
#pragma pop_macro("input_center")
#pragma pop_macro("input_left_center")
g_gearbox_data.input_stage.motor_on = &input_stage_motor;
g_gearbox_data.input_stage.motor_reverse = &reverse_direction;
g_gearbox_data.input_stage.motor_slow = &motor_lowspeed;
g_gearbox_data.input_stage.current_mask = 0;
g_gearbox_data.input_stage.target_mask = 0; /* don't care for neutral */
#pragma push_macro("spindle_stopped")
#undef spindle_stopped
g_gearbox_data.is_spindle_stopped = __comp_inst->spindle_stopped;
#pragma pop_macro("spindle_stopped")
g_gearbox_data.do_stop_spindle = &stop_spindle;
g_gearbox_data.spindle_on_before_shift = false;
g_gearbox_data.start_shift = &start_gear_shift;
g_gearbox_data.trigger_estop = &estop_out;
g_gearbox_data.notify_spindle_at_speed = &spindle_at_speed;
g_gearbox_data.delay = 0;
g_gearbox_data.next = NULL;
}
static void gearshift_stop_spindle(void)
{
g_gearbox_data.spindle_on_before_shift =
!(*g_gearbox_data.is_spindle_stopped);
*g_gearbox_data.do_stop_spindle = true;
}
/* combine values of all pins in a group to a bitmask */
static unsigned char get_bitmask_from_pingroup(pin_group_t *group)
{
unsigned char mask = 0;
int i;
for (i = 0; i < MH400E_PINS_IN_GROUP; i++)
{
mask |= *(group->p[i]) << i;
}
return mask;
}
/* Update current mask values for each shaft */
static void update_current_pingroup_masks(void)
{
g_gearbox_data.backgear.current_mask =
get_bitmask_from_pingroup(&g_gearbox_data.backgear.status_pins);
g_gearbox_data.midrange.current_mask =
get_bitmask_from_pingroup(&g_gearbox_data.midrange.status_pins);
g_gearbox_data.input_stage.current_mask =
get_bitmask_from_pingroup(&g_gearbox_data.input_stage.status_pins);
}
static bool estop_on_spindle_running(void)
{
if (!*g_gearbox_data.is_spindle_stopped)
{
/* This is an invalid condition, spindle must be stopped if we are
* shifting and we tested for it before we started.
*
* We expect that estop_out will be looped back to us so that
* it will trigger our handler. */
rtapi_print_msg(RTAPI_MSG_ERR, "mh400e_gearbox FATAL ERROR: detected "
"running spindle while shifting, triggering emergency stop!\n");
*g_gearbox_data.trigger_estop = true;
return true;
}
return false;
}
/* Combine masks from each pin group to a value representing the current
* gear setting. A return of NULL means that a corresponding value could
* not be found, which may indicate a gearshift being in progress- */
static pair_t* get_current_gear(tree_node_t *tree)
{
tree_node_t *result;
unsigned combined = (g_gearbox_data.input_stage.current_mask << 8) |
(g_gearbox_data.midrange.current_mask << 4) |
g_gearbox_data.backgear.current_mask;
/* special case: ignore all other bits for neutral */
if (g_gearbox_data.backgear.current_mask ==
mh400e_gears[MH400E_NEUTRAL_GEAR_INDEX].value)
{
return &(mh400e_gears[MH400E_NEUTRAL_GEAR_INDEX]);
}
result = tree_search(tree, combined);
if (result != NULL)
{
return &(mh400e_gears[result->value]);
}
return NULL;
}
/* Helper to update delays, returns true if time has not elapsed. */
static bool gearshift_wait_delay(long period)
{
if ((period > 0) && (g_gearbox_data.delay > 0))
{
g_gearbox_data.delay = g_gearbox_data.delay - period;
return true;
}
g_gearbox_data.delay = 0;
return false;
}
/* From:
* https://forum.linuxcnc.org/12-milling/33035-retrofitting-a-1986-maho-mh400e?start=460#117021
*
* 1. if u need to go to the left then turn cw
* 2. if u need to go to the right than turn ccw
* 3. if u need to go to the middle and Left-Center is 1 then turn ccw else cw
*
* ┌───┐
* ┘ └──────────────── left
* ┌───┐
* ────────┘ └──────── center
* ┌───┐
* ────────────────┘ └ right
* ┌──────────
* ──────────┘ left-center
*
*/
static bool gearshift_need_reverse(unsigned char target_mask,
unsigned char current_mask)
{
if (MH400E_STAGE_IS_RIGHT(target_mask)) /* CCW, reverse is on */
{
return true;
}
else if (MH400E_STAGE_IS_LEFT(target_mask)) /* CW, reverse is off */
{
return false;
}
else if (MH400E_STAGE_IS_CENTER(target_mask))
{
if (!MH400E_STAGE_IS_LEFT_CENTER(current_mask))/* CW,reverse is off */
{
return false;
}
}
return true;
}
/* State functions */
/* This is more or less an "overshoot" protection check in case we missed the
* target center pos and moved further. We know when we reach an end point
* and we know we can't continue further in this direction, so stop trying and
* go back. Returns true if action needs to be taken. */
static bool gearshift_protect(shaft_data_t *shaft)
{
if (!*shaft->motor_on)
{
return false;
}
if (*shaft->motor_reverse)
{
/* If we move to the left/CW and we reached the furthest left position
* which does not seem to be our desired target, then we should
* disable the motor and trigger an E-STOP, we should never end up#
* in this situation. */
if ((shaft->current_mask == MH400E_STAGE_POS_RIGHT) &&
(shaft->current_mask != shaft->target_mask))
{
rtapi_print_msg(RTAPI_MSG_ERR, "mh400e_gearbox: WARNING: "
"shaft motor at unexpected right position!\n");
}
else
{
return false;
}
}
else
{
/* If we move to the left/CW and we reached the furthest left position
* which does not seem to be our desired target, then we should
* disable the motor and trigger an E-STOP, we should never end up#
* in this situation. */
if ((shaft->current_mask == MH400E_STAGE_POS_LEFT) &&
(shaft->current_mask != shaft->target_mask))
{
rtapi_print_msg(RTAPI_MSG_ERR, "mh400e_gearbox: WARNING: "
"shaft motor at unexpected left position!\n");
}
else
{
return false;
}
}
return true;
}
/* Generic function that has the exact same logic, valid for all of the
* three shafts. */
static void gearshift_stage(shaft_data_t *shaft, statefunc me, statefunc next,
long period)
{
if (estop_on_spindle_running())
{
return;
}
if (gearshift_wait_delay(period))
{
g_gearbox_data.next = me;
return;
}
if (shaft->state == SHAFT_STATE_OFF)
{
/* Are the pins already in the desired state? */
if (shaft->current_mask == shaft->target_mask)
{
g_gearbox_data.next = next;
}
else
{
shaft->state = SHAFT_STATE_ON;
if (gearshift_need_reverse(shaft->target_mask,
shaft->current_mask))
{
*shaft->motor_reverse = true;
g_gearbox_data.delay = MH400E_REVERSE_MOTOR_INTERVAL;
}
g_gearbox_data.next = me;
}
}
else if (shaft->state == SHAFT_STATE_ON)
{
/* Did we reach the desired position? */
if (shaft->current_mask == shaft->target_mask)
{
if (*shaft->motor_on)
{
/* De-energize the shaft motor */
*shaft->motor_on = false;
}
else
{
/* Second time we enter this state the motor will be off,
* that means that we already did the waiting that may have
* been set in the "if" below. If reverse direction was
* not active originally, then this does nothing */
*shaft->motor_reverse = false;
}
/* If reverse direction has been set, disable it in 100ms */
if (*shaft->motor_reverse)
{
g_gearbox_data.delay = MH400E_GENERIC_PIN_INTERVAL;
g_gearbox_data.next = me;
return;
}
else
{
*shaft->motor_slow = false;
}
if (*shaft->motor_slow)
{
g_gearbox_data.delay = MH400E_GENERIC_PIN_INTERVAL;
g_gearbox_data.next = me;
return;
}
/* We are done here, proceed to the next stage */
shaft->state = SHAFT_STATE_OFF;
g_gearbox_data.delay = MH400E_GENERIC_PIN_INTERVAL;
g_gearbox_data.next = next;
}
else
{
/* Protect furthest lect/CW and right/CCW end positions by not
* allowing the motor to continue running if we reached them,
* this should never happen, but it's better to have a safety
* measure to prevent hardware damage. The function will
* immediately stop the motor and trigger an emergency stop if this
* error condition is detected. */
if (gearshift_protect(shaft))
{
*shaft->motor_on = false;
shaft->state = SHAFT_STATE_RESTART;
g_gearbox_data.delay = MH400E_REVERSE_MOTOR_INTERVAL;
g_gearbox_data.next = me;
return;
}
/* Going to the center requres lowering the motor speed */
if (MH400E_STAGE_IS_CENTER(shaft->target_mask) &&
!(*shaft->motor_slow))
{
*shaft->motor_slow = true;
}
else if (!(*shaft->motor_on))
{
/* Energize motor if it is not yet running */
*shaft->motor_on = true;
}
g_gearbox_data.delay = MH400E_GEAR_STAGE_POLL_INTERVAL;
g_gearbox_data.next = me;
}
}
else if (shaft->state == SHAFT_STATE_RESTART)
{
/* Protection function restarted us, motor is already off and
* we came here after a certain delay. We now need to check what to do
* and re-energize */
if (*shaft->motor_reverse)
{
*shaft->motor_reverse = false;
g_gearbox_data.delay = MH400E_GENERIC_PIN_INTERVAL;
g_gearbox_data.next = me;
return;
}
if (*shaft->motor_slow)
{
*shaft->motor_slow = false;
g_gearbox_data.delay = MH400E_GENERIC_PIN_INTERVAL;
}
/* Going back to the OFF state will retrigger the shift logic for
* this shaft */
shaft->state = SHAFT_STATE_OFF;
g_gearbox_data.next = me;
}
}
static void gearshift_stop(long period)
{
if (gearshift_wait_delay(period))
{
g_gearbox_data.next = gearshift_stop;
return;
}
twitch_stop(period);
if (!twitch_stop_completed())
{
g_gearbox_data.delay = MH400E_TWITCH_KEEP_PIN_OFF;
g_gearbox_data.next = gearshift_stop;
return;
}
if (*g_gearbox_data.start_shift)
{
*g_gearbox_data.start_shift = false;
if (g_gearbox_data.spindle_on_before_shift)
{
*g_gearbox_data.do_stop_spindle = false;
g_gearbox_data.delay = MH400E_WAIT_SPINDLE_AT_SPEED;
g_gearbox_data.next = gearshift_stop;
return;
}
}
if (g_gearbox_data.spindle_on_before_shift)
{
*g_gearbox_data.notify_spindle_at_speed = true;
}
/* We are done shifting, reset everything */
g_gearbox_data.next = NULL;
g_gearbox_data.spindle_on_before_shift = false;
}
static void gearshift_backgear(long period)
{
gearshift_stage(&(g_gearbox_data.backgear), gearshift_backgear,
gearshift_stop, period);
}
static void gearshift_midrange(long period)
{
gearshift_stage(&(g_gearbox_data.midrange), gearshift_midrange,
gearshift_backgear, period);
}
static void gearshift_input_stage(long period)
{
gearshift_stage(&(g_gearbox_data.input_stage), gearshift_input_stage,
gearshift_midrange, period);
}
/* Call this function once per each thread cycle to handle gearshifting,
* implies that gearshift_start() has been called in order to set the
* target gear. */
static void gearshift_handle(long period)
{
twitch_handle(period);
if (g_gearbox_data.next == NULL)
{
rtapi_print_msg(RTAPI_MSG_ERR, "mh400e_gearbox FATAL ERROR: "
"gearshift function not set up, triggering E-Stop!\n");
*g_gearbox_data.trigger_estop = true;
return;
}
g_gearbox_data.next(period);
}
/* Start shifting process */
static void gearshift_start(pair_t *target_gear, long period)
{
if (estop_on_spindle_running())
{
return;
}
g_gearbox_data.backgear.target_mask = (target_gear->value) & 0x000f;
g_gearbox_data.midrange.target_mask = (target_gear->value & 0x00f0) >> 4;
g_gearbox_data.input_stage.target_mask =
(target_gear->value & 0x0f00) >> 8;
/* Make sure to leave 100ms between setting start_gear_shift to "on"
* and further operations */
g_gearbox_data.delay = MH400E_GENERIC_PIN_INTERVAL;
*g_gearbox_data.start_shift = true;
twitch_start(period);
/* Special case: if we want to go to the neutral position, we
* only care about the backgear stage, so we can jump right to it */
if (g_gearbox_data.backgear.target_mask ==
mh400e_gears[MH400E_NEUTRAL_GEAR_INDEX].value) {
g_gearbox_data.next = gearshift_backgear;
}
else
{
g_gearbox_data.next = gearshift_input_stage;
}
}
/* Reset pins and state machine if an emergency stop was triggered. */
static void gearbox_handle_estop(void)
{
*g_gearbox_data.input_stage.motor_on = false;
*g_gearbox_data.midrange.motor_on = false;
*g_gearbox_data.backgear.motor_on = false;
/* There are no separate pins for revers/slow for each shaft, each
* shaft structure has pointers to the same pins, so its enough to
* reset them only on one shaft. */
*g_gearbox_data.backgear.motor_reverse = false;
*g_gearbox_data.backgear.motor_slow = false;
gearshift_stop(0); /* Will stop and reset twitching as well */
}
static bool gearshift_in_progress(void)
{
return g_gearbox_data.next != NULL;
}