0

我正在使用 Qt GUI 来跟踪传感器的运动。mainwindow.cpp 文件是:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ATC3DG.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include "QTimer"
#include "qtimer.h"
#include "math.h"

double square(double x)
{
    return x*x;
}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_start_clicked()
{
    points.clear();   //points is a global std::vector<cv::Point3> declared in mainwindow.h
    errorCode = InitializeBIRDSystem();
    errorCode = GetBIRDSystemConfiguration(&ATC3DG.m_config);
    id = 0;
    errorCode = SetSystemParameter(SELECT_TRANSMITTER, &id, sizeof(id));
    EM_time = new QTimer(this);
    connect(EM_time, SIGNAL(timeout()), this, SLOT(showValues()));
    EM_time->start();
}

void MainWindow::showValues()
{
    EM_time->stop();
    pRecord = &record;
    {
        sensorID = 0;
        {
            errorCode = GetAsynchronousRecord(sensorID, pRecord, sizeof(record));
            unsigned int status = GetSensorStatus(sensorID);
            if ( status == VALID_STATUS )
            {
                points.push_back(cv::Point3f(record.x, record.y, record.z));
                QString str;
                str.sprintf("%f, %f, %f",record.x, record.y, record.z );
                this->ui->label->setText(str);
            }
        }
    }
    EM_time->start();
}

void MainWindow::on_stop_clicked()
{
    EM_time->stop();
    double sum = 0;
    double dist;
    QString str;

    for (int i=0; i<points.size()-1; i++)
    {
       dist = sqrt(square(points[i].x - points[i+1].x) + square(points[i].y - points[i+1].y) + square(points[i].z - points[i+1].z));
       sum = sum+dist;
    }
    str.sprintf("%d cm", sum*2.54);
    this->ui->distance->setText(str);
}

ATC3DG.h 是传感器的头文件。record.x、record.y、record.z 以英寸为单位给出传感器的 x、y 和 z 位置的 3D 位置。基本上我正在做的是,当我单击开始按钮时,传感器会打开,QTimer 会从超时期间发出的信号开始,并且showvalues()函数将开始执行。该函数在 Qt GUI的标签中显示传感器的位置。在这个循环中,点将被传感器的所有位置值填充。

停止按钮停止计时器并使用向量中包含的所有点计算距离。这是使用以下方法完成的:

double sum=0;
double dist;
for (int i=0; i<points.size()-1; i++)
    {
       dist = sqrt(square(points[i].x - points[i+1].x) + square((int)points[i].y - (int)points[i+1].y) + square(points[i].z - points[i+1].z));
       sum = sum+dist;
    }

总和给了我完全奇怪的价值观。例如,当传感器仅移动了大约 5 或 6 英寸时,它显示的值在 100 秒等范围内。

我的 mainwindow.h 文件是:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ATC3DG.h"
#include "QTimer"
#include <opencv2/core/core.hpp>

namespace Ui {
class MainWindow;
}

class CSystem
{
public:
    SYSTEM_CONFIGURATION m_config;
};
class CSensor
{
public: SENSOR_CONFIGURATION m_config;
};
class CXmtr
{
public: TRANSMITTER_CONFIGURATION m_config;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


public slots:
    void on_start_clicked();
    void showValues();
    void on_stop_clicked();

private:
    Ui::MainWindow *ui;
private:
    DOUBLE_POSITION_ANGLES_RECORD record, *pRecord;
    CSystem ATC3DG;
    CSensor *pSensor;
    CXmtr *pXmtr;
    int errorCode;
    int sensorID;
    int i;
    short id;
    QTimer *EM_time;
    std::vector<cv::Point3f> points;
};

#endif // MAINWINDOW_H
4

1 回答 1

1

我可以在您的代码中看到的问题:

  1. 过度使用大括号(它们什么都不做)——这看起来很奇怪,可能会导致错误
  2. GetAsynchronousRecord建议异步操作,您立即使用价值!我不知道这个库,但这看起来很可疑。
  3. 以相同的方法启动和停止计时器。
  4. 您正在计算可能非常嘈杂的数据的距离总和。这意味着当您在一段时间内不移动传感器时,您正在计算噪声总和,因此当传感器根本不移动时,您的距离很大。您必须在计算这样的距离之前过滤数据(最简单的是低通滤波器)。
于 2014-10-08T15:50:23.113 回答