LocalizationRemoteGui.cpp
Go to the documentation of this file.
2
3#include <cmath>
4#include <mutex>
5
10
14#include <ArmarXGui/interface/RemoteGuiInterface.h>
16
18{
19
20 LocalizationRemoteGui::LocalizationRemoteGui(const RemoteGuiInterfacePrx& remoteGui,
22 remoteGui(remoteGui),
23 runningTask(new RunningTask<LocalizationRemoteGui>(this, &LocalizationRemoteGui::run)),
24 callee(callee)
25 {
26 createRemoteGuiTab();
27
28 tab = RemoteGui::TabProxy(remoteGui, REMOTE_GUI_TAB_MAME);
29
30 runningTask->start();
31 }
32
34 {
35 if (runningTask->isRunning())
36 {
37 runningTask->stop();
38 }
39 }
40
41 void
42 LocalizationRemoteGui::createRemoteGuiTab()
43 {
45
46 ARMARX_CHECK_NOT_NULL(remoteGui); // createRemoteGui() has to be called first
47
48 {
49 guiTab.localizationCorrection.group.setLabel("Localization correction");
50
51 const float posMax = 10'000;
52 guiTab.localizationCorrection.x.setName("x");
53 guiTab.localizationCorrection.x.setRange(-posMax, posMax);
54 guiTab.localizationCorrection.x.setDecimals(0);
55 guiTab.localizationCorrection.x.setSteps(int(posMax / 10));
56 guiTab.localizationCorrection.x.setValue(0.F);
57
58 guiTab.localizationCorrection.y.setName("y");
59 guiTab.localizationCorrection.y.setRange(-posMax, posMax);
60 guiTab.localizationCorrection.y.setDecimals(0);
61 guiTab.localizationCorrection.y.setSteps(int(posMax / 10));
62 guiTab.localizationCorrection.y.setValue(0.F);
63
64 const float angleMax = M_PIf32;
65
66 guiTab.localizationCorrection.yaw.setName("yaw");
67 guiTab.localizationCorrection.yaw.setRange(-angleMax, angleMax);
68 // guiTab.localizationCorrection.yaw.setDecimals(2);
69 guiTab.localizationCorrection.yaw.setSteps(360);
70 guiTab.localizationCorrection.yaw.setValue(0.F);
71
72 guiTab.controlGroup.applyButton.setLabel("Apply");
73
75 int row = 10;
76 {
77
78 grid.add(guiTab.localizationCorrection.x, {row, 1});
79 ++row;
80
81 grid.add(guiTab.localizationCorrection.y, {row, 1});
82 ++row;
83
84 grid.add(guiTab.localizationCorrection.yaw, {row, 1});
85 ++row;
86
87 grid.add(guiTab.controlGroup.applyButton, {row, 1});
88 ++row;
89 }
90
93 guiTab.localizationCorrection.group.addChild(root);
94 }
95
96 guiTab.hbox.addChild(guiTab.localizationCorrection.group);
97
98 guiTab.create(REMOTE_GUI_TAB_MAME, remoteGui, guiTab.hbox);
99
100 // boxLayout.addChild(line);
101 // remoteGui->createTab(REMOTE_GUI_TAB_MAME, guiTab.hbox);
102 }
103
104 void
105 LocalizationRemoteGui::run()
106 {
107 constexpr int kCycleDurationMs = 100;
108
109 CycleUtil c(kCycleDurationMs);
110 while (!runningTask->isStopped())
111 {
112 {
113
114 {
115 std::lock_guard g{mtx};
116
118 tab.receiveUpdates();
119 guiTab.receiveUpdates();
120 }
121
123 handleEvents(tab);
124
125 {
126 std::lock_guard g{mtx};
127
129 tab.sendUpdates();
130 guiTab.sendUpdates();
131 }
132 }
133
134 c.waitForCycleDuration();
135 }
136 }
137
138 void
139 LocalizationRemoteGui::handleEvents(RemoteGui::TabProxy& tab)
140 {
142
143 std::lock_guard g{handleEventsMtx};
144
145 const Eigen::Vector3f correction{guiTab.localizationCorrection.x.getValue(),
146 guiTab.localizationCorrection.y.getValue(),
147 guiTab.localizationCorrection.yaw.getValue()};
148 // ARMARX_INFO << correction;
149
150 const Eigen::Vector3f diff = correction - localizationCorrection;
151
152 // check if values have changed
153 if ((diff.cwiseAbs().array() > 0.01F).any())
154 {
155 localizationCorrection = correction;
156
157 const Eigen::Isometry3f trafo =
158 Eigen::Translation3f(Eigen::Vector3f{correction.x(), correction.y(), 0.F}) *
159 Eigen::Isometry3f(Eigen::AngleAxisf(correction.z(), Eigen::Vector3f::UnitZ()));
160 // ARMARX_INFO << "Localization correction";
161 callee.onLocalizationCorrection(trafo);
162 }
163
164 bool wasButtonClicked = false;
165
166 // wasClicked can throw: "Reason: Could find value with name: 6"
167 try
168 {
169 wasButtonClicked = guiTab.controlGroup.applyButton.wasClicked();
170 }
171 catch (armarx::LocalException& e)
172 {
174 }
175
176 if (wasButtonClicked)
177 {
179 callee.onApplyLocalizationCorrection();
180 }
181 }
182
183 void
185 {
186 // std::lock_guard g{mtx};
187
188 if (!runningTask->isStopped())
189 {
190 runningTask->stop();
191 }
192
193 ARMARX_DEBUG << "Removing tab: " << REMOTE_GUI_TAB_MAME;
194 remoteGui->removeTab(REMOTE_GUI_TAB_MAME);
195 }
196
197 void
199 {
201
202 // std::lock_guard g{mtx};
203 tab.internalSetDisabled(guiTab.controlGroup.applyButton.getName(), false);
204 }
205
206 void
208 {
210
211 // std::unique_lock g{mtx};
212
213 // it might happen, that this function is triggered from handleEvents
214 // std::unique_lock ul{handleEventsMtx};
215 // ul.try_lock();
216
217 tab.internalSetDisabled(guiTab.controlGroup.applyButton.getName(), true);
218 }
219
220 void
222 {
224
225 // std::lock_guard g{mtx};
226
227 guiTab.localizationCorrection.x.setValue(0.F);
228 guiTab.localizationCorrection.y.setValue(0.F);
229 guiTab.localizationCorrection.yaw.setValue(0.F);
230
231 tab.sendUpdates();
232 guiTab.sendUpdates();
233 }
234
235} // namespace armarx::localization_and_mapping::components::cartographer_localization_and_mapping
constexpr T c
LocalizationRemoteGui(const RemoteGuiInterfacePrx &remoteGui, LocalizationRemoteGuiCallee &callee)
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
std::string GetHandledExceptionString()
void setLabel(std::string const &label)
Definition Widgets.cpp:123
void addChild(Widget const &child)
Definition Widgets.cpp:95
void setRange(float min, float max)
Definition Widgets.cpp:375
void setRange(float min, float max)
Definition Widgets.cpp:330
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition Widgets.cpp:438
void setLabel(std::string const &text)
Definition Widgets.cpp:420
void create(std::string const &name, Ice::ObjectPrx const &remoteGuiObject, Widget const &rootWidget)
Definition Tab.cpp:31
void setName(std::string const &name)
Definition Widgets.cpp:19
#define ARMARX_TRACE
Definition trace.h:77