-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexcitationfn.c
489 lines (412 loc) · 16.7 KB
/
excitationfn.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
486
487
488
489
/*
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 "interfacing/Sorensen120.h"
#include "interfacing/K617meter.h"
#include "mathTools.h"
#define BUFSIZE 1024
void graphData(char* fileName);
void collectAndRecordData(char* fileName, int steprange, int stepsize, float bias, float N2Offset, float oneD, float twoA, float startValue, int dwell);
void writeFileHeader(char* fileName, char* comments,
float bias, float oneD, float twoA, float N2Sweep,
int dwell);
int main (int argc, char **argv)
{
float bias, N2Offset, oneD, twoA, startValue;
int stepsize, scanrange;
int minstepsize,maxstepsize;
int dwell;
int i;
time_t rawtime;
struct tm * timeinfo;
char buffer[BUFSIZE],filePath[BUFSIZE],fileName[BUFSIZE],comments[BUFSIZE];
char dataCollectionFileName[] = "/home/pi/.takingData";
FILE *fp,*dataCollectionFlagFile;
// Make sure the correct number of arguments were supplied. If not,
// prompt the user with the proper form for input.
int expectedArguments=10;
if (argc == expectedArguments){
bias = atof(argv[1]);
N2Offset = atof(argv[2]);
oneD = atof(argv[3]);
twoA = atof(argv[4]);
startValue = atof(argv[5]);
scanrange =atoi(argv[6]);
stepsize = atoi(argv[7]);
dwell= atoi(argv[8]);
strcpy(comments,argv[9]);
} else{
printf("Hey, you made an error in\n");
printf("your input, please examine\n");
printf("the following usage to fix your error.\n");
printf("\n");
printf("You supplied %d arguments, %d were expected\n", argc-1,expectedArguments-1);
printf("\n");
printf("Usage:\n");
printf(" sudo ./excitationfn <filament bias (V)> ------ (remember neg., read from Filament P1 and P2 PS)\n");
printf(" <N2 Offset (V)> ---------- (read from //Target// PS\n");
printf(" <1D (V)> ----------------- (read from 1D PS)\n");
printf(" <2A (V)> ----------------- (read from 2A PS)\n");
printf(" <He Offset (V)> ---------- (set by computer) (remember neg.!, recommend -100)\n");
printf(" <He scan range (V)> ------ (traverse in steps down this many volts from He Offset. 0-63, recommend 59 )\n");
printf(" <step size (.05 V)> ------ ( 1-24, recommend 24 )\n");
printf(" <dwell time (s)> --------- ( 1-5, recommend 1 ) \n");
printf(" <comments> --------------- (Don't forget to enclose in quotes)\n");
printf("\n");
printf("Step sizes:\n");
printf(" 1: %1.2fV 9: %1.2fV 17: %1.2fV\n",1*HPCAL,9*HPCAL,17*HPCAL );
printf(" 2: %1.2fV 10: %1.2fV 18: %1.2fV\n",2*HPCAL,10*HPCAL,18*HPCAL);
printf(" 3: %1.2fV 11: %1.2fV 19: %1.2fV\n",3*HPCAL,11*HPCAL,19*HPCAL);
printf(" 4: %1.2fV 12: %1.2fV 20: %1.2fV\n",4*HPCAL,12*HPCAL,20*HPCAL);
printf(" 5: %1.2fV 13: %1.2fV 21: %1.2fV\n",5*HPCAL,13*HPCAL,21*HPCAL);
printf(" 6: %1.2fV 14: %1.2fV 22: %1.2fV\n",6*HPCAL,14*HPCAL,22*HPCAL);
printf(" 7: %1.2fV 15: %1.2fV 23: %1.2fV\n",7*HPCAL,15*HPCAL,23*HPCAL);
printf(" 8: %1.2fV 16: %1.2fV 24: %1.2fV\n",8*HPCAL,16*HPCAL,24*HPCAL);
printf(" \n");
return 1;
}
// Indicate that data is being collected.
dataCollectionFlagFile=fopen(dataCollectionFileName,"w");
if (!dataCollectionFlagFile) {
printf("Unable to open file \n");
fflush(stdout);
exit(1);
}
// set up USB interface
initializeBoard();
initializeUSB1208();
i = initializeK617(K617METER,GPIBBRIDGE1);
if(i != 0) printf("ERROR INITIALIZING K617\n");
i = resetGPIBBridge(GPIBBRIDGE1);
if(i != 0) printf("ERROR RESETTING GPIB BRIDGE\n");
// get file name. use format "EX"+$DATE+$TIME+".dat"
time(&rawtime);
timeinfo=localtime(&rawtime);
struct stat st = {0};
strftime(filePath,80,"/home/pi/RbData/%F",timeinfo); //INCLUDE
if (stat(filePath, &st) == -1){
mkdir(filePath,S_IRWXU | S_IRWXG | S_IRWXO );
}
strftime(fileName,80,"EX%F_%H%M%S",timeinfo);
sprintf(buffer,"%s.dat",fileName);
printf("-------------------------\n");
printf("|%s|\n",buffer);
printf("-------------------------\n");
sprintf(buffer,"%s/%s.dat",filePath,fileName);
fp=fopen(buffer,"w");
if (!fp) {
printf("Unable to open file: %s\n",buffer);
fflush(stdout);
exit(1);
}
fprintf(fp,"#File:\t%s\n",buffer);
fprintf(fp,"#Comments:\t%s\n",comments);
fprintf(fp,"#USB1208->HP3617Aconversion:\t%2.6f\n",HPCAL);
// Start Input Error Checking
minstepsize=1;
maxstepsize=24;
if (stepsize<minstepsize){
printf("Step size too small, using %d (%0.3fV) instead.\n",minstepsize,minstepsize*HPCAL);
stepsize=minstepsize;
}
else if (stepsize > maxstepsize){
printf("Step size too large, using %d (%0.3fV) instead.\n",maxstepsize,maxstepsize*HPCAL);
stepsize=maxstepsize;
}
if (abs(startValue) + abs(scanrange) > 180)
{
printf("-----------\n");
printf("Error! Trying to reach too high of voltages.\n");
printf("-----------\n");
scanrange=180+startValue;
}
// End Input Error Checking
fclose(fp);
writeFileHeader(buffer, comments, bias, N2Offset, oneD, twoA, dwell);
collectAndRecordData(buffer, scanrange, stepsize,
bias, N2Offset, oneD, twoA, startValue, dwell);
setUSB1208AnalogOut(HETARGET,0);
closeUSB1208();
sprintf(buffer,"%s/%s",filePath,fileName);
graphData(buffer);
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];
int heColumn=6;
gnuplot = popen("gnuplot","w");
/*
* Here's the Column order:
* 1. Aout
* 2. V_fil
* 3. V_N2
* 4. V_sw
* 5. V_he
* 6. e_fil_Eng
* 7. e_trg_Eng
* 8. Count Rate
* 9. Sqrt Count Rate
* 10. I_f
* 11. I_f Standard Deviation
* 12. Ion Gauge Reading
* 13. Ion Gauge Reading Std. Dev.
*/
if (gnuplot != NULL){
// First print to the terminal screen.
// Set up the output for printing to terminal
fprintf(gnuplot, "set terminal dumb size 54,24\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 'He Potential'\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 %d:abs(9):10 with yerrorbars\n", fileName,heColumn);
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 %d:abs(11):12 with yerrorbars\n", fileName,heColumn);
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 cairolatex png size 3.7in, 2.5in resolution 200\n");
fprintf(gnuplot, "set terminal png size 530, 360\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 'Count Rate (Hz)'\n");
// Print the plot
sprintf(buffer, "plot '%s.dat' using %d:abs(9):10 with yerrorbars\n", fileName, heColumn);
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 %d:abs(11):12 with yerrorbars\n", fileName, heColumn);
fprintf(gnuplot, "%s",buffer);
}
pclose(gnuplot);
}
void writeFileHeader(char* fileName, char* comments,
float bias, float oneD, float twoA, float N2Sweep,
int dwell){
float returnFloat;
FILE* fp;
fp=fopen(fileName,"a");
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);
fprintf(fp,"#V_fil:\t%.2f\n",bias);
fprintf(fp,"#V_1D:\t%.2f\n",oneD);
fprintf(fp,"#V_2A:\t%.2f\n",twoA);
fprintf(fp,"#NumberOfSecondsPerCountMeasurement:\t%d\n",dwell);
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\t",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);
printf("T_trg:\t%.2f\n",returnFloat);
getSVCN7500(CN_TARGET,&returnFloat);
fprintf(fp,"#T_trg_set:\t%f\n",returnFloat);
/** End System Stats Recording **/
fprintf(fp,"#MagnitudeOfCurrent(*10^-X):\t%d\n",0);
// Print the header for the information in the datafile
fprintf(fp,"Aout\tV_fil\tV_N2\tV_1D\tV_2A\tV_he\te_fil_Eng\te_trg_Eng\tCountRate\tCountRateStDev\tI_f\tI_fStDev\tIonGauge\tIGStdDev\n");
fclose(fp);
}
void collectAndRecordData(char* fileName, int scanrange, int stepsize, float bias, float N2Offset, float oneD, float twoA, float startValue, int dwell) {
float primaryEnergy, secondaryEnergy;
float current, pressure;
float totalHeOffset;
__u16 value;
long returnCounts;
int nSamples,i,k,hpCycles,hpEndRange,endStepRange,stepRange,stepRangeStart=0;
int err,sorensenSet=startValue,endStepRangeStart;
FILE* fp;
err=resetGPIBBridge(GPIBBRIDGE1);
delay(200);
err=initSorensen120(SORENSEN120,GPIBBRIDGE1);
hpEndRange=scanrange%60;
hpCycles=scanrange/61 + 1;
stepRange = 1023;
endStepRange = 1+(int)(hpEndRange/(HPCAL));
printf("Step range:%d\n",stepRange);
err = setSorensen120Volts(-sorensenSet,SORENSEN120,GPIBBRIDGE1);
if(err!=0)
{
printf("Error setting Sorensen Code: %d, Trying again\n",err);
err = setSorensen120Volts(-sorensenSet,SORENSEN120,GPIBBRIDGE1);
if(err!=0)
{
printf("Error setting Sorensen Code: %d, You need to fix something\n",err);
}
}
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 = 2;
float* measurement = malloc(nSamples*sizeof(float));
printf("\n");
printf("aout e_fil_Eng e_trg_Eng V_He Counts Current\n");
// Now that I'm going to use the Sorensen supply in combination
// with the HP supply, we'll need two for loops. One for setting
// the Sorensen to each of the needed values, the second for
// rastering the HP supply over the range.
for (k=0;k<hpCycles;k++){
// START SET SORENSEN
setUSB1208AnalogOut(HETARGET,0);
sorensenSet = startValue-k*60;
if(abs(sorensenSet) > 120){
endStepRangeStart=abs(sorensenSet)-120;
sorensenSet=-120;
endStepRange = 1+(int)((hpEndRange+endStepRangeStart)/(HPCAL));
endStepRangeStart=1+(int)((endStepRangeStart)/(HPCAL));
stepRangeStart=endStepRangeStart;
}
// The supply expects a positive value for the voltage,
// and we are providing a negative value as input.
printf("Setting Sorensen to %d\n", sorensenSet);
err = setSorensen120Volts(-sorensenSet,SORENSEN120,GPIBBRIDGE1);
if(err!=0)
{
printf("Error setting Sorensen Code: %d, Trying again\n",err);
err = setSorensen120Volts(-sorensenSet,SORENSEN120,GPIBBRIDGE1);
if(err!=0)
{
printf("Error setting Sorensen Code: %d, You need to fix something\n",err);
}
}
// END SET SORENSEN
if (k==hpCycles-1 && hpEndRange > 0) // If it's the last cycle.
{
stepRange=endStepRange;
}
for (value=stepRangeStart;value<stepRange;value+=stepsize){
//for (value=steprange;value>24;value-=stepsize){
setUSB1208AnalogOut(HETARGET,value);
printf("%04d ",value);
fprintf(fp,"%d\t",value);
fprintf(fp,"%4.2f\t",bias);
fprintf(fp,"%4.2f\t",N2Offset);
fprintf(fp,"%4.2f\t",oneD);
fprintf(fp,"%4.2f\t",twoA);
fprintf(fp,"%4.2f\t",sorensenSet - HPCAL*(float)value);
primaryEnergy = (sorensenSet - HPCAL*(float)value) - bias;
printf("% 6.1f eV ",primaryEnergy);
fprintf(fp,"%4.4f\t",primaryEnergy);
secondaryEnergy = (sorensenSet - HPCAL*(float)value) - (bias + N2Offset) ;
printf("% 6.1f eV ",secondaryEnergy);
fprintf(fp,"%4.4f\t",secondaryEnergy);
totalHeOffset=sorensenSet - HPCAL*(float)value;
printf("% 6.1f eV ",totalHeOffset);
// delay to allow transients to settle
delay(500);
// TODO Change this to collect a measurement
// for every second of dwell time plus one.
// TODO Change this to auto-scale set up a branch
// to test it out, and when we're collecitng excitation
// functions without Rb some time, give it a try!
getReadingK617(&measurement[0], K617METER, GPIBBRIDGE1);
getUSB1208Counter(dwell*10,&returnCounts);
printf("%06ld ",returnCounts);
getReadingK617(&measurement[1], K617METER, GPIBBRIDGE1);
current = 0.0;
// grab several readings and average
for (i=0;i<nSamples;i++){
current+=measurement[i];
}
current=current/(float)nSamples;
printf("%+01.2e\n",current);
fprintf(fp,"%ld\t%Lf\t",returnCounts/dwell,sqrtl(returnCounts)/dwell);
fprintf(fp,"%e\t%f\t",-current,/*0*/stdDeviation(measurement,nSamples));
// Grab several readings and average
pressure=0;
getIonGauge(&pressure);
//printf("IG= %2.2E \n",pressure);
fprintf(fp,"%2.4E\t%2.4E\n",pressure,0.);
}
}
setUSB1208AnalogOut(HETARGET,0);
err = setSorensen120Volts(0,SORENSEN120,GPIBBRIDGE1);
if(err!=0)
{
printf("Error setting Sorensen Code: %d, Trying again\n",err);
err = setSorensen120Volts(0,SORENSEN120,GPIBBRIDGE1);
if(err!=0)
{
printf("Error setting Sorensen Code: %d, You need to fix something\n",err);
}
}
free(measurement);
fclose(fp);
}