我正在获取电压读数并将其转换为压力读数。这是我第一次使用 C 的真实体验,所以我的代码很混乱,但到目前为止运行良好。我面临的问题是让程序计算介于最小和最大期望值之间的读数(存储在数组 data[i] 中)的数量。
这是代码, Sum2 和 count 是给我带来麻烦的区域。Sum2 是添加 999 个值而不是过滤,并且 count 在应该接近 500 时总是导致 998
编辑:
data[i] 中的读数是电压,我正在承受压力。我的校准曲线是 P=(V-2.9674)/.404
//Voltage readings for NI USB-6009 built from the ground up
#include "stdafx.h"
#include "stdio.h"
#include "NIDAQmx.h"
#include "math.h"
#include "tdmwriter.h"
#include "fundtypes.h"
#include "platdefines.h"
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int main(void)
{
int32 error = 0;
TaskHandle taskHandle = 0;
int32 read;
float64 data[1000], Sum=0, Average, Variance, Deviation=0, std_dev, min=0, max=0, num=0, avg=0, minP=0, maxP=0, avgP=0, avgP2=0, minP2=0, maxP2=0, minV=0, maxV=0, avgV=0, Sum2 = 0;
char errBuff[2048] = { '\0' };
int i, count = 0;
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk(DAQmxCreateTask("Pressure Voltage\n", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", 100.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000));
/*********************************************/
// DAQmx TDMS Configure Code
/*********************************************/
DAQmxErrChk(DAQmxConfigureLogging(taskHandle, "C:\\TestData\\LogFile.tdms", DAQmx_Val_LogAndRead, "ECS Test Data", DAQmx_Val_OpenOrCreate));
//DAQmxWriteAnalogF64(taskHandle,1000, 1 /*autoStart*/, -1 /*timeout*/, DAQmx_Val_GroupByScanNumber, data, 1000, NULL);
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk(DAQmxStartTask(taskHandle));
printf("Voltage due to Pressure:\n");
printf("\n");
printf("Recording Data...\n");
DAQmxErrChk(DAQmxWaitUntilTaskDone(taskHandle, 10.0));
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 1000, &read, NULL));
printf("Acquired %d points\n", (int)read);
/*********************************************/
//Display Values
/*********************************************/
printf("\n");
printf("values:\n");
printf("Voltage 1= %f\n",data[10]);
printf("Voltage 2= %f\n", data[100]);
printf("Voltage 3= %f\n", data[200]);
printf("Voltage 4= %f\n", data[300]);
printf("Voltage 5= %f\n", data[400]);
printf("Voltage 6= %f\n", data[500]);
printf("Voltage 7= %f\n", data[600]);
printf("Voltage 8= %f\n", data[700]);
printf("Voltage 9= %f\n", data[800]);
printf("Voltage 10= %f\n", data[900]);
printf("\n");
//for (int i = 1; i < 1000; i++) {
// printf("Voltage %i= %f\n",i, data[i]);
//}
//printf("\n");
/*********************************************/
//Average Values
/*********************************************/
for (i = 1; i < 999; ++i) {
Sum = Sum + data[i];
}
Average = (Sum / 999);
printf("Average= %f\n", Average);
/*********************************************/
//Standard Deviation
/*********************************************/
for (i = 1; i < 999; ++i) {
Deviation = Deviation + pow((Average - data[i]), 2);
}
Variance = Deviation / 999;
printf("Variance= %f\n", Variance);
std_dev = sqrt(Variance);
printf("Standard Deviation= %f\n", std_dev);
printf("\n");
/*********************************************/
//Min and Max Values
/*********************************************/
{
max = fmax(data[2], data[999]);
}
{
min = fmin(data[2], data[999]);
}
printf("Min: %f\n", min);
printf("Max: %f\n", max);
printf("Log File located in C:\\TestData. Please rename LogFile.tdms after testing\n");
printf("\n");
/*********************************************/
//Convert to Pressure Readings
/*********************************************/
printf("Pressure Readings (inches H2O):\n");
{
minP = (min - 2.9674) / .404;
maxP = (max - 2.9674) / .404;
avgP = (Average - 2.9674) / .404;
}
printf("Min Pressure: %f\n", minP);
printf("Max Pressure: %f\n", maxP);
printf("Average Pressure: %f\n", avgP);
printf("\n");
/*********************************************/
//New Voltage and Pressure Averages
/*********************************************/
{//target min and max pressure
minP2 = avgP - (avgP / 10);
maxP2 = avgP + (avgP / 10);
}
{//target min and max voltage
minV = (minP2*.404) + 2.9674;
maxV = (maxP2*.404) + 2.9674;
}
{//Sum of values in desired range
for (i = 1; i < 999; ++i) {
if (minV < data[i] && data[i] < maxV); { Sum2 = Sum2 + data[i]; }
}
}
{//Number of values in desired range
for (i = 1; i < 999; i++)
{
if (minV < data[i] && data[i] < maxV);
{
count++;
}
}
}
{//New average voltage
avgV = Sum2 / count;
}
{//New average pressure
avgP2 = ((avgV - 2.9674) / .404);
}
printf("Adjusted Values:\n");
printf("Min P2= %f\n", minP2);
printf("Max P2= %f\n", maxP2);
printf("Min Voltage= %f\n", minV);
printf("Max Voltage= %f\n", maxV);
printf("Sum Voltage= %f\n", Sum2);
printf("Count= %d\n", count);
printf("AvgV= %f\n", avgV);
printf("AvgP= %f\n", avgP2);
Error:
if (DAQmxFailed(error))
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0)
{
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
if (DAQmxFailed(error))
printf("DAQmx Error: %s\n", errBuff);
printf("End of program, press Enter key to quit...\n");
getchar();
return 0;
}