-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfaradayRotation.c
More file actions
269 lines (215 loc) · 8.37 KB
/
faradayRotation.c
File metadata and controls
269 lines (215 loc) · 8.37 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
/*
Program to record polarization.
RasPi connected to USB 1208LS.
FARADAY ROTATION
use Aout 0 to set laser wavelength. see page 98-100
usage
$ sudo ./faradayRotation <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 "faradayScanAnalysisTools.h"
#include "interfacing/waveMeter.h"
#define PI 3.14159265358979
#define STEPSIZE 14.0
#define STEPSPERREV 350.0
#define WAITTIME 2
#define LOCKIN 0
#define AMMETER 1
#define DETTYPE AMMETER
#define BUFSIZE 1024
int recordNumberDensity(char* fileName);
void collectDiscreteFourierData(FILE* fp, int* photoDetector, int numPhotoDetectors,int motor, int revolutions);
int main (int argc, char **argv)
{
int i;
int revolutions,dataPointsPerRevolution;
time_t rawtime;
float returnFloat;
//float probeOffset,mag1Voltage,mag2Voltage;
struct tm * timeinfo;
char fileName[BUFSIZE], comments[BUFSIZE];
char dailyFileName[BUFSIZE];
char dataCollectionFileName[] = "/home/pi/.takingData";
FILE *fp,*dataCollectionFlagFile,*configFile;
if (argc==2){
strcpy(comments,argv[1]);
} else {
printf("usage '~$ sudo ./faradayRotation <comments in quotes>'\n");
printf(" Don't forget to edit the config file! \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);
}
//printf("Data collection flag file created!\n"); //DEBUG
revolutions=1;
dataPointsPerRevolution=(int)STEPSPERREV/STEPSIZE;
// Set up interfacing devices
initializeBoard();
initializeUSB1208();
//printf("Initialized Board\n"); //DEBUG
// Get file name. Use format "FDayScan"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
struct stat st = {0};
strftime(fileName,BUFSIZE,"/home/pi/RbData/%F",timeinfo); //INCLUDE
if (stat(fileName, &st) == -1){
mkdir(fileName,S_IRWXU | S_IRWXG | S_IRWXO );
}
strftime(fileName,BUFSIZE,"/home/pi/RbData/%F/FDayRotation%F_%H%M%S.dat",timeinfo); //INCLUDE
strftime(dailyFileName,BUFSIZE,"/home/pi/RbData/%F/FDayRotation%F.dat",timeinfo); //INCLUDE
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");
exit(1);
}
fprintf(fp,"#File:\t%s\n#Comments:\t%s\n",fileName,comments);
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);
getPVCN7500(CN_RESERVE,&returnFloat);
fprintf(fp,"#T_res:\t%f\n",returnFloat);
getSVCN7500(CN_RESERVE,&returnFloat);
fprintf(fp,"#T_res_set:\t%f\n",returnFloat);
getPVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg:\t%f\n",returnFloat);
getSVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg_set:\t%f\n",returnFloat);
printf("Reading in configuration file...\n");
char line[1024];
fgets(line,1024,configFile);
while(line[0]=='#'){
fprintf(fp,"%s",line);
fgets(line,1024,configFile);
}
printf("Read in configuration file!\n");
fclose(configFile);
fprintf(fp,"#Revolutions:\t%d\n",revolutions);
fprintf(fp,"#DataPointsPerRev:\t%d\n",dataPointsPerRevolution);
fprintf(fp,"#NumVoltages:\t%d\n",1);
//fprintf(fp,"#PumpWavelength:\t%f\n",getPumpFrequency(&returnFloat));
fprintf(fp,"#ProbeWavelength:\t%f\n",getProbeFrequency(&returnFloat));
// UNCOMMENT THE FOLLOWING LINES WHEN COLLECTING with lock-ins
int numPhotoDetectors = 2;
int photoDetectors[] = {BOTLOCKIN, TOPLOCKIN};
char* names[]={"HORIZ", "VERT"};
// UNCOMMENT THE ABOVE LINES WHEN COLLECTING STANDARD DATA
// UNCOMMENT THE FOLLOWING LINES WHEN USING THE ammeters
//int numPhotoDetectors = 3;
//int photoDetectors[] = {BOTTOM_KEITHLEY,TOP_KEITHLEY,BROWN_KEITHLEY};
//char* names[]={"HORIZ","VERT","REF"};
// UNCOMMENT THE ABOVE LINES WHEN USING THE ammeters
int motor = PROBE_MOTOR;
//int motor = PUMP_MOTOR;
// Write the header for the data to the file.
fprintf(fp,"STEP");
for(i=0;i<numPhotoDetectors;i++){
fprintf(fp,"\t%s\t%ssd",names[i],names[i]);
}
fprintf(fp,"\n");
fclose(fp);
fp=fopen(fileName,"a");
homeMotor(motor);
fflush(stdout);
collectDiscreteFourierData(fp, photoDetectors, numPhotoDetectors, motor, revolutions);
fclose(fp);
//printf("Processing Data...\n");
plotRawData(fileName);
//analyzeData(fileName, 1, revolutions, dataPointsPerRevolution, FOI);
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)
{
float sumSin=0;
float sumCos=0;
int count=0;
int steps,i,j,k;
float angle;
int nSamples=16;
float* measurement = malloc(nSamples*sizeof(float));
float* involts = calloc(numPhotoDetectors,sizeof(float));
float* stdDev = calloc(numPhotoDetectors,sizeof(float));
for (k=0;k<revolutions;k++){ //revolutions
for (steps=0;steps < STEPSPERREV;steps+=(int)STEPSIZE){ // steps
// (STEPSPERREV) in increments of STEPSIZE
delay(600); // watching the o-scope, it looks like it takes ~100ms for the ammeter to settle after a change in LP.
// UPDATE: With the Lock-in at a time scale of 100 ms, it takes 500 ms to settle.
// UPDATE: With time scale of 30 ms, takes 300 ms to settle.
// UNCOMMENT if using lock-ins.
// COMMENT OUT if using ammeters.
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]+fabs(measurement[i]);
delay(WAITTIME);
} // nSamples
involts[j]=involts[j]/(float)nSamples;
stdDev[j]=stdDeviation(measurement,nSamples);
} // numPhotoDet1
// UNCOMMENT if using ammeters.
// COMMENT OUT if using Lock-ins.
//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]+fabs(measurement[i]);
// delay(WAITTIME);
// } // nSamples
// involts[j]=involts[j]/(float)nSamples;
// stdDev[j]=stdDeviation(measurement,nSamples);
//} // numPhotoDet1
fprintf(fp,"%d\t",steps+(int)STEPSPERREV*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]);
}
angle=2.0*PI*(steps)/STEPSPERREV;
sumSin+=involts[0]*sin(2*angle);
sumCos+=involts[0]*cos(2*angle);
count++;
stepMotor(motor,CLK,(int)STEPSIZE);
} // steps
} // revolutions
free(measurement);
free(stdDev);
free(involts);
}