-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickPolarization.c
More file actions
338 lines (284 loc) · 10 KB
/
quickPolarization.c
File metadata and controls
338 lines (284 loc) · 10 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
/*
Program to record polarization.
RasPi connected to USB 1208LS.
Target energy: USB1208LS Analog out Ch1 controls HP3617A. See pg 31 my lab book
PMT Counts: data received from CTR in USB1208
*/
#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 "fileTools.h"
#include "polarizationAnalysisTools.h"
#include "interfacing/interfacing.h"
#include "interfacing/RS485Devices.h"
#include "interfacing/Sorensen120.h"
#include "interfacing/K617meter.h"
#include "interfacing/K6485meter.h"
#define GPIBBRIDGE1 0XC9 // the gpib bridge can have many gpib devices attached to it, so will also need the GPIB address of each
// this is the GPIB addresses of each respective instrument attached to this bridge
#define SORENSEN120 0x0C
#ifndef DEFINITIONS_H
#define DEFINITIONS_H
#include "mathTools.h"
#endif
#define UNDENIABLESHIFT 10 // Sometimes one stepper motor position can be the same as another, we know for
// sure that we won't mistake two angles that are 10 steps from each other
// for each other though.
#define VALLEY1 75
#define PEAK1 225
#define VALLEY2 375
#define PEAK2 525
#define VALLEY3 675
#define PEAK3 825
#define VALLEY4 975
#define PEAK4 1125
#define STEPSPERREV 1200
#define DATAPOINTS (DATAPOINTSPERREV * REVOLUTIONS)
#define PI 3.14159265358979
int getPolarizationData(char* fileName, float VHe, int dwell, int nMeasurements, float leakageCurrent, int scale);
void plotCommand(FILE* gnuplot, char* fileName, char* buffer);
void plotData(char* fileName);
int main (int argc, char **argv)
{
int dwell;
float VHe;
float leakageCurrent;
int ammeterScale,nMeasurements;
char rawDataFileName[80],comments[1024];
char fileDirectory[80];
char buffer[1024];
char dataCollectionFileName[] = "/home/pi/.takingData";
// Variables for getting time information to identify
// when we recorded the data
time_t rawtime;
struct tm * timeinfo;
float returnFloat;
// Setup time variables
time(&rawtime);
timeinfo=localtime(&rawtime);
struct stat st = {0};
// Get parameters.
if (argc==7){
VHe=atoi(argv[1]);
dwell=atoi(argv[2]);
nMeasurements=atoi(argv[3]);
ammeterScale=atof(argv[4]);
leakageCurrent=atof(argv[5]);
strcpy(comments,argv[6]);
} else {
printf("There is one option for using this program: \n\n");
printf("usage '~$ sudo ./quickPolarization <VHe> (0-1023)\n");
printf(" <dwell> (1-5)s \n");
printf(" <nMeasurements> \n");
printf(" <ammeterScale> (assumed neg.)\n");
printf(" <leakageCurrent> (just mantissa, assumed neg.)\n");
printf(" <comments_in_double_quotes>' \n");
return 1;
}
// Indicate that data is being collected.
FILE* dataCollectionFlagFile;
dataCollectionFlagFile=fopen(dataCollectionFileName,"w");
if (!dataCollectionFlagFile) {
printf("unable to open file \n");
exit(1);
}
initializeBoard();
initializeUSB1208();
// RUDAMENTARIY ERROR CHECKING
if (VHe<0) VHe=0;
if (VHe>180) VHe=180;
// Create Directory for the day
strftime(fileDirectory,80,"/home/pi/RbData/%F",timeinfo);
if (stat(fileDirectory, &st) == -1){
mkdir(fileDirectory,S_IRWXU | S_IRWXG | S_IRWXO );
}
// Create file name. Use format "QPOL"+$DATE+$TIME+".dat"
strftime(buffer,80,"QPOL%F_%H%M%S.dat",timeinfo);
printf("---------------------------\n");
printf("|%s|\n", buffer);
printf("---------------------------\n");
sprintf(rawDataFileName,"%s/%s",fileDirectory,buffer);
FILE* rawData;
// Write the header for the raw data file.
rawData=fopen(rawDataFileName,"w");
if (!rawData) {
printf("Unable to open file: %s\n", rawDataFileName);
exit(1);
}
fprintf(rawData,"#File\t%s\n",rawDataFileName);
fprintf(rawData,"#Comments\t%s\n",comments);
printf("Comments:\t%s\n\n",comments);
getIonGauge(&returnFloat);
printf("IonGauge %2.2E Torr \n",returnFloat);
fprintf(rawData,"#IonGauge(Torr):\t%2.2E\n",returnFloat);
getConvectron(GP_TOP2,&returnFloat);
printf("CVGauge(Source Foreline): %2.2E Torr\n", returnFloat);
fprintf(rawData,"#CVGauge(Source Foreline)(Torr):\t%2.2E\n", returnFloat);
getConvectron(GP_TOP1,&returnFloat);
printf("CVGauge(Target Foreline): %2.2E Torr\n", returnFloat);
fprintf(rawData,"#CVGauge(Target Foreline)(Torr):\t%2.2E\n", returnFloat);
returnFloat=-1.0;
//getPVCN7500(CN_RESERVE,&returnFloat);
fprintf(rawData,"#CURTEMP_R(degC):\t%f\n",returnFloat);
//getSVCN7500(CN_RESERVE,&returnFloat);
fprintf(rawData,"#SETTEMP_R(degC):\t%f\n",returnFloat);
//getPVCN7500(CN_TARGET,&returnFloat);
fprintf(rawData,"#CURTEMP_T(degC):\t%f\n",returnFloat);
//getSVCN7500(CN_TARGET,&returnFloat);
fprintf(rawData,"#SETTEMP_T(degC):\t%f\n",returnFloat);
fprintf(rawData,"#V_He:\t%f\n",VHe);
fprintf(rawData,"#LEAKCURR:\t%f\n",leakageCurrent);
fprintf(rawData,"#SCALE:\t%d\n",ammeterScale);
fprintf(rawData,"#REV:\t%d\n",REVOLUTIONS);
fprintf(rawData,"#DATAPPR:\t%d\n",DATAPOINTSPERREV);
fprintf(rawData,"#STPPERREV:\t%d\n",STEPSPERREV);
fprintf(rawData,"#DATPTS:\t%d\n",DATAPOINTS);
fprintf(rawData,"#DWELL(s):\t%d\n",dwell);
fclose(rawData);
// Collect raw data
getPolarizationData(rawDataFileName, VHe, dwell, nMeasurements, leakageCurrent, ammeterScale);
closeUSB1208();
// Remove the file indicating that we are taking data.
fclose(dataCollectionFlagFile);
remove(dataCollectionFileName);
return 0;
}
int getCountsAndCurrent(int dwell,long *sumCounts,float *avgCurrent){
long returnCounts;
int i;
float returnCurrent;
*avgCurrent=0.0;
*sumCounts=0;
for(i=0;i<dwell;i++){
//writeRS485to232Bridge("READ?",echoData,0xCA);
//current += atof(echoData);
getUSB1208AnalogIn(0,&returnCurrent);
*avgCurrent += returnCurrent/dwell;
getUSB1208Counter(10,&returnCounts);
*sumCounts += returnCounts;
}
return 0;
}
int getPolarizationData(char* fileName, float VHe, int dwell, int nMeasurements, float leakageCurrent, int scale){
// Variables for stepper motor control.
int j, i;
int retryCounter;
// Variables for data collections.
long sumCounts;
long allCountsPlus=0;
long allCountsMinus=0;
float current=0;
float countRatePlus=0;
float countRateMinus=0;
float sorensenValue, hpValue;
if (VHe<0)
{
VHe=-1;
sorensenValue=0;
}
else if (VHe < 60)
{
sorensenValue=0;
}
else if (VHe < 120)
{
sorensenValue=60;
}
else if (VHe < 180)
{
sorensenValue=120;
}
else
{
VHe=180;
sorensenValue=120;
};
hpValue=VHe-sorensenValue;
// If VHe is negative, we don't change the voltage on the polarimeter.
if (VHe >= 0){
setUSB1208AnalogOut(HETARGET,(int)hpValue/HPCAL);
i=resetGPIBBridge(GPIBBRIDGE1);
delay(200);
i=initSorensen120(SORENSEN120,GPIBBRIDGE1);
i = setSorensen120Volts(sorensenValue,SORENSEN120,GPIBBRIDGE1);
retryCounter=0;
if(i!=0 && retryCounter < 5){
retryCounter++;
printf("Error setting Sorensen Code: %d\n",i);
printf("Trying to set again after .5 s\n");
delay(500);
i = setSorensen120Volts(sorensenValue,SORENSEN120,GPIBBRIDGE1);
}
// The supply can take some time to get to he desired voltage, pause for 2 seconds to allow for this process.
delay(2000);
}
// NOTE THAT THIS SETS THE FINAL ELECTRON ENERGY. THIS ALSO DEPENDS ON BIAS AND TARGET OFFSET. AN EXCIATION FN WILL TELL THE
// USER WHAT OUT TO USE, OR JUST MANUALLY SET THE TARGET OFFSET FOR THE DESIRED ENERGY
// Begin File setup
FILE* rawData=fopen(fileName,"a");
if (!rawData) {
printf("Unable to open file: %s\n",fileName);
exit(1);
}
// End File setup
fprintf(rawData,"MEASUREMENT\tCOUNT+45\tCURRENT+45\tCOUNT-45\tCURRENT-45\n");// This line withough a comment is vital for being able to quickly process data. DON'T REMOVE
printf("\n");
printf("MEASUREMENT\tCOUNT+45\tCURRENT+45\tCOUNT-45\tCURRENT-45\n");// This line withough a comment is vital for being able to quickly process data. DON'T REMOVE
for (j=0;j<nMeasurements;j++){
homeMotor(POL_MOTOR);
stepMotor(POL_MOTOR,CCLK,VALLEY1);
getCountsAndCurrent(dwell,&sumCounts,¤t);
allCountsPlus=allCountsPlus+sumCounts;
countRatePlus=countRatePlus+(float)sumCounts /(current*pow(10,9-scale));
printf("%d\t\t%ld\t%1.2e\t",j,sumCounts,current+leakageCurrent);
fprintf(rawData,"%d\t%ld\t%1.3e\t",j,sumCounts,current+leakageCurrent);
homeMotor(POL_MOTOR);
stepMotor(POL_MOTOR,CLK,STEPSPERREV-VALLEY4);
getCountsAndCurrent(dwell,&sumCounts,¤t);
allCountsMinus=allCountsMinus+sumCounts;
countRateMinus=countRateMinus+(float)sumCounts /(current*pow(10,9-scale));
printf("%ld\t%1.2e\n",sumCounts,current+leakageCurrent);
fprintf(rawData,"%ld\t%1.3e\n",sumCounts,current+leakageCurrent);
stepMotor(POL_MOTOR,CCLK,STEPSPERREV-VALLEY4+UNDENIABLESHIFT);
}
printf("Total Cts.:\t%ld\t%ld\n",allCountsPlus,allCountsMinus);
printf("Rate:\t%2.2e\t%2.2e\n",(float)countRatePlus /nMeasurements,countRateMinus/nMeasurements);
fclose(rawData);
return 0;
}
void plotData(char* fileName){
FILE* gnuplot;
char buffer[1024];
char fileNameBase[1024];
char* extension;
// Create rough graphs of data.
gnuplot = popen("gnuplot","w");
strcpy(fileNameBase,fileName);
extension = strstr(fileNameBase,".dat");
strcpy(extension,"");
if (gnuplot != NULL){
fprintf(gnuplot, "set terminal dumb size 100,32\n");
fprintf(gnuplot, "set output\n");
sprintf(buffer, "set title '%s'\n", fileNameBase);
fprintf(gnuplot, buffer);
fprintf(gnuplot, "set key autotitle columnheader\n");
fprintf(gnuplot, "set xlabel 'Step'\n");
fprintf(gnuplot, "set ylabel 'Counts'\n");
fprintf(gnuplot, "set yrange [*:*]\n");
plotCommand(gnuplot, fileName,buffer);
}
pclose(gnuplot);
}
void plotCommand(FILE* gnuplot, char* fileName, char* buffer){
sprintf(buffer, "plot '%s' using 1:2\n",fileName);
fprintf(gnuplot, buffer);
}