LaserScannerPluginWidgetController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * \package RobotAPI::gui-plugins::LaserScannerPluginWidgetController
17  * \author Fabian Paus ( fabian dot paus at kit dot edu )
18  * \date 2017
19  * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <QGraphicsView>
26 #include <QGraphicsLineItem>
27 
28 #include <string>
29 
30 using namespace armarx;
31 
33 {
34  widget.setupUi(getWidget());
35 }
36 
37 
39 {
40 
41 }
42 
43 
45 {
46 
47 }
48 
50 {
51 
52 }
53 
54 
56 {
57  usingProxy(laserScannerUnitName);
58 
59  connect(this, SIGNAL(newSensorValuesReported()), this, SLOT(onNewSensorValuesReported()), Qt::QueuedConnection);
60  connect(widget.deviceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onDeviceSelected(int)));
61 }
62 
63 
65 {
66  laserScannerUnit = getProxy<LaserScannerUnitInterfacePrx>(laserScannerUnitName);
67  std::string topicName = laserScannerUnit->getReportTopicName();
68  usingTopic(topicName);
69 
70  laserScanners = laserScannerUnit->getConnectedDevices();
71 }
72 
73 QPointer<QDialog> LaserScannerPluginWidgetController::getConfigDialog(QWidget* parent)
74 {
75  if (!dialog)
76  {
77  dialog = new SimpleConfigDialog(parent);
78  dialog->addProxyFinder<LaserScannerUnitInterfacePrx>({"LaserScannerUnit", "", "*"});
79  }
80  return qobject_cast<SimpleConfigDialog*>(dialog);
81 }
82 
84 {
85  if (dialog)
86  {
87  laserScannerUnitName = dialog->getProxyName("LaserScannerUnit");
88  }
89 }
90 
91 void LaserScannerPluginWidgetController::reportSensorValues(const std::string& device, const std::string& name, const LaserScan& newScan, const TimestampBasePtr& timestamp, const Ice::Current& c)
92 {
93  {
94  std::unique_lock lock(scanMutex);
95 
96  LaserScan& scan = scans[device];
97  // TODO: Do some filtering? aggregation?
98  scan = newScan;
99  }
100 
102 }
103 
105 {
106  QComboBox* deviceBox = widget.deviceComboBox;
107 
108  std::unique_lock lock(scanMutex);
109  for (auto& pair : scans)
110  {
111  QString deviceName(QString::fromStdString(pair.first.c_str()));
112  if (deviceBox->findText(deviceName) < 0)
113  {
114  deviceBox->addItem(deviceName);
115  }
116  }
117 
118  std::string deviceName(deviceBox->currentText().toUtf8().data());
119  float stepSize = 0.25f;
120  for (LaserScannerInfo const& scanner : laserScanners)
121  {
122  if (scanner.device == deviceName)
123  {
124  stepSize = scanner.stepSize;
125  }
126  }
127 
128  QGraphicsView* view = widget.graphicsView;
129 
130  scene.reset(new QGraphicsScene());
131  widget.graphicsView->setScene(scene.get());
132  int outerR = std::min(view->width() / 2, view->height() / 2);
133  scene->addEllipse(-outerR, -outerR, 2 * outerR, 2 * outerR, QPen(QColor(255, 255, 255)));
134  int r = outerR - 10;
135  //scene.addEllipse(-r, -r, 2 * r, 2 * r, QPen(QColor(200, 200, 200)));
136  QColor stepColor(QColor::fromRgb(100, 100, 255));
137  QPen stepPen(stepColor);
138  QBrush stepBrush(stepColor);
139  auto line = [&](float angle, float d)
140  {
141  float di = d * r;
142  QGraphicsEllipseItem* item = scene->addEllipse(-di, -di, 2 * di, 2 * di, stepPen, stepBrush);
143  // Angles for Qt ellipse are in 16th of degree (who thought that would be a great idea?)
144  item->setStartAngle(std::round(16.0f * angle * 180.0 / M_PI) + 90 * 16);
145  item->setSpanAngle(std::round(stepSize * 16.0f * 180.0 / M_PI));
146  };
147 
148  LaserScan& scan = scans[deviceName];
149  float maxDistance = 1000.0f;
150  for (LaserScanStep& step : scan)
151  {
152  if (step.distance > maxDistance)
153  {
154  maxDistance = step.distance;
155  }
156  }
157  float ringDistance = 1000.0f;
158  int numberOfRings = (std::size_t)std::ceil(maxDistance / ringDistance);
159  std::deque<int>& history = numberOfRingsHistory[deviceName];
160  history.push_back(numberOfRings);
161  if (history.size() > 256)
162  {
163  history.pop_front();
164  }
165  int maxNumberOfRings = *std::max_element(history.begin(), history.end());
166  float outerRadius = maxNumberOfRings * ringDistance;
167 
168  for (LaserScanStep& step : scan)
169  {
170  line(step.angle, step.distance / outerRadius);
171  }
172 
173  for (int ringIndex = 1; ringIndex <= maxNumberOfRings; ++ringIndex)
174  {
175  float ri = 1.0f * ringIndex / maxNumberOfRings * r;
176  scene->addEllipse(-ri, -ri, 2 * ri, 2 * ri, QPen(QColor(200, 200, 200)));
177  }
178 
179  view->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
180 }
181 
183 {
184  if (index < 0 && index >= widget.deviceComboBox->count())
185  {
186  return;
187  }
188  std::string deviceName = widget.deviceComboBox->itemText(index).toStdString();
189  for (LaserScannerInfo const& scanner : laserScanners)
190  {
191  if (scanner.device == deviceName)
192  {
193  widget.frameLabel->setText(QString::fromStdString(scanner.frame));
194  widget.minAngleLabel->setText(QString::number(scanner.minAngle));
195  widget.maxAngleLabel->setText(QString::number(scanner.minAngle));
196  widget.stepSizeLabel->setText(QString::number(scanner.stepSize));
197  }
198  }
199 
200 }
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::LaserScannerPluginWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Definition: LaserScannerPluginWidgetController.cpp:44
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::LaserScannerPluginWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: LaserScannerPluginWidgetController.cpp:49
armarx::LaserScannerPluginWidgetController::onConnectComponent
void onConnectComponent() override
Definition: LaserScannerPluginWidgetController.cpp:64
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::LaserScannerPluginWidgetController::reportSensorValues
void reportSensorValues(const std::string &device, const std::string &name, const LaserScan &scan, const TimestampBasePtr &timestamp, const Ice::Current &c) override
Definition: LaserScannerPluginWidgetController.cpp:91
armarx::LaserScannerPluginWidgetController::LaserScannerPluginWidgetController
LaserScannerPluginWidgetController()
Controller Constructor.
Definition: LaserScannerPluginWidgetController.cpp:32
armarx::LaserScannerPluginWidgetController::onDeviceSelected
void onDeviceSelected(int index)
Definition: LaserScannerPluginWidgetController.cpp:182
armarx::LaserScannerPluginWidgetController::onInitComponent
void onInitComponent() override
Definition: LaserScannerPluginWidgetController.cpp:55
LaserScannerPluginWidgetController.h
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
armarx::LaserScannerPluginWidgetController::getConfigDialog
QPointer< QDialog > getConfigDialog(QWidget *parent) override
getConfigDialog returns a pointer to the a configuration widget of this controller.
Definition: LaserScannerPluginWidgetController.cpp:73
armarx::LaserScannerPluginWidgetController::onNewSensorValuesReported
void onNewSensorValuesReported()
Definition: LaserScannerPluginWidgetController.cpp:104
armarx::LaserScannerPluginWidgetController::~LaserScannerPluginWidgetController
~LaserScannerPluginWidgetController() override
Controller destructor.
Definition: LaserScannerPluginWidgetController.cpp:38
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::LaserScannerPluginWidgetController::configured
void configured() override
This function must be implemented by the user, if he supplies a config dialog.
Definition: LaserScannerPluginWidgetController.cpp:83
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::LaserScannerPluginWidgetController::newSensorValuesReported
void newSensorValuesReported()
armarx::SimpleConfigDialog
A config-dialog containing one (or multiple) proxy finders.
Definition: SimpleConfigDialog.h:84