bug fix for multiple digitizers

This commit is contained in:
Ryan Tang 2023-03-16 17:28:55 -04:00
parent 6211852acf
commit ac81be92f7
8 changed files with 90 additions and 66 deletions

View File

@ -18,5 +18,5 @@ LIBS += -lcurl -lCAEN_FELib -lX11
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input
HEADERS += ClassDigitizer2Gen.h Event.h influxdb.h mainwindow.h digiSettingsPanel.h Digiparameters.h scope.h manyThread.h CustomWidgets.h
HEADERS += ClassDigitizer2Gen.h Event.h influxdb.h mainwindow.h digiSettingsPanel.h Digiparameters.h scope.h manyThread.h CustomWidgets.h macro.h
SOURCES += ClassDigitizer2Gen.cpp influxdb.cpp main.cpp mainwindow.cpp digiSettingsPanel.cpp scope.cpp

View File

@ -21,8 +21,7 @@
#include "ClassDigitizer2Gen.h"
#include "CustomWidgets.h"
#define MaxNumberOfDigitizer 20
#include "macro.h"
//^#######################################################
class DigiSettingsPanel : public QWidget{

6
macro.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef MACRO_H
#define MACRO_H
#define MaxNumberOfDigitizer 20
#endif

View File

@ -559,7 +559,7 @@ void MainWindow::OpenDigitizers(){
LogMsg("Opened digitizer : <font style=\"color:red;\">" + QString::number(digi[i]->GetSerialNumber()) + "</font>");
readDataThread[i] = new ReadDataThread(digi[i], this);
readDataThread[i] = new ReadDataThread(digi[i], i, this);
connect(readDataThread[i], &ReadDataThread::sendMsg, this, &MainWindow::LogMsg);
//connect(readDataThread[i], &ReadDataThread::checkFileSize, this, &MainWindow::CheckOutFileSize);
//connect(readDataThread[i], &ReadDataThread::endOfLastData, this, &MainWindow::CheckOutFileSize);
@ -644,6 +644,7 @@ void MainWindow::CloseDigitizers(){
if( digiSetting != NULL ) digiSetting->close();
if( readDataThread[i] != NULL ){
LogMsg("Waiting for digitizer .....");
readDataThread[i]->quit();
readDataThread[i]->wait();
delete readDataThread[i];
@ -672,13 +673,25 @@ void MainWindow::CloseDigitizers(){
//^###################################################################### Open Scope
void MainWindow::OpenScope(){
if( digi ){
if( !scope ){
scope = new Scope(digi, nDigi, readDataThread);
connect(scope, &Scope::CloseWindow, this, [=](){ bnStartACQ->setEnabled(true); });
connect(scope, &Scope::UpdateScalar, this, &MainWindow::UpdateScalar);
connect(scope, &Scope::SendLogMsg, this, &MainWindow::LogMsg);
connect(scope, &Scope::TellACQOnOff, this, [=](const bool onOff){
if( influx ){
influx->ClearDataPointsBuffer();
influx->AddDataPoint(onOff ? "StartStop value=1" : "StartStop value=0");
influx->WriteData(DatabaseName.toStdString());
}
});
if( influx ){
influx->ClearDataPointsBuffer();
influx->AddDataPoint("StartStop value=1");
influx->WriteData(DatabaseName.toStdString());
}
if( digiSetting ) {
connect(scope, &Scope::UpdateSettingsPanel, digiSetting, &DigiSettingsPanel::ShowSettingsToPanel);
@ -689,7 +702,7 @@ void MainWindow::OpenScope(){
}else{
scope->show();
digiSetting->EnableControl();
if( digiSetting ) digiSetting->EnableControl();
}
}
@ -829,11 +842,11 @@ void MainWindow::UpdateScalar(){
//=========== another method, directly readValue
for( int ch = 0; ch < digi[iDigi]->GetNChannels(); ch ++){
digiMTX.lock();
digiMTX[iDigi].lock();
std::string timeStr = digi[iDigi]->ReadValue(PHA::CH::ChannelRealtime, ch); // for refreashing SelfTrgRate and SavedCount
haha[ch] = digi[iDigi]->ReadValue(PHA::CH::SelfTrgRate, ch);
std::string kakaStr = digi[iDigi]->ReadValue(PHA::CH::ChannelSavedCount, ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
unsigned long kaka = std::stoul(kakaStr.c_str()) ;
unsigned long time = std::stoul(timeStr.c_str()) ;

View File

@ -19,6 +19,7 @@
#include <vector>
#include <time.h> // time in nano-sec
#include "macro.h"
#include "ClassDigitizer2Gen.h"
#include "influxdb.h"

View File

@ -5,25 +5,28 @@
#include <QThread>
#include <QMutex>
#include "macro.h"
#include "ClassDigitizer2Gen.h"
#include "macro.h"
static QMutex digiMTX;
static QMutex digiMTX[MaxNumberOfDigitizer];
//^#===================================================== ReadData Thread
class ReadDataThread : public QThread {
Q_OBJECT
public:
ReadDataThread(Digitizer2Gen * dig, QObject * parent = 0) : QThread(parent){
ReadDataThread(Digitizer2Gen * dig, int digiID, QObject * parent = 0) : QThread(parent){
this->digi = dig;
this->ID = digiID;
isSaveData = false;
}
void SetSaveData(bool onOff) {this->isSaveData = onOff;}
void run(){
clock_gettime(CLOCK_REALTIME, &ta);
while(true){
digiMTX.lock();
digiMTX[ID].lock();
int ret = digi->ReadData();
digiMTX.unlock();
digiMTX[ID].unlock();
if( ret == CAEN_FELib_Success){
if( isSaveData) digi->SaveDataToFile();
@ -54,6 +57,7 @@ signals:
//void checkFileSize();
private:
Digitizer2Gen * digi;
int ID;
timespec ta, tb;
bool isSaveData;
};

107
scope.cpp
View File

@ -81,12 +81,12 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
if( !allowChange ) return;
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::ChannelEnable, "False", -1);
digi[iDigi]->WriteValue(PHA::CH::ChannelEnable, "True", ch);
ReadScopeSettings();
UpdateSettingsPanel();
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
allowChange = false;
@ -143,9 +143,9 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
if( chkSetAllChannel->isChecked() ) ch = -1;
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::WaveAnalogProbe0, (cbAnaProbe[0]->currentData()).toString().toStdString(), ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
connect(cbAnaProbe[1], &RComboBox::currentIndexChanged, this, [=](){
@ -153,9 +153,9 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
if( chkSetAllChannel->isChecked() ) ch = -1;
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::WaveAnalogProbe1, (cbAnaProbe[1]->currentData()).toString().toStdString(), ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
//TODO --- add None
@ -190,36 +190,36 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
if( chkSetAllChannel->isChecked() ) ch = -1;
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::WaveDigitalProbe0, (cbDigProbe[0]->currentData()).toString().toStdString(), ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
connect(cbDigProbe[1], &RComboBox::currentIndexChanged, this, [=](){
if( !allowChange ) return;
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
if( chkSetAllChannel->isChecked() ) ch = -1;
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::WaveDigitalProbe1, (cbDigProbe[1]->currentData()).toString().toStdString(), ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
connect(cbDigProbe[2], &RComboBox::currentIndexChanged, this, [=](){
if( !allowChange ) return;
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
if( chkSetAllChannel->isChecked() ) ch = -1;
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::WaveDigitalProbe2, (cbDigProbe[2]->currentData()).toString().toStdString(), ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
connect(cbDigProbe[3], &RComboBox::currentIndexChanged, this, [=](){
if( !allowChange ) return;
int iDigi = cbScopeDigi->currentIndex();
int ch = cbScopeCh->currentIndex();
if( chkSetAllChannel->isChecked() ) ch = -1;
digiMTX.lock();
digiMTX[iDigi].lock();
digi[iDigi]->WriteValue(PHA::CH::WaveDigitalProbe3, (cbDigProbe[3]->currentData()).toString().toStdString(), ch);
digiMTX.unlock();
digiMTX[iDigi].unlock();
});
layout->addWidget(cbAnaProbe[0], rowID, 0);
@ -286,7 +286,6 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
QLabel * lbinfo2 = new QLabel("Maximum time range is " + QString::number(MaxDisplayTraceDataLength * PHA::TraceStep) + " ns due to processing speed.", this);
layout->addWidget(lbinfo2, rowID, 0, 1, 5);
//------------ close button
rowID ++;
bnScopeStart = new QPushButton("Start", this);
@ -318,17 +317,12 @@ Scope::Scope(Digitizer2Gen **digi, unsigned int nDigi, ReadDataThread ** readDat
}
Scope::~Scope(){
updateTraceThread->Stop();
updateTraceThread->quit();
updateTraceThread->wait();
delete updateTraceThread;
for( int i = 0; i < 6; i++) delete dataTrace[i];
delete plot;
}
void Scope::ReadScopeSettings(){
@ -338,8 +332,6 @@ void Scope::ReadScopeSettings(){
if( !digi[iDigi] && digi[iDigi]->IsDummy() ) return;
printf("%s\n", __func__);
allowChange = false;
for( int i = 0 ; i < 2; i++){
@ -390,33 +382,32 @@ void Scope::StartScope(){
if( !digi ) return;
int iDigi = cbScopeDigi->currentIndex();
for( int iDigi = 0 ; iDigi < nDigi; iDigi ++ ){
if( digi[iDigi]->IsDummy() ) return;
if( digi[iDigi]->IsDummy() ) return;
printf("%s\n", __func__);
int ch = cbScopeCh->currentIndex();
int ch = cbScopeCh->currentIndex();
//*---- set digitizer to take full trace; since in scope mode, no data saving, speed would be fast (How fast?)
//* when the input rate is faster than trigger rate, Digitizer will stop data taking.
//*---- set digitizer to take full trace; since in scope mode, no data saving, speed would be fast (How fast?)
//* when the input rate is faster than trigger rate, Digitizer will stop data taking.
ReadScopeSettings();
ReadScopeSettings();
digi[iDigi]->WriteValue(PHA::CH::ChannelEnable, "False", -1);
digi[iDigi]->WriteValue(PHA::CH::ChannelEnable, "True", ch);
digi[iDigi]->SetPHADataFormat(0);
digi[iDigi]->WriteValue(PHA::CH::ChannelEnable, "False", -1);
digi[iDigi]->WriteValue(PHA::CH::ChannelEnable, "True", ch);
digi[iDigi]->SetPHADataFormat(0);
digi[iDigi]->StartACQ();
digi[iDigi]->StartACQ();
readDataThread[iDigi]->SetSaveData(false);
readDataThread[iDigi]->start();
readDataThread[iDigi]->SetSaveData(false);
readDataThread[iDigi]->start();
updateTraceThread->start();
ScopeControlOnOff(false);
emit TellSettingsPanelControlOnOff();
updateTraceThread->start();
ScopeControlOnOff(false);
emit TellSettingsPanelControlOnOff();
}
emit TellACQOnOff(true);
allowChange = true;
}
@ -431,15 +422,16 @@ void Scope::StopScope(){
if(digi){
for(int i = 0; i < nDigi; i++){
if( digi[i]->IsDummy() ) continue;
digiMTX.lock();
digiMTX[i].lock();
digi[i]->StopACQ();
digi[i]->WriteValue(PHA::CH::ChannelEnable, "True", -1);
digiMTX.unlock();
digiMTX[i].unlock();
readDataThread[i]->quit();
readDataThread[i]->wait();
}
emit TellACQOnOff(false);
}
ScopeControlOnOff(true);
@ -458,23 +450,26 @@ void Scope::UpdateScope(){
if( digi ){
digiMTX.lock();
digiMTX[iDigi].lock();
std::string time = digi[iDigi]->ReadValue(PHA::CH::ChannelRealtime, ch); // for refreashing SelfTrgRate and SavedCount
std::string haha = digi[iDigi]->ReadValue(PHA::CH::SelfTrgRate, ch);
leTriggerRate->setText(QString::fromStdString(haha));
if( atoi(haha.c_str()) == 0 ) {
digiMTX.unlock();
for( int j = 0; j < 4; j++){
//unsigned int traceLength = qMin((int) digi[iDigi]->evt->traceLenght, MaxDisplayTraceDataLength);
unsigned int traceLength = qMin( atoi(digi[iDigi]->GetSettingValue(PHA::CH::RecordLength, ch).c_str())/sample2ns, MaxDisplayTraceDataLength );
if( atoi(haha.c_str()) == 0 ) {
digiMTX[iDigi].unlock();
for( int j = 0; j < 6; j++){
QVector<QPointF> points;
for( int i = 0 ; i < dataTrace[j]->count(); i++) points.append(QPointF(sample2ns * i , j > 1 ? 0 : (j+1)*1000));
for( unsigned int i = 0 ; i < traceLength; i++) points.append(QPointF(sample2ns * i , j > 1 ? 0 : (j+1)*1000));
dataTrace[j]->replace(points);
}
plot->axes(Qt::Horizontal).first()->setRange(0, sample2ns * traceLength);
return;
}
unsigned int traceLength = qMin((int) digi[iDigi]->evt->traceLenght, MaxDisplayTraceDataLength);
for( int j = 0; j < 2; j++) {
QVector<QPointF> points;
for( unsigned int i = 0 ; i < traceLength; i++) points.append(QPointF(sample2ns * i , digi[iDigi]->evt->analog_probes[j][i]));
@ -485,7 +480,7 @@ void Scope::UpdateScope(){
for( unsigned int i = 0 ; i < traceLength; i++) points.append(QPointF(sample2ns * i , (j+1)*1000 + 4000*digi[iDigi]->evt->digital_probes[j][i]));
dataTrace[j+2]->replace(points);
}
digiMTX.unlock();
digiMTX[iDigi].unlock();
plot->axes(Qt::Horizontal).first()->setRange(0, sample2ns * traceLength);
}
@ -494,6 +489,7 @@ void Scope::UpdateScope(){
void Scope::ProbeChange(RComboBox * cb[], const int size ){
printf("%s\n", __func__);
QStandardItemModel * model[size] = {NULL};
for( int i = 0; i < size; i++){
model[i] = qobject_cast<QStandardItemModel*>(cb[i]->model());
@ -512,20 +508,21 @@ void Scope::ProbeChange(RComboBox * cb[], const int size ){
}
}
digiMTX.lock();
int ID = cbScopeDigi->currentIndex();
digiMTX[ID].lock();
if( size == 2) {// analog probes
for( int j = 0; j < 2; j++ )dataTrace[j]->setName(cb[j]->currentText());
}
if( size == 4){ // digitial probes
for( int j = 2; j < 6; j++ )dataTrace[j]->setName(cb[j-2]->currentText());
}
digiMTX.unlock();
digiMTX[ID].unlock();
}
void Scope::ScopeControlOnOff(bool on){
bnScopeStop->setEnabled(!on);
bnScopeStop->setEnabled(!on);
bnScopeStart->setEnabled(on);
bnScopeReset->setEnabled(on);
bnScopeReadSettings->setEnabled(on);
@ -549,6 +546,7 @@ void Scope::ScopeControlOnOff(bool on){
sbPileUpGuard->setEnabled(on);
cbBaselineAvg->setEnabled(on);
cbLowFreqFilter->setEnabled(on);
}
void Scope::ScopeReadSpinBoxValue(int iDigi, int ch, RSpinBox *sb, const Reg digPara){
@ -567,6 +565,7 @@ void Scope::ScopeReadComboBoxValue(int iDigi, int ch, RComboBox *cb, const Reg d
}
void Scope::ScopeMakeSpinBox(RSpinBox * &sb, QString str, QGridLayout *layout, int row, int col, const Reg digPara){
printf("%s\n", __func__);
QLabel * lb = new QLabel(str, this);
lb->setAlignment(Qt::AlignRight | Qt::AlignCenter);
layout->addWidget(lb, row, col);

View File

@ -16,6 +16,7 @@
#include <QMouseEvent>
#include <QGestureEvent>
#include "macro.h"
#include "ClassDigitizer2Gen.h"
#include "manyThread.h"
#include "CustomWidgets.h"
@ -133,6 +134,7 @@ signals:
void UpdateScalar();
void UpdateSettingsPanel();
void TellSettingsPanelControlOnOff();
void TellACQOnOff(const bool onOff);
private: