-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
executable file
·224 lines (182 loc) · 6.98 KB
/
main.c
File metadata and controls
executable file
·224 lines (182 loc) · 6.98 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
#include <stdlib.h>
#include <stdio.h>
#include <NIDAQmx.h>
//Includes for writing to temp files
#include<unistd.h>
#include<string.h>
#include<errno.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
// This is the name of the DAQ device
#define DAQ_DEVICE_NAME "cDAQ1"
// This is the name of the module port we measure from
#define DAQ_MODULE_NAME "Mod3"
#define DAQ_DEVICE DAQ_DEVICE_NAME DAQ_MODULE_NAME
// The END channel is INCLUSIVE
// These are the pin indices used for DIFFERENTIAL mode
// Pin X will automatically be paired with its corresponding
// x+8 pin.
// This setup assumes we are using pins 0, 1, 2, and 3
// with their complementary pins 8, 9, 10, and 11
// which are automatically read by the NIDAQ library
// when we are in differential mode
#define DIFF_INPUTS_BEGIN "0"
#define DIFF_INPUTS_END "3"
// We'll pass this into the CreateAIVoltageChan to specify
// the channels we want to work with.
#define DIFF_CHANNELS DAQ_DEVICE "/ai" DIFF_INPUTS_BEGIN ":" DIFF_INPUTS_END
#define PHYS_CHANNELS DIFF_CHANNELS
// The name assigned to our task
#define DAQ_TASK_NAME ""
// The name we assign to our voltage channel
#define CHANNEL_NAME ""
// The minimum and maximum volts we expect to measure
// The NIDAQ device only supports max of -10/10 volts
// This is not good if we have a 12V supply we want to measure
#define MIN_VOLTS -10.0
#define MAX_VOLTS 10.0
// Number of samples to collect each second for each channel
//#define SAMPLES_PER_SEC 1000
// The number of samples we want to take for each channel
//#define SAMPLES_PER_CHANNEL 16000
//#define SAMPLES_PER_CHANNEL 30000
// The amount of time to wait to read the samples
#define SAMPLES_WAIT_TIMEOUT_SECS 100
// The number of differential channel pairs we will read from
//#define NUM_CHANNEL_PAIRS 4
#define NUM_CHANNEL_PAIRS 4
// The number of samples we expect to collect
#define ARRAY_SIZE_IN_SAMPLES NUM_CHANNEL_PAIRS*SAMPLES_PER_CHANNEL
// The resistance of the resister that we measure the voltage diff over
#define RESISTOR_OHMS 0.003
// The voltage we assume that all the lines are running at
#define LINE_VOLTAGE 12
int main(int argc, char** argv){
if(argc != 3){
printf("Invalid input argument count. Given %d, Expected %d\n", argc-1, 2);
printf("Usage: ./main [SAMPLES_PER_SECOND] [SAMPLING_TIME_IN_SECONDS]\n");
exit(-1);
}
int SAMPLES_PER_SEC = atoi(argv[1]);
int SAMPLING_SECS = atoi(argv[2]);
int SAMPLES_PER_CHANNEL = SAMPLING_SECS*SAMPLES_PER_SEC;
//-- open input file --
char str[] = "----- Test String -----";
FILE *fp = tmpfile();
if (fp == NULL) {
puts("unable to create fp file");
}
int32 error = 0;
TaskHandle taskHandle = 0;
int32 samples_read_per_channel;
float64 data[ARRAY_SIZE_IN_SAMPLES];
char errBuff[2048]={'\0'};
//fprintf(fp, "DAQ_TASK_NAME = %s\n", DAQ_TASK_NAME);
//fprintf(fp, "taskHandle = %d\n", taskHandle);
//fprintf(fp, "&taskHandle = %d\n", &taskHandle);
//DAQmxErrChk (DAQmxCreateTask(DAQ_TASK_NAME, &taskHandle));
createTask();
//fprintf(fp, "After Task created! ---> taskHandle = %d\n", taskHandle);
//printf("Task created!\n");
// Start in differential mode
createAIVoltageChan();
/*
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle,
PHYS_CHANNELS,
//"cDAQ1Mod3/ai0:3, cDAQ1Mod3/ai8:11",
CHANNEL_NAME,
DAQmx_Val_Diff,
//DAQmx_Val_NRSE,
//DAQmx_Val_RSE,
MIN_VOLTS, MAX_VOLTS,
DAQmx_Val_Volts, NULL));
*/
//fprintf(fp, "After Voltage Chan created! ---> taskHandle = %d\n", taskHandle);
//printf("Voltage Chan created!\n");
// Setup the sample clock and the rate at which we collect samples
/*
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, NULL, SAMPLES_PER_SEC,
DAQmx_Val_Rising,
DAQmx_Val_FiniteSamps,
SAMPLES_PER_CHANNEL ));
*/
setSampleClockAndRate();
//fprintf(fp, "After Setup the sample clock + rate ---> taskHandle = %d\n", taskHandle);
//printf("Sampling rate set!\n");
// DAQmx Start Code
startTask();
//DAQmxErrChk(DAQmxStartTask(taskHandle));
//fprintf(fp, "After DAQmx Start Code ---> taskHandle = %d\n", taskHandle);
//printf("Task started!\n");
// DAQmx Read Code -- i.e: take samples
// The samples are written interleaved with the GroupByScanNumber
takeSamples();
/*
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, SAMPLES_PER_CHANNEL,
SAMPLES_WAIT_TIMEOUT_SECS,
DAQmx_Val_GroupByScanNumber,
//DAQmx_Val_GroupByChannel,
data, ARRAY_SIZE_IN_SAMPLES,
&samples_read_per_channel,
NULL));
*/
// DAQmx Stop and clear task
finalize();
Error:
/*
fprintf(fp, "---> Right after Error:, taskHandle = %d\n", taskHandle);
if( DAQmxFailed(error) ){
DAQmxGetExtendedErrorInfo(errBuff,2048);
}
if( taskHandle != 0 ) {
//printf("NIDAQ testing complete!\n");
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
//printf("We read [%d] samples for each channel\n", samples_read_per_channel);
//printf("We took [%d] samples per second\n", SAMPLES_PER_SEC);
// Print out the data we collected on differences across the paired pins
float64 power;
float64 time;
int i,j;
// Print the header of the csv
//printf("time, ");
fprintf(fp, "time, ");
//---
//---
for(i = 0; i < NUM_CHANNEL_PAIRS-1; i++){
//printf("line%d, ",i);
fprintf(fp, "line%d, ",i);
}
//printf("line%d\n",i);
fprintf(fp, "line%d\n",i);
// Print the data
for(i = 0; i < ARRAY_SIZE_IN_SAMPLES; i+=NUM_CHANNEL_PAIRS){
time = (float)i/(NUM_CHANNEL_PAIRS*SAMPLES_PER_SEC);
//printf("%2.6f, ", time);
fprintf(fp, "%2.6f, ", time);
//printf("Sample %07d: [", i/NUM_CHANNEL_PAIRS);
for(j = 0; j < NUM_CHANNEL_PAIRS-1; j++){
//power = (data[i+j]/RESISTOR_OHMS)*(LINE_VOLTAGE - data[i+j]);
power = (data[i+j]/RESISTOR_OHMS)*LINE_VOLTAGE;
//power = (data[i+j]/RESISTOR_OHMS)*data[i+j];
//power = data[i+j];
//printf("%2.6f, ", power);
fprintf(fp, "%2.6f, ", power);
}
power = (data[i+j]/RESISTOR_OHMS)*LINE_VOLTAGE;
//printf("%2.6f]\n", power);
//printf("%2.6f\n", power);
fprintf(fp, "%2.6f\n", power);
}
}
if( DAQmxFailed(error) ){
//printf("DAQmx Error: %s\n",errBuff);
fprintf(fp, "DAQmx Error: %s\n",errBuff);
//return 0;
}
*/
//rewind() function sets the file pointer at the beginning of the sream
rewind(fp);
while (!feof(fp))
putchar(fgetc(fp));
return 0;
}