-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRbAbsorbScan.c
More file actions
304 lines (253 loc) · 9.82 KB
/
RbAbsorbScan.c
File metadata and controls
304 lines (253 loc) · 9.82 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
/*
Program to record an absorption profile. This is accomplished by
scanning the frequency of the laser over the transition range
(roughly 377103.463 to 377113.463 GHz or -4 to +6 GHz detuning)
while monitoring the intensity of the laser passing through a cell
containing alkali metal.
RasPi connected to USB 1208LS.
*/
#include "mathTools.h" // For calculating standard deviations
#include "interfacing/kenBoard.h" // For controlling stepper motors.
#include "interfacing/USB1208.h" // For accessing the photodiode signals
//#include "interfacing/vortexLaser.h" // For changing the detuning of the laser.
#include "interfacing/sacherLaser.h" // For changing the detuning of the laser.
#include "interfacing/RS485Devices.h" // For talking to wavemeter, Omega, etc.
#include "interfacing/grandvillePhillips.h" // For getting pressures.
#include "interfacing/flipMirror.h" // For manipulating the flip mirror.
#include "interfacing/laserFlag.h" // For blocking the lasers.
#include "interfacing/omegaCN7500.h" // For accessing temperatures
#include "interfacing/waveMeter.h" // For accessing the laser's frequency
#define BUFSIZE 1024
#define NUMCHANNELS 3
#define WAITTIME 2
void graphData(char* fileName);
void writeFileHeader(char* fileName, char* comments);
void collectAndRecordData(char* fileName, float startvalue, float endvalue, float stepsize);
void findMaxMinIntensity(float* maxes, float* mins,int* channels, int numChannels, int stepRange);
int main (int argc, char **argv)
{
// Variables for recording the time.
time_t rawtime;
struct tm * timeinfo;
// Variables for describing the scan.
float startvalue,endvalue,stepsize;
// Variables for storing text
char fileName[BUFSIZE],comments[BUFSIZE];
// A file used to indicate that we are collecting data.
char dataCollectionFileName[] = "/home/pi/.takingData";
// Used to store error codes
int err;
FILE *dataCollectionFlagFile, *fp;
/* Check to make sure that the proper arguments were supplied. */
if (argc==5) {
startvalue=atof(argv[1]);
endvalue=atof(argv[2]);
stepsize=atof(argv[3]);
strcpy(comments,argv[4]);
} else {
printf("Usage:\n");
printf("$ sudo ./RbAbsorbScan <begin> <end> <step> <comments>\n");
printf(" Suggested values: (33.8) (34.4) (.01) \n");
return 0;
}
// Get file name. use format "RbAbs"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
struct stat st = {0};
strftime(fileName,BUFSIZE,"/home/pi/RbData/%F",timeinfo);
if (stat(fileName, &st) == -1){ // Create the directory for the Day's data
mkdir(fileName,S_IRWXU | S_IRWXG | S_IRWXO );
}
strftime(fileName,BUFSIZE,"/home/pi/RbData/%F/RbAbs%F_%H%M%S.dat",timeinfo);
// Echo the filename and comments, if the program is being
// run by a script, this information isn't available on the
// screen and it's nice to know.
printf("\n%s\n",fileName);
printf("\n%s\n",comments);
// Indicate that data is being collected.
dataCollectionFlagFile=fopen(dataCollectionFileName,"w");
if (!dataCollectionFlagFile) {
printf("Unable to open file: %s\n",dataCollectionFileName);
exit(1);
}
initializeBoard();
initializeUSB1208();
if (endvalue>117.5) endvalue=117.5;
if (startvalue>117.5) endvalue=117.5;
if (startvalue<0) startvalue=0;
if (endvalue<0) endvalue=0;
if (startvalue>endvalue) {
printf("error: startvalue > endvalue.\nYeah, i could just swap them in code.. or you could just enter them in correctly. :-)\n");
return 1;
}
writeFileHeader(fileName, comments);
fp=fopen(fileName,"a");
if (!fp) {
printf("unable to open file: %s\n",fileName);
exit(1);
}
err=setMirror(0);
if(err>0) printf("Error Occured While setting Flip Mirror: %d\n",err);
collectAndRecordData(fileName, startvalue, endvalue, stepsize);
setSacherTemperature(startvalue); // Return to the start value.
// If running repeat runs with crontab,
// this ensures that the run will start quickly.
closeUSB1208();
graphData(fileName);
fclose(dataCollectionFlagFile);
remove(dataCollectionFileName);
return 0;
}
void collectAndRecordData(char* fileName, float startvalue, float endvalue, float stepsize){
float value;
FILE* fp;
int k=0,i;
int nSamples;
int count=0;
float involts[NUMCHANNELS];
float kensWaveLength;
fp=fopen(fileName,"a");
if (!fp) {
printf("unable to open file: %s\n",fileName);
exit(1);
}
// Allocate some memory to store measurements for calculating
// error bars.
nSamples = 16;
float* measurement = malloc(nSamples*sizeof(float));
value=startvalue;
setSacherTemperature(value);
delay(3000);
//for (value=endvalue;value <= endvalue && value >= startvalue;value-=stepsize){// high to low
for (value=startvalue;value < endvalue && value >= startvalue;value+=stepsize){// low to high
if(count%15==0) printf("TEMP Det VERTICAL | HORIZONTAL | REFERENCE\n");
setSacherTemperature(value);
printf("%2.3f\t",value);
fprintf(fp,"%2.3f\t",value);
// delay to allow transients to settle
delay(400);
getProbeDetuning(&kensWaveLength);// Getting the wavelength invokes a significant delay
// So we no longer need the previous delay statement.
//kensWaveLength = -1;
fprintf(fp,"%03.4f\t",kensWaveLength);
printf("%+03.1f\t",kensWaveLength);
for(k=0;k<NUMCHANNELS;k++){
involts[k]=0.0;
}
// grab several readings and average
for(k=1;k<NUMCHANNELS;k++){
// When measuring using the lock-in, use this piece of code.
// for (i=0;i<nSamples;i++){
// getMCPAnalogIn(k,&measurement[i]);
// involts[k-1]=involts[k-1]+measurement[i];
// delay(10);
// }
// When measuring using the ammeter, use this piece of code.
for (i=0;i<nSamples;i++){
getUSB1208AnalogIn(k,&measurement[i]);
involts[k-1]=involts[k-1]+measurement[i];
delay(10);
}
involts[k-1]=fabs(involts[k-1])/(float)nSamples;
fprintf(fp,"%0.4f\t%0.4f",involts[k-1],stdDeviation(measurement,nSamples));
printf(" %0.2f %0.2f ",involts[k-1],stdDeviation(measurement,nSamples));
if(k<NUMCHANNELS){
printf("|");
fprintf(fp,"\t");
};
}
for (i=0;i<nSamples;i++){
getUSB1208AnalogIn(k,&measurement[i]);
involts[k-1]=involts[k-1]+measurement[i];
delay(10);
}
involts[k-1]=fabs(involts[k-1])/(float)nSamples;
fprintf(fp,"%0.4f\t%0.4f",involts[k-1],stdDeviation(measurement,nSamples));
printf(" %0.2f %0.2f ",involts[k-1],stdDeviation(measurement,nSamples));
fprintf(fp,"\n");
printf("\n");
count++;
}
fprintf(fp,"\n");
fclose(fp);
free(measurement);
}
void writeFileHeader(char* fileName, char* comments){
FILE* fp;
float returnFloat;
fp=fopen(fileName,"w");
if (!fp) {
printf("unable to open file: %s\n",fileName);
exit(1);
}
fprintf(fp,"#File:\t%s\n",fileName);
fprintf(fp,"#Comments:\t%s\n",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%f\n",returnFloat);
printf("T_res:\t%.2f\n",returnFloat);
getSVCN7500(CN_RESERVE,&returnFloat);
fprintf(fp,"#T_res_set:\t%.1f\n",returnFloat);
getPVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg:\t%f\n",returnFloat);
printf("T_trg:\t%.2f\n",returnFloat);
getSVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg_set:\t%.1f\n",returnFloat);
/** End System Stats Recording **/
//fprintf(fp,"VOLT\tPUMP\tStdDev\tPROBE\tStdDev\tREF\tStdDev\n");
fprintf(fp,"VOLT\tDET\tVERT\tVERTsd\tHORIZ\tHORIZsd\tREF\tREFsd\n");
fclose(fp);
}
void graphData(char* fileName){
char fileNameBase[1024];
char buffer[BUFSIZE];
char* extension;
FILE* gnuplot;
// Create graphs for data see gnutest.c for an explanation of
// how this process works.
gnuplot = popen("gnuplot","w");
strcpy(fileNameBase,fileName);
extension = strstr(fileNameBase,".dat");
strcpy(extension,"");
if (gnuplot != NULL){
sprintf(buffer, "set title '%s'\n", fileName);
fprintf(gnuplot, buffer);
fprintf(gnuplot, "set key autotitle columnheader\n");
fprintf(gnuplot, "set xlabel 'Temperature (Detuning)'\n");
fprintf(gnuplot, "set ylabel 'Transmitted Current'\n");
fprintf(gnuplot, "set yrange [-.1:*]\n");
fprintf(gnuplot, "set xrange [*:*]\n");
//fprintf(gnuplot, "set x2range [*:*]\n");
fprintf(gnuplot, "set x2tics nomirror\n");
//sprintf(buffer, "plot '%s' using 1:6:7 with errorbars, '%s' using ($1*%f+%f):6:7 axes x2y1\n",fileName,fileName,aoutConv,aoutInt);
//fprintf(gnuplot, "set terminal cairolatex size 3.7in, 2.5in resolution 200\n");
fprintf(gnuplot, "set terminal png\n");
sprintf(buffer, "set output '%s.png'\n", fileNameBase);
fprintf(gnuplot, buffer);
sprintf(buffer, "plot '%s' using 1:7:8 with errorbars,\
'%s' using 1:5:6 with errorbars,\
'%s' using 1:3:4 with errorbars\n", fileName,fileName,fileName);
fprintf(gnuplot, buffer);
fprintf(gnuplot, "unset output\n");
fprintf(gnuplot, "set terminal dumb size 54,14\n");
fprintf(gnuplot, "set output\n");
sprintf(buffer, "plot '%s' using 1:7:8 with errorbars\n",fileName);
fprintf(gnuplot, buffer);
sprintf(buffer, "plot '%s' using 1:5:6 with errorbars\n",fileName);
fprintf(gnuplot, buffer);
sprintf(buffer, "plot '%s' using 1:3:4 with errorbars\n",fileName);
fprintf(gnuplot, buffer);
}
pclose(gnuplot);
}