forked from cacao-org/AOloopControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOloopControl_fpspeckle_mod.c
343 lines (263 loc) · 10.3 KB
/
AOloopControl_fpspeckle_mod.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
/**
* @file AOloopControl_fpspeckle_mod.c
* @brief AO loop control - FOCAL PLANE SPECKLE MODULATION / CONTROL
*
* REAL TIME COMPUTING ROUTINES
*
*
*
* @bug No known bugs.
*
*/
#define _GNU_SOURCE
#include "CommandLineInterface/CLIcore.h"
#include "AOloopControl/AOloopControl.h"
#include "00CORE/00CORE.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "AOloopControl_IOtools/AOloopControl_IOtools.h"
#include <string.h>
#include <math.h>
// defined in AOloopControl.c
extern AOLOOPCONTROL_CONF *AOconf; // configuration - this can be an array
// defined in AOloopControl.c
extern AOloopControl_var aoloopcontrol_var;
// optimize LO - uses simulated downhill simplex
int_fast8_t AOloopControl_OptimizePSF_LO(const char *psfstream_name, const char *IDmodes_name, const char *dmstream_name, long delayframe, long NBframes)
{
long IDmodes;
long IDdmstream;
long IDdm;
// long psfxsize, psfysize;
long dmxsize, dmysize;
long NBmodes;
long mode;
double ampl;
double x;
long ii, jj;
long IDdmbest;
long IDpsfarray;
char imname[200];
if(aoloopcontrol_var.aoconfID_looptiming == -1)
{
// LOOPiteration is written in cnt1 of loop timing array
if(sprintf(imname, "aol%ld_looptiming", aoloopcontrol_var.LOOPNUMBER) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
aoloopcontrol_var.aoconfID_looptiming = AOloopControl_IOtools_2Dloadcreate_shmim(imname, " ", aoloopcontrol_var.AOcontrolNBtimers, 1, 0.0);
}
ampl = 0.01; // modulation amplitude
// IDpsf = image_ID(psfstream_name);
IDmodes = image_ID(IDmodes_name);
IDdmstream = image_ID(dmstream_name);
// psfxsize = data.image[IDpsf].md[0].size[0];
// psfysize = data.image[IDpsf].md[0].size[1];
IDdmbest = create_2Dimage_ID("dmbest", dmxsize, dmysize);
IDdm = create_2Dimage_ID("dmcurr", dmxsize, dmysize);
dmxsize = data.image[IDdm].md[0].size[0];
dmysize = data.image[IDdm].md[0].size[1];
NBmodes = data.image[IDmodes].md[0].size[2];
for(ii=0; ii<dmxsize*dmysize; ii++)
data.image[IDdmbest].array.F[ii] = data.image[IDdm].array.F[ii];
for(mode=0; mode<NBmodes; mode ++)
{
for(x=-ampl; x<1.01*ampl; x += ampl)
{
// apply DM pattern
for(ii=0; ii<dmxsize*dmysize; ii++)
data.image[IDdm].array.F[ii] = data.image[IDdmbest].array.F[ii]+ampl*data.image[IDmodes].array.F[dmxsize*dmysize*mode+ii];
data.image[IDdmstream].md[0].write = 1;
memcpy(data.image[IDdmstream].array.F, data.image[IDdm].array.F, sizeof(float)*dmxsize*dmysize);
data.image[IDdmstream].md[0].cnt0++;
data.image[IDdmstream].md[0].cnt1 = data.image[aoloopcontrol_var.aoconfID_looptiming].md[0].cnt1;
data.image[IDdmstream].md[0].write = 0;
}
}
return(0);
}
//
// modulate using linear combination of two probes A and B
//
//
// delay is in sec
//
int_fast8_t AOloopControl_DMmodulateAB(const char *IDprobeA_name, const char *IDprobeB_name, const char *IDdmstream_name, const char *IDrespmat_name, const char *IDwfsrefstream_name, double delay, long NBprobes)
{
long IDprobeA;
long IDprobeB;
long dmxsize, dmysize;
long dmsize;
long IDdmstream;
long IDrespmat;
long IDwfsrefstream;
long wfsxsize, wfsysize;
long wfssize;
long IDdmC;
long IDwfsrefC;
float *coeffA;
float *coeffB;
int k;
long act, wfselem;
char imname[200];
FILE *fp;
char flogname[200];
int loopOK;
long dmframesize, wfsframesize;
char timestr[200];
time_t t;
struct tm *uttime;
struct timespec *thetime = (struct timespec *)malloc(sizeof(struct timespec));
long ii;
int semval;
if(aoloopcontrol_var.aoconfID_looptiming == -1)
{
// LOOPiteration is written in cnt1 of loop timing array
if(sprintf(imname, "aol%ld_looptiming", aoloopcontrol_var.LOOPNUMBER) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
aoloopcontrol_var.aoconfID_looptiming = AOloopControl_IOtools_2Dloadcreate_shmim(imname, " ", aoloopcontrol_var.AOcontrolNBtimers, 1, 0.0);
}
IDprobeA = image_ID(IDprobeA_name);
dmxsize = data.image[IDprobeA].md[0].size[0];
dmysize = data.image[IDprobeA].md[0].size[1];
dmsize = dmxsize*dmysize;
IDprobeB = image_ID(IDprobeB_name);
IDdmstream = image_ID(IDdmstream_name);
IDrespmat = image_ID(IDrespmat_name);
IDwfsrefstream = image_ID(IDwfsrefstream_name);
wfsxsize = data.image[IDwfsrefstream].md[0].size[0];
wfsysize = data.image[IDwfsrefstream].md[0].size[1];
wfssize = wfsxsize*wfsysize;
coeffA = (float*) malloc(sizeof(float)*NBprobes);
coeffB = (float*) malloc(sizeof(float)*NBprobes);
IDdmC = create_3Dimage_ID("MODdmC", dmxsize, dmysize, NBprobes);
IDwfsrefC = create_3Dimage_ID("WFSrefC", wfsxsize, wfsysize, NBprobes);
coeffA[0] = 0.0;
coeffB[0] = 0.0;
for(k=1; k<NBprobes; k++)
{
coeffA[k] = cos(2.0*M_PI*(k-1)/(NBprobes-1));
coeffB[k] = sin(2.0*M_PI*(k-1)/(NBprobes-1));
}
// prepare MODdmC and WFSrefC
for(k=0; k<NBprobes; k++)
{
for(act=0; act<dmsize; act++)
data.image[IDdmC].array.F[k*dmsize+act] = coeffA[k]*data.image[IDprobeA].array.F[act] + coeffB[k]*data.image[IDprobeB].array.F[act];
for(wfselem=0; wfselem<wfssize; wfselem++)
for(act=0; act<dmsize; act++)
data.image[IDwfsrefC].array.F[k*wfssize+wfselem] += data.image[IDdmC].array.F[k*dmsize+act]*data.image[IDrespmat].array.F[act*wfssize+wfselem];
}
save_fl_fits("MODdmC", "!test_MODdmC.fits");
save_fl_fits("WFSrefC", "!test_WFSrefC.fits");
t = time(NULL);
uttime = gmtime(&t);
if(sprintf(flogname, "logfpwfs_%04d-%02d-%02d_%02d:%02d:%02d.txt", 1900+uttime->tm_year, 1+uttime->tm_mon, uttime->tm_mday, uttime->tm_hour, uttime->tm_min, uttime->tm_sec) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
if((fp=fopen(flogname,"w"))==NULL)
{
printf("ERROR: cannot create file \"%s\"\n", flogname);
exit(0);
}
fclose(fp);
dmframesize = sizeof(float)*dmsize;
wfsframesize = sizeof(float)*wfssize;
list_image_ID();
if (sigaction(SIGINT, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
if (sigaction(SIGTERM, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
if (sigaction(SIGBUS, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
/* if (sigaction(SIGSEGV, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}*/
if (sigaction(SIGABRT, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
if (sigaction(SIGHUP, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
if (sigaction(SIGPIPE, &data.sigact, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
k = 0;
loopOK = 1;
while(loopOK == 1)
{
printf("Applying probe # %d %ld %ld\n", k, IDdmstream, IDwfsrefstream);
fflush(stdout);
// apply probe
char *ptr0;
ptr0 = (char*) data.image[IDdmC].array.F;
ptr0 += k*dmframesize;
data.image[IDdmstream].md[0].write = 1;
memcpy(data.image[IDdmstream].array.F, (void*) ptr0, dmframesize);
sem_getvalue(data.image[IDdmstream].semptr[0], &semval);
if(semval<SEMAPHORE_MAXVAL)
sem_post(data.image[IDdmstream].semptr[0]);
data.image[IDdmstream].md[0].cnt0++;
data.image[IDdmstream].md[0].cnt1 = data.image[aoloopcontrol_var.aoconfID_looptiming].md[0].cnt1;
data.image[IDdmstream].md[0].write = 0;
// apply wfsref offset
ptr0 = (char*) data.image[IDwfsrefC].array.F;
ptr0 += k*wfsframesize;
data.image[IDwfsrefstream].md[0].write = 1;
memcpy(data.image[IDwfsrefstream].array.F, (void*) ptr0, wfsframesize);
sem_getvalue(data.image[IDwfsrefstream].semptr[0], &semval);
if(semval<SEMAPHORE_MAXVAL)
sem_post(data.image[IDwfsrefstream].semptr[0]);
data.image[IDwfsrefstream].md[0].cnt0++;
data.image[IDwfsrefstream].md[0].cnt1 = data.image[aoloopcontrol_var.aoconfID_looptiming].md[0].cnt1;
data.image[IDwfsrefstream].md[0].write = 0;
// write time in log
t = time(NULL);
uttime = gmtime(&t);
clock_gettime(CLOCK_REALTIME, thetime);
if(sprintf(timestr, "%02d %02d %02d.%09ld", uttime->tm_hour, uttime->tm_min, uttime->tm_sec, thetime->tv_nsec) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
printf("time = %s\n", timestr);
if((fp = fopen(flogname, "a"))==NULL)
{
printf("ERROR: cannot open file \"%s\"\n", flogname);
exit(0);
}
fprintf(fp, "%s %2d %10f %10f\n", timestr, k, coeffA[k], coeffB[k]);
fclose(fp);
usleep((long) (1.0e6*delay));
k++;
if(k==NBprobes)
k = 0;
if((data.signal_INT == 1)||(data.signal_TERM == 1)||(data.signal_ABRT==1)||(data.signal_BUS==1)||(data.signal_SEGV==1)||(data.signal_HUP==1)||(data.signal_PIPE==1))
loopOK = 0;
}
data.image[IDdmstream].md[0].write = 1;
for(ii=0; ii<dmsize; ii++)
data.image[IDdmstream].array.F[ii] = 0.0;
sem_getvalue(data.image[IDdmstream].semptr[0], &semval);
if(semval<SEMAPHORE_MAXVAL)
sem_post(data.image[IDdmstream].semptr[0]);
data.image[IDdmstream].md[0].cnt0++;
data.image[IDdmstream].md[0].cnt1 = data.image[aoloopcontrol_var.aoconfID_looptiming].md[0].cnt1;
data.image[IDdmstream].md[0].write = 0;
data.image[IDwfsrefstream].md[0].write = 1;
for(ii=0; ii<wfssize; ii++)
data.image[IDwfsrefstream].array.F[ii] = 0.0;
sem_getvalue(data.image[IDwfsrefstream].semptr[0], &semval);
if(semval<SEMAPHORE_MAXVAL)
sem_post(data.image[IDwfsrefstream].semptr[0]);
data.image[IDwfsrefstream].md[0].cnt0++;
data.image[IDwfsrefstream].md[0].cnt1 = data.image[aoloopcontrol_var.aoconfID_looptiming].md[0].cnt1;
data.image[IDwfsrefstream].md[0].write = 0;
free(coeffA);
free(coeffB);
return(0);
}