修复协议输入错误系统进入错误状态

This commit is contained in:
yiyi 2025-08-06 01:09:10 +08:00
parent 77e168e253
commit a07ccc6373
4 changed files with 175 additions and 172 deletions

View File

@ -82,6 +82,10 @@ public:
static void _StaticCameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);
static void _StaticDetectionCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint, void* pParam);
private:
// 相机协议相关方法
int InitCamera(std::vector<DeviceInfo>& cameraList);
// 机械臂协议相关方法
int InitRobotProtocol();
@ -102,7 +106,11 @@ private:
// 连接状态检查和更新
void CheckAndUpdateWorkStatus();
int _OpenDevice(int cameraIndex, const char* cameraName, const char* cameraIp, ProjectType& projectType, IVrEyeDevice** pDevice = nullptr);
// 打开相机
int _OpenDevice(int cameraIndex, const char* cameraName, const char* cameraIp, ProjectType& projectType);
bool _SinglePreDetection(int cameraIndex);
int _SingleDetection(int cameraIndex, bool isStart);
// 静态回调函数,供外部使用
void _CameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);

View File

@ -128,130 +128,7 @@ int GrabBagPresenter::Init()
LOG_INFO("Algorithm parameters initialization successful\n");
}
// 通知UI相机个数
int cameraCount = configResult.cameraList.size();
// 初始化相机列表,预分配空间
m_vrEyeDeviceList.resize(cameraCount, std::make_pair("", nullptr));
for(int i = 0; i < cameraCount; i++)
{
m_vrEyeDeviceList[i] = std::make_pair(configResult.cameraList[i].name, nullptr);
}
m_pStatus->OnCameraCountChanged(cameraCount);
if(cameraCount > 0){
if (cameraCount >= 1) {
// 尝试打开相机1
LOG_INFO("Attempting to connect to camera 1 with IP: %s\n", configResult.cameraList[0].ip.c_str());
// 发送页面提示信息
m_pStatus->OnStatusUpdate(QString("正在连接%1: %2").arg(configResult.cameraList[0].name.c_str()).arg(configResult.cameraList[0].ip.c_str()).toStdString());
// 初始化VrEyeDevice模块
IVrEyeDevice* pDevice = nullptr;
IVrEyeDevice::CreateObject(&pDevice);
nRet = pDevice->InitDevice();
ERR_CODE_RETURN(nRet);
// 先设置状态回调
nRet = pDevice->SetStatusCallback(_StaticCameraNotify, this);
ERR_CODE_RETURN(nRet);
// 再打开设备
nRet = pDevice->OpenDevice(configResult.cameraList[0].ip.c_str(), ProjectType::DirectBag == m_projectType);
// 通过回调更新相机1状态
bool camera1Connected = (SUCCESS == nRet);
if(camera1Connected){
LOG_INFO("Camera 1 connection successful, IP: %s\n", configResult.cameraList[0].ip.c_str());
m_pStatus->OnStatusUpdate(QString("%1连接成功").arg(configResult.cameraList[0].name.c_str()).toStdString());
} else {
LOG_ERROR("Camera 1 connection failed, IP: %s, error code: %d\n", configResult.cameraList[0].ip.c_str(), nRet);
m_pStatus->OnStatusUpdate(QString("%1连接失败: %2").arg(configResult.cameraList[0].name.c_str()).arg(nRet).toStdString());
delete pDevice; // 释放失败的设备
pDevice = nullptr;
}
m_vrEyeDeviceList[0] = std::make_pair(configResult.cameraList[0].name, pDevice); // 直接存储到索引0
m_pStatus->OnCamera1StatusChanged(camera1Connected);
m_pStatus->OnStatusUpdate(camera1Connected ? QString("%1连接成功").arg(configResult.cameraList[0].name.c_str()).toStdString() : QString("%1连接失败").arg(configResult.cameraList[0].name.c_str()).toStdString());
m_bCameraConnected = camera1Connected;
}
else {
LOG_WARNING("Camera count is 0, setting camera 1 as disconnected\n");
m_pStatus->OnStatusUpdate("配置文件中未找到相机配置,设置相机为离线状态");
m_pStatus->OnCamera1StatusChanged(false);
m_bCameraConnected = false;
}
if (cameraCount >= 2) {
IVrEyeDevice* pDevice = nullptr;
IVrEyeDevice::CreateObject(&pDevice);
nRet = pDevice->InitDevice();
ERR_CODE_RETURN(nRet);
// 先设置状态回调
nRet = pDevice->SetStatusCallback(_StaticCameraNotify, this);
ERR_CODE_RETURN(nRet);
// 尝试打开相机2
nRet = pDevice->OpenDevice(configResult.cameraList[1].ip.c_str(), ProjectType::DirectBag == m_projectType);
// 通过回调更新相机2状态
bool camera2Connected = (SUCCESS == nRet);
if(camera2Connected)
{
LOG_INFO("Camera 2 connection successful, IP: %s\n", configResult.cameraList[1].ip.c_str());
} else {
LOG_ERROR("Camera 2 connection failed, IP: %s, error code: %d\n", configResult.cameraList[1].ip.c_str(), nRet);
delete pDevice; // 释放失败的设备
pDevice = nullptr;
}
m_vrEyeDeviceList[1] = std::make_pair(configResult.cameraList[1].name, pDevice); // 直接存储到索引1
m_pStatus->OnCamera2StatusChanged(camera2Connected);
m_pStatus->OnStatusUpdate(camera2Connected ? QString("%1连接成功").arg(configResult.cameraList[1].name.c_str()).toStdString() : QString("%1连接失败").arg(configResult.cameraList[1].name.c_str()).toStdString());
// 只要有一个相机连接成功就认为相机连接正常
if (camera2Connected) {
m_bCameraConnected = true;
}
}
else {
// 如果只有一个相机则将相机2状态设为未连接
m_pStatus->OnCamera2StatusChanged(false);
}
} else {
IVrEyeDevice* pDevice = nullptr;
IVrEyeDevice::CreateObject(&pDevice);
nRet = pDevice->InitDevice();
ERR_CODE_RETURN(nRet);
// 先设置状态回调
nRet = pDevice->SetStatusCallback(&GrabBagPresenter::_StaticCameraNotify, this);
LOG_DEBUG("SetStatusCallback result: %d\n", nRet);
ERR_CODE_RETURN(nRet);
// 尝试打开相机1
nRet = pDevice->OpenDevice(nullptr, ProjectType::DirectBag == m_projectType);
// 通过回调更新相机1状态
bool camera1Connected = (SUCCESS == nRet);
if(camera1Connected)
{
m_vrEyeDeviceList.resize(1);
m_vrEyeDeviceList[0] = std::make_pair("相机", pDevice); // 直接存储到索引0
} else {
delete pDevice; // 释放失败的设备
}
m_pStatus->OnCamera1StatusChanged(camera1Connected);
m_pStatus->OnStatusUpdate(camera1Connected ? "相机连接成功" : "相机连接失败");
m_bCameraConnected = camera1Connected;
}
// 设置默认相机索引为第一个连接的相机
m_currentCameraIndex = 1; // 默认从1开始
for (int i = 0; i < static_cast<int>(m_vrEyeDeviceList.size()); i++) {
if (m_vrEyeDeviceList[i].second != nullptr) {
m_currentCameraIndex = i + 1; // 找到第一个连接的相机
break;
}
}
InitCamera(configResult.cameraList);
LOG_INFO("Camera initialization completed. Connected cameras: %zu, default camera index: %d\n",
m_vrEyeDeviceList.size(), m_currentCameraIndex);
@ -291,6 +168,53 @@ int GrabBagPresenter::Init()
return SUCCESS;
}
// 相机协议相关方法
int GrabBagPresenter::InitCamera(std::vector<DeviceInfo>& cameraList)
{
// 通知UI相机个数
int cameraCount = cameraList.size();
// 初始化相机列表,预分配空间
m_vrEyeDeviceList.resize(cameraCount, std::make_pair("", nullptr));
for(int i = 0; i < cameraCount; i++)
{
m_vrEyeDeviceList[i] = std::make_pair(cameraList[i].name, nullptr);
}
m_pStatus->OnCameraCountChanged(cameraCount);
if(cameraCount > 0){
if (cameraCount >= 1) {
// 尝试打开相机
int nRet = _OpenDevice(1, cameraList[0].name.c_str(), cameraList[0].ip.c_str(), m_projectType);
m_pStatus->OnCamera1StatusChanged(nRet == SUCCESS);
m_bCameraConnected = (nRet == SUCCESS);
}
if (cameraCount >= 2) {
// 尝试打开相机
int nRet = _OpenDevice(2, cameraList[1].name.c_str(), cameraList[1].ip.c_str(), m_projectType);
m_pStatus->OnCamera2StatusChanged(nRet == SUCCESS);
m_bCameraConnected = (nRet == SUCCESS);
}
} else {
m_vrEyeDeviceList.resize(1, std::make_pair("", nullptr));
_OpenDevice(1, "相机", nullptr, m_projectType);
}
// 设置默认相机索引为第一个连接的相机
m_currentCameraIndex = 1; // 默认从1开始
for (int i = 0; i < static_cast<int>(m_vrEyeDeviceList.size()); i++) {
if (m_vrEyeDeviceList[i].second != nullptr) {
m_currentCameraIndex = i + 1; // 找到第一个连接的相机
break;
}
}
return SUCCESS;
}
// 初始化机械臂协议
int GrabBagPresenter::InitRobotProtocol()
{
@ -598,27 +522,19 @@ void GrabBagPresenter::OnRobotConnectionChanged(bool connected)
bool GrabBagPresenter::OnRobotWorkSignal(bool startWork, int cameraIndex)
{
LOG_INFO("Received robot work signal: %s for camera index: %d\n", (startWork ? "start work" : "stop work"), cameraIndex);
int nRet = SUCCESS;
if (startWork) {
QString cameraName = (cameraIndex >= 1 && cameraIndex <= static_cast<int>(m_vrEyeDeviceList.size())) ?
QString::fromStdString(m_vrEyeDeviceList[cameraIndex - 1].first) : QString("相机%1").arg(cameraIndex);
QString message = QString("收到信号,启动%1检测").arg(cameraName);
if (nullptr != m_pStatus) {
m_pStatus->OnStatusUpdate(message.toStdString());
}
nRet = StartDetection(cameraIndex);
} else {
QString cameraName = (cameraIndex >= 1 && cameraIndex <= static_cast<int>(m_vrEyeDeviceList.size())) ?
QString::fromStdString(m_vrEyeDeviceList[cameraIndex - 1].first) : QString("相机%1").arg(cameraIndex);
QString message = QString("收到信号,停止%1检测").arg(cameraName);
if (nullptr != m_pStatus) {
m_pStatus->OnStatusUpdate(message.toStdString());
}
nRet = StopDetection();
}
if(!_SinglePreDetection(cameraIndex)){
return false;
}
int nRet = _SingleDetection(cameraIndex, startWork);
if(nRet != SUCCESS){
m_currentWorkStatus = WorkStatus::Ready;
if (m_pStatus) {
m_pStatus->OnWorkStatusChanged(WorkStatus::Error);
}
}
return nRet == SUCCESS;
}
@ -648,9 +564,10 @@ int GrabBagPresenter::StartDetection(int cameraIdx, bool isAuto)
}
int cameraIndex = m_currentCameraIndex;
m_currentWorkStatus = WorkStatus::Working;
// 通知UI工作状态变更为"正在工作"
if (m_pStatus) {
m_currentWorkStatus = WorkStatus::Working;
m_pStatus->OnWorkStatusChanged(WorkStatus::Working);
}
@ -678,8 +595,7 @@ int GrabBagPresenter::StartDetection(int cameraIdx, bool isAuto)
int arrayIndex = cameraIndex - 1; // 转换为数组索引从0开始
// 检查相机是否连接
if (arrayIndex < 0 || arrayIndex >= static_cast<int>(m_vrEyeDeviceList.size()) ||
m_vrEyeDeviceList[arrayIndex].second == nullptr) {
if (arrayIndex < 0 || arrayIndex >= static_cast<int>(m_vrEyeDeviceList.size()) || m_vrEyeDeviceList[arrayIndex].second == nullptr) {
LOG_ERROR("Camera %d is not connected\n", cameraIndex);
QString cameraName = (arrayIndex >= 0 && arrayIndex < static_cast<int>(m_vrEyeDeviceList.size())) ?
QString::fromStdString(m_vrEyeDeviceList[arrayIndex].first) : QString("相机%1").arg(cameraIndex);
@ -822,6 +738,89 @@ void GrabBagPresenter::SetCameraStatusCallback(VzNL_OnNotifyStatusCBEx fNotify,
}
}
// 打开相机
int GrabBagPresenter::_OpenDevice(int cameraIndex, const char* cameraName, const char* cameraIp, ProjectType& projectType)
{
IVrEyeDevice* pDevice = nullptr;
IVrEyeDevice::CreateObject(&pDevice);
int nRet = pDevice->InitDevice();
ERR_CODE_RETURN(nRet);
// 先设置状态回调
nRet = pDevice->SetStatusCallback(&GrabBagPresenter::_StaticCameraNotify, this);
LOG_DEBUG("SetStatusCallback result: %d\n", nRet);
ERR_CODE_RETURN(nRet);
// 尝试打开相机1
nRet = pDevice->OpenDevice(cameraIp, ProjectType::DirectBag == projectType);
// 通过回调更新相机1状态
bool cameraConnected = (SUCCESS == nRet);
if(!cameraConnected){
delete pDevice; // 释放失败的设备
pDevice = nullptr;
}
int arrIdx = cameraIndex - 1;
if(m_vrEyeDeviceList.size() > arrIdx){
m_vrEyeDeviceList[arrIdx] = std::make_pair(cameraName, pDevice); // 直接存储到索引0
}
m_pStatus->OnCamera1StatusChanged(cameraConnected);
m_pStatus->OnStatusUpdate(cameraConnected ? "相机连接成功" : "相机连接失败");
m_bCameraConnected = cameraConnected;
return nRet;
}
// 判断是否可以开始检测
bool GrabBagPresenter::_SinglePreDetection(int cameraIndex)
{
if(m_vrEyeDeviceList.empty()){
LOG_ERROR("No camera device found\n");
if (nullptr != m_pStatus) {
m_pStatus->OnStatusUpdate("未找到相机设备");
}
return false;
}
if(cameraIndex < 1 || cameraIndex > static_cast<int>(m_vrEyeDeviceList.size())){
LOG_ERROR("Invalid camera index: %d, valid range: 1-%zu\n", cameraIndex, m_vrEyeDeviceList.size());
return false;
}
if(m_vrEyeDeviceList[cameraIndex - 1].second == nullptr){
LOG_ERROR("Camera %d is not connected\n", cameraIndex);
return false;
}
return true;
}
int GrabBagPresenter::_SingleDetection(int cameraIndex, bool isStart)
{
int nRet = SUCCESS;
if (isStart) {
QString cameraName = (cameraIndex >= 1 && cameraIndex <= static_cast<int>(m_vrEyeDeviceList.size())) ?
QString::fromStdString(m_vrEyeDeviceList[cameraIndex - 1].first) : QString("相机%1").arg(cameraIndex);
QString message = QString("收到信号,启动%1检测").arg(cameraName);
if (nullptr != m_pStatus) {
m_pStatus->OnStatusUpdate(message.toStdString());
}
nRet = StartDetection(cameraIndex);
} else {
QString cameraName = (cameraIndex >= 1 && cameraIndex <= static_cast<int>(m_vrEyeDeviceList.size())) ?
QString::fromStdString(m_vrEyeDeviceList[cameraIndex - 1].first) : QString("相机%1").arg(cameraIndex);
QString message = QString("收到信号,停止%1检测").arg(cameraName);
if (nullptr != m_pStatus) {
m_pStatus->OnStatusUpdate(message.toStdString());
}
nRet = StopDetection();
}
return nRet;
}
// 静态回调函数实现
void GrabBagPresenter::_StaticCameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam)
{
@ -1379,30 +1378,21 @@ void GrabBagPresenter::OnSerialConnectionChanged(bool connected)
bool GrabBagPresenter::OnSerialWorkSignal(bool startWork, int cameraIndex)
{
LOG_INFO("Serial work signal received: %s, camera index: %d\n",
startWork ? "start" : "stop", cameraIndex);
LOG_INFO("Serial work signal received: %s, camera index: %d\n", startWork ? "start" : "stop", cameraIndex);
if (startWork) {
// 开始检测
int result = StartDetection(cameraIndex, true);
if (result == SUCCESS) {
LOG_INFO("Started detection via serial signal for camera %d\n", cameraIndex);
return true;
} else {
LOG_ERROR("Failed to start detection via serial signal for camera %d, error: %d\n", cameraIndex, result);
return false;
}
} else {
// 停止检测
int result = StopDetection();
if (result == SUCCESS) {
LOG_INFO("Stopped detection via serial signal\n");
return true;
} else {
LOG_ERROR("Failed to stop detection via serial signal, error: %d\n", result);
return false;
if(!_SinglePreDetection(cameraIndex)){
return false;
}
int nRet = _SingleDetection(cameraIndex, startWork);
if(nRet != SUCCESS){
m_currentWorkStatus = WorkStatus::Ready;
if (m_pStatus) {
m_pStatus->OnWorkStatusChanged(WorkStatus::Error);
}
}
return nRet == SUCCESS;
}
// 设置默认相机索引

View File

@ -3,8 +3,8 @@
#define GRABBAG_VERSION_STRING "1.1.2"
#define GRABBAG_BUILD_STRING "1"
#define GRABBAG_FULL_VERSION_STRING "V1.1.2_1"
#define GRABBAG_BUILD_STRING "2"
#define GRABBAG_FULL_VERSION_STRING "V1.1.2_2"
// 获取版本信息的便捷函数
inline const char* GetGrabBagVersion() {

View File

@ -1,5 +1,10 @@
# 1.1.2 2025-08-05
1. 更換opencv320
## build_2
1. 修复协议输入错误的cameraid 导致系统进入异常无法再触发
## build_1
1. 更換opencv320
# 1.1.1 2025-7-30
1. 缓存数据存储格式修改