Files
HPPA/HPPA/OneMotorControl.cpp
tangchao0503 08dee5bbec fix:
1、将马达的信号槽连接改为函数指针方式;
2、给速度引入负值,表示反向移动马达;
2026-09-16 16:49:13 +08:00

578 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "OneMotorControl.h"
OneMotorControl::OneMotorControl(QWidget* parent) : QDialog(parent)
{
ui.setupUi(this);
connect(this->ui.connect_btn, SIGNAL(pressed()), this, SLOT(onConnectMotor()));
connect(this->ui.right_btn, SIGNAL(pressed()), this, SLOT(onxMotorRight()));
connect(this->ui.right_btn, SIGNAL(released()), this, SLOT(onxMotorStop()));
connect(this->ui.left_btn, SIGNAL(pressed()), this, SLOT(onxMotorLeft()));
connect(this->ui.left_btn, SIGNAL(released()), this, SLOT(onxMotorStop()));
connect(this->ui.move2loc_pushButton, SIGNAL(pressed()), this, SLOT(onxMove2Loc()));
connect(this->ui.zero_start_btn, SIGNAL(released()), this, SLOT(zeroStart()));
connect(this->ui.rangeMeasurement_btn, SIGNAL(pressed()), this, SLOT(onx_rangeMeasurement()));
// 从 AppSettings 读取速度参数
AppSettings& settings = AppSettings::instance();
ui.speed_lineEdit->setText(QString::number(settings.scanSpeed()));
ui.return_speed_lineEdit->setText(QString::number(settings.returnSpeed()));
// 连接信号,当控件数值变化时保存到 AppSettings
connect(ui.speed_lineEdit, &QLineEdit::editingFinished, [this]() {
AppSettings::instance().setScanSpeed(ui.speed_lineEdit->text().toDouble());
});
connect(ui.return_speed_lineEdit, &QLineEdit::editingFinished, [this]() {
AppSettings::instance().setReturnSpeed(ui.return_speed_lineEdit->text().toDouble());
});
}
OneMotorControl::~OneMotorControl()
{
m_motorThread.quit();
m_motorThread.wait();
}
void OneMotorControl::onConnectMotor()
{
connectMotor(true);
}
void OneMotorControl::setScanSpeed(double speed)
{
ui.speed_lineEdit->setText(QString::number(speed));
}
void OneMotorControl::connectMotor(bool isNotification)
{
if (getMotorsConnectionStatus())
{
if (isNotification)
{
QMessageBox msgBox;
msgBox.setText(QString::fromLocal8Bit("马达已连接!"));
msgBox.exec();
}
return;
}
if (m_multiAxisController != nullptr)
{
disconnect(m_multiAxisController, &IrisMultiMotorController::broadcastLocationSignal, this, &OneMotorControl::display_x_loc);
disconnect(this, &OneMotorControl::moveSignal, m_multiAxisController, qOverload<int, double, int>(&IrisMultiMotorController::move));
disconnect(this, qOverload<int, double, double, int>(&OneMotorControl::move2LocSignal), m_multiAxisController, qOverload<int, double, double, int>(&IrisMultiMotorController::moveTo));
disconnect(this, &OneMotorControl::stopSignal, m_multiAxisController, &IrisMultiMotorController::stop);
disconnect(this, &OneMotorControl::zeroStartSignal, m_multiAxisController, &IrisMultiMotorController::zeroStart);
disconnect(this, &OneMotorControl::rangeMeasurement, m_multiAxisController, &IrisMultiMotorController::rangeMeasurement);
disconnect(this, &OneMotorControl::testConnectivitySignal, m_multiAxisController, &IrisMultiMotorController::testConnectivity);
disconnect(m_multiAxisController, &IrisMultiMotorController::broadcastConnectivity, this, &OneMotorControl::display_motors_connectivity);
m_motorThread.quit();
m_motorThread.wait();
m_multiAxisController = nullptr;
}
try
{
FileOperation* fileOperation = new FileOperation();
string directory = fileOperation->getDirectoryOfExe();
QString configFilePath = QString::fromStdString(directory) + "\\oneMotorConfigFile.cfg";
m_multiAxisController = new IrisMultiMotorController(configFilePath);
}
catch (std::exception const& e)
{
QMessageBox msgBox;
msgBox.setText(QString::fromLocal8Bit("请连接马达!"));
msgBox.exec();
return;
}
m_multiAxisController->moveToThread(&m_motorThread);
connect(&m_motorThread, &QThread::finished, m_multiAxisController, &QObject::deleteLater);
connect(m_multiAxisController, &IrisMultiMotorController::broadcastLocationSignal, this, &OneMotorControl::display_x_loc);
connect(this, &OneMotorControl::moveSignal, m_multiAxisController, qOverload<int, double, int>(&IrisMultiMotorController::move));
connect(this, qOverload<int, double, double, int>(&OneMotorControl::move2LocSignal), m_multiAxisController, qOverload<int, double, double, int>(&IrisMultiMotorController::moveTo));
connect(this, &OneMotorControl::stopSignal, m_multiAxisController, &IrisMultiMotorController::stop);
connect(this, &OneMotorControl::zeroStartSignal, m_multiAxisController, &IrisMultiMotorController::zeroStart);
connect(this, &OneMotorControl::rangeMeasurement, m_multiAxisController, &IrisMultiMotorController::rangeMeasurement);
connect(this, &OneMotorControl::testConnectivitySignal, m_multiAxisController, &IrisMultiMotorController::testConnectivity);
connect(m_multiAxisController, &IrisMultiMotorController::broadcastConnectivity, this, &OneMotorControl::display_motors_connectivity);
m_motorThread.start();
emit testConnectivitySignal(0, 1000);
}
void OneMotorControl::display_x_loc(std::vector<double> loc)
{
double tmp = round(loc[0] * 100) / 100;
this->ui.realTimeLoc_lineEdit->setText(QString::number(tmp));
emit broadcastLocationSignal(loc);
}
void OneMotorControl::display_motors_connectivity(std::vector<int> connectivity)
{
//std::cout << "-----------------------------------"<<connectivity.size()<< std::endl;
if (connectivity[0])
{
m_xMotorConnectionStatus = true;
this->ui.motor_state_label->setStyleSheet(R"(
QLabel
{
background-color: #08FACE;
border-radius: 4px;
}
)");
}
else
{
m_xMotorConnectionStatus = false;
this->ui.motor_state_label->setStyleSheet(R"(
QLabel
{
background-color: red;
border-radius: 4px;
}
)");
}
if (getMotorsConnectionStatus())
{
this->ui.connect_btn->setText(QString::fromLocal8Bit("已连接"));
}
else
{
this->ui.connect_btn->setText(QString::fromLocal8Bit("重新连接"));
}
}
void OneMotorControl::zeroStart()
{
zeroStartSignal(0);
}
void OneMotorControl::onx_rangeMeasurement()
{
double s0 = ui.speed_lineEdit->text().toDouble();
emit rangeMeasurement(0, s0, 1000);
}
void OneMotorControl::onxMove2Loc()
{
double s = ui.speed_lineEdit->text().toDouble();
double l = ui.move2loc_lineEdit->text().toDouble();
emit move2LocSignal(0, l, s, 1000);
}
void OneMotorControl::onxMotorRight()
{
double s = ui.speed_lineEdit->text().toDouble();
emit moveSignal(0, abs(s), 1000);
}
void OneMotorControl::onxMotorLeft()
{
double s = ui.speed_lineEdit->text().toDouble();
emit moveSignal(0, abs(s)*-1, 1000);
}
void OneMotorControl::onxMotorStop()
{
emit stopSignal(0);
}
void OneMotorControl::setImager(ImagerOperationBase* imager)
{
m_Imager = imager;
}
void OneMotorControl::record_dark()
{
double s = ui.speed_lineEdit->text().toDouble();
if (m_darkCaptureCoordinator == nullptr)
{
m_darkCaptureCoordinator = new DarkAndWhiteCaptureCoordinator(0, m_multiAxisController, m_Imager);
}
m_darkCaptureCoordinator->startStepMotion(s);
}
void OneMotorControl::record_white()
{
double s = ui.speed_lineEdit->text().toDouble();
if (m_whiteCaptureCoordinator == nullptr)
{
m_whiteCaptureCoordinator = new DarkAndWhiteCaptureCoordinator(1, m_multiAxisController, m_Imager);
}
m_whiteCaptureCoordinator->startStepMotion(s);
}
void OneMotorControl::run()
{
if (m_coordinator)//当高光谱相机停止采集后,马达还未回到原点时,上次任务的m_coordinator还没有被销毁
{
onSequenceComplete_motorBack2Origin(0);
}
qRegisterMetaType<OneMotionCapturePathLine>("OneMotionCapturePathLine");
m_coordinator = new OneMotionCaptureCoordinator(m_multiAxisController, m_Imager);
m_coordinator->setObjectName("testOneMotionCaptureCoordinator");
connect(this, &OneMotorControl::start, m_coordinator, &OneMotionCaptureCoordinator::startStepMotion);
connect(this, &OneMotorControl::stopStepMotionSignal, m_coordinator, &OneMotionCaptureCoordinator::stopStepMotion);
connect(m_coordinator, &OneMotionCaptureCoordinator::sequenceCompleteSignal_hyperImagerStopRecord,
this, &OneMotorControl::sequenceCompleteSignal_hyperImagerStopRecord);
connect(m_coordinator, &OneMotionCaptureCoordinator::sequenceCompleteSignal_motorBack2Origin,
this, &OneMotorControl::onSequenceComplete_motorBack2Origin);
OneMotionCapturePathLine tmp;
tmp.speedRecord = ui.speed_lineEdit->text().toDouble();
tmp.speedBack = ui.return_speed_lineEdit->text().toDouble();
emit start(tmp);
}
void OneMotorControl::stop()
{
emit stopStepMotionSignal();
}
void OneMotorControl::multiPosHyperAutoExposure()
{
//所有该自动曝光的位置
std::vector<double> maxRangeLocations = m_multiAxisController->getMaxPos();
double maxPos = maxRangeLocations[0];
std::vector<double> locations;
locations.push_back(maxPos * 0.2);
locations.push_back(maxPos * 0.5);
locations.push_back(maxPos * 0.8);
//创建协调器,并连接信号槽
m_coordinator_gonggashan_autoexpose = new OneMotorMultiPosCoordinator(m_multiAxisController, m_Imager);
//connect(this, SIGNAL(stopStepMotionSignal()), m_coordinator_gonggashan_autoexpose, SLOT(stopStepMotion()));
connect(m_coordinator_gonggashan_autoexpose, &OneMotorMultiPosCoordinator::sequenceComplete, this, &OneMotorControl::onSequenceComplete_gonggashan_autoexpose);
connect(m_coordinator_gonggashan_autoexpose, &OneMotorMultiPosCoordinator::hyperAutoExposureDoneSignal, this, &OneMotorControl::hyperAutoExposureDoneSignal_gonggashan);
m_coordinator_gonggashan_autoexpose->startStepMotion(ui.speed_lineEdit->text().toDouble(), locations);
}
void OneMotorControl::onSequenceComplete_gonggashan_autoexpose(int state)
{
emit multiPosAutoexposeSequenceCompleteSignal();
m_coordinator_gonggashan_autoexpose->deleteLater();
}
void OneMotorControl::onSequenceComplete_motorBack2Origin(int state)
{
emit sequenceComplete_motorBack2Origin();
// Use deleteLater() instead of delete: this slot may have been called directly
// from OneMotionCaptureCoordinator's call stack (direct connection), so deleting
// the object here would cause a crash when execution returns to the destroyed object.
m_coordinator->deleteLater();
m_coordinator = nullptr;
}
bool OneMotorControl::getMotorsConnectionStatus()
{
return m_xMotorConnectionStatus;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
OneMotorControl_LiftingPlatform::OneMotorControl_LiftingPlatform(QWidget* parent) : QDialog(parent)
{
ui.setupUi(this);
connect(this->ui.connect_btn, SIGNAL(pressed()), this, SLOT(onConnectMotor()));
connect(this->ui.right_btn, SIGNAL(pressed()), this, SLOT(onxMotorRight()));
connect(this->ui.right_btn, SIGNAL(released()), this, SLOT(onxMotorStop()));
connect(this->ui.left_btn, SIGNAL(pressed()), this, SLOT(onxMotorLeft()));
connect(this->ui.left_btn, SIGNAL(released()), this, SLOT(onxMotorStop()));
connect(this->ui.move2loc_pushButton, SIGNAL(pressed()), this, SLOT(onxMove2Loc()));
connect(this->ui.zero_start_btn, SIGNAL(released()), this, SLOT(zeroStart()));
connect(this->ui.rangeMeasurement_btn, SIGNAL(pressed()), this, SLOT(onx_rangeMeasurement()));
// 从 AppSettings 读取速度参数
AppSettings& settings = AppSettings::instance();
ui.speed_lineEdit->setText(QString::number(settings.scanSpeed()));
ui.return_speed_lineEdit->setText(QString::number(settings.returnSpeed()));
// 连接信号,当控件数值变化时保存到 AppSettings
connect(ui.speed_lineEdit, &QLineEdit::editingFinished, [this]() {
AppSettings::instance().setScanSpeed(ui.speed_lineEdit->text().toDouble());
});
connect(ui.return_speed_lineEdit, &QLineEdit::editingFinished, [this]() {
AppSettings::instance().setReturnSpeed(ui.return_speed_lineEdit->text().toDouble());
});
}
OneMotorControl_LiftingPlatform::~OneMotorControl_LiftingPlatform()
{
m_motorThread.quit();
m_motorThread.wait();
}
void OneMotorControl_LiftingPlatform::onConnectMotor()
{
connectMotor(true);
}
void OneMotorControl_LiftingPlatform::connectMotor(bool isNotification)
{
if (getMotorsConnectionStatus())
{
if (isNotification)
{
QMessageBox msgBox;
msgBox.setText(QString::fromLocal8Bit("马达已连接!"));
msgBox.exec();
}
return;
}
if (m_multiAxisController != nullptr)
{
disconnect(m_multiAxisController, &IrisMultiMotorController::broadcastLocationSignal, this, &OneMotorControl_LiftingPlatform::display_x_loc);
disconnect(this, &OneMotorControl_LiftingPlatform::moveSignal, m_multiAxisController, qOverload<int, double, int>(&IrisMultiMotorController::move));
disconnect(this, qOverload<int, double, double, int>(&OneMotorControl_LiftingPlatform::move2LocSignal), m_multiAxisController, qOverload<int, double, double, int>(&IrisMultiMotorController::moveTo));
disconnect(this, &OneMotorControl_LiftingPlatform::stopSignal, m_multiAxisController, &IrisMultiMotorController::stop);
disconnect(this, &OneMotorControl_LiftingPlatform::zeroStartSignal, m_multiAxisController, &IrisMultiMotorController::zeroStart);
disconnect(this, &OneMotorControl_LiftingPlatform::rangeMeasurement, m_multiAxisController, &IrisMultiMotorController::rangeMeasurement);
disconnect(this, &OneMotorControl_LiftingPlatform::testConnectivitySignal, m_multiAxisController, &IrisMultiMotorController::testConnectivity);
disconnect(m_multiAxisController, &IrisMultiMotorController::broadcastConnectivity, this, &OneMotorControl_LiftingPlatform::display_motors_connectivity);
m_motorThread.quit();
m_motorThread.wait();
m_multiAxisController = nullptr;
}
try
{
FileOperation* fileOperation = new FileOperation();
string directory = fileOperation->getDirectoryOfExe();
QString configFilePath = QString::fromStdString(directory) + "\\oneMotorConfigFile_LiftingPlatform.cfg";
m_multiAxisController = new IrisMultiMotorController(configFilePath);
}
catch (std::exception const& e)
{
QMessageBox msgBox;
msgBox.setText(QString::fromLocal8Bit("请连接马达!"));
msgBox.exec();
return;
}
m_multiAxisController->moveToThread(&m_motorThread);
connect(&m_motorThread, &QThread::finished, m_multiAxisController, &QObject::deleteLater);
connect(m_multiAxisController, &IrisMultiMotorController::broadcastLocationSignal, this, &OneMotorControl_LiftingPlatform::display_x_loc);
connect(this, &OneMotorControl_LiftingPlatform::moveSignal, m_multiAxisController, qOverload<int, double, int>(&IrisMultiMotorController::move));
connect(this, qOverload<int, double, double, int>(&OneMotorControl_LiftingPlatform::move2LocSignal), m_multiAxisController, qOverload<int, double, double, int>(&IrisMultiMotorController::moveTo));
connect(this, &OneMotorControl_LiftingPlatform::stopSignal, m_multiAxisController, &IrisMultiMotorController::stop);
connect(this, &OneMotorControl_LiftingPlatform::zeroStartSignal, m_multiAxisController, &IrisMultiMotorController::zeroStart);
connect(this, &OneMotorControl_LiftingPlatform::rangeMeasurement, m_multiAxisController, &IrisMultiMotorController::rangeMeasurement);
connect(this, &OneMotorControl_LiftingPlatform::testConnectivitySignal, m_multiAxisController, &IrisMultiMotorController::testConnectivity);
connect(m_multiAxisController, &IrisMultiMotorController::broadcastConnectivity, this, &OneMotorControl_LiftingPlatform::display_motors_connectivity);
m_motorThread.start();
emit testConnectivitySignal(0, 1000);
}
void OneMotorControl_LiftingPlatform::display_x_loc(std::vector<double> loc)
{
double tmp = round(loc[0] * 100) / 100;
this->ui.realTimeLoc_lineEdit->setText(QString::number(tmp));
emit broadcastLocationSignal(loc);
}
void OneMotorControl_LiftingPlatform::display_motors_connectivity(std::vector<int> connectivity)
{
//std::cout << "-----------------------------------"<<connectivity.size()<< std::endl;
if (connectivity[0])
{
m_xMotorConnectionStatus = true;
this->ui.motor_state_label->setStyleSheet(R"(
QLabel
{
background-color: #08FACE;
border-radius: 4px;
}
)");
}
else
{
m_xMotorConnectionStatus = false;
this->ui.motor_state_label->setStyleSheet(R"(
QLabel
{
background-color: red;
border-radius: 4px;
}
)");
}
if (getMotorsConnectionStatus())
{
this->ui.connect_btn->setText(QString::fromLocal8Bit("已连接"));
}
else
{
this->ui.connect_btn->setText(QString::fromLocal8Bit("重新连接"));
}
}
void OneMotorControl_LiftingPlatform::zeroStart()
{
zeroStartSignal(0);
}
void OneMotorControl_LiftingPlatform::onx_rangeMeasurement()
{
double s0 = ui.speed_lineEdit->text().toDouble();
emit rangeMeasurement(0, s0, 1000);
}
void OneMotorControl_LiftingPlatform::onxMove2Loc()
{
double s = ui.speed_lineEdit->text().toDouble();
double l = ui.move2loc_lineEdit->text().toDouble();
emit move2LocSignal(0, l, s, 1000);
}
void OneMotorControl_LiftingPlatform::onxMotorRight()
{
double s = ui.speed_lineEdit->text().toDouble();
emit moveSignal(0, abs(s), 1000);
}
void OneMotorControl_LiftingPlatform::onxMotorLeft()
{
double s = ui.speed_lineEdit->text().toDouble();
emit moveSignal(0, abs(s)*-1, 1000);
}
void OneMotorControl_LiftingPlatform::onxMotorStop()
{
emit stopSignal(0);
}
void OneMotorControl_LiftingPlatform::run()
{
m_coordinator = new OneMotionCoordinator(m_multiAxisController,this);
connect(m_coordinator, &OneMotionCoordinator::sequenceComplete, this, &OneMotorControl_LiftingPlatform::sequenceComplete);
connect(m_coordinator, &OneMotionCoordinator::ArrivalSignal, this, &OneMotorControl_LiftingPlatform::onBack2Origin);
double plantDepthValue = DepthValueLogger::instance().readLatestPlantDepthValue();
double liftingPlatformDepthValue = DepthValueLogger::instance().readLatestLiftingPlatformDepthValue();
double targetDepth = liftingPlatformDepthValue - plantDepthValue;
if (targetDepth < 0)
{
return;
}
m_coordinator->moveToTarget(targetDepth, ui.speed_lineEdit->text().toDouble());
}
void OneMotorControl_LiftingPlatform::stop()
{
emit stopStepMotionSignal();
}
void OneMotorControl_LiftingPlatform::onBack2Origin(double pos)
{
emit back2OriginSignal_TimedDataCollection();
m_coordinator->deleteLater();
m_coordinator = nullptr;
}
bool OneMotorControl_LiftingPlatform::getMotorsConnectionStatus()
{
return m_xMotorConnectionStatus;
}