-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaradayScan.c
More file actions
369 lines (293 loc) · 10.8 KB
/
faradayScan.c
File metadata and controls
369 lines (293 loc) · 10.8 KB
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
/*
Program to record polarization.
RasPi connected to USB 1208LS.
FARADAY SCAN
use Aout 0 to set laser wavelength. see page 98-100
usage
$ sudo ./faradayscan <aoutstart> <aoutstop> <deltaaout> <comments_no_spaces>
2015-12-31
added error calculations. see page 5 and 6 of "FALL15" lab book
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <asm/types.h>
#include <wiringPi.h>
#include "mathTools.h" //includes stdDeviation
#include "interfacing/interfacing.h"
#include "interfacing/sacherLaser.h"
#include "interfacing/K6485meter.h"
#include "faradayScanAnalysisTools.h"
#include "interfacing/waveMeter.h"
#include "probeLaserControl.h"
#define PI 3.14159265358979
#define NUMSTEPS 350
#define STEPSIZE 14
#define STEPSPERREV 350.0
#define DATAPTSPERREV STEPSPERREV/STEPSIZE
#define WAITTIME 2
#define BUFSIZE 1024
int plotData(char* fileName);
int recordNumberDensity(char* fileName);
void collectDiscreteFourierData(FILE* fp, int* photoDetector, int numPhotoDetectors,int motor, int revolutions);
int main (int argc, char **argv)
{
float value, returnFloat, wavelength;
int revolutions, i;
/* Variables for generating a file name with date and time */
time_t rawtime;
struct tm * timeinfo;
char filePath[BUFSIZE],fileName[BUFSIZE], comments[BUFSIZE],buffer[BUFSIZE];
char dataCollectionFileName[] = "/home/pi/.takingData";
FILE *fp,*dataCollectionFlagFile,*configFile;
if (argc==2){
strcpy(comments,argv[1]);
} else {
printf("usage '~$ sudo ./faradayScan <comments in quotes>'\n");
return 1;
}
// Indicate that data is being collected.
dataCollectionFlagFile=fopen(dataCollectionFileName,"w");
if (!dataCollectionFlagFile) {
printf("unable to open file:\t%s\n",dataCollectionFileName);
exit(1);
}
revolutions=1;
// Set up interfacing devices
initializeBoard();
initializeUSB1208();
i = resetGPIBBridge(GPIBBRIDGE1);
i = resetGPIBBridge(GPIBBRIDGE2);
if(i != 0) printf("ERROR RESETTING GPIB BRIDGE\n");
// --- Create the directory for files. Use format "FDayScan"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
struct stat st = {0};
strftime(filePath,BUFSIZE,"/home/pi/RbData/%F",timeinfo);
if (stat(filePath, &st) == -1){
i = mkdir(filePath,S_IRWXU | S_IRWXG | S_IRWXO );
}
if (i != 0) printf("Error making directory\n");
// --- End create directory
// --- Create the filename to record data. Use format "FDayScan"+$DATE+$TIME+".dat"
strftime(fileName,BUFSIZE,"FDayScan%F_%H%M%S",timeinfo);
sprintf(buffer,"%s.dat",fileName);
printf("\n");
printf("-------------------------------\n");
printf("|%s|\n",buffer);
printf("-------------------------------\n");
sprintf(fileName,"%s/%s",filePath,buffer);
// --- End create filename.
printf("%s\n",fileName);
printf("%s\n",comments);
fp=fopen(fileName,"w");
if (!fp) {
printf("Unable to open file: %s\n",fileName);
fflush(stdout);
exit(1);
}
configFile=fopen("/home/pi/RbControl/system.cfg","r");
if (!configFile) {
printf("Unable to open config file\n");
fflush(stdout);
exit(1);
}
fprintf(fp,"#File:\t%s\n#Comments:\t%s\n",fileName,comments);
/** Record System Stats to File **/
/** Pressure Gauges **/
getIonGauge(&returnFloat);
printf("IonGauge %2.2E Torr \n",returnFloat);
fprintf(fp,"#IonGauge(Torr):\t%2.2E\n",returnFloat);
getConvectron(GP_TOP2,&returnFloat);
printf("CVGauge(Source Foreline): %2.2E Torr\n", returnFloat);
fprintf(fp,"#CVGauge(Source Foreline)(Torr):\t%2.2E\n", returnFloat);
getConvectron(GP_TOP1,&returnFloat);
printf("CVGauge(Target Foreline): %2.2E Torr\n", returnFloat);
fprintf(fp,"#CVGauge(Target Foreline)(Torr):\t%2.2E\n", returnFloat);
/** Temperature Controllers **/
getPVCN7500(CN_RESERVE,&returnFloat);
fprintf(fp,"#T_res:\t%.2f\n",returnFloat);
getSVCN7500(CN_RESERVE,&returnFloat);
fprintf(fp,"#T_res_set:\t%.2f\n",returnFloat);
getPVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg:\t%.2f\n",returnFloat);
getSVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg_set:\t%.2f\n",returnFloat);
/** End System Stats Recording **/
char line[1024];
fgets(line,1024,configFile);
while(line[0]=='#'){
fprintf(fp,"%s",line);
fgets(line,1024,configFile);
}
fclose(configFile);
/* For a rigorous look at the structure
int numDet=18,j;
float scanDet[]={-33,-30,-19,-18,-9,-5,-4.5,-4,-3.5,6,6.5,7,7.5,11,20,21,30,33};
*/
/*
int numDet=12,j;
float scanDet[]={-45,-30,-15,-10,-5,-4,4,5,10,15,30,45};
*/
/*
int numDet=10,j;
float scanDet[]={-29,-26,-19,-18,-9,11,20,21,30,33};
*/
/*For the lower densities */
int numDet=6,j;
float scanDet[]={-29,-16,-10,11,16,30};
/*Temporary test
int numDet=1,j;
float scanDet[]={-29};
*/
/* For the higher densities
int numDet=4,j;
float scanDet[]={-29,-16,16,33};
*/
fprintf(fp,"#Revolutions:\t%d\n",revolutions);
fprintf(fp,"#DataPointsPerRev:\t%f\n",DATAPTSPERREV);
fprintf(fp,"#NumVolts:\t%d\n",numDet);
fprintf(fp,"#StepSize:\t%d\n",STEPSIZE);
printf("Homing motor...\n");
homeMotor(PROBE_MOTOR);
// WHEN THE PUMP LASER IS OFF, measure with ammeters.
//int numPd=3;
//int pd[] = {BOTTOM_KEITHLEY,TOP_KEITHLEY,BROWN_KEITHLEY};
//fprintf(fp,"STEP\tPUMP\tPUMPsd\tPROBE\tPROBEsd\tREF\tREFsd\n");// Write the header for the data to the file.
// WHEN THE PUMP LASER IS OFF, measure with ammeters.
// WHEN THE PUMP LASER IS ON, measure with lock-in.
int numPd=1;
int pd[] = {BOTLOCKIN};
fprintf(fp,"STEP\tPUMP\tPUMPsd\n");
// WHEN THE PUMP LASER IS ON, measure with lock-in.
fclose(fp);
fp=fopen(fileName,"a");
//setProbeDetuning(scanDet[0]);
delay(3000);
for(j=0;j<numDet;j++){
setProbeDetuning(scanDet[j]);
value=getSacherTemperature();
delay(1000);
getProbeFrequency(&wavelength);
// wavelength=-1;
fprintf(fp,"\n\n#VOLT:%3.1f(%.2f)\n",value,wavelength);
printf("TEMP:%3.1f(%.2f) ",value,wavelength-LINECENTER);
fflush(stdout);
quickHomeMotor(PROBE_MOTOR);
collectDiscreteFourierData(fp,pd,numPd /*numPhotoDet*/,PROBE_MOTOR,revolutions);
}//end for Volt
fclose(fp);
setProbeDetuning(scanDet[0]);
delay(3000);
printf("\n");
fflush(stdout);
//printf("Processing Data...\n");
//analyzeData(fileName, numDet, revolutions, dataPointsPerRevolution, FOI);
char* extensionStart;
extensionStart=strstr(fileName,".dat");
strcpy(extensionStart,"RotationAnalysis.dat");
//printf("Plotting Data...\n");
//plotData(fileName);
//printf("Calculating number density...\n");
//calculateNumberDensity(fileName, 0, 0);
//printf("Recording number density to file...\n");
//recordNumberDensity(fileName);
closeUSB1208();
// Remove the file indicating that we are taking data.
fclose(dataCollectionFlagFile);
remove(dataCollectionFileName);
return 0;
}
void collectDiscreteFourierData(FILE* fp, int* photoDetector, int numPhotoDetectors,int motor, int revolutions)
{
int count=0;
int steps,i,j,k;
int nSamples=16;
float* measurement = malloc(nSamples*sizeof(float));
/* In the event I decide I want to get readings from the
* ammeters via GPIB, these lines will be useful.
* i=initializeK6485(K6485METERVERT,GPIBBRIDGE2);
* if(i != 0) printf("ERROR INITIALIZING K6485 VERT\n");
* i=initializeK6485(K6485METERHORIZ,GPIBBRIDGE2);
* if(i != 0) printf("ERROR INITIALIZING K6485 HORIZ\n");
* setRangeK6485(K6485METERVERT, GPIBBRIDGE2, 9);
* setRangeK6485(K6485METERHORIZ, GPIBBRIDGE2, 9);
*/
float* involts = calloc(numPhotoDetectors,sizeof(float));
float* stdDev = calloc(numPhotoDetectors,sizeof(float));
for (k=0;k<revolutions;k++){ //revolutions
for (steps=0;steps < NUMSTEPS;steps+=STEPSIZE){ // steps
// (NUMSTEPS) in increments of STEPSIZE
/* watching the o-scope, we find
* that:
* L-IN RELAX Sig. Settle time.
* 30 ms -> 300 ms
* 100 ms -> 500 ms
*/
delay(500);
// ------
// get samples and average
// ------
// COMMENT/UNCOMMENT
// ----- When measuring using the lock-in, use this piece of code.
for(j=0;j<numPhotoDetectors;j++){ // numPhotoDet1
involts[j]=0.0;
for (i=0;i<nSamples;i++){ // nSamples
getMCPAnalogIn(photoDetector[j],&measurement[i]);
involts[j]=involts[j]+measurement[i];
delay(WAITTIME);
} // nSamples
involts[j]=involts[j]/(float)nSamples;
stdDev[j]=stdDeviation(measurement,nSamples);
} // numPhotoDet1
// ----- When measuring using the lock-in, use this piece of code.
// COMMENT/UNCOMMENT
// ----- When measuring using the ammeter, use this piece of code.
//for(j=0;j<numPhotoDetectors;j++){ // numPhotoDet1
// involts[j]=0.0;
// for (i=0;i<nSamples;i++){ // nSamples
// getUSB1208AnalogIn(photoDetector[j],&measurement[i]);
// involts[j]=involts[j]+measurement[i];
// delay(WAITTIME);
// } // nSamples
// involts[j]=involts[j]/(float)nSamples;
// stdDev[j]=stdDeviation(measurement,nSamples);
//} // numPhotoDet1
// ----- When measuring using the ammeter, use this piece of code.
/* THE GPIB WAY ************/
//i = getReadingK6485(&involts[0], K6485METERVERT, GPIBBRIDGE2);
//i = getReadingK6485(&involts[1], K6485METERHORIZ, GPIBBRIDGE2);
//i = getReadingK485(&involts[2], K485METER, GPIBBRIDGE1);
//printf("%02d | ", timeCounter);
//for(k=0;k<NUMCHANNELS;k++){
// fprintf(fp,"%+0.5e\t%+0.5e\t", involts[k], 0.);
// printf("%+0.5e ", involts[k]);
// if(k<NUMCHANNELS) printf(" | ");
// delay(delayTime/NUMCHANNELS);
//}
/******* END GPIB Way */
// ----- When measuring using the ammeter, use this piece of code.
fprintf(fp,"%d\t",steps+NUMSTEPS*k);
for(j=0;j<numPhotoDetectors;j++){
if(j!=numPhotoDetectors-1)
fprintf(fp,"%f\t%f\t",involts[j],stdDev[j]);
else
fprintf(fp,"%f\t%f\n",involts[j],stdDev[j]);
}
count++;
stepMotor(motor,CLK,STEPSIZE);
} // steps
} // revolutions
//setRangeK6485(K6485METERVERT, GPIBBRIDGE2, 0);
//setRangeK6485(K6485METERHORIZ, GPIBBRIDGE2, 0);
free(measurement);
free(stdDev);
free(involts);
}