-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecordRetroReflection.c
More file actions
282 lines (237 loc) · 8.52 KB
/
recordRetroReflection.c
File metadata and controls
282 lines (237 loc) · 8.52 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
/*
Program to record excitation function. This is accomplished by
stepping up the voltage at the target in increments and recording
the number of counts at each of those voltages.
RasPi connected to USB 1208LS.
Target primaryEnergy: USB1208LS Analog out Ch1 controls HP3617A. See pg 31 my lab book
PMT Counts: data received from CTR in USB1208
The step size will be adjusted to the next available value if the specific Voltage
chosen is not available.
Usage:
./excitationfn.c <filament bias> <target offset> <scan range (0-30)> <step size> <comments>
*/
#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 "interfacing/interfacing.h"
#include "mathTools.h"
#define BUFSIZE 1024
#define WAITTIME 2
void graphData(char* fileName);
int main (int argc, char **argv)
{
int i,k;
long totalCounts;
int nSamples;
int num_rev;
int dwell,magnitude;
time_t rawtime;
struct tm * timeinfo;
char buffer[BUFSIZE],fileName[BUFSIZE],comments[BUFSIZE];
char dataCollectionFileName[] = "/home/pi/.takingData";
float returnFloat;
float current, pressure;
long returnCounts;
FILE *fp,*dataCollectionFlagFile;
// Make sure the correct number of arguments were supplied. If not,
// prompt the user with the proper form for input.
if (argc == 5){
dwell= atoi(argv[1]);
magnitude= atoi(argv[2]);
num_rev= atoi(argv[3]);
strcpy(comments,argv[4]);
} else{
printf(" Usage: \n");
printf(" sudo ./recordRetroReflection <time per measurement> <orderOfMagnitudeOfCurrent> <number of revolutions> <comments>\n");
printf(" \n");
return 1;
}
// Indicate that data is being collected.
dataCollectionFlagFile=fopen(dataCollectionFileName,"w");
if (!dataCollectionFlagFile) {
printf("Unable to open file \n");
exit(1);
}
// set up USB interface
initializeBoard();
initializeUSB1208();
// get file name. use format "EX"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
struct stat st = {0};
strftime(fileName,80,"/home/pi/RbData/%F",timeinfo); //INCLUDE
if (stat(fileName, &st) == -1){
mkdir(fileName,S_IRWXU | S_IRWXG | S_IRWXO );
}
strftime(fileName,80,"/home/pi/RbData/%F/MonitorCounts%F_%H%M%S",timeinfo);
sprintf(buffer,"%s.dat",fileName);
printf("\n%s\n",buffer);
fp=fopen(buffer,"w");
if (!fp) {
printf("Unable to open file: %s\n",buffer);
exit(1);
}
fprintf(fp,"#Filename:\t%s\n",buffer);
fprintf(fp,"#USB1208->HP3617Aconversion:\t%2.6f\n",HPCAL);
fprintf(fp,"#DWELL:\t%d\n",dwell);
fprintf(fp,"#Comments:\t%s\n",comments);
getIonGauge(&returnFloat);
printf("IonGauge %2.2E Torr \n",returnFloat);
fprintf(fp,"#IonGauge(Torr):\t%2.2E\n",returnFloat);
getConvectron(GP_TOP1,&returnFloat);
printf("CVGauge(N2) %2.2E Torr\n", returnFloat);
fprintf(fp,"#CVGauge(N2)(Torr):\t%2.2E\n", returnFloat);
getConvectron(GP_TOP2,&returnFloat);
printf("CVGauge(He) %2.2E Torr\n", returnFloat);
fprintf(fp,"#CVGauge(He)(Torr):\t%2.2E\n", returnFloat);
getPVCN7500(CN_RESERVE,&returnFloat);
fprintf(fp,"#CellTemp(Res):\t%f\n",returnFloat);
getPVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#CellTemp(Targ):\t%f\n",returnFloat);
fprintf(fp,"#MagnitudeOfCurrent(*10^-X):\t%d\n",magnitude);
int numPhotoDetectors = 3;
int photoDetector[] = {BROWN_KEITHLEY,BOTTOM_KEITHLEY,TOP_KEITHLEY};
char* names[]={"REF","HORIZ","VERT"};
// Print the header for the information in the datafile
fprintf(fp,"Measurement\tCount\tCountStDev\tCurrent\tCurrentStDev\tIonGauge\tIGStdDev");
for(i=0;i<numPhotoDetectors;i++){
fprintf(fp,"\t%s\t%ssd",names[i],names[i]);
}
fprintf(fp,"\n");
//fclose(fp);
// Allocate some memory to store measurements for calculating
// error bars.
nSamples = 16;
float* measurement = malloc(nSamples*sizeof(float));
float* involts = calloc(numPhotoDetectors,sizeof(float));
float* stdDev = calloc(numPhotoDetectors,sizeof(float));
int steps;
int stepsPerRev=350;
int stepSize=5;
int motor=PUMP_MOTOR;
homeMotor(motor);
totalCounts=0;
for (steps=0;steps < stepsPerRev*num_rev;steps+=stepSize){ // steps
fprintf(fp,"%d\t",steps);
getUSB1208Counter(dwell*10,&returnCounts);
printf("Counts %ld\t",returnCounts);
totalCounts+=returnCounts;
current = 0.0;
// grab several readings and average
for (i=0;i<nSamples;i++){
getUSB1208AnalogIn(K617,&measurement[i]);
current+=measurement[i];
}
current=current/(float)nSamples;
printf("Current %f\t",current);
fprintf(fp,"%ld\t%Lf\t",returnCounts,sqrtl(returnCounts));
fprintf(fp,"%f\t%f\t",-current,stdDeviation(measurement,nSamples));
// Record Pressure
pressure=0;
for (i=0;i<nSamples;i++){
getIonGauge(&measurement[i]);
pressure+=measurement[i];
}
pressure=pressure/(float)nSamples;
printf("IG= %2.2E \n",pressure);
fprintf(fp,"%2.4E\t%2.4E\t",pressure,stdDeviation(measurement,nSamples));
// Record photodiode signals
for(k=0;k<numPhotoDetectors;k++){ // numPhotoDet1
involts[k]=0.0;
for (i=0;i<nSamples;i++){ // nSamples
getUSB1208AnalogIn(photoDetector[k],&measurement[i]);
involts[k]=involts[k]+fabs(measurement[i]);
delay(WAITTIME);
} // nSamples
involts[k]=involts[k]/(float)nSamples;
stdDev[k]=stdDeviation(measurement,nSamples);
} // numPhotoDet1
for(k=0;k<numPhotoDetectors;k++){
if(k!=numPhotoDetectors-1)
fprintf(fp,"%f\t%f\t",involts[k],stdDev[k]);
else
fprintf(fp,"%f\t%f\n",involts[k],stdDev[k]);
}
stepMotor(motor,CLK,stepSize);
}
closeUSB1208();
free(measurement);
free(involts);
free(stdDev);
fclose(fp);
graphData(fileName);
fclose(dataCollectionFlagFile);
remove(dataCollectionFileName);
return 0;
}
void graphData(char* fileName){
// Create graphs for data see gnutest.c for an explanation of
// how this process works.
FILE *gnuplot;
char buffer[BUFSIZE];
gnuplot = popen("gnuplot","w");
if (gnuplot != NULL){
// First print to the terminal screen.
// Set up the output for printing to terminal
fprintf(gnuplot, "set terminal dumb size 100,28\n");
fprintf(gnuplot, "set output\n");
fprintf(gnuplot, "set key autotitle columnheader\n");
// Set up the axis for the first plot
sprintf(buffer, "set title '%s'\n", fileName);
fprintf(gnuplot, "%s",buffer);
fprintf(gnuplot, "set xlabel 'Measurement Number'\n");
fprintf(gnuplot, "set ylabel 'Counts'\n");
fprintf(gnuplot, "set yrange [0:*]\n");
// Print the plot to the screen
sprintf(buffer, "plot '%s.dat' using 1:2:3 with yerrorbars\n", fileName);
fprintf(gnuplot, "%s", buffer);
// Set up the axis for the second plot x axis stays the same
sprintf(buffer, "set title '%s'\n", fileName);
fprintf(gnuplot, "%s",buffer);
fprintf(gnuplot, "set ylabel 'Current'\n");
fprintf(gnuplot, "set yrange [0:*]\n");
// Print the plot to the screen
sprintf(buffer, "plot '%s.dat' using 1:4:5 with yerrorbars\n", fileName);
fprintf(gnuplot, "%s",buffer);
// End printing to screen
// Clear the previous output settings, we no longer want to
// output to the terminal.
fprintf(gnuplot, "unset output\n");
// Then print to an image file.
// Set up the output.
fprintf(gnuplot, "set terminal png\n");
sprintf(buffer, "set output '%s_counts.png'\n", fileName);
fprintf(gnuplot, "%s",buffer);
fprintf(gnuplot, "set key autotitle columnhead\n");
// Set up the axis labels
sprintf(buffer, "set title '%s'\n", fileName);
fprintf(gnuplot, "%s",buffer);
fprintf(gnuplot, "set yrange [0:*]\n");
fprintf(gnuplot, "set ylabel 'Counts'\n");
// Print the plot
sprintf(buffer, "plot '%s.dat' using 1:2:3 with yerrorbars\n", fileName);
fprintf(gnuplot, "%s",buffer);
fprintf(gnuplot, "unset output\n");
sprintf(buffer, "set output '%s_current.png'\n", fileName);
fprintf(gnuplot, "%s",buffer);
// Set up the axis labels, x stays the same
sprintf(buffer, "set title '%s'\n", fileName);
fprintf(gnuplot, "%s",buffer);
fprintf(gnuplot, "set yrange [0:*]\n");
fprintf(gnuplot, "set ylabel 'Current'\n");
// Print the plot
//fprintf(fp,"Aout\tbias\tN2Offset\tTotalHeOffset\tPrimaryElectronEnergy\tSecondaryElectronEnergy\tCount\tCountStDev\tCurrent\tCurrentStDev\tIonGauge\n");
sprintf(buffer, "plot '%s.dat' using 1:4:5 with yerrorbars\n", fileName);
fprintf(gnuplot, "%s",buffer);
}
pclose(gnuplot);
}