带距离传感器的 Arduino IDE 中的移动平均值

发布于 2025-01-17 06:08:53 字数 2662 浏览 4 评论 0原文

我正在回顾之前与我的主题相关的问题,我发现了这个: Movingaverage in C++ with arduino

我遇到的问题非常相似,但我想从 vl6180x adafruit 距离传感器中获取每 150 秒的距离平均值。

平均前 150 秒,打印结果,然后从第二个 151 到 300 打印结果,并让它继续平均我的代码。

我尝试做与我发布的问题链接中相同的操作,但无法编译代码(我正在从 Arduino IDE 进行编程,而且我是初学者)。我的目标是通过移动平均值减少信号噪声。我的代码:

#include <Wire.h>
#include "Adafruit_VL6180X.h"
#define BUF_SIZE 15

Adafruit_VL6180X vl = Adafruit_VL6180X();
float array_calculate_avg(int * buf, int len);

int   buf[BUF_SIZE]   = {0};
int   buf_index       = 0;
float buf_avg         = 0.0;
int len


void setup() {
  Serial.begin(115200);

  // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");
}

void loop() {

   // Reset the index and start over.
    //
    if (BUF_SIZE == buf_index)
    {
        buf_index = 0;
    }

    buf[buf_index++] = vl.readRange();

    buf_avg = array_calculate_avg(buf, BUF_SIZE);

    Serial.print(buf_avg, 4);

    delay(2000);
    
  float lux = vl.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux: "); Serial.println(lux);
  
  uint8_t  range = vl.readRange();
  uint8_t  status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
  }

  // Some error occurred, print it out!
  
  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  delay(50);

 float
array_calculate_avg(int * buf, int len);
{
    int sum = 0;

    for (int i = 0; i < len; ++i)
    {
        sum += buf[i];
    }

    return ((float) sum) / ((float) len);
}   

我的母语不是英语。如果我有严重的语法错误,请道歉:/

I was reviewing previous questions related to my topic, I found this: Moving average in C++ with arduino

The problem I have is very similar, but I want to relize distance averages every 150 seconds from a vl6180x adafruit distance sensor.

Average the first 150 seconds, print the result, then from the second 151 to 300 print the result and have it continue averaging my code.

I tried to do the same thing that is in the link I posted with my problem and I could not compile the code (I am programming from the Arduino IDE, and I am a beginner). My goal is to reduce the signal noise with moving average. My code:

#include <Wire.h>
#include "Adafruit_VL6180X.h"
#define BUF_SIZE 15

Adafruit_VL6180X vl = Adafruit_VL6180X();
float array_calculate_avg(int * buf, int len);

int   buf[BUF_SIZE]   = {0};
int   buf_index       = 0;
float buf_avg         = 0.0;
int len


void setup() {
  Serial.begin(115200);

  // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");
}

void loop() {

   // Reset the index and start over.
    //
    if (BUF_SIZE == buf_index)
    {
        buf_index = 0;
    }

    buf[buf_index++] = vl.readRange();

    buf_avg = array_calculate_avg(buf, BUF_SIZE);

    Serial.print(buf_avg, 4);

    delay(2000);
    
  float lux = vl.readLux(VL6180X_ALS_GAIN_5);

  Serial.print("Lux: "); Serial.println(lux);
  
  uint8_t  range = vl.readRange();
  uint8_t  status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
  }

  // Some error occurred, print it out!
  
  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  delay(50);

 float
array_calculate_avg(int * buf, int len);
{
    int sum = 0;

    for (int i = 0; i < len; ++i)
    {
        sum += buf[i];
    }

    return ((float) sum) / ((float) len);
}   

My native language is not English. Apologies if I have gross grammatical errors :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相对绾红妆 2025-01-24 06:08:53

您的 loop() 函数在 float array_calculate_avg(int * buf, int len); 行之前缺少右大括号 } ,顺便说一句,末尾还有一个分号 ; ,但实际上不应该如此。

Your loop() function is missing a closing brace } right before the float array_calculate_avg(int * buf, int len); line which, btw, also has a semicolon ; at the end, which it shouldn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文