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
|
/******************************************************************************
* Copyright (C) 2020 Alejandro Colomar Andrés *
* SPDX-License-Identifier: GPL-2.0-only *
******************************************************************************/
/******************************************************************************
******* include **************************************************************
******************************************************************************/
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define ALX_NO_PREFIX
#include <libalx/base/compiler.h>
#include <libalx/base/errno.h>
#include <libalx/base/linux.h>
#include <libalx/base/signal.h>
#include <libalx/base/stdio.h>
#include <libalx/base/stdlib.h>
#include <libalx/base/sys.h>
#include <libalx/extra/cv/cv.h>
/******************************************************************************
******* define ***************************************************************
******************************************************************************/
#define ENV_ROB_ADDR "ROB_ADDR"
#define ENV_ROB_PORT "ROB_PORT"
#define ENV_CAMERA_IDX "CAMERA_IDX"
#define ENV_DELAY_US "DELAY_US"
/******************************************************************************
******* enum *****************************************************************
******************************************************************************/
/******************************************************************************
******* struct / union *******************************************************
******************************************************************************/
/******************************************************************************
******* static variables *****************************************************
******************************************************************************/
/* environment variables */
static const char *rob_addr;
static const char *rob_port;
static int camera_idx;
static int delay_us;
/* pid */
static pid_t pid;
/* camera */
static cam_s *camera;
/* rob */
static int rob;
/******************************************************************************
******* static functions (prototypes) ****************************************
******************************************************************************/
static
int cam_init (img_s **img);
static
int cam_deinit (img_s *img);
static
int env_init (void);
static
int rob_init (void);
static
int rob_deinit (void);
static
int rob_reinit (void);
static
int camera_init (void);
static
void camera_deinit (void);
static
int cv_init (img_s **img);
static
void cv_deinit (img_s *img);
static
int session (int i, img_s *img);
static
int wait_rob (void);
static
int proc_cv (uint8_t *restrict blue11, img_s *restrict img);
/******************************************************************************
******* main *****************************************************************
******************************************************************************/
int main (void)
{
img_s *img;
int status;
status = 1;
if (cam_init(&img))
goto err0;
mb();
for (int i = 0; !sigterm; i++) {
if (sigpipe) {
if (rob_reinit())
goto err;
}
status = session(i, img);
if (status)
goto err;
usleep(delay_us);
mb();
}
status = 0;
err:
cam_deinit(img);
err0:
fprintf(stderr, "cam#%"PRIpid": OUT: %i\n", pid, status);
perrorx(NULL);
return status;
}
/******************************************************************************
******* static functions (definitions) ***************************************
******************************************************************************/
static
int cam_init (img_s **img)
{
int status;
pid = getpid();
status = -1;
if (sigpipe_init())
goto err0;
status--;
if (sigterm_init())
goto err0;
status--;
if (env_init())
goto err0;
status--;
if (rob_init())
goto err0;
status--;
if (camera_init())
goto err1;
status--;
if (cv_init(img))
goto err2;
return 0;
err2:
camera_deinit();
err1:
rob_deinit();
err0:
fprintf(stderr, "cam#%"PRIpid": ERROR: cam_init(): %i\n", pid, status);
return status;
}
static
int cam_deinit (img_s *img)
{
int status;
status = 0;
cv_deinit(img);
camera_deinit();
if (rob_deinit())
status -= 4;
return status;
}
static
int env_init (void)
{
int status;
status = -1;
rob_addr = getenv(ENV_ROB_ADDR);
if (!rob_addr)
goto err;
status--;
rob_port = getenv(ENV_ROB_PORT);
if (!rob_port)
goto err;
status--;
if (getenv_i32(&camera_idx, ENV_CAMERA_IDX))
goto err;
status--;
if (getenv_i32(&delay_us, ENV_DELAY_US))
goto err;
return 0;
err:
fprintf(stderr, "cam#%"PRIpid": ERROR: env_init(): %i\n", pid, status);
return status;
}
static
int rob_init (void)
{
rob = tcp_client_open(rob_addr, rob_port);
return rob < 0;
}
static
int rob_deinit (void)
{
return close(rob);
}
static
int rob_reinit (void)
{
rob_deinit();
return rob_init();
}
static
int camera_init (void)
{
int status;
status = -1;
if (alx_cv_alloc_cam(&camera))
goto err;
alx_cv_init_cam(camera, NULL, camera_idx, 0);
return 0;
err:
fprintf(stderr, "cam#%"PRIpid": ERROR: camera_init(): %i\n",pid,status);
return status;
}
static
void camera_deinit (void)
{
alx_cv_deinit_cam(camera);
alx_cv_free_cam(camera);
}
static
int cv_init (img_s **img)
{
int status;
status = -1;
if (alx_cv_init_img(img))
goto err;
return 0;
err:
fprintf(stderr, "cam#%"PRIpid": ERROR: cv_init(): %i\n", pid, status);
return status;
}
static
void cv_deinit (img_s *img)
{
alx_cv_deinit_img(img);
}
static
int session (int i, img_s *img)
{
uint8_t blue11;
char buf[BUFSIZ];
ptrdiff_t len;
ssize_t n;
int status;
clock_t time_0;
clock_t time_1;
double time_tot;
time_0 = clock();
status = -1;
if (wait_rob())
goto err;
status--;
if (proc_cv(&blue11, img))
goto err;
status--;
if (sbprintf(buf, &len, "cam#%"PRIpid":session#%i: img[B][1][1] = %"PRIu8"",
pid, i, blue11))
goto err;
printf("%s\n", buf);
status--;
n = send(rob, buf, len, 0);
if (n < 0)
goto err;
time_1 = clock();
time_tot = ((double) time_1 - time_0) / CLOCKS_PER_SEC;
printf("session time: %5.3lf s;\n", time_tot);
return 0;
err:
fprintf(stderr, "cam#%"PRIpid": ERROR: session(): %i\n", pid, status);
return status;
}
static
int wait_rob (void)
{
char buf[BUFSIZ];
ssize_t n;
int status;
status = -1;
n = recv(rob, buf, ARRAY_SIZE(buf) - 1, 0);
if (n < 0)
goto err;
status--;
if (!n)
goto err;
return 0;
err:
fprintf(stderr, "cam#%"PRIpid": ERROR: wait_rob(): %i\n", pid, status);
return status;
}
static
int proc_cv (uint8_t *restrict blue11, img_s *restrict img)
{
clock_t time_0;
clock_t time_1;
double time_tot;
int status;
time_0 = clock();
status = -1;
if (alx_cv_cam_read(img, camera))
goto err;
status--;
if (alx_cv_component(img, ALX_CV_CMP_BGR_B))
goto err;
status--;
if (alx_cv_pixel_get_u8(img, blue11, 1, 1))
goto err;
time_1 = clock();
time_tot = ((double) time_1 - time_0) / CLOCKS_PER_SEC;
printf("cv time: %5.3lf s;\n", time_tot);
return 0;
err:
fprintf(stderr, "cam#%"PRIpid": ERROR: proc_cv(): %i\n", pid, status);
return status;
}
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
|