LocalizationRemoteGui.cpp
Go to the documentation of this file.
2
3#include <cmath>
4#include <mutex>
5
10
11#include <ArmarXGui/interface/RemoteGuiInterface.h>
16
18{
19
20 LocalizationRemoteGui::LocalizationRemoteGui(const RemoteGuiInterfacePrx& remoteGui,
22 const Eigen::Isometry3f& initialWorldToMap) :
23 remoteGui(remoteGui),
24 runningTask(new RunningTask<LocalizationRemoteGui>(this, &LocalizationRemoteGui::run)),
25 callee(callee),
26 initialWorldToMap(initialWorldToMap),
27 lastSentValues(Eigen::Vector3f::Zero())
28 {
29 createRemoteGuiTab();
30 tab = RemoteGui::TabProxy(remoteGui, REMOTE_GUI_TAB_NAME);
31 runningTask->start();
32 }
33
35 {
36 if (runningTask->isRunning())
37 {
38 runningTask->stop();
39 }
40 }
41
42 void
43 LocalizationRemoteGui::createRemoteGuiTab()
44 {
46 ARMARX_CHECK_NOT_NULL(remoteGui);
47
48 const float x0 = initialWorldToMap.translation().x();
49 const float y0 = initialWorldToMap.translation().y();
50 const float yaw0 =
51 std::atan2(initialWorldToMap.linear()(1, 0), initialWorldToMap.linear()(0, 0));
52
53 lastSentValues = {x0, y0, yaw0};
54
55 guiTab.worldToMap.group.setLabel("World to map registration");
56
57 constexpr float posMax = 1e6F; // [mm]
58 guiTab.worldToMap.x.setName("x");
59 guiTab.worldToMap.x.setRange(-posMax, posMax);
60 guiTab.worldToMap.x.setDecimals(1);
61 guiTab.worldToMap.x.setSteps(static_cast<int>(posMax / 10));
62 guiTab.worldToMap.x.setValue(x0);
63
64 guiTab.worldToMap.y.setName("y");
65 guiTab.worldToMap.y.setRange(-posMax, posMax);
66 guiTab.worldToMap.y.setDecimals(1);
67 guiTab.worldToMap.y.setSteps(static_cast<int>(posMax / 10));
68 guiTab.worldToMap.y.setValue(y0);
69
70 guiTab.worldToMap.yaw.setName("yaw");
71 guiTab.worldToMap.yaw.setRange(-static_cast<float>(M_PI), static_cast<float>(M_PI));
72 guiTab.worldToMap.yaw.setDecimals(4);
73 guiTab.worldToMap.yaw.setSteps(3600);
74 guiTab.worldToMap.yaw.setValue(yaw0);
75
76 guiTab.worldToMap.applyImmediately.setName("applyImmediately");
77 // guiTab.worldToMap.applyImmediately.setLabel("Apply immediately");
78 guiTab.worldToMap.applyImmediately.setValue(false);
79
80 guiTab.worldToMap.applyButton.setLabel("Apply");
81
83 int row = 0;
84
85 grid.add(armarx::RemoteGui::Client::Label("x [mm]"), {.row = row, .column = 0})
86 .add(guiTab.worldToMap.x, {.row = row, .column = 1});
87 ++row;
88
89 grid.add(armarx::RemoteGui::Client::Label("y [mm]"), {.row = row, .column = 0})
90 .add(guiTab.worldToMap.y, {.row = row, .column = 1});
91 ++row;
92
93 grid.add(armarx::RemoteGui::Client::Label("yaw [rad]"), {.row = row, .column = 0})
94 .add(guiTab.worldToMap.yaw, {.row = row, .column = 1});
95 ++row;
96
97 grid.add(armarx::RemoteGui::Client::Label("Apply immediately"), {.row = row, .column = 0})
98 .add(guiTab.worldToMap.applyImmediately, {.row = row, .column = 1});
99 ++row;
100
101 grid.add(
102 guiTab.worldToMap.applyButton, {.row = row, .column = 0}, {.rows = 1, .columns = 2});
103 ++row;
104
105 armarx::RemoteGui::Client::VBoxLayout root = {grid, armarx::RemoteGui::Client::VSpacer()};
106 guiTab.worldToMap.group.addChild(root);
107 guiTab.hbox.addChild(guiTab.worldToMap.group);
108
109 guiTab.create(REMOTE_GUI_TAB_NAME, remoteGui, guiTab.hbox);
110 }
111
112 void
113 LocalizationRemoteGui::run()
114 {
115 constexpr int kCycleDurationMs = 100;
116 CycleUtil c(kCycleDurationMs);
117
118 while (!runningTask->isStopped())
119 {
120 {
121 std::lock_guard g{mtx};
122 tab.receiveUpdates();
123 guiTab.receiveUpdates();
124 }
125
126 handleEvents(tab);
127
128 {
129 std::lock_guard g{mtx};
130 tab.sendUpdates();
131 guiTab.sendUpdates();
132 }
133
134 c.waitForCycleDuration();
135 }
136 }
137
138 void
139 LocalizationRemoteGui::handleEvents(RemoteGui::TabProxy& /*tab*/)
140 {
142 std::lock_guard g{handleEventsMtx};
143
144 const Eigen::Vector3f current{guiTab.worldToMap.x.getValue(),
145 guiTab.worldToMap.y.getValue(),
146 guiTab.worldToMap.yaw.getValue()};
147
148 const bool valuesChanged = (current - lastSentValues).cwiseAbs().maxCoeff() > 1e-5F;
149
150 const auto buildTransform = [&]() -> Eigen::Isometry3f
151 {
152 Eigen::Isometry3f t = Eigen::Isometry3f::Identity();
153 t.translation().x() = current.x();
154 t.translation().y() = current.y();
155 t.linear() =
156 Eigen::AngleAxisf(current.z(), Eigen::Vector3f::UnitZ()).toRotationMatrix();
157 return t;
158 };
159
160 if (valuesChanged and guiTab.worldToMap.applyImmediately.getValue())
161 {
162 lastSentValues = current;
163 callee.onWorldToMapTransformUpdate(buildTransform());
164 }
165
166 bool wasButtonClicked = false;
167 try
168 {
169 wasButtonClicked = guiTab.worldToMap.applyButton.wasClicked();
170 }
171 catch (armarx::LocalException&)
172 {
174 }
175
176 if (wasButtonClicked)
177 {
178 lastSentValues = current;
179 callee.onWorldToMapTransformUpdate(buildTransform());
180 }
181 }
182
183 void
185 {
186 if (!runningTask->isStopped())
187 {
188 runningTask->stop();
189 }
190 ARMARX_DEBUG << "Removing tab: " << REMOTE_GUI_TAB_NAME;
191 remoteGui->removeTab(REMOTE_GUI_TAB_NAME);
192 }
193
194 void
196 {
198 tab.internalSetDisabled(guiTab.worldToMap.applyButton.getName(), false);
199 tab.internalSetDisabled(guiTab.worldToMap.applyImmediately.getName(), false);
200 }
201
202 void
204 {
206 tab.internalSetDisabled(guiTab.worldToMap.applyButton.getName(), true);
207 tab.internalSetDisabled(guiTab.worldToMap.applyImmediately.getName(), true);
208 }
209
210} // namespace armarx::localization_and_mapping::components::cartographer_localization_and_mapping
#define M_PI
Definition MathTools.h:17
constexpr T c
LocalizationRemoteGui(const RemoteGuiInterfacePrx &remoteGui, LocalizationRemoteGuiCallee &callee, const Eigen::Isometry3f &initialWorldToMap)
#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 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 setName(std::string const &name)
Definition Widgets.cpp:19
#define ARMARX_TRACE
Definition trace.h:77