-
Notifications
You must be signed in to change notification settings - Fork 0
/
AOloopControl_PredictiveControl_builPFloop_WatchInput.c
433 lines (321 loc) · 10.5 KB
/
AOloopControl_PredictiveControl_builPFloop_WatchInput.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
/**
* @file AOloopControl_PredictiveControl_builPFloop_WatchInput.c
* @brief Assemble input data from telemetry stream
*
*
*
*
* ## Change log
* - 20180518 Guyon File creation (split from AOloopControl_PredictiveControl)
*
*
* @author O. Guyon
*
* @bug No known bugs.
*
*/
#define _GNU_SOURCE
/* =============================================================================================== */
/* =============================================================================================== */
/* HEADER FILES */
/* =============================================================================================== */
/* =============================================================================================== */
#include <math.h>
#include <stdint.h>
#include <unistd.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <pthread.h>
#ifdef __MACH__
#include <mach/mach_time.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0
int clock_gettime(int clk_id, struct mach_timespec *t) {
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);
uint64_t time;
time = mach_absolute_time();
double nseconds = ((double)time * (double)timebase.numer)/((double)timebase.denom);
double seconds = ((double)time * (double)timebase.numer)/((double)timebase.denom * 1e9);
t->tv_sec = seconds;
t->tv_nsec = nseconds;
return 0;
}
#else
#include <time.h>
#endif
#include <fitsio.h>
#include "CommandLineInterface/CLIcore.h"
#include "00CORE/00CORE.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "AOloopControl/AOloopControl.h"
#include "AOloopControl_PredictiveControl/AOloopControl_PredictiveControl.h"
/**
* ## Purpose
*
* Listens to real-time telemetry streams buffers aol<loop>_modeval_ol_logbuff0
* and aol<loop>_modeval_ol_logbuff1
*
* When new buffer is complete, extract the requested values from it
* and write to aol<loop>_modevalol_PFb<PFblock>
*
* ## Arguments
*
* @param[in]
* loop LONG
* loop index
*
* @param[in]
* PFblock LONG
* Block number
*
* @param[in]
* PFblockStart LONG
* Block start index
*
* @param[in]
* PFblockEnd LONG
* Block end index
*
* #param[in]
* NBbuff LONG
* Number of input buffers to merge for each output
*
*/
long AOloopControl_PredictiveControl_builPFloop_WatchInput(
long loop,
long PFblock,
long PFblockStart,
long PFblockEnd,
long NBbuff
)
{
long IDinb0;
long IDinb1;
char imnameb0[500];
char imnameb1[500];
long cnt0, cnt1;
long cnt0_old, cnt1_old;
long IDinb;
long twaitus = 10000; // 0.01 sec
long PFblockSize;
long PFblockOrder;
float PFblockLag;
float PFblockdgain;
FILE *fp;
char fname[500];
int ret;
int Tupdate = 0;
time_t t;
struct tm *uttime;
struct timespec timenow;
long xsize, ysize, zsize, xysize;
long zsizein;
int cube;
long IDout;
uint32_t *imsizearray;
uint8_t datatype;
char imnameout[500];
long ii, kk;
long ave;
char inmaskname[200];
char inmaskfname[200];
char outmaskfname[200];
long IDinmask;
PROCESSINFO *processinfo;
if(data.processinfo==1)
{
// CREATE PROCESSINFO ENTRY
// see processtools.c in module CommandLineInterface for details
//
char pinfoname[200]; // short name for the processinfo instance
// avoid spaces, name should be human-readable
sprintf(pinfoname, "PFwatchInput-loop%ld-block%ld", loop, PFblock);
processinfo = processinfo_shm_create(pinfoname, 0);
processinfo->loopstat = 0; // loop initialization
strcpy(processinfo->source_FUNCTION, __FUNCTION__);
strcpy(processinfo->source_FILE, __FILE__);
processinfo->source_LINE = __LINE__;
char msgstring[200];
sprintf(msgstring, "%ld->%ld %ld buffers", PFblockStart, PFblockEnd, NBbuff);
processinfo_WriteMessage(processinfo, msgstring);
}
// CATCH SIGNALS
if (sigaction(SIGTERM, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGTERM\n");
if (sigaction(SIGINT, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGINT\n");
if (sigaction(SIGABRT, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGABRT\n");
if (sigaction(SIGBUS, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGBUS\n");
if (sigaction(SIGSEGV, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGSEGV\n");
if (sigaction(SIGHUP, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGHUP\n");
if (sigaction(SIGPIPE, &data.sigact, NULL) == -1)
printf("\ncan't catch SIGPIPE\n");
PFblockSize = PFblockEnd - PFblockStart;
if(sprintf(imnameb0, "aol%ld_modeval_ol_logbuff0", loop) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
if(sprintf(imnameb1, "aol%ld_modeval_ol_logbuff1", loop) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
IDinb0 = read_sharedmem_image(imnameb0);
IDinb1 = read_sharedmem_image(imnameb1);
cnt0_old = data.image[IDinb0].md[0].cnt0;
cnt1_old = data.image[IDinb1].md[0].cnt0;
xsize = data.image[IDinb0].md[0].size[0];
ysize = data.image[IDinb0].md[0].size[1];
xysize = xsize*ysize;
zsizein = data.image[IDinb0].md[0].size[2];
zsize = data.image[IDinb0].md[0].size[2]*NBbuff;
datatype = data.image[IDinb0].md[0].datatype;
list_image_ID();
if(system("mkdir -p PredictiveControl") != 0)
printERROR(__FILE__, __func__, __LINE__, "system() returns non-zero value");
if(sprintf(inmaskname, "inmaskPFb%ld", PFblock) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
IDinmask = create_2Dimage_ID(inmaskname, xysize, 1);
for(ii=0; ii<xysize; ii++)
data.image[IDinmask].array.F[ii] = 0.0;
for(ii=PFblockStart; ii<PFblockEnd; ii++)
data.image[IDinmask].array.F[ii] = 1.0;
if(sprintf(inmaskfname, "!./PredictiveControl/inmaskPF%ld.fits", PFblock) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
save_fits(inmaskname, inmaskfname);
if(sprintf(outmaskfname, "!./PredictiveControl/outmaskPF%ld.fits", PFblock) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
save_fits(inmaskname, outmaskfname);
printf("Create aol%ld_modevalol_PFb%ld : %ld x 1 x %ld\n", loop, PFblock, PFblockSize, zsize);
fflush(stdout);
imsizearray = (uint32_t*) malloc(sizeof(uint32_t)*3);
imsizearray[0] = PFblockSize;
imsizearray[1] = 1;
imsizearray[2] = zsize;
if(sprintf(imnameout, "aol%ld_modevalol_PFb%ld", loop, PFblock) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
IDout = create_image_ID(imnameout, 3, imsizearray, datatype, 1, 1);
free(imsizearray);
COREMOD_MEMORY_image_set_semflush(imnameout, -1);
printf("Done\n");
fflush(stdout);
if(data.processinfo==1)
processinfo->loopstat = 1; // Notify processinfo that we are entering loop
long buffindex = 0;
long outcnt = 0;
long loopcnt = 0;
int loopOK = 1;
while(loopOK==1)
{
if(data.processinfo==1)
{
while(processinfo->CTRLval == 1) // pause
usleep(50);
if(processinfo->CTRLval == 2) // single iteration
processinfo->CTRLval = 1;
if(processinfo->CTRLval == 3) // exit loop
{
loopOK = 0;
}
}
cnt0 = data.image[IDinb0].md[0].cnt0;
cnt1 = data.image[IDinb1].md[0].cnt0;
if(cnt0!=cnt0_old)
{
cube = 0;
cnt0_old = cnt0;
IDinb = IDinb0;
Tupdate = 1;
}
if(cnt1!=cnt1_old)
{
cube = 1;
cnt1_old = cnt1;
IDinb = IDinb1;
Tupdate = 1;
}
if(Tupdate == 1)
{
data.image[IDout].md[0].write = 1;
long kkin;
for(kkin=0; kkin<zsizein; kkin++)
{
kk = buffindex*zsizein + kkin;
for(ii=0; ii<PFblockSize; ii++)
data.image[IDout].array.F[kk*PFblockSize + ii] = data.image[IDinb].array.F[kkin*xysize + (ii+PFblockStart)];
}
data.image[IDout].md[0].write = 0;
printf("[%3ld/%3ld %d]\n", buffindex, NBbuff, cube);
Tupdate = 0;
buffindex ++;
}
if(buffindex == NBbuff) // write output
{
t = time(NULL);
uttime = gmtime(&t);
clock_gettime(CLOCK_REALTIME, &timenow);
printf("%02d:%02d:%02ld.%09ld NEW TELEMETRY BUFFER AVAILABLE [%ld]\n", uttime->tm_hour, uttime->tm_min, timenow.tv_sec % 60, timenow.tv_nsec, outcnt);
data.image[IDout].md[0].write = 1;
for(ii=0; ii<PFblockSize; ii++) // Remove time averaged value
{
ave = 0.0;
for(kk=0; kk<zsize; kk++)
ave += data.image[IDout].array.F[kk*PFblockSize + ii];
ave /= zsize;
for(kk=0; kk<zsize; kk++)
data.image[IDout].array.F[kk*PFblockSize + ii] -= ave;
}
COREMOD_MEMORY_image_set_sempost_byID(IDout, -1);
data.image[IDout].md[0].cnt0++;
data.image[IDout].md[0].write = 0;
buffindex = 0;
outcnt++;
}
usleep(twaitus);
// process signals
if(data.signal_TERM == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGTERM);
}
if(data.signal_INT == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGINT);
}
if(data.signal_ABRT == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGABRT);
}
if(data.signal_BUS == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGBUS);
}
if(data.signal_SEGV == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGSEGV);
}
if(data.signal_HUP == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGHUP);
}
if(data.signal_PIPE == 1){
loopOK = 0;
if(data.processinfo==1)
processinfo_SIGexit(processinfo, SIGPIPE);
}
loopcnt++;
if(data.processinfo==1)
processinfo->loopcnt = loopcnt;
}
if(data.processinfo==1)
processinfo_cleanExit(processinfo);
return (IDout);
}