c++ - Arduino super 队列

标签 c++ arduino android-sensors

我写了这个简单的代码,它从 Sharp 中读取一段长度红外传感器,末尾按串口表示平均米数,单位为cm。

当为 Arduino Mega 开发板编写这段代码时,Arduino 启动一个闪烁的 LED(引脚 13)并且程序什么都不做。此代码中的错误在哪里?

#include <QueueList.h>

const int ANALOG_SHARP = 0; //Set pin data from sharp.
QueueList <float> queuea;
float cm;
float qu1;
float qu2;
float qu3;
float qu4;
float qu5;

void setup() {
    Serial.begin(9600);
}

void loop() {
    cm = read_gp2d12_range(ANALOG_SHARP); //Convert to cm (unit).
    queuea.push(cm); //Add item to queue, when I add only this line Arduino crash.
    if ( 5 <= queuea.peek()) {
        Serial.println(average());
    }
}

float read_gp2d12_range(byte pin) { //Function converting to cm (unit).
    int tmp;

    tmp = analogRead(pin);
    if (tmp < 3)
        return -1; // Invalid value.

    return (6787.0 /((float)tmp - 3.0)) - 4.0;
}

float average() { //Calculate average length
    qu1 += queuea.pop();
    qu2 += queuea.pop();
    qu3 += queuea.pop();
    qu4 += queuea.pop();
    qu5 += queuea.pop();

    float aver = ((qu1+qu2+qu3+qu4+qu5)/5);
    return aver;
}

最佳答案

我同意 vhallac 列出的 peek() -> count() 错误。但我还要指出,除非有充分的理由不这样做,否则您应该考虑按 2 的幂取平均值。

原因是在微 Controller 上,除法很慢。通过对 2 的幂(2、4、8、16 等)进行平均,您可以简单地计算总和,然后对其进行位移。

计算 2 的平均值:(v1 + v2) >> 1

计算 4 的平均值:(v1 + v2 + v3 + v4) >> 2

要计算 n 个值的平均值(其中 n 是 2 的幂),只需将总和右移 [log2(n)]。

只要您的 sum 变量的数据类型足够大并且不会溢出,这就更容易也更快。

注意:这通常不适用于 float 。事实上,微 Controller 并未针对 float 进行优化。您应该考虑在末尾平均后而不是之前从 int(我假设您正在读取 ADC 正在读取)转换为 float。

通过从 int 转换为 float 然后对 float 进行平均,与将 int 转换为 float 相比,对 int 进行平均会损失更多的精度。

其他:

您正在使用 += 运算符而没有初始化变量(qu1qu2 等)——这是一种很好的做法如果您要使用 +=,请初始化它们,但看起来 = 可以正常工作。

对于 float ,我会将average函数写成:

float average(QueueList<float> & q, int n)
{
    float sum = 0;
    for(int i=0; i<n; i++)
    {
        sum += q.pop();
    }

    return (sum / (float) n);
}

并调用它:average(queuea, 5);

您可以使用它来对任意数量的传感器读数进行平均,然后使用相同的代码对完全不同的 QueueList 中的 float 进行平均。将读数的数量作为参数传递给平均值,在您需要调整它的情况下真的会派上用场。

长话短说:

这是我的做法:

#include <QueueList.h>

const int ANALOG_SHARP=0;   // set pin data from sharp
const int AvgPower = 2;     // 1 for 2 readings, 2 for 4 readings, 3 for 8, etc.
const int AvgCount = pow(2,AvgPow);

QueueList <int> SensorReadings;


void setup(){
    Serial.begin(9600);
}

void loop()
{
    int reading = analogRead(ANALOG_SHARP);
    SensorReadings.push(reading);

    if(SensorReadings.count() > AvgCount)
    {
        int avg = average2(SensorReadings, AvgPower);
        Serial.println(gpd12_to_cm(avg));
    }
}

float gp2d12_to_cm(int reading)
{
    if(reading <= 3){ return -1; }

    return((6787.0 /((float)reading - 3.0)) - 4.0);
}

int average2(QueueList<int> & q, int AvgPower)
{
    int AvgCount = pow(2, AvgPower);
    long sum = 0;
    for(int i=0; i<AvgCount; i++)
    {
        sum += q.pop();
    }

    return (sum >> AvgPower);
}

关于c++ - Arduino super 队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7295234/

相关文章:

c++ - 如何获取参数包中元素的索引

c++ - 从具有命名构造函数问题的基类继承

c++ - 如何使用 Boost.Asio 正确实现 async/await 语法

android - 通过 com 端口通过 USB 将 arduino 连接到 ANDROID 手机

c - 从 Arduino 中的 PCA 9554 (i2c) 读取

java - 可以从具有多个功能的 EventListener 中只实现一个功能吗?

c++ - 如何使用 binder 和 bind2nd 仿函数?

c++ - arduino到树莓派串行通信

android - onAccuracyChanged,为什么?

安卓加速度计全系列