添加心跳检测、修改产量统计逻辑、添加删除ng、ng_result图片功能

CigaretteSH
CJ980606 6 months ago
parent 43303c2842
commit c36f7190a6

@ -86,7 +86,7 @@ void CaptureThread::process(void)
mvIMPACT::acquire::Statistics statistics(pDev_); mvIMPACT::acquire::Statistics statistics(pDev_);
mvIMPACT::acquire::Request* pRequest = 0; mvIMPACT::acquire::Request* pRequest = 0;
mvIMPACT::acquire::Request* pPreviousRequest = nullptr; mvIMPACT::acquire::Request* pPreviousRequest = nullptr;
const unsigned int timeout_ms = 100; const unsigned int timeout_ms = 10;
Ready = true; Ready = true;
while (!boTerminated_) while (!boTerminated_)
{ {

@ -123,7 +123,7 @@ void CaptureThreadBasler::process(void)
#endif #endif
#endif #endif
const unsigned int timeout_ms = 100; const unsigned int timeout_ms = 10;
// This smart pointer will receive the grab result data. // This smart pointer will receive the grab result data.
Pylon::CGrabResultPtr ptrGrabResult; Pylon::CGrabResultPtr ptrGrabResult;

@ -294,7 +294,7 @@ void CaptureThreadHIK::process(void)
while (!boTerminated_)//循环取流,拍照取图 while (!boTerminated_)//循环取流,拍照取图
{ {
//nRet = MV_CC_GetOneFrameTimeout(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 200); //nRet = MV_CC_GetOneFrameTimeout(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 200);
nRet = MV_CC_GetImageForBGR(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 100); nRet = MV_CC_GetImageForBGR(CamHandle, g_pImage_buf, nDataSize, &stFrameInfo, 10);
if (MV_OK == nRet) if (MV_OK == nRet)
{ {
m_cntGrabbedImages++; m_cntGrabbedImages++;

@ -47,7 +47,10 @@ void CleanWorkThread::doWork()
LPCWSTR strDriver = (LPCWSTR)iDriver.utf16(); LPCWSTR strDriver = (LPCWSTR)iDriver.utf16();
ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace; ULARGE_INTEGER freeDiskSpaceAvailable, totalDiskSpace, totalFreeDiskSpace;
qint64 gb = (1024 * 1024 * 1024); qint64 gb = (1024 * 1024 * 1024);
QString dir(g_conf_path.save_pics_path + "/ALL"); QString dir(g_conf_path.save_pics_path);
QString dirALL(g_conf_path.save_pics_path + "/ALL");
QString dirNg(g_conf_path.save_pics_path + "/ng");
QString dirNg_result(g_conf_path.save_pics_path + "/ng_result");
if (delSelection == 2) if (delSelection == 2)
{ {
@ -58,6 +61,9 @@ void CleanWorkThread::doWork()
qint64 delDays = g_sys_conf.save_days;/// qint64 delDays = g_sys_conf.save_days;///
qint64 freeSize = g_sys_conf.freesize; qint64 freeSize = g_sys_conf.freesize;
CleanImageFile(dir, delDays--); CleanImageFile(dir, delDays--);
CleanImageFile(dirALL, delDays--);
CleanImageFile(dirNg, delDays--);
CleanImageFile(dirNg_result, delDays--);
//调用函数获取磁盘参数(单位为字节Byte),转化为GB需要除以(1024*1024*1024) //调用函数获取磁盘参数(单位为字节Byte),转化为GB需要除以(1024*1024*1024)
GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace); GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace);
spaceSize = ((qint64)totalFreeDiskSpace.QuadPart * (100 / freeSize)) / (qint64)totalDiskSpace.QuadPart; spaceSize = ((qint64)totalFreeDiskSpace.QuadPart * (100 / freeSize)) / (qint64)totalDiskSpace.QuadPart;
@ -65,6 +71,9 @@ void CleanWorkThread::doWork()
//delDays = g_sys_conf.save_days; //delDays = g_sys_conf.save_days;
while ((spaceSize < 1) && (delDays > 1)) { //磁盘剩余空间小于百分之十 while ((spaceSize < 1) && (delDays > 1)) { //磁盘剩余空间小于百分之十
CleanImageFile(dir, delDays--); CleanImageFile(dir, delDays--);
CleanImageFile(dirALL, delDays--);
CleanImageFile(dirNg, delDays--);
CleanImageFile(dirNg_result, delDays--);
GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace); GetDiskFreeSpaceEx(strDriver, &freeDiskSpaceAvailable, &totalDiskSpace, &totalFreeDiskSpace);
spaceSize = ((qint64)totalFreeDiskSpace.QuadPart * (100 / freeSize)) / (qint64)totalDiskSpace.QuadPart; spaceSize = ((qint64)totalFreeDiskSpace.QuadPart * (100 / freeSize)) / (qint64)totalDiskSpace.QuadPart;
} }

@ -56,7 +56,10 @@ bool PLCDevice::init_plc_tcp(PLCDevice* PLCptr)
} }
#endif #endif
#ifdef __ModebusClient #ifdef __ModebusClient
PLCptr->g_modbus = modbus_new_tcp("192.168.1.176", 4444); const char* ip = g_sys_conf.TcpIP.data();
qDebug() << "Client ip:" << ip;
qDebug() << "Client port:" << g_sys_conf.TcpPort;
PLCptr->g_modbus = modbus_new_tcp(ip, g_sys_conf.TcpPort);
modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位flag=1时显示modbus消息的字节 modbus_set_debug(PLCptr->g_modbus, 0); // 用flag设置debug调试标志位flag=1时显示modbus消息的字节
modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时 modbus_set_response_timeout(PLCptr->g_modbus, 1, 0); // 设置响应超时
// 侦听主站连接 // 侦听主站连接

@ -273,6 +273,7 @@ Cigarette::Cigarette(QWidget* parent)
g_modbus_conf.debug = 0; g_modbus_conf.debug = 0;
g_modbus_conf.reset = 0; g_modbus_conf.reset = 0;
g_modbus_conf.alarm = 0; g_modbus_conf.alarm = 0;
g_modbus_conf.heart = 0;
read_modbus_config(g_modbus_conf); //初始化modbus地址 read_modbus_config(g_modbus_conf); //初始化modbus地址
//保存图片线程 //保存图片线程
@ -483,6 +484,11 @@ Cigarette::Cigarette(QWidget* parent)
//报警标签双击消警 //报警标签双击消警
connect(ui.label_alarm, SIGNAL(QlabelDoubleClick()), this, SLOT(OnCancelAlarm()));// connect(ui.label_alarm, SIGNAL(QlabelDoubleClick()), this, SLOT(OnCancelAlarm()));//
//心跳检测
heartbeat_pTimer = new QTimer(this);
connect(heartbeat_pTimer, SIGNAL(timeout()), this, SLOT(heartbeat()));
heartbeat_pTimer->start(5000);
//无参对有参需要利用信号转发器QSignalMapper(this) //无参对有参需要利用信号转发器QSignalMapper(this)
//信号转发器 //信号转发器
image_lable_DBsignalMapper0 = new QSignalMapper(this); image_lable_DBsignalMapper0 = new QSignalMapper(this);
@ -1585,6 +1591,12 @@ void Cigarette::admin_timeout()
} }
} }
//心跳检测
void Cigarette::heartbeat()
{
int ret = m_PLCDevice->write_bit_2_plc(g_modbus_conf.heart, 1);
}
void Cigarette::handleTimeout()//获取时间读取PLC产量、剔除数检测相机状态读取PLC报警信息 void Cigarette::handleTimeout()//获取时间读取PLC产量、剔除数检测相机状态读取PLC报警信息
{ {
QDateTime ts_start = QDateTime::currentDateTime(); QDateTime ts_start = QDateTime::currentDateTime();
@ -2699,6 +2711,10 @@ bool Cigarette::read_modbus_config(ModbusConf& conf)
{ {
conf.alarm = atoi(line.substr(pos + 1).c_str()); conf.alarm = atoi(line.substr(pos + 1).c_str());
} }
else if (tmp_key == "HEART")
{
conf.heart = atoi(line.substr(pos + 1).c_str());
}
} }
} }
cfg_file.close(); cfg_file.close();
@ -3703,39 +3719,70 @@ void Cigarette::record_output_statistic(qint64 quantity, int Kick[NumberOfSuppor
QString file_name, file_path; QString file_name, file_path;
for (int i = 0; i < NumberOfSupportedCameras; i++) for (int i = 0; i < NumberOfSupportedCameras; i++)
{ {
if (SingleCamInfo[i].IsOpen && quantity > 0) { if (SingleCamInfo[i].IsOpen) //&& quantity > 0
{
std::fstream cfg_file; std::fstream cfg_file;
file_name = QString(STATISTIC_FILE).arg(i); file_name = QString(STATISTIC_FILE).arg(i);
file_path = g_conf_path.config_path + "/" + file_name; file_path = g_conf_path.config_path + "/" + file_name;
char buf[256]; char buf[256];
memset(buf, 0, 256);
sprintf(buf, "%s", file_path.toLocal8Bit().constData());
cfg_file.open(buf, std::ios::app);
if (cfg_file.good())
{
if (last_shift == 2 || last_shift == 256) {
memset(buf, 0, 256);
sprintf(buf, "\n\n**************************************************************************************\n");
cfg_file.write(buf, strlen(buf));
//QDate date = QDate::currentDate();
QDateTime date = QDateTime::currentDateTime(); QDateTime date = QDateTime::currentDateTime();
memset(buf, 0, 256); string nowtime = date.toString("yyyy-MM-dd_HH:mm:ss").toStdString().c_str();
sprintf(buf, "%s:\n", date.toString("yyyy-MM-dd_HH-mm-ss").toStdString().c_str());
cfg_file.write(buf, strlen(buf));
}
memset(buf, 0, 256);
double ng_rate; double ng_rate;
if (ng[i] > 0) if (ng[i] > 0)
{
ng_rate = double(ng[i]) / production_number[i]; ng_rate = double(ng[i]) / production_number[i];
}
else else
{
ng_rate = 0; ng_rate = 0;
}
if (quantity > 0)
{
sprintf(buf, "%c 总产量:%010I64d 剔除率:%02.5f%% 合格率:%02.5f%% NG率:%02.5f%%\n", (shift == 0) ? 'A' : ((shift == 1) ? 'B' : 'C'), quantity, ((double)Kick[i] / quantity) * 100, (1.0 - ng_rate) * 100, ng_rate * 100); sprintf(buf, "%c 总产量:%010I64d 剔除率:%02.5f%% 合格率:%02.5f%% NG率:%02.5f%%\n", (shift == 0) ? 'A' : ((shift == 1) ? 'B' : 'C'), quantity, ((double)Kick[i] / quantity) * 100, (1.0 - ng_rate) * 100, ng_rate * 100);
cfg_file.write(buf, strlen(buf));
memset(buf, 0, 256);
} }
cfg_file.close(); else
{
sprintf(buf, "%c 总产量:%010I64d 剔除率:%02.5f%% 合格率:%02.5f%% NG率:%02.5f%%\n", (shift == 0) ? 'A' : ((shift == 1) ? 'B' : 'C'), quantity, 0.00, 0.00, 0.00);
}
std::ifstream file(file_path.toStdString());
std::ifstream file_(file_path.toStdString());
std::vector<std::string> lines; //用于存储文件的所有行
if (file_.good())
{
file_.close();
}
else
{
std::ofstream outFile(file_path.toStdString());
std::string fristNews = "**************************************************************************************\n" + nowtime + "\n" + buf;
outFile << fristNews << endl;
outFile.close();
}
if (file.is_open())
{
std::string line;
while (std::getline(file, line))
{
lines.push_back(line);
}
file.close();
// 在第一行添加数据
std::string newData = "**************************************************************************************\n"+nowtime +"\n" + buf;
if (!lines.empty())
{
lines.insert(lines.begin(), newData); // 在vector的开始位置插入新数据
}
// 将修改后的数据写回文件
std::ofstream outFile(file_path.toStdString());
if (outFile.is_open())
{
for (const auto& l : lines)
{
outFile << l << std::endl;
}
outFile.close();
}
}
} }
} }
last_shift = shift; last_shift = shift;

@ -129,6 +129,8 @@ private slots:
void OnExit(); void OnExit();
void OnRestart(); void OnRestart();
void OnAdmin(); void OnAdmin();
void heartbeat();//心跳检测
#ifdef __DEBUG #ifdef __DEBUG
void TestImg(); void TestImg();
void TestImgs(); void TestImgs();
@ -197,6 +199,7 @@ public:
QTimer* m_op_delay; //操作员权限剩余时间 QTimer* m_op_delay; //操作员权限剩余时间
QTimer* m_admin_delay; //操作员权限剩余时间 QTimer* m_admin_delay; //操作员权限剩余时间
QTimer* clean_pTimer; //定时清理任务 QTimer* clean_pTimer; //定时清理任务
QTimer* heartbeat_pTimer; //心跳检测
QSignalMapper* image_lable_DBsignalMapper0; QSignalMapper* image_lable_DBsignalMapper0;
QSignalMapper* image_lable_DBsignalMapper1; QSignalMapper* image_lable_DBsignalMapper1;

@ -204,7 +204,7 @@ public:
int debug; //调试模式PLC产生模拟的line4信号 int debug; //调试模式PLC产生模拟的line4信号
int reset; //复位 int reset; //复位
int alarm; //报警 int alarm; //报警
int heart; //心跳
ModbusConf() ModbusConf()
{ {
for (int i = 0; i < NumberOfSupportedCameras; i++) { for (int i = 0; i < NumberOfSupportedCameras; i++) {
@ -219,6 +219,7 @@ public:
debug = 0; //调试模式PLC产生模拟的line4信号 debug = 0; //调试模式PLC产生模拟的line4信号
reset = 0; //复位 reset = 0; //复位
alarm = 0; //报警 alarm = 0; //报警
heart = 0; //心跳
} }
}; };

@ -338,7 +338,7 @@ void SyncWorkThread::run()
m.copyTo(merge_image(roi)); m.copyTo(merge_image(roi));
if (merge_index == work_camera_nums * unit_count) { if (merge_index == work_camera_nums * unit_count) {
if (IsNGForAll) if (!IsNG)
{ {
ngReason = 1; ngReason = 1;
} }
@ -350,7 +350,7 @@ void SyncWorkThread::run()
//条盒TJ 小盒BJ //条盒TJ 小盒BJ
file_name = g_conf_path.save_pics_path + "/" file_name = g_conf_path.save_pics_path + "/"
+ now_ts.toString("yyyy-MM-dd") + "/"+"416_BJ_01_" + now_ts.toString("yyyy-MM-dd") + "/"+"416_BJ_01_"
+ now_ts.toString("yyyy-MM-dd_HH-mm-ss.zzz_") + ng_reason_maps[ngReason] + ".jpg"; + now_ts.toString("yyyy-MM-dd_HH-mm-ss.zzz") + ".jpg";
g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), merge_image)); g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), merge_image));
#ifdef __TCPSend #ifdef __TCPSend

@ -158,9 +158,9 @@ void threadSendTCP::sendData(_TCPSendInfo* TCPSendInfo, int Num) {
} }
#endif #endif
QDateTime ts_end = QDateTime::currentDateTime(); //QDateTime ts_end = QDateTime::currentDateTime();
int time_process = ts_start.msecsTo(ts_end); //int time_process = ts_start.msecsTo(ts_end);
qDebug() << "end-now_ts=" << time_process; //qDebug() << "end-now_ts=" << time_process;
} }
//字符串转ASCII //字符串转ASCII
std::vector<int> threadSendTCP::stringToAscii(const std::string& str) std::vector<int> threadSendTCP::stringToAscii(const std::string& str)

@ -161,10 +161,10 @@ void WorkThread::run()
bool IsNG = false; bool IsNG = false;
int ngReason = 0; int ngReason = 0;
QMap<int, QString> ng_reason_maps; QMap<int, QString> ng_reason_maps;
ng_reason_maps[0] = "unknow"; ng_reason_maps[0] = "1";
ng_reason_maps[1] = "less_than_setting"; ng_reason_maps[1] = "0";
ng_reason_maps[2] = "too_diff_from_model"; ng_reason_maps[2] = "0";
ng_reason_maps[3] = "out_of_setting_range"; ng_reason_maps[3] = "0";
for (int index = 0; index < unit_count; index++) for (int index = 0; index < unit_count; index++)
{ {
if (vec_results[index].size() < local_SysConf.no[local_camera_number][index]) if (vec_results[index].size() < local_SysConf.no[local_camera_number][index])
@ -255,6 +255,8 @@ void WorkThread::run()
if ((local_SysConf.save == 2) || (local_SysConf.save == 1)) if ((local_SysConf.save == 2) || (local_SysConf.save == 1))
{ {
for (int index = 0; index < unit_count; index++) for (int index = 0; index < unit_count; index++)
{
if (vec_results[index].size() < local_SysConf.no[local_camera_number][index])
{ {
cv::Mat m = vec_in[index].clone(); cv::Mat m = vec_in[index].clone();
QString file_name = g_conf_path.save_pics_path + "/ng/" + QString file_name = g_conf_path.save_pics_path + "/ng/" +
@ -283,6 +285,8 @@ void WorkThread::run()
//g_save_queue->put(std::make_pair(file_name.toStdString(), m)); //g_save_queue->put(std::make_pair(file_name.toStdString(), m));
g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m)); g_save_queue->put(std::make_pair(file_name.toLocal8Bit().constData(), m));
} }
}
} }
} }
#ifndef SYNC_CAMERA #ifndef SYNC_CAMERA

Loading…
Cancel
Save