Files
HPPA/HPPA/focusWindow.cpp
tangchao0503 6469bff15d fix & add
1、在界面上实现选择相机类型的功能;
2、创建了一个光谱仪操作的纯虚基类(ImagerOperationBase)并实现了大部分的操作,具体类型的光谱仪应继承此类并实现纯虚函数;
3、添加了 resonon 的 nir 320 相机,修改 resonon 的 pica l 相机的实现:继承 ImagerOperationBase;
4、重构类 QMotorDoubleSlider,提高其通用性,所有马达相关的 slider 都使用此类;
5、适配 resonon nir 320 显微镜使用的 2 轴线性平台,有些特殊的马达参数设置(setMotorParamMicroscope 函数)绑定了 nir 的相机类型参数;
6、修改:将线性平台的量程信息保存在cfg配置文件中,并修改配置文件读写类来读写此量程信息;
2024-12-11 17:33:29 +08:00

340 lines
8.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stdafx.h"
#include "focusWindow.h"
focusWindow::focusWindow(QWidget *parent, ImagerOperationBase* imager)
{
ui.setupUi(this);
//读取配置文件
string HPPACfgFile = getPathofEXE() + "\\HPPA.cfg";
Configfile configfile;
configfile.setConfigfilePath(HPPACfgFile);
if (!configfile.isConfigfileExist())
configfile.createConfigFile();
configfile.parseConfigfile();
configfile.getPositionRestriction(m_iMaxPos, m_iMinPos);
disableBeforeConnect(true);
setAttribute(Qt::WA_DeleteOnClose);//设置关闭窗体就调用窗体的析构函数
m_Imager = imager;
m_FocusState = 0;
m_ctrlFocusMotor = nullptr;
m_AutoFocusThread = new QThread();
connect(this->ui.connectMotor_btn, SIGNAL(clicked()), this, SLOT(onConnectMotor()));
connect(this->ui.logicZero_btn, SIGNAL(clicked()), this, SLOT(onMove2MotorLogicZero()));
connect(this->ui.max_btn, SIGNAL(clicked()), this, SLOT(onMove2MotorMax()));
connect(this->ui.add_btn, SIGNAL(clicked()), this, SLOT(onAdd()));
connect(this->ui.subtract_btn, SIGNAL(clicked()), this, SLOT(onSubtract()));
connect(this->ui.ultrasound_radioButton, SIGNAL(released()), this, SLOT(onUltrasound_radioButton()));
connect(this->ui.autoFocus_btn, SIGNAL(clicked()), this, SLOT(onAutoFocus()));
connect(this->ui.manualFocus_btn, SIGNAL(clicked()), this, SLOT(onManualFocus()));
connect(this->ui.updateCurrentLocation_btn, SIGNAL(clicked()), this, SLOT(onUpdateCurrentLocation()));
connect(this->ui.moveto_btn, SIGNAL(clicked()), this, SLOT(onMoveto()));
//查找可用串口,并显示
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info);
if (serial.open(QIODevice::ReadWrite))
{
ui.motorPort_comboBox->addItem(serial.portName());
ui.ultrasoundPort_comboBox->addItem(serial.portName());
serial.close();
}
}
//设置自动调焦进度条
ui.autoFocusProgress_progressBar->setMinimum(0);
ui.autoFocusProgress_progressBar->setMaximum(100);
ui.autoFocusProgress_progressBar->reset();
}
focusWindow::~focusWindow()
{
printf("destroy focusWindow-------------------------\n");
emit StartManualFocusSignal(0);//当用户没有点击停止调焦就关闭窗口
delete m_ctrlFocusMotor;
//delete thread1, progressThread;
}
void focusWindow::disableBeforeConnect(bool disable)
{
ui.controlMotor_groupBox->setDisabled(disable);
ui.autoFocus_btn->setDisabled(disable);
}
bool test(void *pCaller, int *x, int *y, void **pvdata)
{
focusWindow *p = (focusWindow *)pCaller;
p->m_Imager->GetFrameSize(*x, *y);
USHORT *pusData = new USHORT[(*x)*(*y)];
p->m_Imager->imagerStartCollect();
p->m_Imager->getFrame(pusData);
p->m_Imager->imagerStopCollect();
BYTE *pbData = (BYTE*)pusData;
*pvdata = pbData;
return true;
}
void focusWindow::onConnectMotor()
{
if (m_ctrlFocusMotor != nullptr)
{
printf("不能重复连接-------------------------\n");
return;
}
bool isUltrasound = ui.ultrasound_radioButton->isChecked();
QString motorPortTmp = ui.motorPort_comboBox->currentText();
QString ultrasoundPortTmp = ui.ultrasoundPort_comboBox->currentText();
QRegExp rx("\\d+$");
rx.indexIn(motorPortTmp, 0);
int motorPort = rx.cap(0).toInt();
rx.indexIn(ultrasoundPortTmp, 0);
int ultrasoundPort = rx.cap(0).toInt();
if (isUltrasound)
{
PortInfo motor;
motor.iPortType = 0;
motor.indexParity = 0;
motor.iPortNumber = motorPort;
motor.indexBaudRate = 0x13;
motor.indexBytesize = 3;
motor.indexStopBits = 0;
PortInfo ultrasound;
ultrasound.iPortType = 0;
ultrasound.indexParity = 0;
ultrasound.iPortNumber = ultrasoundPort;
ultrasound.indexBaudRate = 0x0C;
ultrasound.indexBytesize = 3;
ultrasound.indexStopBits = 0;
m_ctrlFocusMotor = new CFocusMotorControl();
m_ctrlFocusMotor->SetLogicZero(m_iMinPos);
m_ctrlFocusMotor->SetLimit(m_iMinPos, m_iMaxPos);
m_ctrlFocusMotor->InitSystem(motor, ultrasound, test, this);
//m_ctrlFocusMotor->MoveToLogicZero();
}
else
{
PortInfo motor;
motor.iPortType = 0;
motor.indexParity = 0;
motor.iPortNumber = motorPort;
motor.indexBaudRate = 0x13;
motor.indexBytesize = 3;
motor.indexStopBits = 0;
m_ctrlFocusMotor = new CFocusMotorControl();
m_ctrlFocusMotor->SetLogicZero(m_iMinPos);
m_ctrlFocusMotor->SetLimit(m_iMinPos, m_iMaxPos);
m_ctrlFocusMotor->InitSystem(motor, test, this);
m_ctrlFocusMotor->MoveToLogicZero();
}
disableBeforeConnect(false);
}
void focusWindow::onMove2MotorLogicZero()
{
m_ctrlFocusMotor->MoveToLogicZero();
}
void focusWindow::onMove2MotorMax()
{
m_ctrlFocusMotor->MoveToPos(m_iMaxPos);
}
void focusWindow::onAdd()
{
DriverInfo di;
m_ctrlFocusMotor->GetDriverStatus(di);
int stepSize = ui.addStepSize_lineEdit->text().toInt();
m_ctrlFocusMotor->MoveToPos(di.iAbsPosition + stepSize);
}
void focusWindow::onSubtract()
{
DriverInfo di;
m_ctrlFocusMotor->GetDriverStatus(di);
int stepSize = ui.subtractStepSize_lineEdit->text().toInt();
m_ctrlFocusMotor->MoveToPos(di.iAbsPosition - stepSize);
}
void focusWindow::onAutoFocus()
{
bool isUltrasound = ui.ultrasound_radioButton->isChecked();
WorkerThread2 *thread1 = new WorkerThread2(m_ctrlFocusMotor, isUltrasound);
connect(thread1, SIGNAL(AutoFocusFinishedSignal()), this, SLOT(onAutoFocusFinished()));
thread1->start();
this->setDisabled(true);
WorkerThread4 *progressThread = new WorkerThread4(m_ctrlFocusMotor);
connect(progressThread, SIGNAL(AutoFocusProgressSignal(int)), this, SLOT(onAutoFocusProgress(int)));
progressThread->start();
ui.autoFocusProgress_progressBar->reset();
}
void focusWindow::onManualFocus()
{
m_FocusState += 1;
std::cout << "点击调焦按钮!" << std::endl;
if (m_FocusState % 2 == 1)
{
//开始调焦
emit StartManualFocusSignal(1);
ui.manualFocus_btn->setText(QString::fromLocal8Bit("停止调焦"));
ui.manualFocus_btn->setStyleSheet("QWidget{background-color:rgb(255,0,0);}");
ui.autoFocus_btn->setDisabled(true);
//std::cout << "------------------------------------------" << m_FocusState << std::endl;
}
else
{
emit StartManualFocusSignal(0);
m_Imager->setFocusControlState(false);
ui.manualFocus_btn->setText(QString::fromLocal8Bit("调焦"));
ui.manualFocus_btn->setStyleSheet("QWidget{background-color:rgb(0,255,0);}");
ui.autoFocus_btn->setDisabled(false);
}
}
void focusWindow::onUpdateCurrentLocation()
{
DriverInfo di;
m_ctrlFocusMotor->GetDriverStatus(di);
ui.currentLocation_lineEdit->setText(QString::number(di.iAbsPosition));
}
void focusWindow::onMoveto()
{
int pos = ui.currentLocation_lineEdit->text().toInt();
//需要做参数有效性验证,查看是否在有效范围内
m_ctrlFocusMotor->MoveToPos(pos);
}
void focusWindow::onAutoFocusFinished()
{
this->setDisabled(false);
onUpdateCurrentLocation();
}
void focusWindow::onAutoFocusProgress(int progress)
{
//std::cout << "进度:" << progress << std::endl;
ui.autoFocusProgress_progressBar->setValue(progress);
}
void focusWindow::onUltrasound_radioButton()
{
bool isUltrasound = ui.ultrasound_radioButton->isChecked();
if (isUltrasound)
{
ui.ultrasoundPort_comboBox->setEnabled(true);
}
else
{
ui.ultrasoundPort_comboBox->setEnabled(false);
}
}
WorkerThread2::WorkerThread2(CFocusMotorControl * ctrlFocusMotor, bool isUltrasound)
{
m_ctrlFocusMotor = ctrlFocusMotor;
m_bIsUltrasound = isUltrasound;
}
void WorkerThread2::run()
{
//配置文件
string HPPACfgFile = getPathofEXE() + "\\HPPA.cfg";
Configfile configfile;
configfile.setConfigfilePath(HPPACfgFile);
if (!configfile.isConfigfileExist())
configfile.createConfigFile();
configfile.parseConfigfile();
int coarse, fine;
configfile.getTuningStepSize(coarse, fine);
float fa, fb;
configfile.getFitParams(fa, fb);
int max_FocusRange, min_FocusRange;
configfile.getAutoFocusRange(max_FocusRange, min_FocusRange);
if (m_bIsUltrasound)
{
m_ctrlFocusMotor->SetFitParams(fa, fb);
m_ctrlFocusMotor->StartAutoFocus(5, 2);
}
else
{
m_ctrlFocusMotor->StartAutoFocus(min_FocusRange, max_FocusRange, coarse, fine);
}
emit AutoFocusFinishedSignal();
}
WorkerThread4::WorkerThread4(CFocusMotorControl * ctrlFocusMotor)
{
m_ctrlFocusMotor = ctrlFocusMotor;
}
void WorkerThread4::run()
{
while (true)
{
int progress = m_ctrlFocusMotor->GetProgressIndex();
//std::cout << "WorkerThread4::run----自动调焦进度:" << progress << std::endl;
emit AutoFocusProgressSignal(progress);
if (progress == 100)
{
//std::cout << "自动调焦完成!" << std::endl;
break;
}
msleep(200);
}
}