Files
952-11/HanddleAI/HanddleAi/back/triggerManager.cpp
T
2024-10-24 14:41:35 +08:00

241 lines
9.2 KiB
C++

#include <triggerManager.h>
#include <threadPoolManager.h>
#include <backStruct.h>
#include <cameraManager.h>
#include <uiModule/uiModule_switchProduct.h>
#include <appCore.hpp>
#include <detProcess.hpp>
#include <lightSource.hpp>
#define debugTesting 0
void triggerManager::initialization(detProcess* _detProcessIn)
{
_detProcess = _detProcessIn;
dManager = &_detProcessIn->_detDataManager;
_cameraManager = &_detProcessIn->_cameraManager;
_threadManager = &_detProcessIn->_threadManager;
_lightSourceManager = &_detProcessIn->_lightSourceManager;
startPos = _core._appConfig._triggerStruct.startPos;
triggerFileName = QString::fromStdString(_core._appConfig._triggerStruct.moduleName);
IpAndPort = QString::fromStdString(_core._appConfig._triggerStruct.initStr);
readAllCmd = QByteArray::fromStdString(_core._appConfig._triggerStruct.readAllCmd);
triggerFileName = QString::fromStdString(_core._appConfig._triggerStruct.moduleName);
sleepTime = _core._appConfig._triggerStruct.sleepTime;
readNum = _core._appConfig._triggerStruct.readNum;
writeType = _core._appConfig._triggerStruct.writeType;
}
void triggerManager::run()
{
if(connectHost() == false)
return;
QThread::msleep(3000);
int cnt = 0;
QString heartbeatCmd = QString::fromStdString(_core._appConfig._triggerStruct.heartCmd);
while (runFlag)
{
reciveCommond();
QThread::msleep(10);
//心跳
cnt++;
if (cnt > 30)
{
sendFinsMsg(heartbeatCmd);
// qDebug() << "heartbeat";
}
}
}
bool triggerManager::connectHost()
{
if (init == true)
return true;
status = NotInit;
m_lib = new QLibrary(triggerFileName);
libLoadedflag = m_lib->load();
if (!m_lib->isLoaded())
{
emit _core._qtFunc.sendErrorToMainwindow(dllNotFound);
emit _core._qtFunc.sendInitString("找不到" + triggerFileName);
return false;
}
if (libLoadedflag)
{
CreateFunction createFun = (CreateFunction)m_lib->resolve("create");
ReleaseFunction releaseFun = (ReleaseFunction)m_lib->resolve("destory");
bool initFailure = false;
if (createFun && releaseFun)
{
trigger = createFun();
destory_finstcp = releaseFun;
QByteArray initByte =
QByteArray::fromStdString
("sleeptime_"+std::to_string(sleepTime)
+ "_readnum_" + std::to_string(readNum)
+ "_writetype_" + writeType);
trigger->reciveData(vec_mat, QByteArray(IpAndPort.toUtf8()));
emit _core._qtFunc.sendInitString(triggerFileName + "连接到" + IpAndPort);
initFailure = trigger->initParas(initByte);
}
if (initFailure == false)
{
emit _core._qtFunc.sendErrorToMainwindow(triggerInitFailure);
delete m_lib;
m_lib = nullptr;
emit _core._qtFunc.sendInitString(triggerFileName + "初始化失败");
status = InitFailure;
return false;
}
else
{
status = InitOK;
init = true;
emit _core._qtFunc.sendInitString(triggerFileName + "成功");
return true;
}
}
return false;
}
void triggerManager::reciveCommond()
{
//auto start = std::chrono::high_resolution_clock::now();
if (!sendFinsMsg(readAllCmd))
qCritical() << "[TCP] " << " finstcp_ptr reciveData error!";
QByteArray readByte;
if (!reciveFinsMsg(readByte))
qCritical() << "[TCP] " << " finstcp_ptr sendData error!";
tLocker.reFreshLocker(readByte);//经过检查后再触发
decodeCameraCMD(readByte);
}
void triggerManager::decodeCameraCMD(QByteArray byte)
{
#if debugTesting
//{
correctionData = new correctionStruct();
//correctionData->in_X_X0Y = { 536.37,657.3,351.32 };
//correctionData->in_Y_X0Y = { -1383.29,-1234.18,-1242.13 };
//correctionData->in_Z_X0Y = { -131.51,-131.51,-131.51 };
//correctionData->in_Z_X0Y_Value = { -78.6552,-78.6552,-78.6552 };
//correctionData->in_X_X0Z = { 615.24,191.31,191.5 };
//correctionData->in_Y_X0Z = { -372.66,-372.66,-372.66 };
//correctionData->in_Z_X0Z = { -925.76,-928.33,-1001.64 };
//correctionData->in_Y_X0Z_Value = { -79.4859,-79.4859,-79.4859 };
//correctionData->in_X_Y0Z = { -213.89,-213.89,-213.89 };
//correctionData->in_Y_Y0Z = { -1006.12,-1083.8,-1005.4 };
//correctionData->in_Z_Y0Z = { -602.89,-620.53,-740.52 };
//correctionData->in_X_Y0Z_Value = { 80.9952,80.9952,80.9952 };
correctionData->in_X_X0Y = { 536.37,657.3,351.32 };
correctionData->in_Y_X0Y = {-1383.29,-1234.18,-1242.13 };
correctionData->in_Z_X0Y = { -131.51,-130.61,-130.66 };
correctionData->in_Z_X0Y_Value = { -78.6552,-82.3836,-83.4522 };
correctionData->in_X_X0Z = { 615.24,191.31,191.5};
correctionData->in_Y_X0Z = { -372.66,-370.9,-370.18};
correctionData->in_Z_X0Z = { -925.76,-928.33,-1001.64 };
correctionData->in_Y_X0Z_Value = { -79.4859,-79.4859,-79.8369};
correctionData->in_X_Y0Z = { -213.89,-209.83,-212.4 };
correctionData->in_Y_Y0Z = { -1006.12,-1083.8,-1005.4 };
correctionData->in_Z_Y0Z = { -602.89,-620.53,-740.52 };
correctionData->in_X_Y0Z_Value = { 80.9952,84.006,81.1005 };
// correctionData = new correctionStruct();
//correctionData->in_X_X0Y = { -258.71,-447.8,-447.2 };
//correctionData->in_Y_X0Y = { -1099.67,-1133.42,-938.83 };
//correctionData->in_Z_X0Y = { 711.77,711.62,711.730000 };
//correctionData->in_Z_X0Y_Value = { 63.9795,64.5333,65.859300 };
//correctionData->in_X_X0Z = { -402.3,-455.91,-236.75 };
//correctionData->in_Y_X0Z = { -539.46,-536.21,-536.2 };
//correctionData->in_Z_X0Z = { -418.88,425.12,425.13 };
//correctionData->in_Y_X0Z_Value = { 118.15,-228.19,-256.5 };
//correctionData->in_X_Y0Z = { 395.250000,395.23,328.34 };
//correctionData->in_Y_Y0Z = { -1147.29,-1147.28,-940.82 };
//correctionData->in_Z_Y0Z = { 118.15,-228.19,-256.5 };
//correctionData->in_X_Y0Z_Value = { -36.652200,-36.7302,41.0436 };
std::shared_ptr <detData> data = dManager->insertDetData("181964", "TEST", 0, 0);
data->_productDataStruct = _appConfig->productDatas[0];
data->imgAmountMin = 0;
data->correctionData = correctionData;
_threadManager->addTask(data);//添加任务
correctionData = nullptr;
//}
//if (checkTrigger(1, 2, byte) == true)
{
//std::shared_ptr <detData> data = dManager->insertDetData("Camera1" , "TEST", 0 , 0);
//data->correctionData = correctionData;
//_threadManager->addTask(data);//添加任务
//auto camera = _cameraManager->getCamera("123");
//if(camera != nullptr)
// camera->captureImage(data);
}
//if (checkTrigger(2, 2, byte) == true)
{
//std::shared_ptr <detData> data = dManager->insertDetData("Camera1", "TEST", 1, 1);
//data->correctionData = correctionData;
//data->oriImgs.push_back(cv::imread("E://Cisco2-2024//TestImg//HFFGG6534_1_1_Battlestar20240326_180201.bmp",-1));
//_threadManager->addTask(data);//添加任务
//auto camera = _cameraManager->getCamera("123");
//if (camera != nullptr)
// camera->captureImage(data);
}
//auto camera = _cameraManager->getCamera("181964");
//if (camera != nullptr)
// camera->trigger();
//double rangeData = _cameraManager->cameras.find("181964")->second->getCameraValue();
QThread::msleep(10000000);
return;
#endif
#if debugTesting == 0
if (byte.isEmpty())
return;
double cameraData = 0;
//算法测试
#if 0
if (checkTrigger(1, 1, byte) == true)
{
correctionData = new correctionStruct();
correctionData->in_X_X0Y = { -258.71,-447.8,-447.2 };
correctionData->in_Y_X0Y = { -1099.67,-1133.42,-938.83 };
correctionData->in_Z_X0Y = { 711.77,711.62,711.730000 };
correctionData->in_Z_X0Y_Value = { 63.9795,64.5333,65.859300 };
correctionData->in_X_X0Z = { -402.3,-455.91,-236.75 };
correctionData->in_Y_X0Z = { -539.46,-536.21,-536.2 };
correctionData->in_Z_X0Z = { -418.88,425.12,425.13 };
correctionData->in_Y_X0Z_Value = { 118.15,-228.19,-256.5 };
correctionData->in_X_Y0Z = { 395.250000,395.23,328.34 };
correctionData->in_Y_Y0Z = { -1147.29,-1147.28,-940.82 };
correctionData->in_Z_Y0Z = { 118.15,-228.19,-256.5 };
correctionData->in_X_Y0Z_Value = { -36.652200,-36.7302,41.0436 };
std::shared_ptr <detData> data = dManager->insertDetData("Camera1", "correctionDataThread", "TEST", 0);
data->correctionData = correctionData;
_threadManager->addTask(data);//添加任务
correctionData = nullptr;
}
#endif
#endif
checkCallbackAndTrigger(byte);
}
int triggerManager::getValue(int dmPos, QByteArray byte)
{
int triggerPos = (dmPos - startPos) * 2;
if (byte.size() < triggerPos)
return false;
int val1 = byte.at(triggerPos) * 256;
int val2 = static_cast<unsigned char>(byte.at(triggerPos + 1));
return val1 + val2;
}