Skip to content

Commit e022156

Browse files
Added encoders testing
1 parent bbf68a5 commit e022156

16 files changed

+280
-46
lines changed

trikControl/src/configurerHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ qreal ConfigurerHelper::configureReal(const trikKernel::Configurer &configurer,
4343
, const QString &parameterName)
4444
{
4545
bool ok = false;
46-
int parameter = configurer.attributeByPort(port, parameterName).toDouble(&ok);
46+
const qreal parameter = configurer.attributeByPort(port, parameterName).toDouble(&ok);
4747
if (!ok) {
4848
QLOG_ERROR() << QString("Incorrect configuration for parameter \"%1\" for port \"%2\": \"%3\" ")
4949
.arg(parameterName).arg(port).arg(configurer.attributeByPort(port, parameterName));

trikControl/src/encoder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ int Encoder::read()
5757
if (status() == DeviceInterface::Status::ready) {
5858
QByteArray command(2, '\0');
5959
command[0] = static_cast<char>(mI2cCommandNumber);
60-
int data = mCommunicator.read(command);
61-
60+
const int data = mCommunicator.read(command);
6261
return data / mTicksInDegree;
6362
} else {
6463
return 0;

trikControl/system-config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ equal to its class name.
167167
<sonarSensor class="rangeSensor" rawValue1="750" rawValue2="400" normalizedValue1="10" normalizedValue2="20" />
168168
<lightSensor class="digitalSensor" min="30000" max="350000" />
169169
<volumeSensor class="digitalSensor" min="0" max="100" />
170-
<encoder95 class="encoder" ticksInDegree="43.389445" />
170+
<!-- <encoder95 class="encoder" ticksInDegree="43.389445" /> -->
171+
<encoder95 class="encoder" ticksInDegree="1.569444" />
171172
<encoder126 class="encoder" ticksInDegree="32.964168" />
172173
<colorSensor3x3 class="colorSensor" m="3" n="3" />
173174
</deviceTypes>

trikGui/abstractIndicator.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright 2015 CyberTech Labs Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <QtCore/qglobal.h>
18+
19+
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
20+
#include <QtGui/QWidget>
21+
#else
22+
#include <QtWidgets/QWidget>
23+
#endif
24+
25+
namespace trikGui {
26+
27+
/// Base class for a widget that can show some reading from sensor, encoder and so on.
28+
class AbstractIndicator : public QWidget
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
/// Constructor.
34+
/// @param parent - parent of this widget in Qt widget parent-child system.
35+
AbstractIndicator(QWidget *parent = 0) : QWidget(parent) {}
36+
37+
public slots:
38+
/// Rereads sensor and updates widget contents.
39+
virtual void renew() = 0;
40+
};
41+
42+
}

trikGui/encoderIndicator.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2015 CyberTech Labs Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#include "encoderIndicator.h"
16+
17+
#include <QtCore/QString>
18+
19+
#include <trikControl/encoderInterface.h>
20+
21+
using namespace trikGui;
22+
23+
EncoderIndicator::EncoderIndicator(const QString &port
24+
, trikControl::EncoderInterface &encoder
25+
, QWidget *parent)
26+
: AbstractIndicator(parent)
27+
, mEncoder(encoder)
28+
, mNameLabel(port)
29+
, mValueLabel("0")
30+
{
31+
mNameLabel.setAlignment(Qt::AlignCenter);
32+
mValueLabel.setAlignment(Qt::AlignCenter);
33+
34+
mValueDial.setMinimum(0);
35+
mValueDial.setMaximum(360);
36+
mValueDial.setWrapping(true);
37+
38+
mLayout.addWidget(&mNameLabel);
39+
mLayout.addWidget(&mValueDial);
40+
mLayout.addWidget(&mValueLabel);
41+
setLayout(&mLayout);
42+
43+
setFocusPolicy(Qt::StrongFocus);
44+
}
45+
46+
void EncoderIndicator::renew()
47+
{
48+
const int value = mEncoder.read();
49+
mValueLabel.setText(QString::number(value));
50+
mValueDial.setValue(value);
51+
}

trikGui/encoderIndicator.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* Copyright 2015 CyberTech Labs Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <QtCore/qglobal.h>
18+
19+
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
20+
#include <QtGui/QHBoxLayout>
21+
#include <QtGui/QLabel>
22+
#include <QtGui/QDial>
23+
#else
24+
#include <QtWidgets/QHBoxLayout>
25+
#include <QtWidgets/QLabel>
26+
#include <QtWidgets/QDial>
27+
#endif
28+
29+
#include "abstractIndicator.h"
30+
31+
namespace trikControl {
32+
class EncoderInterface;
33+
}
34+
35+
namespace trikGui {
36+
37+
/// Widget that shows current encoder reading.
38+
class EncoderIndicator : public AbstractIndicator
39+
{
40+
Q_OBJECT
41+
42+
public:
43+
/// Constructor.
44+
/// @param port - port to which sensor is plugged.
45+
/// @param encoder - encoder which we will read.
46+
/// @param parent - parent of this widget in Qt widget parent-child system.
47+
EncoderIndicator(const QString &port, trikControl::EncoderInterface &encoder, QWidget *parent = 0);
48+
49+
public slots:
50+
void renew() override;
51+
52+
private:
53+
trikControl::EncoderInterface &mEncoder;
54+
55+
QHBoxLayout mLayout;
56+
QLabel mNameLabel;
57+
QDial mValueDial;
58+
QLabel mValueLabel;
59+
};
60+
61+
}

trikGui/sensorIndicator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace trikGui;
3131
SensorIndicator::SensorIndicator(const QString &port
3232
, trikControl::SensorInterface &sensor
3333
, QWidget *parent)
34-
: QWidget(parent)
34+
: AbstractIndicator(parent)
3535
, mSensor(sensor)
3636
, mMaxValue(100)
3737
, mMinValue(0)

trikGui/sensorIndicator.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@
1717
#include <QtCore/qglobal.h>
1818

1919
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
20-
#include <QtGui/QWidget>
2120
#include <QtGui/QHBoxLayout>
2221
#include <QtGui/QLabel>
2322
#include <QtGui/QProgressBar>
2423
#else
25-
#include <QtWidgets/QWidget>
2624
#include <QtWidgets/QHBoxLayout>
2725
#include <QtWidgets/QLabel>
2826
#include <QtWidgets/QProgressBar>
2927
#endif
3028

29+
#include "abstractIndicator.h"
30+
3131
namespace trikControl {
3232
class SensorInterface;
3333
}
3434

3535
namespace trikGui {
3636

3737
/// Widget that shows current sensor reading.
38-
class SensorIndicator : public QWidget
38+
class SensorIndicator : public AbstractIndicator
3939
{
4040
Q_OBJECT
4141

@@ -47,8 +47,7 @@ class SensorIndicator : public QWidget
4747
SensorIndicator(const QString &port, trikControl::SensorInterface &sensor, QWidget *parent = 0);
4848

4949
public slots:
50-
/// Rereads sensor and updates widget contents.
51-
void renew();
50+
void renew() override;
5251

5352
private:
5453
trikControl::SensorInterface &mSensor;

trikGui/sensorsSelectionWidget.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,29 @@
2121
using namespace trikGui;
2222

2323
SensorsSelectionWidget::SensorsSelectionWidget(trikControl::BrickInterface &brick
24-
, trikControl::SensorInterface::Type type
24+
, SensorType type
2525
, QWidget *parent)
2626
: TrikGuiDialog(parent)
2727
, mTitle(tr("Select sensors for testing:"))
28+
, mSensorType(type)
2829
, mBrick(brick)
2930
{
30-
QStringList ports = mBrick.sensorPorts(type);
31+
QStringList ports;
32+
33+
switch (type) {
34+
case SensorType::analogSensor: {
35+
ports = mBrick.sensorPorts(trikControl::SensorInterface::Type::analogSensor);
36+
}
37+
case SensorType::digitalSensor: {
38+
ports = mBrick.sensorPorts(trikControl::SensorInterface::Type::digitalSensor);
39+
}
40+
case SensorType::encoder: {
41+
ports = mBrick.encoderPorts();
42+
}
43+
}
44+
45+
ports.sort();
46+
3147
for (const QString &port : ports) {
3248
QListWidgetItem *item = new QListWidgetItem(port, &mList);
3349
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
@@ -48,17 +64,17 @@ SensorsSelectionWidget::SensorsSelectionWidget(trikControl::BrickInterface &bric
4864
setLayout(&mLayout);
4965
}
5066

51-
QString SensorsSelectionWidget::menuEntry(trikControl::SensorInterface::Type type)
67+
QString SensorsSelectionWidget::menuEntry(SensorType type)
5268
{
5369
switch (type) {
54-
case trikControl::SensorInterface::Type::analogSensor: {
70+
case SensorType::analogSensor: {
5571
return tr("Test analog sensors");
5672
}
57-
case trikControl::SensorInterface::Type::digitalSensor: {
73+
case SensorType::digitalSensor: {
5874
return tr("Test digital sensors");
5975
}
60-
case trikControl::SensorInterface::Type::specialSensor: {
61-
return QString();
76+
case SensorType::encoder: {
77+
return tr("Test encoders");
6278
}
6379
}
6480

@@ -110,7 +126,12 @@ void SensorsSelectionWidget::startTesting()
110126
}
111127
}
112128

113-
SensorsWidget sensorsWidget(mBrick, ports);
129+
const auto sensorType = mSensorType == SensorType::analogSensor || mSensorType == SensorType::digitalSensor
130+
? SensorsWidget::SensorType::analogOrDigitalSensor
131+
: SensorsWidget::SensorType::encoder
132+
;
133+
134+
SensorsWidget sensorsWidget(mBrick, ports, sensorType);
114135
emit newWidget(sensorsWidget);
115136
if (sensorsWidget.exec() == TrikGuiDialog::goHomeExit) {
116137
goHome();

trikGui/sensorsSelectionWidget.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ class SensorsSelectionWidget : public TrikGuiDialog
4444
Q_OBJECT
4545

4646
public:
47+
enum class SensorType {
48+
analogSensor
49+
, digitalSensor
50+
, encoder
51+
};
52+
4753
SensorsSelectionWidget(trikControl::BrickInterface &brick
48-
, trikControl::SensorInterface::Type type
54+
, SensorType type
4955
, QWidget *parent = 0);
5056

5157
/// Returns main menu entry string for this widget.
52-
static QString menuEntry(trikControl::SensorInterface::Type type);
58+
static QString menuEntry(SensorType type);
5359

5460
void renewFocus() override;
5561

@@ -63,6 +69,7 @@ class SensorsSelectionWidget : public TrikGuiDialog
6369
QVBoxLayout mLayout;
6470
QLabel mTitle;
6571
QListWidget mList;
72+
const SensorType mSensorType;
6673

6774
trikControl::BrickInterface &mBrick;
6875
};

0 commit comments

Comments
 (0)