From 20848bef1a79308b8a12c54e234b719449feee36 Mon Sep 17 00:00:00 2001 From: tangchao0503 <735056338@qq.com> Date: Wed, 16 Apr 2025 16:25:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E6=A2=B0=E8=87=82=E5=92=8C=E5=85=89?= =?UTF-8?q?=E8=B0=B1=E4=BB=AA=E8=81=94=E5=8A=A8=EF=BC=8C=E5=8F=B3=E4=B8=8B?= =?UTF-8?q?=E8=A7=92=E6=9C=BA=E6=A2=B0=E8=87=82=E6=8E=A7=E5=88=B6=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E4=B8=8D=E4=BC=9A=E8=A7=A6=E5=8F=91=E7=9B=B8=E6=9C=BA?= =?UTF-8?q?=E9=87=87=E9=9B=86=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HPPA/HPPA.cpp | 312 ++++++++++++++++++++------------- HPPA/HPPA.h | 9 +- HPPA/HPPA.ui | 35 +++- HPPA/ImagerOperationBase.cpp | 2 +- HPPA/RobotArmControl.cpp | 330 ++++++++++++++++++++++++++++------- HPPA/RobotArmControl.h | 50 ++++-- HPPA/about.ui | 2 +- 7 files changed, 530 insertions(+), 210 deletions(-) diff --git a/HPPA/HPPA.cpp b/HPPA/HPPA.cpp index 67b7fa5..22e00d4 100644 --- a/HPPA/HPPA.cpp +++ b/HPPA/HPPA.cpp @@ -267,7 +267,8 @@ HPPA::HPPA(QWidget *parent) //轨迹规划 m_pathPlan = new PathPlan(m_xMotor, m_yMotor, ui.xmotor_location_slider, ui.ymotor_location_slider); QDockWidget* dock_pathPlan = new QDockWidget(QString::fromLocal8Bit("轨迹规划"), this); - dock_pathPlan->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); + dock_pathPlan->setObjectName("mDockPathPlan"); + dock_pathPlan->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); dock_pathPlan->setWidget(m_pathPlan); tabifyDockWidget(ui.mDockWidgetLinearStage, dock_pathPlan); mPanelMenu->addAction(dock_pathPlan->toggleViewAction()); @@ -276,6 +277,7 @@ HPPA::HPPA(QWidget *parent) adjustTable* adt = new adjustTable(); QDockWidget* dock_adt = new QDockWidget(QString::fromLocal8Bit("升降桌"), this); + dock_adt->setObjectName("mDockAdjustTable"); dock_adt->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); dock_adt->setWidget(adt); tabifyDockWidget(dock_pathPlan, dock_adt); @@ -285,6 +287,7 @@ HPPA::HPPA(QWidget *parent) PowerControl* pc = new PowerControl(); QDockWidget* dock_pc = new QDockWidget(QString::fromLocal8Bit("电源控制"), this); + dock_pc->setObjectName("mDockPowerControl"); dock_pc->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); dock_pc->setWidget(pc); tabifyDockWidget(dock_adt, dock_pc); @@ -293,8 +296,10 @@ HPPA::HPPA(QWidget *parent) connect(pc, &PowerControl::powerClosed, this, &HPPA::deleteMotor); //机械臂控制 - RobotArmControl* rac = new RobotArmControl(); + rac = new RobotArmControl(); + connect(rac->robotController, SIGNAL(hsiRecordSignal(int)), this, SLOT(recordFromRobotArm(int))); QDockWidget* dock_rac = new QDockWidget(QString::fromLocal8Bit("机械臂控制"), this); + dock_rac->setObjectName("mDockRobotArmControl"); dock_rac->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea); dock_rac->setWidget(rac); tabifyDockWidget(dock_adt, dock_rac); @@ -303,17 +308,37 @@ HPPA::HPPA(QWidget *parent) createActionGroups(); connect(mImagerGroup, &QActionGroup::triggered, this, &HPPA::selectingImager); - connect(this->ui.mAction_is_motor_enable, SIGNAL(triggered()), this, SLOT(onMotorSwitch())); - QSettings settings; - bool isMotorEnable = settings.value("isMotorEnable").toBool(); - this->ui.mAction_is_motor_enable->setChecked(isMotorEnable); + createMoveplatformActionGroup(); + connect(moveplatformActionGroup, &QActionGroup::triggered, this, &HPPA::selectingMoveplatform); } -void HPPA::onMotorSwitch() +void HPPA::recordFromRobotArm(int fileCounter) { - QSettings settings; - settings.setValue("isMotorEnable", this->ui.mAction_is_motor_enable->isChecked()); - settings.sync(); + qDebug() << "recordFromRobotArm" << fileCounter; + + if (fileCounter == -1) + { + m_Imager->setRecordControlState(false); + + ui.action_start_recording->setText(QString::fromLocal8Bit("采集")); + ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(0,255,0);}"); + qDebug() << "recordFromRobotArm: 1111111111111111111111"; + + return; + } + + if (fileCounter - 1 == 0) + { + ui.ImageViewerTabWidget->clear(); + } + + onCreateTab(fileCounter-1); + m_numberOfRecording = fileCounter - 1; + emit StartRecordSignal(); + + ui.action_start_recording->setText(QString::fromLocal8Bit("采集中")); + ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(255,0,0);}"); + qDebug() << "recordFromRobotArm: 2222222222222222222222"; } void HPPA::createActionGroups() @@ -357,6 +382,39 @@ void HPPA::selectingImager(QAction* selectedAction) settings.sync(); } +void HPPA::createMoveplatformActionGroup() +{ + moveplatformActionGroup = new QActionGroup(this); + moveplatformActionGroup->addAction(ui.mAction_is_no_motor); + moveplatformActionGroup->addAction(ui.mAction_2AxisMotor); + moveplatformActionGroup->addAction(ui.mAction_RobotArm); + + // 读取上次选择的结果 + QSettings settings; + QString lastSelectedAction = settings.value("LastSelectedMoveplatform").toString(); + + // 恢复上次选择的结果 + if (lastSelectedAction == "mAction_is_no_motor") + { + ui.mAction_is_no_motor->setChecked(true); + } + else if (lastSelectedAction == "mAction_2AxisMotor") + { + ui.mAction_2AxisMotor->setChecked(true); + } + else if (lastSelectedAction == "mAction_RobotArm") + { + ui.mAction_RobotArm->setChecked(true); + } +} + +void HPPA::selectingMoveplatform(QAction* selectedAction) +{ + QSettings settings; + settings.setValue("LastSelectedMoveplatform", selectedAction->objectName()); + settings.sync(); +} + HPPA::~HPPA() { QString strPath = QCoreApplication::applicationDirPath() + "/UILayout.ini"; @@ -420,40 +478,52 @@ void HPPA::CalculateIntegratioinTimeRange() void HPPA::onStartRecordStep1() { - bool isMotorEnable = this->ui.mAction_is_motor_enable->isChecked(); - if (!isMotorEnable)//停用马达,仅采集光谱仪数据 + QAction* checked = moveplatformActionGroup->checkedAction(); + if (!checked) + { + QMessageBox msgBox; + msgBox.setText(QString::fromLocal8Bit("请选择扫描平台!")); + msgBox.exec(); + return; + } + + //判断是否覆盖存在的文件 + FileOperation* fileOperation = new FileOperation(); + string directory = fileOperation->getDirectoryFromString(); + //string imgPath = directory + "\\tmp_image"; + string imgPath = directory + "\\" + m_FilenameLineEdit->text().toStdString(); + int x = _access(imgPath.c_str(), 0); + if (!x)//如果文件存在就执行此if的代码 + { + enum QMessageBox::StandardButton response = QMessageBox::question(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("文件存在!是否覆盖?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);; + if (response == QMessageBox::Yes)// + { + //std::cout << "覆盖" << std::endl; + } + else + { + //std::cout << "不覆盖" << std::endl; + //m_RecordState -= 1;//不覆盖的话就需要还原这个参数,并停止采集 + return; + } + } + + m_Imager->setFrameNumber(this->frame_number->text().toInt()); + m_Imager->setFileName2Save(imgPath); + + QString checkedName = checked->objectName(); + + if (checkedName == "mAction_is_no_motor") { m_RecordState += 1; if (m_RecordState % 2 == 1) { - //判断是否覆盖存在的文件 - FileOperation* fileOperation = new FileOperation(); - string directory = fileOperation->getDirectoryFromString(); - //string imgPath = directory + "\\tmp_image"; - string imgPath = directory + "\\" + m_FilenameLineEdit->text().toStdString() + "_" + std::to_string(1) + ".bil"; - int x = _access(imgPath.c_str(), 0); - if (!x)//如果文件存在就执行此if的代码 - { - enum QMessageBox::StandardButton response = QMessageBox::question(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("文件存在!是否覆盖?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);; - if (response == QMessageBox::Yes)// - { - //std::cout << "覆盖" << std::endl; - } - else - { - //std::cout << "不覆盖" << std::endl; - m_RecordState -= 1;//不覆盖的话就需要还原这个参数,并停止采集 - return; - } - } - ui.ImageViewerTabWidget->clear(); onCreateTab(0); m_numberOfRecording = 0; - m_Imager->setFrameNumber(this->frame_number->text().toInt()); - m_Imager->setFileName2Save(imgPath); + emit StartRecordSignal();//发射开始采集信号 ui.action_start_recording->setText(QString::fromLocal8Bit("停止采集")); @@ -468,98 +538,84 @@ void HPPA::onStartRecordStep1() } return; } - - //确保x马达所在位置 小于 x马达最大采集限制位置 - int validLineCount = 0; - for (size_t i = 0; i < m_pathPlan->getRecordLineTableWidget()->rowCount(); i++) + else if (checkedName == "mAction_2AxisMotor") { - //xx = xx + ui.xmotor_location_slider->value() < m_pathPlan->getRecordLineTableWidget()->item(i, 1)->text().toDouble(); - - if (ui.xmotor_location_slider->value() < m_pathPlan->getRecordLineTableWidget()->item(i, 1)->text().toDouble()) - { - validLineCount++; - } - - } - - if (validLineCount < m_pathPlan->getRecordLineTableWidget()->rowCount()) - { - QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("轨迹x马达限位错误!")); - return; - } - - //确保采集线存在 - if (m_pathPlan->getRecordLineTableWidget()->rowCount() <= 0) - { - QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("请先生成轨迹!")); - return; - } - - //开始采集 - m_RecordState += 1; - - if (m_RecordState % 2 == 1) - { - //判断是否覆盖存在的文件 - FileOperation * fileOperation = new FileOperation(); - string directory = fileOperation->getDirectoryFromString(); - //string imgPath = directory + "\\tmp_image"; - string imgPath = directory + "\\" + m_FilenameLineEdit->text().toStdString(); - int x = _access(imgPath.c_str(), 0); - if (!x)//如果文件存在就执行此if的代码 - { - enum QMessageBox::StandardButton response = QMessageBox::question(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("文件存在!是否覆盖?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);; - if (response == QMessageBox::Yes)// - { - //std::cout << "覆盖" << std::endl; - } - else - { - //std::cout << "不覆盖" << std::endl; - m_RecordState -= 1;//不覆盖的话就需要还原这个参数,并停止采集 - return; - } - } - - - // - operateWidget = QObject::sender()->objectName(); - - //设置文件保存名 - m_Imager->setFileName2Save(imgPath); - - //删除所有tab - ui.ImageViewerTabWidget->clear(); - - //创建记录x马达位置的文件 - string x_location = removeFileExtension(imgPath) + "x_location.pos"; - m_hTimesFile = fopen(x_location.c_str(), "w+"); - - //开始循环 - m_ForLoopControl->setLoopCount(m_pathPlan->getRecordLineTableWidget()->rowCount());//为循环控制线程设置循环次数 - emit StartLoopSignal(); - - - //所有行设置为灰色 + //确保x马达所在位置 小于 x马达最大采集限制位置 + int validLineCount = 0; for (size_t i = 0; i < m_pathPlan->getRecordLineTableWidget()->rowCount(); i++) { - for (size_t j = 0; j < m_pathPlan->getRecordLineTableWidget()->columnCount(); j++) + //xx = xx + ui.xmotor_location_slider->value() < m_pathPlan->getRecordLineTableWidget()->item(i, 1)->text().toDouble(); + + if (ui.xmotor_location_slider->value() < m_pathPlan->getRecordLineTableWidget()->item(i, 1)->text().toDouble()) { - m_pathPlan->getRecordLineTableWidget()->item(i, j)->setBackgroundColor(QColor(240, 240, 240)); + validLineCount++; } + } - ui.action_start_recording->setText(QString::fromLocal8Bit("停止采集")); - ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(255,0,0);}"); + if (validLineCount < m_pathPlan->getRecordLineTableWidget()->rowCount()) + { + QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("轨迹x马达限位错误!")); + return; + } + + //确保采集线存在 + if (m_pathPlan->getRecordLineTableWidget()->rowCount() <= 0) + { + QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("请先生成轨迹!")); + return; + } + + //开始采集 + m_RecordState += 1; + + if (m_RecordState % 2 == 1) + { + // + operateWidget = QObject::sender()->objectName(); + + //删除所有tab + ui.ImageViewerTabWidget->clear(); + + //创建记录x马达位置的文件 + string x_location = removeFileExtension(imgPath) + "x_location.pos"; + m_hTimesFile = fopen(x_location.c_str(), "w+"); + + //开始循环 + m_ForLoopControl->setLoopCount(m_pathPlan->getRecordLineTableWidget()->rowCount());//为循环控制线程设置循环次数 + emit StartLoopSignal(); + + + //所有行设置为灰色 + for (size_t i = 0; i < m_pathPlan->getRecordLineTableWidget()->rowCount(); i++) + { + for (size_t j = 0; j < m_pathPlan->getRecordLineTableWidget()->columnCount(); j++) + { + m_pathPlan->getRecordLineTableWidget()->item(i, j)->setBackgroundColor(QColor(240, 240, 240)); + } + } + + ui.action_start_recording->setText(QString::fromLocal8Bit("停止采集")); + ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(255,0,0);}"); + } + else + { + m_Imager->setRecordControlState(false);//光谱仪停止采集 + + m_ForLoopControl->m_boolQuitLoop = true;//循环停止 + + ui.action_start_recording->setText(QString::fromLocal8Bit("采集")); + ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(0,255,0);}"); + } + + return; } - else + else if (checkedName == "mAction_RobotArm") { - m_Imager->setRecordControlState(false);//光谱仪停止采集 + //先判断是否选择了任务,执行函数RobotArmControl::executeTask,修复界面显示bug + rac->executeTaskWithHyperImager(); - m_ForLoopControl->m_boolQuitLoop = true;//循环停止 - - ui.action_start_recording->setText(QString::fromLocal8Bit("采集")); - ui.mainToolBar->widgetForAction(ui.action_start_recording)->setStyleSheet("QWidget{background-color:rgb(0,255,0);}"); + return; } } @@ -822,7 +878,7 @@ void HPPA::timerEvent(QTimerEvent *event) } ByteBack xMotorState = m_xMotor->GetState();//执行有问题 - std::cout << "------------------------------------------x马达速度:" << xMotorState.Speed << std::endl; + //std::cout << "------------------------------------------x马达速度:" << xMotorState.Speed << std::endl; if (xMotorState.Speed == -1000000) { @@ -1714,7 +1770,7 @@ void HPPA::onconnect() } else if (imagerSelected == "mActionPika_XC2") { - + m_Imager = new ResononPicaLImager(); } else if (imagerSelected == "mActionCorning_410") { @@ -1911,7 +1967,7 @@ void HPPA::onReference() msgBox.setText(QString::fromLocal8Bit("请确保白板放置正确!")); msgBox.exec(); - bool isMotorEnable = this->ui.mAction_is_motor_enable->isChecked(); + bool isMotorEnable = this->ui.mAction_2AxisMotor->isChecked(); if (isMotorEnable) { //移动x马达 @@ -1933,7 +1989,7 @@ void HPPA::recordWhiteFinish() { ui.mainToolBar->widgetForAction(ui.action_reference)->setStyleSheet("QWidget{background-color:rgb(0,255,0);}"); - bool isMotorEnable = this->ui.mAction_is_motor_enable->isChecked(); + bool isMotorEnable = this->ui.mAction_2AxisMotor->isChecked(); if (isMotorEnable) { //移动x马达 @@ -1948,6 +2004,7 @@ void HPPA::recordWhiteFinish() void HPPA::onPlotHyperspectralImageRgbImage() { + //return; //获取绘图控件 QWidget* currentWidget = ui.ImageViewerTabWidget->widget(m_numberOfRecording); QList currentImageViewer = currentWidget->findChildren(); @@ -2070,6 +2127,19 @@ void HPPA::onRecordFinishedSignal_WhenFrameNumberMeet() void HPPA::onRecordFinishedSignal_WhenFrameNumberNotMeet() { + QAction* checked = moveplatformActionGroup->checkedAction(); + QString checkedName; + if (checked) + { + checkedName = checked->objectName(); + } + + if (checkedName == "mAction_RobotArm")//机械臂会在停止采集后的瞬间又开始采集,导致ui.action_start_recording显示异常 + { + return; + } + + std::cout << "停止采集原因:(1)帧数没有采集够时,马达到达最大位置;(2)手动停止采集。" << std::endl; ui.action_start_recording->setText(QString::fromLocal8Bit("采集")); diff --git a/HPPA/HPPA.h b/HPPA/HPPA.h index 3f6d56b..4d65c6f 100644 --- a/HPPA/HPPA.h +++ b/HPPA/HPPA.h @@ -239,6 +239,11 @@ private: QActionGroup* mImagerGroup = nullptr; void createActionGroups(); void selectingImager(QAction* selectedAction); + QActionGroup* moveplatformActionGroup = nullptr; + void createMoveplatformActionGroup(); + void selectingMoveplatform(QAction* selectedAction); + RobotArmControl* rac; + PathPlan* m_pathPlan; @@ -322,8 +327,6 @@ public Q_SLOTS: void ontimerMoveXmotor(); void ontimerMoveYmotor(); - void onMotorSwitch(); - // void onimagerSimulatorMove(int x, int y); void OnSendLogToCallClass(QString str); @@ -336,6 +339,8 @@ public Q_SLOTS: void requestFinished(QNetworkReply* reply); + void recordFromRobotArm(int fileCounter); + signals: void StartFocusSignal(); void StartLoopSignal(); diff --git a/HPPA/HPPA.ui b/HPPA/HPPA.ui index 341a2e3..f08f8b2 100644 --- a/HPPA/HPPA.ui +++ b/HPPA/HPPA.ui @@ -6,8 +6,8 @@ 0 0 - 1318 - 882 + 1194 + 834 @@ -84,7 +84,7 @@ 0 0 - 1318 + 1194 30 @@ -166,7 +166,9 @@ color:white; 鎵弿骞冲彴 - + + + @@ -1550,18 +1552,37 @@ QDockWidget::title { 椹揪 - + true - true + false true - 寮鍚 + + + + + + true + + + 2 杞寸嚎鎬ч┈杈 + + + + + true + + + false + + + 鏈烘鑷 diff --git a/HPPA/ImagerOperationBase.cpp b/HPPA/ImagerOperationBase.cpp index 21f9133..f36ced7 100644 --- a/HPPA/ImagerOperationBase.cpp +++ b/HPPA/ImagerOperationBase.cpp @@ -287,7 +287,7 @@ void ImagerOperationBase::start_record() //在最后一次画图前需要进行一次拉伸 //m_RgbImage - emit PlotSignal();//采集完成后进行一次画图,以防采集帧数不是帧率的倍数时,画图不全 + //emit PlotSignal();//(1)采集完成后进行一次画图,以防采集帧数不是帧率的倍数时,画图不全(2)使用机械臂采集时,会在停止采集后的瞬间开始采集,会导致上次采集最后发射的此信号调用的槽函数报错,为了兼容,注释掉 if (m_iFrameCounter >= m_iFrameNumber) { diff --git a/HPPA/RobotArmControl.cpp b/HPPA/RobotArmControl.cpp index 85d5689..4f434e8 100644 --- a/HPPA/RobotArmControl.cpp +++ b/HPPA/RobotArmControl.cpp @@ -3,15 +3,13 @@ RobotArmControl::RobotArmControl(QWidget* parent): QDialog(parent) { ui.setupUi(this); - robotController = new RobotController(this); - connect(robotController, SIGNAL(commandResponse(bool, const QJsonObject)), this, SLOT(onCommandResponse(bool, const QJsonObject))); - + robotController = new RobotController(this); connect(ui.get_task_list_btn, SIGNAL(clicked()), this, SLOT(getTaskList())); connect(ui.get_pose_btn, SIGNAL(clicked()), this, SLOT(getPose())); connect(ui.connect2arm_btn, SIGNAL(clicked()), this, SLOT(connectRobotArm())); - connect(ui.execute_task_btn, SIGNAL(clicked()), this, SLOT(executeTask())); + connect(ui.execute_task_btn, SIGNAL(clicked()), this, SLOT(executeTaskWithoutHyperImager())); connect(ui.pause_task_btn, SIGNAL(clicked()), this, SLOT(pauseTask())); connect(ui.continue_task_btn, SIGNAL(clicked()), this, SLOT(continueTask())); @@ -21,7 +19,6 @@ RobotArmControl::RobotArmControl(QWidget* parent): QDialog(parent) m_pModel = new QStringListModel(ui.taskList_listView); - //将字符串列表模型设置到树形视图上 ui.taskList_listView->setModel(m_pModel); } @@ -41,32 +38,37 @@ void RobotArmControl::monitorRobotArm(const ECData& data) ui.pose_z_label->setText(QString::number(z)); } -void RobotArmControl::onCommandResponse(bool success, const QJsonObject& response) +void RobotArmControl::onCommandResponse(QString str, const QJsonObject& response) { - if (success) + //qDebug() << "response:" << response; + + QString re; + if (response.contains("result")) { - QString re; - if (response.contains("result")) - { - re = response["result"].toVariant().toString(); - ui.textEdit->append("Result: " + re); - } - else if (response.contains("error")) - { - auto errorStr = response["error"].toObject()["message"].toString(); - - ui.textEdit->append("Error: " + errorStr); - } + re = response["result"].toVariant().toString(); + ui.textEdit->append(str + " Result: " + re); + } + else if (response.contains("error")) + { + //auto delete11 = response["error"].toObject(); + //qDebug() << "response[\"error\"]:" << delete11; + + auto errorStr = response["error"].toObject()["message"].toString(); + + ui.textEdit->append(str + " Error: " + errorStr); } } void RobotArmControl::getTaskList() { - QString pythonScript = "D:\\PycharmProjects\\ELITE_ROBOTS\\get_jbi_filename.py"; + FileOperation* fileOperation = new FileOperation(); + string directory = fileOperation->getDirectoryOfExe(); + + QString pythonScript = QString::fromStdString(directory) + "\\get_jbi_filename.py"; QProcess process; - process.start("C:\\ProgramData\\anaconda3\\python.exe", QStringList() << pythonScript); + process.start("python.exe", QStringList() << pythonScript); process.waitForFinished(); QString output = process.readAllStandardOutput(); @@ -94,7 +96,9 @@ void RobotArmControl::getTaskList() void RobotArmControl::getPose() { - robotController->getRobotPose(); + QJsonObject response; + bool x = robotController->getRobotPose(response); + onCommandResponse("getPose", response); } void RobotArmControl::connectRobotArm() @@ -103,7 +107,7 @@ void RobotArmControl::connectRobotArm() robotMonitor->connectToHost("192.168.1.100"); } -void RobotArmControl::executeTask() +void RobotArmControl::executeTaskWithHyperImager() { QModelIndex index = ui.taskList_listView->currentIndex(); if (-1 == index.row()) @@ -114,17 +118,83 @@ void RobotArmControl::executeTask() } QString fileName = index.data(Qt::DisplayRole).toString(); - robotController->runJbi(fileName); + QJsonObject response; + bool x; + + x = robotController->checkJbiExist(fileName, response); + onCommandResponse("checkJbiExist", response); + if (!x) + { + return; + } + + x = robotController->setServoStatus(1, response); + onCommandResponse("setServoStatus", response); + if (!x) + { + return; + } + + x = robotController->runJbi(fileName, response, true); + onCommandResponse("runJbi", response); + if (!x) + { + return; + } +} + +void RobotArmControl::executeTaskWithoutHyperImager() +{ + QModelIndex index = ui.taskList_listView->currentIndex(); + if (-1 == index.row()) + { + //弹窗提示用户选择文件 + ui.textEdit->append("Please select file on the left!"); + return; + } + QString fileName = index.data(Qt::DisplayRole).toString(); + + QJsonObject response; + bool x; + + x = robotController->checkJbiExist(fileName, response); + onCommandResponse("checkJbiExist", response); + if (!x) + { + return; + } + + x = robotController->setServoStatus(1, response); + onCommandResponse("setServoStatus", response); + if (!x) + { + return; + } + + x = robotController->runJbi(fileName, response, false); + onCommandResponse("runJbi", response); + if (!x) + { + return; + } } void RobotArmControl::pauseTask() { - robotController->pauseTask(); + QJsonObject response; + bool x; + + x = robotController->pauseTask(response); + onCommandResponse("pauseTask", response); } void RobotArmControl::continueTask() { - robotController->continueTask(); + QJsonObject response; + bool x; + + x = robotController->continueTask(response); + onCommandResponse("continueTask", response); } @@ -134,10 +204,10 @@ void RobotArmControl::continueTask() RobotController::RobotController(QObject* parent) : QObject(parent), socket(new QTcpSocket(this)) { - connect(socket, &QTcpSocket::readyRead, this, &RobotController::onReadyRead); + //connect(socket, &QTcpSocket::readyRead, this, &RobotController::onReadyRead); - timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(getPoint())); + m_timer = new QTimer(this); + connect(m_timer, SIGNAL(timeout()), this, SLOT(getPoint())); } RobotController::~RobotController() @@ -158,7 +228,6 @@ bool RobotController::connectToRobot(const QString& ip, quint16 port) qDebug() << "Connected successfully!"; return true; - } void RobotController::disconnectFromRobot() @@ -169,10 +238,97 @@ void RobotController::disconnectFromRobot() } } +bool RobotController::processResponse(QJsonObject response, QString& result) +{ + //qDebug() << "response:" << response; + + if (response.contains("result")) + { + result = response["result"].toVariant().toString(); + + //qDebug() << "result1:" << result; + + return true; + } + else if (response.contains("error")) + { + result = response["error"].toObject()["message"].toString(); + //qDebug() << "result2:" << result; + + return false; + } +} + +bool RobotController::processResponse_getJbiState(QJsonObject response, QString& result) +{ + //qDebug() << "response:" << response; + + if (response.contains("result")) + { + QString resultStr = response["result"].toString(); + + QJsonDocument resultDoc = QJsonDocument::fromJson(resultStr.toUtf8()); + QJsonObject resultObj = resultDoc.object(); + //qDebug() << "resultObj:" << resultObj; + + int runState = resultObj["runState"].toInt(); + //qDebug() << "runState:" << runState; + + result = QString::number(runState); + //qDebug() << "result:" << result; + + return true; + } + else if (response.contains("error")) + { + result = response["error"].toObject()["message"].toString(); + + return false; + } +} + void RobotController::getPoint() { - getJbiState(); - getCurrentJobLine(); + QJsonObject response; + getJbiState(response);//0 停止状态,1 暂停状态,2 急停状态,3 运行状态,4 错误状态 + QString result; + bool x = processResponse_getJbiState(response, result); + //qDebug() << "getJbiState:" << result; + + if (result.toInt() != 3) + { + m_timer->stop(); + + m_iCurrentJbiJobLine = 0; + m_iFileCounter = 0; + + //发射信号,相机停止采集 + emit hsiRecordSignal(-1); + + return; + } + + QJsonObject response2; + getCurrentJobLine(response2); + QString result2; + bool x2 = processResponse(response2, result2); + + int m_iCurrentJbiJobLine_tmp = result2.toInt(); + + + if (m_iCurrentJbiJobLine_tmp != m_iCurrentJbiJobLine) + { + m_iFileCounter++; + + qDebug() << "Changed! CurrentJobLine:" << m_iCurrentJbiJobLine_tmp; + //发射信号,相机停止采集 + emit hsiRecordSignal(-1); + + m_iCurrentJbiJobLine = m_iCurrentJbiJobLine_tmp; + + //发射信号,相机开始采集 + emit hsiRecordSignal(m_iFileCounter); + } } void RobotController::sendCommand(const QString& cmd, const QJsonValue& params, int id) @@ -185,95 +341,151 @@ void RobotController::sendCommand(const QString& cmd, const QJsonValue& params, QJsonDocument doc(request); QByteArray data = doc.toJson(QJsonDocument::Compact) + "\n"; - qDebug() << "send command:" << data.constData(); + //qDebug() << "send command:" << data.constData(); socket->write(data); + socket->waitForBytesWritten(); } -void RobotController::onReadyRead() +bool RobotController::onReadyRead(QJsonObject& re) { QByteArray data = socket->readAll(); QJsonDocument doc = QJsonDocument::fromJson(data); if (!doc.isNull() && doc.isObject()) { - QJsonObject response = doc.object(); - //qDebug() << "Received all:" << response; + re = doc.object(); + //qDebug() << "Received all:" << re; - if (response.contains("result")) + if (re.contains("result")) { - //qDebug() << "Received result:" << response["result"].toVariant(); + //qDebug() << "Received result:" << re["result"].toVariant(); + return true; } - else if (response.contains("error")) + else if (re.contains("error")) { - //qDebug() << "Received error:" << response["error"]; + //qDebug() << "Received error:" << re["error"]; + return false; } - emit commandResponse(true, doc.object()); + //emit commandResponse(true, doc.object()); } else { - emit commandResponse(false, QJsonObject()); + //emit commandResponse(false, QJsonObject()); + + re = QJsonObject(); + return false; } } -void RobotController::getRobotPose() +bool RobotController::getRobotPose(QJsonObject& re) { sendCommand("getRobotPose"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::getRobotState() +bool RobotController::getRobotState(QJsonObject& re) { sendCommand("getRobotState"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::getJbiState() +bool RobotController::getJbiState(QJsonObject& re) { sendCommand("getJbiState"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::getCurrentJobLine() +bool RobotController::getCurrentJobLine(QJsonObject& re) { sendCommand("getCurrentJobLine"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::getRobotMode() +bool RobotController::getRobotMode(QJsonObject& re) { sendCommand("getRobotMode"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::runJbi(const QString& jbiFilename) +bool RobotController::checkJbiExist(const QString& jbiFilename, QJsonObject& re) { QJsonObject paramsRunJbi; paramsRunJbi["filename"] = jbiFilename;//使用对象结构 - QJsonObject params_set_servo_status; - params_set_servo_status["status"] = 1;//使用对象结构 + sendCommand("checkJbiExist", paramsRunJbi); + socket->waitForReadyRead(m_iTimeout); - //sendCommand("checkJbiExist", paramsRunJbi); - sendCommand("set_servo_status", params_set_servo_status); - sendCommand("runJbi", paramsRunJbi); - - //timer->start(1000); + return onReadyRead(re); } -void RobotController::pauseTask() +bool RobotController::setServoStatus(int status, QJsonObject& re) +{ + QJsonObject params_set_servo_status; + params_set_servo_status["status"] = status;//使用对象结构 + + sendCommand("set_servo_status", params_set_servo_status); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); +} + +bool RobotController::runJbi(const QString& jbiFilename, QJsonObject& re, bool isRecordHsi) +{ + QJsonObject paramsRunJbi; + paramsRunJbi["filename"] = jbiFilename;//使用对象结构 + + sendCommand("runJbi", paramsRunJbi); + socket->waitForReadyRead(m_iTimeout); + + if (isRecordHsi) + { + m_timer->start(1000); + } + + return onReadyRead(re); +} + +bool RobotController::pauseTask(QJsonObject& re) { sendCommand("pause"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::run() +bool RobotController::run(QJsonObject& re) { sendCommand("run"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::continueTask() +bool RobotController::continueTask(QJsonObject& re) { sendCommand("run"); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } -void RobotController::setRobotPowerStatus(int status) +bool RobotController::setRobotPowerStatus(int status, QJsonObject& re) { sendCommand("set_robot_power_status", QJsonArray{ status }); + socket->waitForReadyRead(m_iTimeout); + + return onReadyRead(re); } diff --git a/HPPA/RobotArmControl.h b/HPPA/RobotArmControl.h index 9548609..29497a5 100644 --- a/HPPA/RobotArmControl.h +++ b/HPPA/RobotArmControl.h @@ -18,6 +18,7 @@ #include #include "ui_RobotArmControl.h" +#include "fileOperation.h" struct ECData @@ -93,29 +94,38 @@ public: bool connectToRobot(const QString& ip, quint16 port = 8055); void disconnectFromRobot(); void sendCommand(const QString& cmd, const QJsonValue& params = QJsonArray(), int id = 1); + bool processResponse(QJsonObject response, QString& re); + bool processResponse_getJbiState(QJsonObject response, QString& result); - void getRobotPose(); - void getRobotState();//停止状态 0,暂停状态 1,急停状态 2,运行状态 3,报警状态 4,碰撞状态 5 - void getJbiState();//0 停止状态,1 暂停状态,2 急停状态,3 运行状态,4 错误状态 - void getCurrentJobLine(); - void getRobotMode();//示教模式 0,自动模式 1,远程模式 2 - void runJbi(const QString& jbiFilename); - void pauseTask(); - void run(); - void continueTask(); - void setRobotPowerStatus(int status); + bool getRobotPose(QJsonObject& re); + bool getRobotState(QJsonObject& re);//停止状态 0,暂停状态 1,急停状态 2,运行状态 3,报警状态 4,碰撞状态 5 + bool getJbiState(QJsonObject& re);//0 停止状态,1 暂停状态,2 急停状态,3 运行状态,4 错误状态 + bool getCurrentJobLine(QJsonObject& re); + bool getRobotMode(QJsonObject& re);//示教模式 0,自动模式 1,远程模式 2 + bool checkJbiExist(const QString& jbiFilename, QJsonObject& re); + bool setServoStatus(int status, QJsonObject& re); + bool runJbi(const QString& jbiFilename, QJsonObject& re, bool isRecordHsi); + bool pauseTask(QJsonObject& re); + bool run(QJsonObject& re); + bool continueTask(QJsonObject& re); + bool setRobotPowerStatus(int status, QJsonObject& re); -signals: - void commandResponse(bool success, const QJsonObject& response); - -private slots: - void onReadyRead(); - void getPoint(); + bool onReadyRead(QJsonObject& re); private: QTcpSocket* socket; - QTimer* timer; + QTimer* m_timer; + int m_iTimeout = 3000; + int m_iCurrentJbiJobLine = 0; + int m_iFileCounter = 0; + +signals: + void commandResponse(bool success, const QJsonObject& response); + void hsiRecordSignal(int); + +private slots: + void getPoint(); }; class RobotArmControl : public QDialog @@ -127,14 +137,16 @@ public: ~RobotArmControl(); RobotController* robotController; + void onCommandResponse(QString str, const QJsonObject& response); public Q_SLOTS: - void onCommandResponse(bool success, const QJsonObject& response); + void getTaskList(); void getPose(); void connectRobotArm(); - void executeTask(); + void executeTaskWithHyperImager(); + void executeTaskWithoutHyperImager(); void pauseTask(); void continueTask(); diff --git a/HPPA/about.ui b/HPPA/about.ui index 3c7ecba..2526256 100644 --- a/HPPA/about.ui +++ b/HPPA/about.ui @@ -70,7 +70,7 @@ - 鐗堟湰锛1.8.7 + 鐗堟湰锛1.8.8