diff --git a/HPPA/CaptureCoordinator.cpp b/HPPA/CaptureCoordinator.cpp index 8abec83..062a09f 100644 --- a/HPPA/CaptureCoordinator.cpp +++ b/HPPA/CaptureCoordinator.cpp @@ -441,3 +441,163 @@ void TwoMotionCaptureCoordinator::processNextPathLine() m_isMoving2XMin = true; emit moveTo(loc, speed, 1000); } + +OneMotionCaptureCoordinator::OneMotionCaptureCoordinator( + IrisMultiMotorController* motorCtrl, + ImagerOperationBase* cameraCtrl, + QObject* parent) + : QObject(parent) + , m_motorCtrl(motorCtrl) + , m_cameraCtrl(cameraCtrl) + , m_isRunning(false) +{ + connect(this, SIGNAL(moveTo(int, double, double, int)), + m_motorCtrl, SLOT(moveTo(int, double, double, int))); + connect(this, SIGNAL(moveSignal(int, bool, double, int)), m_motorCtrl, SLOT(move(int, bool, double, int))); + connect(this, &OneMotionCaptureCoordinator::stopMotorSignal, m_motorCtrl, &IrisMultiMotorController::stop); + + connect(m_motorCtrl, &IrisMultiMotorController::motorStopSignal, + this, &OneMotionCaptureCoordinator::handleMotorStoped); + //connect(m_motorCtrl, &IrisMultiMotorController::moveFailed, + // this, &OneMotionCaptureCoordinator::handleError); + + connect(this, &OneMotionCaptureCoordinator::startRecordHSISignal, + m_cameraCtrl, &ImagerOperationBase::start_record); + connect(this, &OneMotionCaptureCoordinator::stopRecordHSISignal, + m_cameraCtrl, &ImagerOperationBase::stop_record); + connect(m_cameraCtrl, &ImagerOperationBase::RecordFinishedSignal_WhenFrameNumberMeet, + this, &OneMotionCaptureCoordinator::handleCaptureCompleteWhenFrameNumberMeet); +} + +OneMotionCaptureCoordinator::~OneMotionCaptureCoordinator() +{ + +} + +void OneMotionCaptureCoordinator::startStepMotion(OneMotionCapturePathLine pathLine) +{ + QMutexLocker locker(&m_dataMutex); + + if (m_isRunning) + { + emit errorOccurred("Sequence already running"); + return; + } + + m_isRunning = true; + m_pathLine = pathLine; + + getLocBeforeStart(); + m_pathLine.startPosition = m_locBeforeStart[0]; + m_pathLine.timestamp1 = QDateTime::currentDateTime(); + + //移动马达并开始采集高光谱 + emit moveSignal(0, false, m_pathLine.speedRecord, 1000); + emit startRecordHSISignal(); +} + +void OneMotionCaptureCoordinator::stopStepMotion() +{ + QMutexLocker locker(&m_dataMutex); + + if (m_cameraCtrl != nullptr) + { + m_cameraCtrl->stop_record(); + } + + emit stopMotorSignal(0); +} + +void OneMotionCaptureCoordinator::handleCaptureCompleteWhenFrameNumberMeet() +{ + emit stopMotorSignal(0); +} + +void OneMotionCaptureCoordinator::getLocBeforeStart() +{ + QEventLoop loop; + bool received = false; + + QTimer timer; + timer.setSingleShot(true); + connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); + + QMetaObject::Connection conn = QObject::connect(m_motorCtrl, &IrisMultiMotorController::locationSignal, + [&](std::vector pos) { + m_locBeforeStart = pos; + received = true; + loop.quit(); + }); + + QMetaObject::invokeMethod(m_motorCtrl, "getLoc", Qt::QueuedConnection); + timer.start(3000); + + loop.exec(); + + disconnect(conn); +} + +void OneMotionCaptureCoordinator::move2LocBeforeStart() +{ + std::cout << "\nmove2LocBeforeStart." << std::endl; + + emit moveTo(0, m_locBeforeStart[0], m_pathLine.speedBack, 1000); + + m_isRunning = false; +} + +bool OneMotionCaptureCoordinator::saveToCsv(const QString& filename) +{ + //QMutexLocker locker(&m_dataMutex); + + QFile file(filename); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + { + return false; + } + + QTextStream out(&file); + out << "startTime,stopTime,startPosition,stopPosition\n"; + + out << m_pathLine.timestamp1.toString("yyyy-MM-dd HH:mm:ss.zzz") << "," + << m_pathLine.timestamp2.toString("yyyy-MM-dd HH:mm:ss.zzz") << "," + << QString::number(m_pathLine.startPosition, 'f', 4) << "," + << QString::number(m_pathLine.stopPosition, 'f', 4) << "\n"; + + file.close(); + return true; +} + +void OneMotionCaptureCoordinator::handleMotorStoped(int motorID, double pos) +{ + if (!m_isRunning) return; + + QMutexLocker locker(&m_dataMutex); + + // 记录位置信息 + m_pathLine.stopPosition = pos; + m_pathLine.timestamp2 = QDateTime::currentDateTime(); + + //光谱仪停止采集,马达回到初始位置 + emit stopRecordHSISignal(); + if (m_cameraCtrl != nullptr) + { + m_cameraCtrl->stop_record(); + } + move2LocBeforeStart(); +} + +void OneMotionCaptureCoordinator::handleCaptureComplete(double index) +{ + if (!m_isRunning) return; + + QMutexLocker locker(&m_dataMutex); + +} + +void OneMotionCaptureCoordinator::handleError(const QString& error) +{ + QMutexLocker locker(&m_dataMutex); + m_isRunning = false; + emit errorOccurred(error); +} diff --git a/HPPA/CaptureCoordinator.h b/HPPA/CaptureCoordinator.h index ebeb60a..3b35f15 100644 --- a/HPPA/CaptureCoordinator.h +++ b/HPPA/CaptureCoordinator.h @@ -105,3 +105,65 @@ private: int m_numCurrentPathLine; }; + + +struct OneMotionCapturePathLine +{ + double startPosition; + double stopPosition; + double speedRecord; + double speedBack; + + QDateTime timestamp1;//开始 + QDateTime timestamp2;//结束 + + OneMotionCapturePathLine() + : startPosition(0), stopPosition(0), speedRecord(0), speedBack(0), + timestamp1(QDateTime::currentDateTime()), timestamp2(QDateTime::currentDateTime()) {} +}; +Q_DECLARE_METATYPE(OneMotionCapturePathLine); + +class OneMotionCaptureCoordinator : public QObject +{ + Q_OBJECT +public: + OneMotionCaptureCoordinator(IrisMultiMotorController* motorCtrl, + ImagerOperationBase* cameraCtrl, + QObject* parent = nullptr); + ~OneMotionCaptureCoordinator(); + + bool saveToCsv(const QString& filename); + +public slots: + void startStepMotion(OneMotionCapturePathLine pathLine); + void stopStepMotion(); + + void handleCaptureCompleteWhenFrameNumberMeet(); + +signals: + void sequenceComplete(int); + void errorOccurred(const QString& error); + void moveTo(int, double, double, int); + void moveSignal(int, bool, double, int); + void stopMotorSignal(int axis); + + void startRecordHSISignal(); + void stopRecordHSISignal(); + +private slots: + void handleMotorStoped(int motorID, double pos); + void handleCaptureComplete(double index); + void handleError(const QString& error); + +private: + IrisMultiMotorController* m_motorCtrl; + ImagerOperationBase* m_cameraCtrl; + OneMotionCapturePathLine m_pathLine; + mutable QMutex m_dataMutex; + + bool m_isRunning; + + std::vector m_locBeforeStart; + void getLocBeforeStart(); + void move2LocBeforeStart(); +}; \ No newline at end of file diff --git a/HPPA/HPPA.cpp b/HPPA/HPPA.cpp index d85aa6e..b6a1914 100644 --- a/HPPA/HPPA.cpp +++ b/HPPA/HPPA.cpp @@ -302,7 +302,6 @@ HPPA::HPPA(QWidget *parent) //һ omc = new OneMotorControl(); - connect(omc, SIGNAL(startRecordLineSignal(int)), this, SLOT(recordHyperSpecImg(int))); dock_omc = new QDockWidget(QString::fromLocal8Bit(""), this); dock_omc->setObjectName("mDockOneMotorControl"); @@ -341,19 +340,6 @@ HPPA::HPPA(QWidget *parent) } } -void HPPA::recordHyperSpecImg(int status) -{ - if (status == 1) - { - emit StartRecordSignal();//俪ʼɼź - } - else if (status == 0) - { - m_Imager->setRecordControlState(false);//ֹͣɼ - } -} - - void HPPA::recordFromRobotArm(int fileCounter) { //qDebug() << "recordFromRobotArm" << fileCounter; @@ -585,7 +571,7 @@ void HPPA::onStartRecordStep1() { m_RecordState -= 1; - string tmp = imgPath + "_" + std::to_string(1) + ".bil"; + string tmp = imgPath + "_" + std::to_string(0) + ".bil"; int x = _access(tmp.c_str(), 0); if (!x)//ļھִдifĴ { @@ -650,11 +636,12 @@ void HPPA::onStartRecordStep1() //Ӧȿ˶˶ٿʼDzɼ俪ʼɼźţ m_Imager->setFileName2Save(imgPath); m_Imager->setFrameNumber(this->frame_number->text().toInt()); - omc->moveMotorAndRecordHyperSpecImg(500); + omc->setImager(m_Imager); + omc->run(); } else { - omc->moveMotor2StartPosAndStopRecord(); + omc->stop(); m_RecordState -= 1; ui.action_start_recording->setText(QString::fromLocal8Bit("ɼ")); @@ -963,7 +950,7 @@ void HPPA::onLeftMouseButtonPressed(int x, int y) FileOperation * fileOperation = new FileOperation(); string directory = fileOperation->getDirectoryFromString(); - string imgPath = directory + "\\" + m_FilenameLineEdit->text().toStdString() + "_" + std::to_string(m_TabWidgetCurrentIndex + 1) + ".bil"; + string imgPath = directory + "\\" + m_FilenameLineEdit->text().toStdString() + "_" + std::to_string(m_TabWidgetCurrentIndex) + ".bil"; ImageReaderWriter *ImageReader = new ImageReaderWriter(imgPath.c_str()); @@ -2361,12 +2348,6 @@ void HPPA::onRecordFinishedSignal_WhenFrameNumberMeet() ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(0,255,0);}"); m_RecordState++;//Զֹͣɼ - - if (checkedName == "mAction_1AxisMotor") - { - omc->moveMotor2StartPosAndStopRecord(); - } - } void HPPA::onRecordFinishedSignal_WhenFrameNumberNotMeet() diff --git a/HPPA/HPPA.h b/HPPA/HPPA.h index b3e4dc1..fed2af2 100644 --- a/HPPA/HPPA.h +++ b/HPPA/HPPA.h @@ -350,7 +350,6 @@ public Q_SLOTS: void requestFinished(QNetworkReply* reply); void recordFromRobotArm(int fileCounter); - void recordHyperSpecImg(int status); void createOneMotorScenario(); signals: diff --git a/HPPA/OneMotorControl.cpp b/HPPA/OneMotorControl.cpp index 8002e2a..3687b86 100644 --- a/HPPA/OneMotorControl.cpp +++ b/HPPA/OneMotorControl.cpp @@ -53,10 +53,6 @@ void OneMotorControl::onConnectMotor() connect(this->ui.rangeMeasurement_btn, SIGNAL(pressed()), this, SLOT(onx_rangeMeasurement())); connect(this, SIGNAL(rangeMeasurement(int, double, int)), m_multiAxisController, SLOT(rangeMeasurement(int, double, int))); - connect(this, SIGNAL(recordHyperSpecImgOneMotorSignal(int, double, double)), m_multiAxisController, SLOT(recordHyperSpecImgOneMotor(int, double, double))); - - connect(m_multiAxisController, SIGNAL(startRecordLineSignal(int)), this, SIGNAL(startRecordLineSignal(int))); - connect(this, SIGNAL(testConnectivitySignal(int, int)), m_multiAxisController, SLOT(testConnectivity(int, int))); connect(m_multiAxisController, SIGNAL(broadcastConnectivity(std::vector)), this, SLOT(display_motors_connectivity(std::vector))); @@ -121,15 +117,36 @@ void OneMotorControl::onxMotorStop() emit stopSignal(0); } -void OneMotorControl::moveMotorAndRecordHyperSpecImg(int updateIntervalMs) +void OneMotorControl::setImager(ImagerOperationBase* imager) { - double runSpeed = ui.speed_lineEdit->text().toDouble(); - double returnSpeed = ui.return_speed_lineEdit->text().toDouble(); - - emit recordHyperSpecImgOneMotorSignal(updateIntervalMs, runSpeed, returnSpeed); + m_Imager = imager; } -void OneMotorControl::moveMotor2StartPosAndStopRecord() +void OneMotorControl::run() { - m_multiAxisController->cancelRecord(); + if (m_coordinator == nullptr) + { + qRegisterMetaType("OneMotionCapturePathLine"); + m_coordinator = new OneMotionCaptureCoordinator(m_multiAxisController, m_Imager); + connect(this, SIGNAL(start(OneMotionCapturePathLine)), m_coordinator, SLOT(startStepMotion(OneMotionCapturePathLine))); + connect(this, SIGNAL(stopStepMotionSignal()), m_coordinator, SLOT(stopStepMotion())); + + connect(m_coordinator, SIGNAL(sequenceComplete(int)), this, SLOT(onSequenceComplete())); + } + + 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::onSequenceComplete() +{ + emit sequenceComplete(); } diff --git a/HPPA/OneMotorControl.h b/HPPA/OneMotorControl.h index c646e6a..c085875 100644 --- a/HPPA/OneMotorControl.h +++ b/HPPA/OneMotorControl.h @@ -6,6 +6,7 @@ #include "IrisMultiMotorController.h" #include "fileOperation.h" +#include "CaptureCoordinator.h" class OneMotorControl : public QDialog { @@ -15,8 +16,10 @@ public: OneMotorControl(QWidget* parent = nullptr); ~OneMotorControl(); - void moveMotorAndRecordHyperSpecImg(int updateIntervalMs); - void moveMotor2StartPosAndStopRecord(); + void setImager(ImagerOperationBase* imager); + + void run(); + void stop(); public Q_SLOTS: @@ -32,6 +35,8 @@ public Q_SLOTS: void onxMotorLeft(); void onxMotorStop(); + void onSequenceComplete(); + signals: void moveSignal(int, bool, double, int); void move2LocSignal(int, double, double, int); @@ -42,12 +47,17 @@ signals: void zeroStartSignal(int); void testConnectivitySignal(int, int); - void recordHyperSpecImgOneMotorSignal(int updateIntervalMs, double runSpeed, double returnSpeed); + void start(OneMotionCapturePathLine); + void stopStepMotionSignal(); + + void sequenceComplete(); - void startRecordLineSignal(int); private: Ui::OneMotorControl_UI ui; QThread m_motorThread; IrisMultiMotorController* m_multiAxisController; + + OneMotionCaptureCoordinator* m_coordinator = nullptr; + ImagerOperationBase* m_Imager; };