Skip to content

Commit bd41871

Browse files
committed
admt: Fix displayed turn count
- Code cleanup potential memory leaks - Disabled ECC on startup - Adusted layout for calibration tab - Replaced maps with QMap Signed-off-by: John Lloyd Juanillo <[email protected]>
1 parent aa54bcf commit bd41871

File tree

5 files changed

+390
-433
lines changed

5 files changed

+390
-433
lines changed

plugins/admt/include/admt/admtcontroller.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include "scopy-admt_export.h"
2626

27-
#include <bitset>
2827
#include <iio.h>
2928

3029
#include <QElapsedTimer>
@@ -34,14 +33,10 @@
3433
#include <iioutil/connectionprovider.h>
3534
#include <pluginbase/statusbarmanager.h>
3635

37-
#include <algorithm>
3836
#include <cmath>
39-
#include <complex>
4037
#include <cstdint>
4138
#include <cstdlib>
42-
#include <iterator>
4339
#include <math.h>
44-
#include <numeric>
4540
#include <vector>
4641

4742
using namespace std;
@@ -266,6 +261,7 @@ public Q_SLOTS:
266261
Q_SIGNALS:
267262
void streamData(double value);
268263
void streamBufferedData(const QVector<double> &value);
264+
void requestDisconnect();
269265

270266
private:
271267
QWidget *m_page;
@@ -288,4 +284,4 @@ public Q_SLOTS:
288284
};
289285
} // namespace scopy::admt
290286

291-
#endif // ADMTCONTROLLER_H
287+
#endif // ADMTCONTROLLER_H

plugins/admt/include/admt/harmoniccalibration.h

+23-8
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class SCOPY_ADMT_EXPORT HarmonicCalibration : public QWidget
104104
bool running() const;
105105
void setRunning(bool newRunning);
106106
void requestDisconnect();
107+
107108
public Q_SLOTS:
108109
void run(bool);
109110
void stop();
@@ -150,16 +151,28 @@ public Q_SLOTS:
150151
double rotation, angle, count, temp = 0.0, motor_rpm, amax, rotate_vmax, dmax, disable, target_pos, current_pos,
151152
ramp_mode, afeDiag0, afeDiag1, afeDiag2;
152153

154+
QPen channel0Pen, channel1Pen, channel2Pen, channel3Pen, channel4Pen, channel5Pen, channel6Pen, channel7Pen;
155+
156+
QMap<string, string> deviceRegisterMap;
157+
QMap<string, int> GENERALRegisterMap;
158+
QMap<string, bool> DIGIOENRegisterMap, FAULTRegisterMap, DIAG1RegisterMap;
159+
QMap<string, double> DIAG2RegisterMap, DIAG1AFERegisterMap;
160+
161+
QVector<double> acquisitionAngleList, acquisitionABSAngleList, acquisitionTmp0List, acquisitionTmp1List,
162+
acquisitionSineList, acquisitionCosineList, acquisitionRadiusList, acquisitionAngleSecList,
163+
acquisitionSecAnglIList, acquisitionSecAnglQList, graphDataList, graphPostDataList;
164+
153165
QPushButton *openLastMenuButton, *calibrationStartMotorButton, *calibrateDataButton, *extractDataButton,
154166
*clearCalibrateDataButton, *clearCommandLogButton, *applySequenceButton, *readAllRegistersButton;
155167
QButtonGroup *rightMenuButtonGroup;
156168

157-
QLineEdit *acquisitionMotorRPMLineEdit, *calibrationMotorRPMLineEdit, *motorTargetPositionLineEdit,
158-
*displayLengthLineEdit, *dataGraphSamplesLineEdit, *tempGraphSamplesLineEdit,
159-
*acquisitionMotorCurrentPositionLineEdit, *calibrationH1MagLineEdit, *calibrationH2MagLineEdit,
160-
*calibrationH3MagLineEdit, *calibrationH8MagLineEdit, *calibrationH1PhaseLineEdit,
161-
*calibrationH2PhaseLineEdit, *calibrationH3PhaseLineEdit, *calibrationH8PhaseLineEdit,
162-
*calibrationMotorCurrentPositionLineEdit, *AFEDIAG0LineEdit, *AFEDIAG1LineEdit, *AFEDIAG2LineEdit;
169+
QLineEdit *acquisitionMotorRPMLineEdit, *calibrationCycleCountLineEdit, *calibrationSamplesPerCycleLineEdit,
170+
*calibrationMotorRPMLineEdit, *motorTargetPositionLineEdit, *displayLengthLineEdit,
171+
*dataGraphSamplesLineEdit, *tempGraphSamplesLineEdit, *acquisitionMotorCurrentPositionLineEdit,
172+
*calibrationH1MagLineEdit, *calibrationH2MagLineEdit, *calibrationH3MagLineEdit,
173+
*calibrationH8MagLineEdit, *calibrationH1PhaseLineEdit, *calibrationH2PhaseLineEdit,
174+
*calibrationH3PhaseLineEdit, *calibrationH8PhaseLineEdit, *calibrationMotorCurrentPositionLineEdit,
175+
*AFEDIAG0LineEdit, *AFEDIAG1LineEdit, *AFEDIAG2LineEdit;
163176

164177
QLabel *rawAngleValueLabel, *rotationValueLabel, *angleValueLabel, *countValueLabel, *tempValueLabel,
165178
*motorAmaxValueLabel, *motorRotateVmaxValueLabel, *motorDmaxValueLabel, *motorDisableValueLabel,
@@ -249,10 +262,10 @@ public Q_SLOTS:
249262
ToolTemplate *createRegistersWidget();
250263
ToolTemplate *createUtilityWidget();
251264

252-
void readDeviceProperties();
265+
bool readDeviceProperties();
253266
void initializeADMT();
254267
bool readSequence();
255-
bool writeSequence(const map<string, int> &settings);
268+
bool writeSequence(QMap<string, int> settings);
256269
void applySequence();
257270
bool changeCNVPage(uint32_t page);
258271
void initializeMotor();
@@ -263,6 +276,7 @@ public Q_SLOTS:
263276
void stopCurrentMotorPositionMonitor();
264277
void currentMotorPositionTask(int sampleRate);
265278
bool resetGENERAL();
279+
void stopTasks();
266280

267281
#pragma region Acquisition Methods
268282
bool updateChannelValues();
@@ -383,6 +397,7 @@ public Q_SLOTS:
383397
QWidget *parent = nullptr);
384398
void configureCoeffRow(QWidget *container, QHBoxLayout *layout, QLabel *hLabel, QLabel *hMagLabel,
385399
QLabel *hPhaseLabel);
400+
void initializeChannelColors();
386401
#pragma endregion
387402

388403
#pragma region Connect Methods

plugins/admt/src/admtcontroller.cpp

+28-22
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@
2828
#include <cstdlib>
2929
#include <cstring>
3030
#include <iioutil/connectionprovider.h>
31-
#include <iomanip>
32-
#include <iostream>
3331
#include <iterator>
3432
#include <list>
3533
#include <math.h>
3634
#include <numeric>
3735
#include <string>
38-
#include <thread>
3936
#include <vector>
4037

4138
static const size_t maxAttrSize = 512;
@@ -252,13 +249,17 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
252249

253250
double *scaleVal = new double(1);
254251
int scaleRet = iio_channel_attr_read_double(channel, scaleAttr, scaleVal);
255-
if(scaleRet != 0)
252+
if(scaleRet != 0) {
253+
delete scaleVal;
256254
return static_cast<double>(UINT64_MAX); // return QString("Cannot read scale attribute");
255+
}
257256
scale = *scaleVal;
257+
delete scaleVal;
258258

259259
char *offsetDst = new char[maxAttrSize];
260260
iio_channel_attr_read(channel, offsetAttr, offsetDst, maxAttrSize);
261261
offsetAttrVal = std::atoi(offsetDst);
262+
delete[] offsetDst;
262263

263264
iio_buffer *iioBuffer = iio_device_create_buffer(admtDevice, bufferSize, false);
264265
if(iioBuffer == NULL)
@@ -273,14 +274,9 @@ double ADMTController::getChannelValue(const char *deviceName, const char *chann
273274
if(numBytesRead < 0)
274275
return static_cast<double>(UINT64_MAX); // return QString("Cannot refill buffer.");
275276

276-
pointerIncrement = reinterpret_cast<ptrdiff_t>(iio_buffer_step(iioBuffer));
277-
pointerEnd = static_cast<int8_t *>(iio_buffer_end(iioBuffer));
278-
279277
const struct iio_data_format *format = iio_channel_get_data_format(channel);
280278
const struct iio_data_format channelFormat = *format;
281279
unsigned int repeat = channelFormat.repeat;
282-
uint8_t bitLength = static_cast<uint8_t>(channelFormat.bits);
283-
size_t offset = static_cast<uint8_t>(channelFormat.shift);
284280

285281
QString result;
286282
std::list<char> rawSamples;
@@ -726,7 +722,6 @@ void ADMTController::getPreCalibrationFFT(const vector<double> &PANG, vector<dou
726722
void ADMTController::postcalibrate(vector<double> PANG, int cycleCount, int samplesPerCycle, bool CCW)
727723
{
728724
int circshiftData = 0;
729-
QString result = "";
730725

731726
/* Check CCW flag to know if array is to be reversed */
732727
if(CCW)
@@ -1220,12 +1215,12 @@ int ADMTController::getAbsAngleTurnCount(uint16_t registerValue)
12201215
// Bits 15:8: Turn count in quarter turns
12211216
uint8_t turnCount = (registerValue & 0xFF00) >> 8;
12221217

1223-
if(turnCount <= 0xD5) {
1218+
if(turnCount <= 0xD7) {
12241219
// Straight binary turn count
12251220
return turnCount / 4; // Convert from quarter turns to whole turns
1226-
} else if(turnCount == 0xD6) {
1221+
} else if(turnCount >= 0xD8 && turnCount <= 0xDB) {
12271222
// Invalid turn count
1228-
return -1;
1223+
return -10;
12291224
} else {
12301225
// 2's complement turn count
12311226
int8_t signedTurnCount = static_cast<int8_t>(turnCount); // Handle as signed value
@@ -1688,9 +1683,7 @@ int ADMTController::streamIO()
16881683
iio_library_get_version(&major, &minor, git_tag);
16891684
bool has_repeat = ((major * 10000) + minor) >= 8 ? true : false;
16901685

1691-
double *scaleAttrValue = new double(1);
16921686
int offsetAttrValue = 0;
1693-
char *offsetDst = new char[maxAttrSize];
16941687

16951688
if(!m_iioCtx)
16961689
return result; // Check if the context is valid
@@ -1703,13 +1696,19 @@ int ADMTController::streamIO()
17031696
iio_device_find_channel(admtDevice, channelName, isOutput); // Find the rotation channel
17041697
if(channel == NULL)
17051698
return result;
1706-
iio_channel_enable(channel); // Enable the channel
1699+
iio_channel_enable(channel);
1700+
double *scaleAttrValue = new double(1);
17071701
int scaleRet = iio_channel_attr_read_double(channel, scaleAttrName, scaleAttrValue); // Read the scale attribute
1708-
if(scaleRet != 0)
1702+
if(scaleRet != 0) {
1703+
delete scaleAttrValue;
17091704
return scaleRet;
1705+
}
1706+
1707+
char *offsetDst = new char[maxAttrSize];
17101708
iio_channel_attr_read(channel, offsetAttrName, offsetDst,
17111709
maxAttrSize); // Read the offset attribute
17121710
offsetAttrValue = atoi(offsetDst);
1711+
delete[] offsetDst;
17131712
struct iio_buffer *buffer = iio_device_create_buffer(admtDevice, samples, isCyclic); // Create a buffer
17141713

17151714
while(!stopStream) {
@@ -1739,6 +1738,7 @@ int ADMTController::streamIO()
17391738
}
17401739
}
17411740

1741+
delete scaleAttrValue;
17421742
iio_buffer_destroy(buffer);
17431743
return 0;
17441744
}
@@ -1765,9 +1765,7 @@ void ADMTController::bufferedStreamIO(int totalSamples, int targetSampleRate)
17651765
iio_library_get_version(&major, &minor, git_tag);
17661766
bool has_repeat = ((major * 10000) + minor) >= 8 ? true : false;
17671767

1768-
double *scaleAttrValue = new double(1);
17691768
int offsetAttrValue = 0;
1770-
char *offsetDst = new char[maxAttrSize];
17711769

17721770
if(!m_iioCtx)
17731771
return; // result; // Check if the context is valid
@@ -1779,11 +1777,16 @@ void ADMTController::bufferedStreamIO(int totalSamples, int targetSampleRate)
17791777
struct iio_channel *channel =
17801778
iio_device_find_channel(admtDevice, channelName, isOutput); // Find the rotation channel
17811779
if(channel == NULL)
1782-
return; // result;
1783-
iio_channel_enable(channel); // Enable the channel
1780+
return;
1781+
iio_channel_enable(channel);
1782+
double *scaleAttrValue = new double(1);
17841783
int scaleRet = iio_channel_attr_read_double(channel, scaleAttrName, scaleAttrValue); // Read the scale attribute
1785-
if(scaleRet != 0)
1784+
if(scaleRet != 0) {
1785+
delete scaleAttrValue;
17861786
return; // scaleRet;
1787+
}
1788+
1789+
char *offsetDst = new char[maxAttrSize];
17871790
iio_channel_attr_read(channel, offsetAttrName, offsetDst,
17881791
maxAttrSize); // Read the offset attribute
17891792
offsetAttrValue = atoi(offsetDst);
@@ -1831,6 +1834,9 @@ void ADMTController::bufferedStreamIO(int totalSamples, int targetSampleRate)
18311834
}
18321835

18331836
Q_EMIT streamBufferedData(bufferedValues);
1837+
1838+
delete scaleAttrValue;
1839+
delete[] offsetDst;
18341840
}
18351841

18361842
void ADMTController::handleStreamBufferedData(const QVector<double> &value) { streamBufferedValues = value; }

plugins/admt/src/admtplugin.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ bool ADMTPlugin::onConnect()
100100
harmonicCalibration = new HarmonicCalibration(m_admtController, isDebug);
101101
m_toolList[0]->setTool(harmonicCalibration);
102102

103+
connect(m_admtController, &ADMTController::requestDisconnect, this, &ADMTPlugin::disconnectDevice,
104+
Qt::QueuedConnection);
105+
103106
return true;
104107
}
105108

0 commit comments

Comments
 (0)