/
daq_calibration.cpp
191 lines (155 loc) · 5.02 KB
/
daq_calibration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "daq_calibration.h"
#include <main_window.h>
#include <iostream>
extern "C" Plugin::Object *createRTXIPlugin(void)
{
return new DAQCalibration();
}
struct find_daq_t
{
int index;
DAQ::Device *device;
};
static void findDAQDevice(DAQ::Device *dev,void *arg)
{
struct find_daq_t *info = static_cast<struct find_daq_t *>(arg);
if(!info->index)
info->device = dev;
info->index--;
}
static void buildDAQDeviceList(DAQ::Device *dev,void *arg)
{
QComboBox *deviceList = static_cast<QComboBox *>(arg);
deviceList->addItem(QString::fromStdString(dev->getName()));
}
static DefaultGUIModel::variable_t vars[] = {
{ "Channel", "Data for channel to calibrate", DefaultGUIModel::INPUT, },
{ "Offset", "Calibration offset value", DefaultGUIModel::STATE, },
};
static size_t num_vars = sizeof(vars) / sizeof(DefaultGUIModel::variable_t);
DAQCalibration::DAQCalibration(void) : DefaultGUIModel("DAQ Calibration Utility", ::vars, ::num_vars) {
setWhatsThis("<p><b>DAQCalibration:</b><br>QWhatsThis description.</p>");
DefaultGUIModel::createGUI(vars, num_vars);
customizeGUI();
update(INIT);
refresh();
QTimer::singleShot(0, this, SLOT(resizeMe()));
}
DAQCalibration::~DAQCalibration(void) { }
void DAQCalibration::execute(void)
{
if(data_idx < 1/period)
{
channel_data.push_back(input(0));
data_idx++;
if(data_idx == 1/period)
calibrate();
}
return;
}
void DAQCalibration::calibrate()
{
// Offset for the channel
offset = std::accumulate(channel_data.begin(), channel_data.end(), 0.0) / channel_data.size();
// Apply channel settings
DAQ::Device *dev;
{
struct find_daq_t info = { deviceList->currentIndex(), 0, };
DAQ::Manager::getInstance()->foreachDevice(findDAQDevice, &info);
dev = info.device;
}
if(dev)
{
// Get channel
DAQ::index_t a_chan = analogChannelList->currentIndex();
DAQ::type_t a_type = static_cast<DAQ::type_t>(analogSubdeviceList->currentIndex());
// Store offset in DAQ
dev->setAnalogCalibrationValue(a_type, a_chan, offset);
}
// Pause
statusBar->showMessage(tr("Status: Calibration complete."));
pauseButton->setChecked(true);
}
void DAQCalibration::update(DefaultGUIModel::update_flags_t flag) {
switch (flag) {
case INIT:
period = RT::System::getInstance()->getPeriod() * 1e-9; // s
setState("Offset", offset);
offset = 0;
break;
case MODIFY:
break;
case UNPAUSE:
data_idx = 0;
offset = 0;
break;
case PAUSE:
data_idx = 0;
channel_data.clear();
break;
case PERIOD:
period = RT::System::getInstance()->getPeriod() * 1e-9; // s
break;
default:
break;
}
}
void DAQCalibration::customizeGUI(void) {
QGridLayout *customlayout = DefaultGUIModel::getLayout();
DefaultGUIModel::pauseButton->setText("Calibrate");
QGroupBox *daq_group = new QGroupBox;
QHBoxLayout *daqlayout = new QHBoxLayout;
daqlayout->addWidget(new QLabel(tr("Device:")));
deviceList = new QComboBox;
daqlayout->addWidget(deviceList);
DAQ::Manager::getInstance()->foreachDevice(buildDAQDeviceList,deviceList);
QObject::connect(deviceList,SIGNAL(activated(int)),this,SLOT(updateDevice(void)));
analogSubdeviceList = new QComboBox;
analogSubdeviceList->addItem("Input");
analogSubdeviceList->addItem("Output");
QObject::connect(analogSubdeviceList,SIGNAL(activated(int)),this,SLOT(updateDevice(void)));
daqlayout->addWidget(analogSubdeviceList);
analogChannelList = new QComboBox;
daq_group->setLayout(daqlayout);
daqlayout->addWidget(analogChannelList);
QGroupBox *status_group = new QGroupBox;
QHBoxLayout *status_layout = new QHBoxLayout;
statusBar = new QStatusBar();
statusBar->setSizeGripEnabled(false);
statusBar->showMessage(tr("Status: Please specify a channel..."));
status_group->setLayout(status_layout);
status_layout->addWidget(statusBar);
updateDevice();
customlayout->addWidget(daq_group, 0, 0);
customlayout->addWidget(status_group, 2, 0);
setLayout(customlayout);
}
void DAQCalibration::updateDevice(void)
{
DAQ::Device *dev;
DAQ::type_t type;
{
struct find_daq_t info = { deviceList->currentIndex(), 0, };
DAQ::Manager::getInstance()->foreachDevice(findDAQDevice,&info);
dev = info.device;
}
if(!dev)
return;
analogChannelList->clear();
type = static_cast<DAQ::type_t>(analogSubdeviceList->currentIndex());
for(size_t i=0; i<dev->getChannelCount(type); ++i)
analogChannelList->addItem(QString::number(i));
}