#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_mainwindow.h"
#include <QMainWindow>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <QTimer>
#include <vector>
#include <GoSdk/GoSdk.h>
#include <stdlib.h>
#include <memory.h>
#include <stdio.h>
#include <QTimer>
#include <vector>
#include<algorithm>
#include<QLCDNumber>
#include <QDebug>
#define RECEIVE_TIMEOUT (20000000)
#define INVALID_RANGE_16BIT ((signed short)0x8000) // gocator transmits range data as 16-bit signed integers. 0x8000 signifies invalid range data.
#define DOUBLE_MAX ((k64f)1.7976931348623157e+308) // 64-bit double - largest positive value.
#define INVALID_RANGE_DOUBLE ((k64f)-DOUBLE_MAX) // floating point value to represent invalid range data.
#define SENSOR_IP "192.168.1.10"
#define NM_TO_MM(VALUE) (((k64f)(VALUE))/1000000.0)
#define UM_TO_MM(VALUE) (((k64f)(VALUE))/1000.0)
using namespace std;
typedef struct
{
k32u count;
}DataContext;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initial_thisGocator();
kAssembly api = kNULL;
kStatus status;
unsigned int i, j, k;
GoSystem system = kNULL;
GoSensor sensor = kNULL;
GoDataSet dataset = kNULL;
DataContext contextPointer;
bool isGocatorStart = false; //防止多次触发信号量
GoProfilePositionX positionX = kNULL;
GoDataMsg dataObj;
kIpAddress ipAddress;
// data callback function
// 接受传感器数据的回调函数
vector<double> vec01;
private slots:
void Timeout_slot();
void on_Button_startDuantiao_clicked();
void on_Button_cStart_clicked();
void on_Button_cStop_clicked();
void on_Button_stopDuantiao_clicked();
private:
Ui::MainWindow *ui;
QTimer myTimer01;
double pre_Height = 0;
double Kernal_size;
double get_dataaverage(vector<double> & );
double cal_MinValue(vector<double> & v){return *max_element(v.begin(),v.end());}
double cal_MaxValue(vector<double> & v) {return *min_element(v.begin(),v.end());}
vector<double> vec_smoothful(vector<double> & ,int con_kernal);
double cal_Range(vector<double> & );
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QVector>
#include <QDateTime>
#include <vector>
#include<algorithm>
using namespace std;
QDateTime Mytime01;
vector<double> cap_pointz;
QVector<QString> str_01;
int time_cap = 1;
GoStamp *stamp = kNULL;
GoMeasurementData *measurementData = kNULL;
double Point_mylove;
kStatus kCall onData(void* ctx, void* sys, void* dataset)
{
unsigned int i, j, k;
DataContext *context = (DataContext *)ctx;
//printf("onData Callback:\n");
//printf("Data message received:\n");
//printf("Dataset message count: %u\n", GoDataSet_Count(dataset));
for (i = 0; i < GoDataSet_Count(dataset); ++i)
{
GoDataMsg dataObj = GoDataSet_At(dataset, i);
//选择解析传感器的数据包
switch (GoDataMsg_Type(dataObj))
{
case GO_DATA_MESSAGE_TYPE_RANGE:
{
GoRangeMsg rangeMsg = dataObj;
printf("Range Message batch count: %u\n", GoRangeMsg_Count(rangeMsg));
for (k = 0; k < GoRangeMsg_Count(rangeMsg); ++k)
{
short* data = GoRangeMsg_At(rangeMsg, k);
double ZResolution = NM_TO_MM(GoRangeMsg_ZResolution(rangeMsg));
double ZOffset = UM_TO_MM(GoRangeMsg_ZOffset(rangeMsg));
double pointZ;
if (*data != INVALID_RANGE_16BIT)
{
time_cap++;
pointZ = ZOffset + ZResolution * *data;
if (time_cap == 50)
{
//Mytime01 = QDateTime::currentDateTime();
//QString sdt = Mytime01.toString("yyyyMMddhhmmss"); //这里"yyyyMMddhhmmss"月份MM要大写
//str_01.push_back(sdt);
//printf("此时的高度值是 : ");
//int pointZ = 1;
Point_mylove = pointZ;
cap_pointz.push_back(pointZ);
//printf("%f\n", pointZ);
//获取z值,可以将其输出到外面
time_cap = 0;
}
}
else
{
pointZ = INVALID_RANGE_DOUBLE;
}
}
}
break;
//均匀点云
case GO_DATA_MESSAGE_TYPE_STAMP:
{
GoStampMsg stampMsg = dataObj;
printf("Stamp Message batch count: %u\n", GoStampMsg_Count(stampMsg));
for (j = 0; j < GoStampMsg_Count(stampMsg); ++j)
{
stamp = GoStampMsg_At(stampMsg, j);
printf(" Timestamp: %llu\n", stamp->timestamp);
printf(" Encoder: %lld\n", stamp->encoder);
printf(" Frame index: %llu\n", stamp->frameIndex);
}
}
break;
case GO_DATA_MESSAGE_TYPE_MEASUREMENT:
{
GoMeasurementMsg measurementMsg = dataObj;
printf("Measurement Message batch count: %u\n", GoMeasurementMsg_Count(measurementMsg));
for (k = 0; k < GoMeasurementMsg_Count(measurementMsg); ++k)
{
measurementData = GoMeasurementMsg_At(measurementMsg, k);
printf("Measurement ID: %u\n", GoMeasurementMsg_Id(measurementMsg));
printf("Measurement Value: %.1f\n", measurementData->value);
printf("Measurement Decision: %d\n", measurementData->decision);
}
}
break;
}
}
GoDestroy(dataset);
return kOK;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initial_thisGocator();
ui->lcdNumber->setDigitCount(10);
ui->lineEdit_kernal->setText("10");
ui->lcdNumber->setMode(QLCDNumber::Dec);
ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
connect(&myTimer01, &QTimer::timeout,this,&MainWindow::Timeout_slot);
}
MainWindow::~MainWindow()
{
// 删除 句柄
GoDestroy(system);
GoDestroy(api);
delete ui;
}
void MainWindow::Timeout_slot()
{
double Height_mean = get_dataaverage(cap_pointz);
cap_pointz.clear();
QString str_02 = QString::number(Height_mean, 'f', 3);
//QString str_01 = QString::number(cap_pointz[0], 'f', 3);
ui->lcdNumber->display(str_02);
}
void MainWindow::initial_thisGocator()
{
/*
* AcceleratorReceiveMeasurement.c
*
* Gocator 2000 Sample
* Copyright (C) 2011-2018 by LMI Technologies Inc.
*
* Licensed under The MIT License.
* Redistributions of files must retain the above copyright notice.
*
* Purpose: Demonstrates the simple use of the Accelerator by connecting to a sensor and receiving a measurement.
* This allows processing to be performed on the PC rather than on the sensor.
*/
// 创建Gocator API 库
if ((status = GoSdk_Construct(&api)) != kOK)
{
printf("Error: GoSdk_Construct:%d\n", status);
return;
}
// 创建 GoSystem 对象
if ((status = GoSystem_Construct(&system, kNULL)) != kOK)
{
printf("Error: GoSystem_Construct:%d\n", status);
return;
}
// 将IP地址解析为地址数据结构
kIpAddress_Parse(&ipAddress, SENSOR_IP);
// 通过传感器IP地址获取goSensor对象
if ((status = GoSystem_FindSensorByIpAddress(system, &ipAddress, &sensor)) != kOK)
{
printf("Error: GoSystem_FindSensorByIpAddress:%d\n", status);
return;
}
// 创建到GoSensor对象的连接
if ((status = GoSensor_Connect(sensor)) != kOK)
{
printf("Error: GoSensor_Connect:%d\n", status);
return;
}
// 开启传感器数据通道
if ((status = GoSystem_EnableData(system, kTRUE)) != kOK)
{
printf("Error: GoSensor_EnableData:%d\n", status);
return;
}
// set data handler to receive data asynchronously
//注册回调函数
if ((status = GoSystem_SetDataHandler(system, onData, &contextPointer)) != kOK)
{
printf("Error: GoSystem_SetDataHandler:%d\n", status);
return;
}
}
void MainWindow::on_Button_cStart_clicked()
{
if(!isGocatorStart)
{
ui->Button_cStart->setEnabled(false);
ui->Button_cStop->setEnabled(true);
ui->Button_startDuantiao->setEnabled(false);
ui->Button_stopDuantiao->setEnabled(false);
ui->plainTextEdit->clear();
//str_01.clear();
cap_pointz.clear();//清除上一个缓存容器数据
myTimer01.start(100);
// 启动 Gocator 传感器
if ((status = GoSystem_Start(system)) != kOK)
{
qDebug()<<"error: GoSystem_Start";
//printf("Error: GoSystem_Start:%d\n", status);
return;
}
isGocatorStart = true;//此信号来控制gocator以防止多次触发
}
}
void MainWindow::on_Button_cStop_clicked()
{
ui->Button_cStart->setEnabled(true);
ui->Button_cStop->setEnabled(false);
ui->Button_startDuantiao->setEnabled(true);
ui->Button_stopDuantiao->setEnabled(false);
myTimer01.stop();
// 停止 Gocator 传感器
if ((status = GoSystem_Stop(system)) != kOK)
{
qDebug()<<"error: GoSystem_Stop";
//printf("Error: GoSystem_Stop:%d\n", status);
return;
}
isGocatorStart = false;
}
double MainWindow::get_dataaverage(vector<double> & v)
{//计算高度求均值
double temp_sum = 0;
if(v.size()>0)
{
for (vector<double>::iterator it = v.begin(); it != v.end(); ++it)
{
temp_sum += *it;
}
pre_Height = temp_sum / v.size();
}
return pre_Height;
}
vector<double> MainWindow::vec_smoothful(vector<double> & vec_Heght,int con_kernal)
{
vector<double> vec_middel;
for (size_t i = 0; i != vec_Heght.size()-con_kernal-1; i++)
{
vector<double> vec_temp;
for( size_t j = 0; j<con_kernal; j++)
{
double kernal_height = vec_Heght[i+j];
vec_temp.push_back(kernal_height);
}
sort(vec_temp.begin(),vec_temp.end());
vec_middel.push_back(vec_temp[static_cast<int>(ceil(con_kernal/2))]);
}
return vec_middel;
}
double MainWindow::cal_Range(vector<double> & v)
{
return std::abs(cal_MaxValue(v)-cal_MinValue(v));
}
void MainWindow::on_Button_startDuantiao_clicked()
{
if(!isGocatorStart)
{
ui->Button_cStart->setEnabled(false);
ui->Button_cStop->setEnabled(false);
ui->Button_startDuantiao->setEnabled(false);
ui->Button_stopDuantiao->setEnabled(true);
ui->plainTextEdit->clear();
//str_01.clear();
cap_pointz.clear();//清除上一个缓存容器数据
//myTimer01.start(100);
// 启动 Gocator 传感器
Kernal_size = ui->lineEdit_kernal->text().toDouble();
if ((status = GoSystem_Start(system)) != kOK)
{
qDebug()<<"error: GoSystem_Start";
//printf("Error: GoSystem_Start:%d\n", status);
return;
}
isGocatorStart = true;//此信号来控制gocator以防止多次触发
}
}
void MainWindow::on_Button_stopDuantiao_clicked()
{
ui->Button_cStart->setEnabled(true);
ui->Button_cStop->setEnabled(false);
ui->Button_startDuantiao->setEnabled(true);
ui->Button_stopDuantiao->setEnabled(false);
ui->plainTextEdit->clear();
//str_01.clear();
if ((status = GoSystem_Start(system)) != kOK)
{
qDebug()<<"error: GoSystem_Stop";
return;
}
if(cap_pointz.size()>0)
{
vector<double> smoothfulHeight = vec_smoothful(cap_pointz,Kernal_size);
ui->plainTextEdit->appendPlainText(QString("HeightSize of Height is: %1\n").arg(smoothfulHeight.size()));
ui->plainTextEdit->appendPlainText(QString("Range of Height is: %1\n").arg(cal_Range(smoothfulHeight)));
ui->plainTextEdit->appendPlainText(QString("MinValue of Height is: %1\n").arg(cal_MinValue(smoothfulHeight)));
ui->plainTextEdit->appendPlainText(QString("MaxValue of Height is: %1\n").arg(cal_MinValue(smoothfulHeight)));
ui->plainTextEdit->appendPlainText(QString("MeanValue of Height is: %1\n").arg(get_dataaverage(smoothfulHeight)));
}
cap_pointz.clear();//清除上一个缓存容器数据
isGocatorStart = false;//此信号来控制gocator以防止多次触发
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
需要源码的私信我,邮箱留下,有时间会发的