当平均值应始终在 0 和 1 之间时,从圆形数组计算的运行平均值产生常数 < 0 平均值。
我正在为 MSP430 设备编写固件,该设备使用 LED 和光电二极管来检测墨水上的特定类型。该设备以大约 155us 的速度扫描,扫描仪下的样本速度范围为 0.1m/s 至 3.3m/s。该设备的目标是测试墨水并测量墨水(通过)与测试(未通过)的比率,并在比率介于相应值之间时打开绿色 LED,而不是在相应值之间打开红色 LED。我正在使用静态整数数组将连续通过的值和测试值存储到每个数组的相同索引号。在数组的最后一个索引之后,索引被设置回零并覆盖旧值。
绿色_LED_ON;和类似的定义是我的 MCU 的端口定义,并且经过验证是正确的。
event 是测试结果。如果检测到墨水,则 event=DETECTED,反之亦然
test 将是 GUI 设置的平均值,但现在它什么都不是,因为我的这部分功能没有工作
通常我会通过随附的 GUI 设置变量平均值,但出于测试目的,我设置平均值 <0 只是为了找出 LED 在错误时间亮起的原因,我发现平均值 <0。平均值应始终为 0=
笔记:
- 我尝试使用 LED 输出检查代码的各个部分是否正常工作。我注释掉了控制 LED 的平均部分,并通过打开和关闭 LED 验证了 event== 部分是否有效。然后我尝试将该代码放入 flag== 部分,并且 LED 也按预期与该部分相对应。
- 我发现数组值有问题。一旦
n > size
它只测试一次,而不是等待下一个标志更改。我不能有 n>size 因为在我的代码中再往下,n++
将等于 7,这是超出范围的
我添加了一段代码以确保不会发生翻转。选择值 8,000 和 30,000 以匹配可能的最慢运行速度。
我还更改了数组索引递增的位置,并确保它在数组的范围内显示。
这是更新的功能:
void display(char event, char test) {
static int size=5;
static int array[6]={0}; //array with number of passes for each n
static int n=0;
static float sum=0;//total number of passes
static float average=0;//average pass rate over n
static int consecpass=0; //consecutive passes
static int consecfail=0; //consecutive fails
static int totalnumberoftests[6]={0}; //total number of tests conducted. Counts the number of passing or failing tests for the nth value
static float counter=1; //used to count the total number of tests
static int flag=0;
if (event == DETECTED)
{
if (flag==0)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=0;
totalnumberoftests[n]=consecfail;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
flag=1;
consecpass++;
n++;
if(n>=size)n=0;
//GREEN_LED_ON;
//RED_LED_OFF;
}else{
consecfail=0;
consecpass++;
//GREEN_LED_ON;
//RED_LED_OFF;
}
} if (event==NOT_DETECTED){
if(flag==1)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=consecpass;
totalnumberoftests[n]=consecpass;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
flag=0;
consecfail++;
n++;
if(n>=size)n=0;
//RED_LED_ON;
//GREEN_LED_OFF;
}else{
consecpass=0;
consecfail++;
//RED_LED_ON;
//GREEN_LED_OFF;
}
}
if (consecpass>8000)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=consecpass;
totalnumberoftests[n]=consecpass;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
consecpass=0;
n++;
if(n>=size)n=0;
}
if(consecfail>30000)
{
sum=sum-array[n];
counter=counter-totalnumberoftests[n];
array[n]=0;
totalnumberoftests[n]=consecfail;
sum=sum+array[n];
counter=counter+totalnumberoftests[n];
consecfail=0;
n++;
if(n>=size)n=0;
}
average=sum/counter;
if(average<.6 && average > .1)
{
GREEN_LED_ON;
RED_LED_OFF;
}else{
GREEN_LED_OFF;
RED_LED_ON;
}
}
if (n >= size)
声明在标志语句之后以避免我的数组的最终值为 1。这是更改(它在两个 if(flag==) 语句上:
if (flag == 1) {
sum = sum - array[n];
counter = counter - totalnumberoftests[n];
array[n] = consecpass;
totalnumberoftests[n] = consecpass;
sum = sum + array[n];
counter = counter + totalnumberoftests[n];
flag = 0;
consecfail++;
n++;
if (n >= size)
n = 0;
这是原始代码:
void display(char event, char test) {
static int size = 6;
static int array[6] = { 0 }; //array with number of passes for each n
static int n = 0;
static float sum = 0;//total number of passes
static float average = 0;//average pass rate over n
static int consecpass = 0; //consecutive passes
static int consecfail = 0; //consecutive fails
static int totalnumberoftests[6] = { 0 }; //total number of tests conducted. Counts the number of passing or failing tests for the nth value
static float counter = 1; //used to count the total number of tests
static int flag = 0;
if (n >= size) {
n = 0;
}
if (event == DETECTED) {
if (flag == 0) {
n++;
sum = sum - array[n];
counter = counter - totalnumberoftests[n];
array[n] = 0;
totalnumberoftests[n] = consecfail;
sum = sum + array[n];
counter = counter + totalnumberoftests[n];
flag = 1;
consecpass++;
} else {
consecfail = 0;
consecpass++;
}
}
if (event == NOT_DETECTED) {
if (flag == 1) {
n++;
sum = sum - array[n];
counter = counter - totalnumberoftests[n];
array[n] = consecpass;
totalnumberoftests[n] = consecpass;
sum = sum + array[n];
counter = counter + totalnumberoftests[n];
flag = 0;
consecfail++;
} else {
consecpass = 0;
consecfail++;
}
}
average = sum / counter;
if (average < 0) {
GREEN_LED_ON;
RED_LED_OFF;
} else {
GREEN_LED_OFF;
RED_LED_ON;
}
}