-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadialScan.c
485 lines (347 loc) · 10.9 KB
/
RadialScan.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <windows.h>
#include "ansi_c.h"
#include "master.h"
#include "pvcam.h"
#include "ni4882.h"
#include "CoolSNAP.h"
#include "filename.h"
#include "stage.h"
#include "Vortex.h"
#include "RadialScan.h"
#include "zb_serial.h"
extern int BackgroundSubtractFlag;
/*
* main loop ties everything together
* controls the scanning, image gathering, and file generation
*
*/
void main(void){
float scanAngle;
char base_directory[255];
/*
char backgroundFilename[255];
int i;
char filename[512];
char image_filename[512];
*/
if (BackgroundSubtractFlag) {
printf("Background subtract flag set, aborting!");
return;
}
if (get_base_directory(base_directory) == -1) {
return;
}
if (!initial_test(base_directory)) {
return;
}
AcquireImage();
print_time();
printf("Starting Experiment");
SaveExperimentDescription(base_directory);
SaveExperimentDescriptionRadial(base_directory);
BrianStageReset(1);
Sleep(5000);
BrianStageReset(2);
Sleep(5000);
BrianStageMoveRelative(1, 640200);
Sleep(5000);
BrianStageMoveRelative(2, 640200);
Sleep(5000);
//BrianStageMoveRelative(1, -125982);
//Sleep(2000);
print_time();
printf("Vertical stage position %d\n", BrianStageGetPosition(1));
print_time();
printf("Horizontal stage position %d\n", BrianStageGetPosition(2));
getBackgroundImages(1, 2, base_directory);
enableLaser();
scanAngle = -M_PI_2;
while (scanAngle < M_PI_2 - 0.001) {
print_time();
printf("Scan Angle %f", scanAngle);
print_time();
printf("Getting Line");
print_time();
acquireLineAngle(scanAngle, NUM_LINE_SAMPLES);
print_time();
printf("Done getting Line");
print_time();
printf("Resetting Horizontal Stage");
BrianStageReset(2);
print_time();
printf("Resetting Vertical Stage");
BrianStageReset(1);
print_time();
printf("Incrementing Set ID");
IncrementSetId();
scanAngle += M_PI / (NUM_LINES);
}
print_time();
printf("Getting Background Images");
getBackgroundImages(3, 4, base_directory);
save_experiment_description_end(base_directory);
print_time();
printf("Done");
CloseBrianStages();
}
/*
* Scans a line at some angle relative to horizontal and saves the images
*
*/
void acquireLineAngle(float angle, int numSamples) {
int dx, dy;
int sampleNumber = 0;
char image_filename[512];
printf(" ");
dy = floor(DELTA_RHO * Y_SCALE * sin(angle) + 0.5);
dx = floor(DELTA_RHO * X_SCALE * cos(angle) + 0.5);
// move backwards half way
stage_move_hori(-dx * (numSamples - 1) / 2, 1, 0);
stage_move_vert(-dy * (numSamples - 1) / 2, 1, 0);
Sleep(4000);
for (sampleNumber = 0; sampleNumber < numSamples; sampleNumber++) {
print_time();
printf("(%03i)", sampleNumber);
AcquireImage();
GetImageFilename(image_filename);
SaveImage(image_filename);
stage_move_hori(dx, 1, 0);
Sleep(STAGE_MOVE_HORI_SLEEP);
stage_move_vert(dy, 1, 0);
Sleep(STAGE_MOVE_VERT_SLEEP);
}
BrianStageReset(1);
Sleep(5000);
BrianStageReset(2);
Sleep(5000);
}
void SaveExperimentDescriptionRadial(char * save_directory) {
char filename[512];
FILE * handle;
/*
FILE description[512];
int month, day, year, hour, minute, second;
LPSYSTEMTIME time;
*/
sprintf(filename, "%s\\_setup.txt", save_directory);
handle = fopen(filename, "a+");
fprintf(handle, "Number of radial lines: %d\n", NUM_LINES);
fprintf(handle, "Radial scan step size: %f\n", DELTA_RHO);
fprintf(handle, "Number of samples per radial line: %d\n", NUM_LINE_SAMPLES);
fprintf(handle, "X SCALER: %f\n", X_SCALE);
fprintf(handle, "Y SCALER: %f\n", Y_SCALE);
fclose(handle);
}
void SaveExperimentDescription(char * save_directory) {
char filename[512];
FILE * handle;
sprintf(filename, "%s\\_setup.txt", save_directory);
handle = fopen(filename, "a+");
time_t t = time(NULL);
struct tm tm = *localtime(&t);
fprintf(handle, "Start Date: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
fprintf(handle, "--------------------------\n");
fprintf(handle, "Description:\n");
fprintf(handle, "--------------------------\n");
fprintf(handle, "Shutter Time: %i\n", GetExposureTime());
fprintf(handle, "CCD Initial Temp: %f\n", GetCCDCurrentTemp() / 100.0);
fprintf(handle, "Laser Output Status: %i\n", VortexGetOutputStatus());
fprintf(handle, "Laser Diode Current: %f mA\n", VortexGetCurrent());
fprintf(handle, "Laser Diode Temperature: %f\n", VortexGetTemp());
fprintf(handle, "Laser Piezo Voltage: %f V\n", VortexGetVoltage());
fprintf(handle, "Laser Output Power: %f\n", VortexGetPower());
fprintf(handle, "Laser Diode Hours: %i\n", VortexGetDiodeHours());
fprintf(handle, "Laser Controller Hours: %i\n", VortexGetControllerHours());
fprintf(handle, "X Stage Starting Step %i\n", CENTER_X);
fprintf(handle, "Y Stage Starting Step %i\n", CENTER_Y);
fprintf(handle, "Wait after stage move %i mS\n", STAGE_MOVE_HORI_SLEEP);
fprintf(handle, "Vertical Line Pairs Start %i\n", VERT_LINE_PAIRS_START);
fprintf(handle, "Vertical Line Pairs Stop %i\n", VERT_LINE_PAIRS_END);
fprintf(handle, "Horizontal Number of Samples %i\n", BASE_HORIZ_LINE_SAMPLES);
fprintf(handle, "Vertical Step Size %i\n", VERTICAL_STEP_SIZE);
fprintf(handle, "Horizontal Step Size %i\n", HORIZ_STEP_SIZE);
fprintf(handle, "Base Save Directory %s\n", SAVE_DIRECTORY);
fprintf(handle, "Move Stage %i\n", MOVE_STAGE);
fprintf(handle, "Move Stage Vertically %i\n", MOVE_STAGE_VERT);
fprintf(handle, "Use Small Steps: %i\n", USE_SMALL_STEPS);
fclose(handle);
}
void save_experiment_description_end(char * save_directory) {
char filename[512];
//char description[512];
FILE * handle;
sprintf(filename, "%s\\_setup.txt", save_directory);
handle = fopen(filename, "a+");
time_t t = time(NULL);
struct tm tm = *localtime(&t);
fprintf(handle, "Start Date: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
fprintf(handle, "CCD Final Temp: %f\n", GetCCDCurrentTemp() / 100.0);
fprintf(handle, "Laser Output Status: %i\n", VortexGetOutputStatus());
fprintf(handle, "Laser Diode Current: %f mA\n", VortexGetCurrent());
fprintf(handle, "Laser Diode Temperature: %f\n", VortexGetTemp());
fprintf(handle, "Laser Piezo Voltage: %f V\n", VortexGetVoltage());
fprintf(handle, "Laser Output Power: %f\n", VortexGetPower());
fprintf(handle, "Laser Diode Hours: %i\n", VortexGetDiodeHours());
fprintf(handle, "Laser Controller Hours: %i\n", VortexGetControllerHours());
fclose(handle);
}
int get_base_directory(char * basedir) {
int loop = 0;
int exist = 1;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
while (exist) {
sprintf(basedir, "%s\\%04i%02i%02i_%03i", SAVE_DIRECTORY, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, loop);
exist = GetFileAttributes(basedir) != -1;
if (!exist) {
printf("\nCreating Directory: %s \n", basedir);
CreateDirectory(basedir, 0);
SetFilenameBase(basedir);
return 0;
}
loop++;
}
printf("\nUnable to find destination directory");
return -1;
}
/*
*
* returns a string that can be used by sprintf to generate the filename given an image number
*/
int initial_test(char * directory) {
//char filename[512];
//char description[512];
int success;
float ccd_temp;
success = 1;
InitializeCamera();
InitializeVortex();
ccd_temp = (float)GetCCDCurrentTemp();
printf("ccd_temp %d \n", ccd_temp);
InitializingBrianStages();
if (MOVE_STAGE) {
// initialize "com3"
Sleep(6000);
print_time();
stage_move_vert(CENTER_Y, 1, 1);
Sleep(6000);
print_time();
stage_move_hori(CENTER_X, 1, 1);//should set this to be the radial center
Sleep(6000);
print_time();
printf("Vertical stage position %d \n", BrianStageGetPosition(1));
Sleep(2000);
print_time();
printf("Horizontal stage position %d \n", BrianStageGetPosition(2));
// Verify that the stage actually moved
if (MessageBox(NULL, "Did the stage move?", "Stage Test", MB_YESNO) == IDNO) {
printf("\nStage Did Not Move");
success = 0;
}
}
/*
sprintf(filename, "%s\\test.txt", directory);
handle = fopen(filename, "w");
if (!handle) {
printf("\nTest File Could Not Be Opened %s", filename);
success = 0;
}
fclose(handle);
// delete the test file
DeleteFile(filename);
*/
if (abs(GetCCDCurrentTemp() - ccd_temp) > 3.0) {
printf("\nCCD Temperature not stable (%f) (%f)", ccd_temp / 100.0, GetCCDCurrentTemp() / 100.0);
// success = 0;
}
if (!success || MessageBox(NULL, "Run Experiment?", "Start Experiment", MB_YESNO) == IDNO) {
printf("\nExperiment Aborted!!!");
success = 0;
}
return success;
}
int stage_move_vert(int step, int move, int sleep) {
int new_position;
int moved = 0;
int dir;
new_position = stage_offset(step, move);
if (move) {
// moves the stage by step
dir = step > 0 ? 1 : -1;
step = abs(step);
if (USE_SMALL_STEPS) {
while (step > VERTICAL_STEP_SIZE) {
step -= VERTICAL_STEP_SIZE;
BrianStageMoveRelative(1, dir * VERTICAL_STEP_SIZE);
// move_vert_stage_d(dir * VERTICAL_STEP_SIZE);
if (sleep) {
printf("^");
Sleep(STAGE_MOVE_VERT_SLEEP);
}
}
}
if (step > 0) {
BrianStageMoveRelative(1, dir * step);
// move_vert_stage_d(dir * step);
if (sleep) {
printf("%");
Sleep(STAGE_MOVE_VERT_SLEEP);
}
}
}
return new_position;
}
int stage_move_hori(int step, int move, int sleep) {
int new_position;
int moved = 0;
int dir;
new_position = stage_offset(step, move);
if (move) {
// moves the stage by step
dir = step > 0 ? 1 : -1;
step = abs(step);
if (USE_SMALL_STEPS) {
while (step > HORIZ_STEP_SIZE) {
step -= HORIZ_STEP_SIZE;
BrianStageMoveRelative(2, dir * HORIZ_STEP_SIZE);
// move_vert_stage_d(dir * VERTICAL_STEP_SIZE);
if (sleep) {
printf("^");
Sleep(STAGE_MOVE_HORI_SLEEP);
}
}
}
if (step > 0) {
BrianStageMoveRelative(2, dir * step);
// move_vert_stage_d(dir * step);
if (sleep) {
printf("%");
Sleep(STAGE_MOVE_VERT_SLEEP);
}
}
}
return new_position;
}
/*
* keeps track of the vertical stage offset
* first argument is how much the stage is going to be or just was moved
* return argument is new stage offset
*/
int stage_offset(int step, int move) {
static int offset = 0;
if (move) {
offset += step;
}
return offset;
}
int getVerticalPosition(void) {
return BrianStageGetPosition(1);
}
int getHorizontalPosition(void) {
return BrianStageGetPosition(2);
}
void print_time(void) {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}