-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolchanger.comp
349 lines (241 loc) · 6.54 KB
/
toolchanger.comp
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
component toolchanger "This component controls the Boxford 240 Lathe Auto Tool ";
pin in bit enable = false "Toolchanger enable signal";
pin in bit tool_change "Receives signal from M6 that tool change required";
pin out bit tool_changed "Sends signal when tool change finished";
pin in s32 tool_requested "Receives Tx data from M6 (tool number requested) Only allows 1-8";
pin in s32 tool_current "Receives old tool number";
pin out s32 tool_physical = 0; // Reflects current locked tool in ATC
pin out s32 tool_target = 0; // Reflects requested tool number
pin out bit tool_locking = false; // Indicates that tool is about to get locked
pin out bit tool_locked = false;
pin out bit tool_process = false;
pin out s32 event_step = 0;
pin in bit atc_limit "ATC limit switch";
pin in float position_fb "Position-feedback from stepgen";
pin out float position_cmd "Sends location required";
variable float position_req = 0; // Where we want to be
pin out bit homed = false "Sends signal when ATC has finished homing";
pin out bit homing = false "Sends signal when ATC is currently homing";
pin in bit axis_homing;
pin in bit axis_homed;
option singleton yes;
function _;
license "GPL";
;;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define HOMING_DELAY 1;
#define LOCKING_DELAY 1;
#define TOOL_MOVE 52; // tool move including moving away from the lock
#define TOOL_CLEARMOVE 44.75;
#define LATCHING_MOVE 5 // threshold on when to start the slow move
#define LOCK_MOVE 7; // move in oposit direction, locking the tool
#define LOCK_STOP 0.1; // slamming into physical stop
#define STEP_LOCKING 0.001;
#define STEP_SLOW 0.005;
#define STEP_NORMAL 0.01;
#define STEP_FAST 0.03;
static bool delay = false;
static float timer;
static int remaining(const int current, const int target);
enum events {
RESET,
PREPARE_HOMED,
PREPARE_SLEEP,
HOMING_START,
HOMING_SEARCH,
HOMING_LATCH,
HOMING_FINISH,
CHANGE_INIT,
CHANGE_CONTROL,
CHANGE_PROGRESS,
CHANGE_TOOL,
CHANGE_ADVANCE,
CHANGE_LOCK,
CHANGE_COMPLETE,
DEFAULT
};
enum events event;
FUNCTION(_) {
if ( axis_homing ) event = RESET;
if ( tool_change && homed ) {
if ( !tool_process && !tool_changed ) {
event = CHANGE_INIT;
}
}
if( !tool_change ) {
tool_process = false;
tool_changed = false; // reset once toolchange flag reset by system
}
switch (event) {
case RESET:
event_step = 1;
// reset
homed = 0;
if (axis_homed) event = PREPARE_HOMED;
break;
case PREPARE_HOMED:
event_step = 2;
// machine is homed, register time and dispatch sleep event
if (delay == false) {
delay = true;
timer = HOMING_DELAY;
event = PREPARE_SLEEP;
}
break;
case PREPARE_SLEEP:
event_step = 3;
timer -= fperiod;
if (timer <= 0) {
timer = 0;
delay = false;
event = HOMING_START;
}
break;
case HOMING_START:
event_step = 4;
// check if limit switch is tripped
// if it is, release it by moving away
if (atc_limit) {
position_cmd = 25;
} else {
event = HOMING_SEARCH;
}
break;
case HOMING_SEARCH:
event_step = 5;
if (!atc_limit) {
position_cmd = 360 + 50;
} else {
position_cmd = position_fb + LATCHING_MOVE;
event = HOMING_LATCH;
}
break;
case HOMING_LATCH:
event_step = 6;
if (atc_limit) {
position_cmd += 1;
} else {
position_cmd = position_fb - 23;
if (delay == false) {
timer = LOCKING_DELAY;
delay = true;
}
event = HOMING_FINISH;
}
break;
case HOMING_FINISH:
event_step = 7;
timer -= fperiod;
if (timer <= 0) {
timer = 0;
delay = false;
homed = true;
tool_locked = true;
tool_physical = 2;
event = DEFAULT;
}
break;
case CHANGE_INIT:
event_step = 8;
if ( tool_physical == tool_requested ) {
// current tool is the tool requested, do nothing.
tool_changed = true;
event = DEFAULT;
} else {
tool_target = tool_requested;
tool_process = true;
event = CHANGE_CONTROL;
}
break;
case CHANGE_CONTROL:
event_step = 9;
if ( tool_physical == tool_target ) {
// current tool is the tool requested, do nothing.
tool_changed = true;
event = DEFAULT;
} else {
event = CHANGE_PROGRESS;
}
break;
case CHANGE_PROGRESS:
event_step = 10;
if ( remaining(tool_physical, tool_target) == 1 ) {
tool_locking = true;
} else {
tool_locking = false;
}
// add check for lock move
event = CHANGE_TOOL;
break;
case CHANGE_TOOL:
event_step = 11;
if ( tool_locked == true ) {
position_req = position_fb + TOOL_MOVE;
} else {
position_req = position_fb + TOOL_CLEARMOVE;
}
event = CHANGE_ADVANCE;
break;
case CHANGE_ADVANCE:
event_step = 12;
// tool is not in position, lets move forward
if ( position_fb < position_req ) {
if ( tool_locking ) {
if ( position_fb < ( position_req - LATCHING_MOVE ) ) {
position_cmd += STEP_FAST;
} else {
position_cmd += STEP_NORMAL;
}
} else {
position_cmd += STEP_FAST;
}
}
// necessery move has been complete, decide what to do next
if ( position_fb >= position_req ) {
if ( tool_locking == true ) {
position_req = position_fb - LOCK_MOVE - LOCK_STOP;
event = CHANGE_LOCK;
} else {
// moved tool without locking it
tool_locked = false;
event = CHANGE_COMPLETE;
}
}
break;
case CHANGE_LOCK:
event_step = 13;
if ( position_fb > position_req ) {
if ( ( position_req - position_fb ) < LATCHING_MOVE ) {
position_cmd -= STEP_SLOW;
} else {
position_cmd -= STEP_LOCKING;
}
}
if ( position_fb <= position_req ) {
// moved tool and locked it
tool_locked = true;
event = CHANGE_COMPLETE;
}
break;
case CHANGE_COMPLETE:
event_step = 14;
// tool change complete, advance tool counter
tool_physical += 1;
if ( tool_physical > 8 ) tool_physical = tool_physical - 8;
event = CHANGE_CONTROL;
break;
default:
event_step = 15;
break;
}
}
/* function descripgion */
int remaining(const int current, const int target) {
int result;
result = 8 - current + target;
if ( result > 8 ) result = result - 8;
return result;
}