0

我有来自传感器的数据,它为我提供了每个时钟周期 2 种火车(小型和大型)的距离。我想记录测量距离的次数,然后将这个值存储在一个数组中,然后对这个数组进行排序,找出有多少大火车和小火车经过。

例如,我将从传感器那里收到火车 1 的值(500,500,500,500,500、502、523、500、500)。因为火车距离传感器 500 毫米,持续 10 个时钟滴答。而对于火车 2(小型火车)(500、500、500),因为火车经过传感器 3 个时钟滴答声。

通过知道我得到了多少传感器脉冲,我可以确定火车的长度。

但是我怎样才能正确排序呢?火车的脉冲不相等

即一列大火车可能有 10 个值或 11 个值。

这是我以下尝试存储滴答计数。哪个不起作用,我无法对此进行排序。

        int i=0;
        int counter=0;
        int train[6];

        if(adc_result > 5)
        {
            counter++;
        }
        train[i] = counter;
        sprintf(result, "%d", train[i]);
        USARTWriteChar(result);
        USARTWriteChar("\r\n");
        i++;
4

1 回答 1

1

这是一个与您的问题一样具体的答案:

#include <windows.h>
#include <ansi_c.h>
#include <stdio.h>
//Assumption: short train is <= 5 clock cycles long  (simulated as value of 30)
//Assumption: long  train is > 10 clock cycles long  (simulated as value of 50)

int randomGenerator(int min, int max);
static uint32_t temper(uint32_t x);
uint32_t lcg64_temper(uint64_t *seed);

int trainType[2]={0,0};; //[0] counts short, [1] counts long

int main(void) 
{

    int i=0, type;

    while(i++ < 1000)
    {
        //simulated train generator
        type = randomGenerator(0,100);//out of a 1000 cycles will hit 30 and 50 a few times.
        //0==no train has occured, do nothing
        //1==short train has occured, increment short train count
        //2==long train  has occured, increment long train count
        switch(type)    {
            case 30://short train
                trainType[0]++;
                break;
            case 50: //long train
                trainType[1]++;
            default: //all other numbers - ignore
                break;
        }
    }   


    return 0;
}

int randomGenerator(int min, int max)
{
    int random=0, trying=0;
    uint64_t lSeed;

    trying = 1;         
    while(trying)
    {

        srand(clock());
        random = (rand()/32767.0)*(max+1);
        ((random >= min)) ? (trying = 0) : (trying = 1);
    }

    return random;
}
于 2013-11-01T22:44:36.117 回答