-
Notifications
You must be signed in to change notification settings - Fork 11
/
fdpm_mdemo.kl
341 lines (242 loc) · 8.48 KB
/
fdpm_mdemo.kl
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
-- Copyright (c) 2018, G.A. vd. Hoorn
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
PROGRAM fdpm_mdemo
--------------------------------------------------------------------------------
--
-- A simple Dynamic Path Modification demo using Model DPM to control a Fanuc
-- with a mouse in 3D.
--
-- author: G.A. vd. Hoorn
--
--------------------------------------------------------------------------------
%ALPHABETIZE
%COMMENT = 'FDPMMD//r0b'
%NOLOCKGROUP
%NOPAUSE = COMMAND + TPENABLE + ERROR
--------------------------------------------------------------------------------
--
-- remote types & constants
--
--------------------------------------------------------------------------------
%INCLUDE include\libssock.th
--------------------------------------------------------------------------------
--
-- local types & constants
--
--------------------------------------------------------------------------------
TYPE
dpmm_cfg_t = STRUCTURE
f_tp_ready : INTEGER
f_krl_ready : INTEGER
s_tcp_port : INTEGER -- TCP port to listen on
s_tag_nr : INTEGER -- server TAG number to use
um_clear : BOOLEAN -- clear user menu on start
ENDSTRUCTURE
dpmm_t = STRUCTURE
sock : ssock_t -- server socket instance
shutdwn_req : BOOLEAN -- program abort requested status
ENDSTRUCTURE
CONST
LOG_PFIX = 'FDMD '
COND_AH = 4242 -- ABORT handler id (randomly chosen, but less
-- likely to clash than simply '1')
CFG_OK = 0 -- config ok
CFG_NOTDONE = -1 -- configuration not checked: user action required
FILE_ILL_PRM = 2032 -- FILE-032 Illegal parameter
HOST_CTAG_ER = 67144 -- HOST-144 Comm Tag error
SEV_ABORT = 2 -- ABORT severity
RACCEPT_DLAY = 1000 -- 'return to sock_accept' delay
--------------------------------------------------------------------------------
--
-- local variables
--
--------------------------------------------------------------------------------
VAR
cfg_ IN SHADOW : dpmm_cfg_t -- configuration
this_ : dpmm_t -- prog instance
sock_fd_ : FILE -- file descriptor associated with srvr socket
stat_ : INTEGER -- status variable
--------------------------------------------------------------------------------
--
-- remote routine prototypes
--
--------------------------------------------------------------------------------
%INCLUDE include\libssock.h
%INCLUDE include\libind_log.h
--------------------------------------------------------------------------------
--
-- local routine prototypes
--
--------------------------------------------------------------------------------
ROUTINE hndl_client_(this : dpmm_t; fd : FILE) : INTEGER FROM fdpm_mdemo
ROUTINE check_cfg_(cfg : dpmm_cfg_t) : INTEGER FROM fdpm_mdemo
ROUTINE set_offs_(dx : REAL; dy : REAL; dz : REAL) : INTEGER FROM fdpm_mdemo
ROUTINE is_dpm_mpty_ : BOOLEAN FROM fdpm_mdemo
--------------------------------------------------------------------------------
--
-- Main program
--
--------------------------------------------------------------------------------
BEGIN
-- check config
stat_ = check_cfg_(cfg_)
IF (stat_ <> CFG_OK) THEN
log_error_a(LOG_PFIX + 'cfg error:', stat_)
log_error(LOG_PFIX + 'check cfg')
-- errors with config always force user to log window
log_force
POST_ERR(FILE_ILL_PRM, '', 0, SEV_ABORT)
RETURN
ENDIF
-- TODO: should check DPM config is ok
stat_ = 0
this_.shutdwn_req = FALSE
-- enable log output
IF (cfg_.um_clear) THEN log_clear; ENDIF
-- init server socket
stat_ = ssock_ctor(this_.sock, cfg_.s_tcp_port, cfg_.s_tag_nr)
IF (stat_ <> 0) THEN
IF (stat_ = TAG_CONF_ERR) THEN
log_error_a(LOG_PFIX + 'cfg err, TAG idx:', cfg_.s_tag_nr)
ELSE
log_error_a(LOG_PFIX + 'ssock_ctor err:', stat_)
ENDIF
-- nothing we can do, abort
POST_ERR(HOST_CTAG_ER, '', 0, SEV_ABORT)
ENDIF
-- make sure socket is closed (don't care about result)
stat_ = ssock_dconnf(this_.sock)
log_info(LOG_PFIX + 'init done')
-- handshake with TP prog:
-- 1) wait for TP program to tell us that it's ready
log_info(LOG_PFIX + 'wait TP')
WAIT FOR (FLG[cfg_.f_tp_ready] = ON)
-- TODO: make sure and wait for DPM to be ready
-- 2) tell TP prog we are ready
log_info(LOG_PFIX + 'TP is ready')
FLG[cfg_.f_krl_ready] = ON
-- handle clients
WHILE (NOT this_.shutdwn_req) DO
-- inform user
log_info(LOG_PFIX + 'wait client')
-- we need binary mode (UnFormatted)
SET_FILE_ATR(sock_fd_, ATR_UF)
-- wait for connection
CLR_IO_STAT(sock_fd_)
stat_ = ssock_accpt2(this_.sock, sock_fd_)
IF (stat_ <> 0) THEN
-- can't continue on any error
log_error_a(LOG_PFIX + 'sock_accept err:', stat_)
this_.shutdwn_req = TRUE
GOTO lbl_m_break1
ENDIF
-- inform user
log_info(LOG_PFIX + 'connected')
-- client connected, 'spin-off' connection handler
stat_ = hndl_client_(this_, sock_fd_)
-- TODO: handle client errors properly
lbl_m_break1::
stat_ = ssock_dconn2(this_.sock, sock_fd_)
lbl_m_cont0::
log_info(LOG_PFIX + 'disconnected')
-- always delay returning to accept() a bit, to make sure user can
-- escape runaway loops
DELAY (RACCEPT_DLAY)
-- /WHILE (NOT this.shutdwn_req)
ENDWHILE
-- make sure socket is closed (don t care about result)
stat_ = ssock_dconnf(this_.sock)
log_info(LOG_PFIX + 'exit')
END fdpm_mdemo
ROUTINE hndl_client_
VAR
stat__ : INTEGER
dx__ : REAL
dy__ : REAL
dz__ : REAL
BEGIN
stat__ = 0
dx__ = 0.0
dy__ = 0.0
dz__ = 0.0
log_info(LOG_PFIX + 'ready for mouse input')
-- service connected client
WHILE (NOT this.shutdwn_req) DO
-- wait for new offsets (essentially 'forever' as READ blocks)
READ fd(dx__, dy__, dz__)
stat__ = IO_STATUS(fd)
IF (stat__ <> 0) THEN
log_error_a(LOG_PFIX + 'sock err:', stat__)
GOTO lbl_hc_break
ENDIF
IF (NOT is_dpm_mpty_) THEN
-- DPM hasn't copied previous offsets yet, try again next iteration
-- with new offsets (net effect: ignore the currently received
-- offsets, but since everything is relative, this shouldn't
-- matter that much for user experience)
GOTO lbl_hc_cont1
ENDIF
-- push offsets into sysvars
stat__ = set_offs_(dx__, dy__, dz__)
IF (stat__ <> 0) THEN POST_ERR(-stat__, '', 0, SEV_ABORT); ENDIF
lbl_hc_cont1::
-- just yield
DELAY (1)
-- /WHILE (NOT this.shutdwn_req)
ENDWHILE
lbl_hc_break::
log_info(LOG_PFIX + 'hndl_client exit')
RETURN (-ABS(stat__))
END hndl_client_
ROUTINE check_cfg_
BEGIN
-- check for any uninitialised entries
IF (UNINIT(cfg.f_tp_ready )) THEN RETURN (CFG_NOTDONE); ENDIF
IF (UNINIT(cfg.f_krl_ready)) THEN RETURN (CFG_NOTDONE); ENDIF
IF (UNINIT(cfg.s_tcp_port )) THEN RETURN (CFG_NOTDONE); ENDIF
IF (UNINIT(cfg.s_tag_nr )) THEN RETURN (CFG_NOTDONE); ENDIF
IF (UNINIT(cfg.um_clear )) THEN RETURN (CFG_NOTDONE); ENDIF
-- all ok
RETURN (CFG_OK)
END check_cfg_
ROUTINE set_offs_
VAR
stat__ : INTEGER
BEGIN
stat__ = 0
SET_VAR(0, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[1].$INI_OFS', dx, stat__)
IF (stat__ <> 0) THEN GOTO lbl_so_exit; ENDIF
SET_VAR(0, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[2].$INI_OFS', dy, stat__)
IF (stat__ <> 0) THEN GOTO lbl_so_exit; ENDIF
SET_VAR(0, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[3].$INI_OFS', dz, stat__)
IF (stat__ <> 0) THEN GOTO lbl_so_exit; ENDIF
lbl_so_exit::
RETURN (-ABS(stat__))
END set_offs_
ROUTINE is_dpm_mpty_
VAR
stat__ : INTEGER
temp0__ : REAL
temp1__ : REAL
temp2__ : REAL
BEGIN
stat__ = 0
GET_VAR(0, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[1].$INI_OFS', temp0__, stat__)
IF (stat__ <> 0) THEN POST_ERR(stat__, '', 0, SEV_ABORT); ENDIF
GET_VAR(0, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[2].$INI_OFS', temp1__, stat__)
IF (stat__ <> 0) THEN POST_ERR(stat__, '', 0, SEV_ABORT); ENDIF
GET_VAR(0, '*SYSTEM*', '$DPM_SCH[1].$GRP[1].$OFS[3].$INI_OFS', temp2__, stat__)
IF (stat__ <> 0) THEN POST_ERR(stat__, '', 0, SEV_ABORT); ENDIF
RETURN ((temp0__ = 0.0) AND (temp1__ = 0.0) AND (temp2__ = 0.0))
END is_dpm_mpty_