-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaqutils.cpp
More file actions
199 lines (182 loc) · 6.34 KB
/
Copy pathdaqutils.cpp
File metadata and controls
199 lines (182 loc) · 6.34 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
#include "daqutils.h"
TaskHandle DAQHandle;
int createDAQTask(){
int status = DAQmxCreateTask("", &DAQHandle);
return status;
}
int clearDAQTask() {
int status = DAQmxClearTask(DAQHandle);
return status;
}
int startDAQTask(){
int status = DAQmxStartTask(DAQHandle);
return status;
}
int stopDAQTask() {
int status = DAQmxStopTask(DAQHandle);
return status;
}
//int initDAQAOVoltageChan(){
// int status = DAQmxCreateAOVoltageChan(DAQHandle, "Dev1/ao0", "",
// -10, 10, DAQmx_Val_Volts, "");
// std::cout << "Creating Analog Out : " << status << std::endl;
// return status;
//}
int initDAQAIChan(const char deviceName[]) {
int32 terminalConfig = DAQmx_Val_Diff;
int status_0 = DAQmxCreateAIVoltageChan(DAQHandle, deviceName, "", terminalConfig, -5, 5, DAQmx_Val_Volts, NULL);
return status_0;
}
//int setVoltages(double voltage){
// VoltageData[0] = voltage;
// int status = DAQmxWriteAnalogF64(DAQHandle, 1, 1, 0.0, DAQmx_Val_GroupByScanNumber, VoltageData, NULL, NULL);
// std::cout << "Applying voltage for X : " << status << std::endl;
// return status;
//}
int getVoltage(float64 *readArray, int32 *samplesReadPerChannel, int numberOfSamplesPerChannel, int readArraySize){
int status = DAQmxReadAnalogF64(DAQHandle, numberOfSamplesPerChannel, 1, DAQmx_Val_GroupByScanNumber, readArray, readArraySize, samplesReadPerChannel, NULL);
return status;
}
int monitorChannel(const char deviceName[], int numOfChannels) {
std::string totalTimeInSecondsString, stepInMiliSecondsString, numberOfSamplesString;
std::sprintf(logString, "How many seconds data to collect:");
log(logString, PROMPT);
getline (std::cin, totalTimeInSecondsString);
int totalTimeInSeconds = atoi(totalTimeInSecondsString.c_str());
std::sprintf(logString, "Enter the interval between each step in miliseconds:");
log(logString, PROMPT);
getline (std::cin, stepInMiliSecondsString);
int stepInMiliSeconds = atoi(stepInMiliSecondsString.c_str());
int statusDaQTask = createDAQTask();
if (statusDaQTask != 0) {
std::sprintf(logString, "Failed to Create DaQ Task");
log(logString, ERROR);
return -1;
}
int statusInitDaQChain = initDAQAIChan(deviceName);
if (statusInitDaQChain != 0) {
std::sprintf(logString, "Failed to init DaQ chain");
log(logString, ERROR);
clearDAQTask();
return -2;
}
int statusStartDaQTask = startDAQTask();
if (statusStartDaQTask != 0) {
std::sprintf(logString, "Failed to Start DaQ Task.");
log(logString, ERROR);
clearDAQTask();
return -3;
}
std::sprintf(logString, "Enter number of samples to take:");
log(logString, PROMPT);
getline (std::cin, numberOfSamplesString);
int numberOfSamples = atoi(numberOfSamplesString.c_str());
int elapsedTime = 0;
float64 *readArray = NULL;
int32 samplesReadPerChannel = 0;
while(elapsedTime < totalTimeInSeconds*1000) {
readArray = (float64*)calloc( numberOfSamples * numOfChannels, sizeof(float64));
int statusReadVoltage = getVoltage(readArray, &samplesReadPerChannel, numberOfSamples, numberOfSamples * numOfChannels);
if (statusReadVoltage != 0) {
std::sprintf(logString, "Failed to get Voltage.");
log(logString, ERROR);
stopDAQTask();
clearDAQTask();
free(readArray);
return -7;
}
char data[100000];
char data_point[100];
_itoa_s(elapsedTime, data, 10);
for (int i=0; i < samplesReadPerChannel * numOfChannels; i++) {
std::sprintf(data_point, " %.2f", readArray[i] * 1000);
std::strcat(data, data_point);
}
log(data, INFO);
Sleep(stepInMiliSeconds);
elapsedTime += stepInMiliSeconds;
free(readArray);
}
std::sprintf(logString, "Data Recorded Successfully.");
log(logString, PROMPT);
stopDAQTask();
clearDAQTask();
return 0;
}
int testChannel(const char *deviceName, int numOfChannels) {
int64 readArraySize;
int status;
status = createDAQTask();
if (status != 0) {
return -1;
}
status = initDAQAIChan(deviceName);
if (status != 0) {
clearDAQTask();
return -2;
}
status = startDAQTask();
if (status != 0) {
return -3;
}
std::string input;
int numOfVoltageSamples;
int reading;
int i;
char *token = NULL;
while (1) {
std::sprintf(logString, "Enter e for exit or m to switch mode or Number of sample Voltages to get with the reading: ");
log(logString, PROMPT);
getline (std::cin, input);
if (input[0] == 'e' || input[0] == 'E') {
stopDAQTask();
clearDAQTask();
exit(0);
}
if (input[0] == 'm' || input[0] == 'M') {
break;
}
token = std::strtok(const_cast<char *>(input.c_str()), " ");
i = 0;
reading = 0;
while (token != NULL && i < 2) {
switch (i) {
case 0:
numOfVoltageSamples = atoi(token);
break;
case 1:
reading = atoi(token);
break;
default:
break;
}
token = std::strtok(NULL, " ");
i += 1;
}
readArraySize = numOfVoltageSamples * numOfChannels;
float64 *readArray = NULL;
readArray = (float64*)calloc( readArraySize, sizeof(float64));
int32 samplesReadPerChannel = 0;
int status = getVoltage(readArray, &samplesReadPerChannel, numOfVoltageSamples, readArraySize);
if (status == 0) {
std::sprintf(logString, "Number of samples per channel: %d", samplesReadPerChannel);
log(logString, INFO);
std::sprintf(logString, "Reading: %d", reading);
log(logString, INFO);
for (int i=0; i < readArraySize; i++) {
std::sprintf(logString, "Voltage = %.2f mVolts", readArray[i] * 1000);
log(logString, INFO);
}
}
free(readArray);
}
status = stopDAQTask();
if (status != 0) {
return -4;
}
status = clearDAQTask();
if (status != 0) {
return -5;
}
return 0;
}