309 lines
10 KiB
C++
309 lines
10 KiB
C++
#include "PathPlan.h"
|
||
#include <iostream>
|
||
#include <QMessageBox>
|
||
#include <QFileDialog>
|
||
#include <fileOperation.h>
|
||
|
||
PathPlan::PathPlan(VinceControl* xMotor, VinceControl* yMotor, QMotorDoubleSlider* xSlider, QMotorDoubleSlider* ySlider, QWidget* parent)
|
||
: QDialog(parent)
|
||
{
|
||
ui.setupUi(this);
|
||
|
||
m_xMotor = xMotor;
|
||
m_yMotor = yMotor;
|
||
|
||
m_xSlider = xSlider;
|
||
m_ySlider = ySlider;
|
||
|
||
ui.recordLine_tableWidget->setFocusPolicy(Qt::NoFocus);
|
||
ui.recordLine_tableWidget->setStyleSheet("selection-background-color:rgb(255,209,128)");//设置选择的行高亮
|
||
|
||
ui.recordLine_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选择行为,以行为单位
|
||
//ui.recordLine_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//设置选择模式,选择单行
|
||
//QHeaderView* headerView = ui.recordLine_tableWidget->verticalHeader();
|
||
//headerView->setHidden(true);//去除左边默认自带序列号
|
||
|
||
|
||
ui.recordLine_tableWidget->setColumnCount(2);
|
||
ui.recordLine_tableWidget->setHorizontalHeaderLabels(QStringList() << "yPosition" << "xMaxPosition");
|
||
|
||
connect(ui.addRecordLine_btn, SIGNAL(clicked()), this, SLOT(onAddRecordLine_btn()));
|
||
connect(ui.removeRecordLine_btn, SIGNAL(clicked()), this, SLOT(onRemoveRecordLine_btn()));
|
||
connect(ui.generateRecordLine_btn, SIGNAL(clicked()), this, SLOT(onGenerateRecordLine_btn()));
|
||
connect(ui.deleteRecordLine_btn, SIGNAL(clicked()), this, SLOT(onDeleteRecordLine_btn()));
|
||
connect(ui.saveRecordLine2File_btn, SIGNAL(clicked()), this, SLOT(onSaveRecordLine2File_btn()));
|
||
connect(ui.readRecordLineFile_btn, SIGNAL(clicked()), this, SLOT(onReadRecordLineFile_btn()));
|
||
}
|
||
|
||
PathPlan::~PathPlan()
|
||
{}
|
||
|
||
void PathPlan::setMotor(VinceControl* xMotor, VinceControl* yMotor)
|
||
{
|
||
m_xMotor = xMotor;
|
||
m_yMotor = yMotor;
|
||
}
|
||
|
||
QTableWidget* PathPlan::getRecordLineTableWidget()
|
||
{
|
||
return ui.recordLine_tableWidget;
|
||
}
|
||
|
||
void PathPlan::onAddRecordLine_btn()
|
||
{
|
||
//准备数据
|
||
ByteBack MotorState = m_yMotor->GetState();
|
||
double currentPosOfYmotor = m_ySlider->getDistanceFromPulse(MotorState.Location);
|
||
double maxRangeOfXmotro = m_xSlider->maximum();
|
||
|
||
//获取选中行的索引
|
||
int currentRow = ui.recordLine_tableWidget->currentRow();
|
||
std::cout << "currentRow:" << currentRow << std::endl;
|
||
|
||
QTableWidgetItem* Item1 = new QTableWidgetItem(QString::number(currentPosOfYmotor, 10, 2));
|
||
QTableWidgetItem* Item2 = new QTableWidgetItem(QString::number(maxRangeOfXmotro, 10, 2));
|
||
Item1->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||
Item2->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||
if (currentRow == -1)//当没有选中行时
|
||
{
|
||
int RowCount = ui.recordLine_tableWidget->rowCount();//Returns the number of rows. 从1开始的
|
||
ui.recordLine_tableWidget->insertRow(RowCount);//增加一行,形参是从0开始的
|
||
|
||
ui.recordLine_tableWidget->setItem(RowCount, 0, Item1);
|
||
ui.recordLine_tableWidget->setItem(RowCount, 1, Item2);
|
||
}
|
||
else
|
||
{
|
||
ui.recordLine_tableWidget->insertRow(currentRow + 1);//增加一行,形参是从0开始的
|
||
|
||
ui.recordLine_tableWidget->setItem(currentRow + 1, 0, Item1);
|
||
ui.recordLine_tableWidget->setItem(currentRow + 1, 1, Item2);
|
||
}
|
||
}
|
||
|
||
void PathPlan::onRemoveRecordLine_btn()
|
||
{
|
||
int rowIndex = ui.recordLine_tableWidget->currentRow();
|
||
if (rowIndex != -1)
|
||
ui.recordLine_tableWidget->removeRow(rowIndex);
|
||
}
|
||
|
||
void PathPlan::onGenerateRecordLine_btn()
|
||
{
|
||
//求幅宽
|
||
double height = ui.height_lineEdit->text().toDouble();
|
||
double fov = ui.fov_lineEdit->text().toDouble();
|
||
double swath = (height * tan(fov / 2 * PI / 180)) * 2;//tan输入是弧度
|
||
ui.swath_lineEdit->setText(QString::number(swath));
|
||
|
||
|
||
//读取马达测量范围
|
||
double xMotorRange = m_xSlider->maximum();
|
||
double yMotorRange = m_ySlider->maximum();
|
||
|
||
|
||
//确定有多少条采集线,公式:numberOfRecordLine_tmp * swath - repetitiveLength(numberOfRecordLine_tmp - 1) = overallLength
|
||
double overallLength = yMotorRange + swath;
|
||
double repetitiveRate = ui.repetitiveRate_lineEdit->text().toDouble() / 100;
|
||
double repetitiveLength = repetitiveRate * swath;
|
||
double offset = ui.offset_lineEdit->text().toDouble();
|
||
|
||
double numberOfRecordLine_tmp = (overallLength - repetitiveLength - offset) / (swath - repetitiveLength);
|
||
double tmp = numberOfRecordLine_tmp - (int)numberOfRecordLine_tmp;
|
||
int numberOfRecordLine;
|
||
double threshold = ui.LastLineThreshold_lineEdit->text().toDouble();//当numberOfRecordLine_tmp为小数时,判断是否多加一条采集线
|
||
if (tmp > threshold)
|
||
{
|
||
numberOfRecordLine = (int)numberOfRecordLine_tmp + 1;
|
||
//std::cout << "大于:" << threshold << std::endl;
|
||
}
|
||
else
|
||
{
|
||
numberOfRecordLine = (int)numberOfRecordLine_tmp;
|
||
}
|
||
|
||
|
||
|
||
//去掉tableWidget中所有的行
|
||
int rowCount = ui.recordLine_tableWidget->rowCount();
|
||
for (size_t i = 0; i < rowCount; i++)
|
||
{
|
||
ui.recordLine_tableWidget->removeRow(0);
|
||
}
|
||
|
||
|
||
//向tableWidget添加行(采集线)
|
||
QTableWidgetItem* tmpItem;
|
||
for (size_t i = 0; i < numberOfRecordLine; i++)
|
||
{
|
||
//增加一行
|
||
int RowCount = ui.recordLine_tableWidget->rowCount();
|
||
ui.recordLine_tableWidget->insertRow(RowCount);
|
||
|
||
//设置yPosition
|
||
if (tmp > threshold && i == numberOfRecordLine - 1)//设置最后一行的yPosition
|
||
{
|
||
tmpItem = new QTableWidgetItem(QString::number(yMotorRange, 10, 2));
|
||
tmpItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||
ui.recordLine_tableWidget->setItem(i, 0, tmpItem);
|
||
}
|
||
else
|
||
{
|
||
double x = swath * i - i * repetitiveLength + offset;
|
||
tmpItem = new QTableWidgetItem(QString::number(x, 10, 2));
|
||
tmpItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||
ui.recordLine_tableWidget->setItem(i, 0, tmpItem);
|
||
}
|
||
|
||
//设置x马达最大运动位置 → 值设置为x马达量程
|
||
tmpItem = new QTableWidgetItem(QString::number(xMotorRange, 10, 2));
|
||
tmpItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||
ui.recordLine_tableWidget->setItem(i, 1, tmpItem);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
void PathPlan::onDeleteRecordLine_btn()
|
||
{
|
||
int rowCount = ui.recordLine_tableWidget->rowCount();
|
||
for (size_t i = 0; i < rowCount; i++)
|
||
{
|
||
ui.recordLine_tableWidget->removeRow(0);
|
||
}
|
||
}
|
||
|
||
void PathPlan::onSaveRecordLine2File_btn()
|
||
{
|
||
//确保采集线存在
|
||
if (ui.recordLine_tableWidget->rowCount() <= 0)
|
||
{
|
||
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("请先生成轨迹!"));
|
||
return;
|
||
}
|
||
|
||
double height = ui.height_lineEdit->text().toDouble();
|
||
double fov = ui.fov_lineEdit->text().toDouble();
|
||
double swath = ui.swath_lineEdit->text().toDouble();
|
||
double offset = ui.offset_lineEdit->text().toDouble();
|
||
double repetitiveRate = ui.repetitiveRate_lineEdit->text().toDouble();
|
||
double LastLineThreshold = ui.LastLineThreshold_lineEdit->text().toDouble();
|
||
|
||
FileOperation* fileOperation = new FileOperation();
|
||
string directory = fileOperation->getDirectoryOfExe();
|
||
|
||
QString RecordLineFilePath = QFileDialog::getSaveFileName(this, tr("Save RecordLine File"),
|
||
QString::fromStdString(directory),
|
||
tr("RecordLineFile (*.RecordLine)"));
|
||
|
||
if (RecordLineFilePath.isEmpty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
FILE* RecordLineFileHandle = fopen(RecordLineFilePath.toStdString().c_str(), "wb+");
|
||
|
||
fwrite(&height, sizeof(double), 1, RecordLineFileHandle);
|
||
fwrite(&fov, sizeof(double), 1, RecordLineFileHandle);
|
||
fwrite(&swath, sizeof(double), 1, RecordLineFileHandle);
|
||
fwrite(&offset, sizeof(double), 1, RecordLineFileHandle);
|
||
fwrite(&repetitiveRate, sizeof(double), 1, RecordLineFileHandle);
|
||
fwrite(&LastLineThreshold, sizeof(double), 1, RecordLineFileHandle);
|
||
|
||
double number = ui.recordLine_tableWidget->rowCount() * ui.recordLine_tableWidget->columnCount();
|
||
fwrite(&number, sizeof(double), 1, RecordLineFileHandle);
|
||
|
||
double* data = new double[number];
|
||
//double data[number];
|
||
for (size_t i = 0; i < ui.recordLine_tableWidget->rowCount(); i++)
|
||
{
|
||
for (size_t j = 0; j < ui.recordLine_tableWidget->columnCount(); j++)
|
||
{
|
||
data[i * ui.recordLine_tableWidget->columnCount() + j] = ui.recordLine_tableWidget->item(i, j)->text().toDouble();
|
||
}
|
||
}
|
||
|
||
fwrite(data, sizeof(double), number, RecordLineFileHandle);
|
||
|
||
fclose(RecordLineFileHandle);
|
||
delete[] data;
|
||
|
||
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("保存成功!"));
|
||
}
|
||
|
||
void PathPlan::onReadRecordLineFile_btn()
|
||
{
|
||
//打开文件
|
||
FileOperation* fileOperation = new FileOperation();
|
||
string directory = fileOperation->getDirectoryOfExe();
|
||
//string RecordLineFilePath = directory + "\\test.RecordLine";
|
||
|
||
QString RecordLineFilePath = QFileDialog::getOpenFileName(this, tr("Open RecordLine File"),
|
||
QString::fromStdString(directory),
|
||
tr("RecordLineFile (*.RecordLine)"));
|
||
|
||
if (RecordLineFilePath.isEmpty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
FILE* RecordLineFileHandle = fopen(RecordLineFilePath.toStdString().c_str(), "rb");
|
||
double height, fov, swath, offset, repetitiveRate, LastLineThreshold, number;
|
||
|
||
//读取数据
|
||
fread(&height, sizeof(double), 1, RecordLineFileHandle);
|
||
fread(&fov, sizeof(double), 1, RecordLineFileHandle);
|
||
fread(&swath, sizeof(double), 1, RecordLineFileHandle);
|
||
fread(&offset, sizeof(double), 1, RecordLineFileHandle);
|
||
fread(&repetitiveRate, sizeof(double), 1, RecordLineFileHandle);
|
||
fread(&LastLineThreshold, sizeof(double), 1, RecordLineFileHandle);
|
||
fread(&number, sizeof(double), 1, RecordLineFileHandle);
|
||
|
||
double* data = new double[number];
|
||
for (size_t i = 0; i < number; i++)
|
||
{
|
||
fread(data + i, sizeof(double), 1, RecordLineFileHandle);
|
||
//std::cout << *(data + i) << std::endl;
|
||
}
|
||
|
||
//向界面中填写
|
||
ui.height_lineEdit->setText(QString::number(height));
|
||
ui.fov_lineEdit->setText(QString::number(fov));
|
||
ui.swath_lineEdit->setText(QString::number(swath));
|
||
ui.offset_lineEdit->setText(QString::number(offset));
|
||
ui.repetitiveRate_lineEdit->setText(QString::number(repetitiveRate));
|
||
ui.LastLineThreshold_lineEdit->setText(QString::number(LastLineThreshold));
|
||
|
||
|
||
//向tableWidget添加采集线
|
||
//(1)去掉tableWidget中所有的行
|
||
int rowCount = ui.recordLine_tableWidget->rowCount();
|
||
for (size_t i = 0; i < rowCount; i++)
|
||
{
|
||
ui.recordLine_tableWidget->removeRow(0);
|
||
}
|
||
//(2)添加行(采集线)
|
||
int RecordLineCount = number / ui.recordLine_tableWidget->columnCount();
|
||
for (size_t i = 0; i < RecordLineCount; i++)
|
||
{
|
||
ui.recordLine_tableWidget->insertRow(0);
|
||
|
||
}
|
||
//(3)向tableWidget填充数据
|
||
for (size_t i = 0; i < ui.recordLine_tableWidget->rowCount(); i++)
|
||
{
|
||
for (size_t j = 0; j < ui.recordLine_tableWidget->columnCount(); j++)
|
||
{
|
||
QTableWidgetItem* tmp = new QTableWidgetItem(QString::number(data[i * ui.recordLine_tableWidget->columnCount() + j], 10, 5));
|
||
tmp->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
|
||
ui.recordLine_tableWidget->setItem(i, j, tmp);
|
||
}
|
||
}
|
||
|
||
fclose(RecordLineFileHandle);
|
||
delete[] data;
|
||
|
||
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("读取成功!"));
|
||
|
||
}
|