Component.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 navigation::ArmarXObjects::distance_to_obstacle_costmap_provider
17  * @author Fabian Reister ( fabian dot reister at kit dot edu )
18  * @date 2022
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #include "Component.h"
25 
26 #include <SimoxUtility/algorithm/string/string_tools.h>
27 #include <VirtualRobot/SceneObjectSet.h>
28 
34 
37 
40 
41 #include <range/v3/view/for_each.hpp>
42 
44 {
46  {
47  addPlugin(virtualRobotReaderPlugin);
48  addPlugin(costmapWriterPlugin);
49  }
50 
51  const std::string Component::defaultName = "distance_to_obstacle_costmap_provider";
52 
55  {
58 
59  def->required(properties.robotName, "p.robotName", "Robot name.");
60 
61  return def;
62  }
63 
64  void
66  {
67  // Topics and properties defined above are automagically registered.
68 
69  // Keep debug observer data until calling `sendDebugObserverBatch()`.
70  // (Requies the armarx::DebugObserverComponentPluginUser.)
71  // setDebugObserverBatchModeEnabled(true);
72  }
73 
74  void
76  {
77  // Do things after connecting to topics and components.
78 
79  /* (Requies the armarx::DebugObserverComponentPluginUser.)
80  // Use the debug observer to log data over time.
81  // The data can be viewed in the ObserverView and the LivePlotter.
82  // (Before starting any threads, we don't need to lock mutexes.)
83  {
84  setDebugObserverDatafield("numBoxes", properties.numBoxes);
85  setDebugObserverDatafield("boxLayerName", properties.boxLayerName);
86  sendDebugObserverBatch();
87  }
88  */
89 
90  /* (Requires the armarx::ArVizComponentPluginUser.)
91  // Draw boxes in ArViz.
92  // (Before starting any threads, we don't need to lock mutexes.)
93  drawBoxes(properties, arviz);
94  */
95 
96  /* (Requires the armarx::LightweightRemoteGuiComponentPluginUser.)
97  // Setup the remote GUI.
98  {
99  createRemoteGuiTab();
100  RemoteGui_startRunningTask();
101  }
102  */
103 
104  runningTask = new RunningTask<Component>(this, &Component::run, "createAndStoreCostmap");
105  runningTask->start();
106  }
107 
108  void
110  {
111  // FIXME: whenever the static scene changes, update the costmap
112 
114  }
115 
116  bool
118  {
119  const std::vector<std::string> primitiveModelIds = [&]() -> std::vector<std::string>
120  {
121  if (not properties.primitiveModelIds.empty())
122  {
123  const std::vector<std::string> primitiveModelIds =
124  simox::alg::split(properties.primitiveModelIds, ",", true, false);
125  ARMARX_INFO << VAROUT(primitiveModelIds);
126  return primitiveModelIds;
127  }
128 
129  return {};
130  }();
131 
132  const auto loadMode = primitiveModelIds.empty()
133  ? VirtualRobot::RobotIO::RobotDescription::eCollisionModel
134  : VirtualRobot::RobotIO::RobotDescription::eStructure;
135 
136  const auto robot = virtualRobotReaderPlugin->get().getRobot(
137  properties.robotName, armarx::DateTime::Invalid(), loadMode);
138 
139  if (not primitiveModelIds.empty())
140  {
141  ARMARX_INFO << "Using primitive approximation model";
142  robot->setPrimitiveApproximationModel(primitiveModelIds);
143  }
144 
145 
146  ARMARX_CHECK_NOT_NULL(robot);
147 
148  const auto objectPoseClient = ObjectPoseClientPluginUser::getClient();
149 
150  const objpose::ObjectPoseSeq objectPoses = objectPoseClient.fetchObjectPoses();
151 
152  // remove those objects that belong to an object dataset. the manipulation object / distance computation is broken
153  const objpose::ObjectPoseSeq objectPosesStatic =
155 
156  const VirtualRobot::SceneObjectSetPtr objects =
157  armarx::navigation::util::asSceneObjects(objectPosesStatic);
158 
159  ARMARX_CHECK_NOT_NULL(objects);
160  ARMARX_INFO << objects->getSize() << " objects in the scene";
161 
162  ARMARX_INFO << "Static objects";
163  for (const objpose::ObjectPose& objectPose : objectPosesStatic)
164  {
165  ARMARX_INFO << " - " << objectPose.objectID;
166  }
167 
168  ARMARX_INFO << "Creating costmap";
169 
170  // FIXME: move costmap creation out of this component
171  // FIXME create costmap writer enum: type of costmaps
172  algorithms::CostmapBuilder costmapBuilder(
173  robot,
174  objects,
175  algorithms::Costmap::Parameters{.binaryGrid = false, .cellSize = 100},
176  "Platform-navigation-colmodel");
177 
178  const auto costmap = costmapBuilder.create();
179 
180  ARMARX_INFO << "Storing costmap in the memory.";
181  return costmapWriterPlugin->get().store(
182  costmap, "distance_to_obstacles", getName(), armarx::Clock::Now());
183  }
184 
185  void
187  {
188  runningTask->stop();
189  }
190 
191  void
193  {
194  }
195 
196  std::string
198  {
199  return Component::defaultName;
200  }
201 
202  std::string
204  {
205  return Component::defaultName;
206  }
207 
208  /* (Requires the armarx::LightweightRemoteGuiComponentPluginUser.)
209  void
210  Component::createRemoteGuiTab()
211  {
212  using namespace armarx::RemoteGui::Client;
213 
214  // Setup the widgets.
215 
216  tab.boxLayerName.setValue(properties.boxLayerName);
217 
218  tab.numBoxes.setValue(properties.numBoxes);
219  tab.numBoxes.setRange(0, 100);
220 
221  tab.drawBoxes.setLabel("Draw Boxes");
222 
223  // Setup the layout.
224 
225  GridLayout grid;
226  int row = 0;
227  {
228  grid.add(Label("Box Layer"), {row, 0}).add(tab.boxLayerName, {row, 1});
229  ++row;
230 
231  grid.add(Label("Num Boxes"), {row, 0}).add(tab.numBoxes, {row, 1});
232  ++row;
233 
234  grid.add(tab.drawBoxes, {row, 0}, {2, 1});
235  ++row;
236  }
237 
238  VBoxLayout root = {grid, VSpacer()};
239  RemoteGui_createTab(getName(), root, &tab);
240  }
241 
242 
243  void
244  Component::RemoteGui_update()
245  {
246  if (tab.boxLayerName.hasValueChanged() || tab.numBoxes.hasValueChanged())
247  {
248  std::scoped_lock lock(propertiesMutex);
249  properties.boxLayerName = tab.boxLayerName.getValue();
250  properties.numBoxes = tab.numBoxes.getValue();
251 
252  {
253  setDebugObserverDatafield("numBoxes", properties.numBoxes);
254  setDebugObserverDatafield("boxLayerName", properties.boxLayerName);
255  sendDebugObserverBatch();
256  }
257  }
258  if (tab.drawBoxes.wasClicked())
259  {
260  // Lock shared variables in methods running in seperate threads
261  // and pass them to functions. This way, the called functions do
262  // not need to think about locking.
263  std::scoped_lock lock(propertiesMutex, arvizMutex);
264  drawBoxes(properties, arviz);
265  }
266  }
267  */
268 
269 
270  /* (Requires the armarx::ArVizComponentPluginUser.)
271  void
272  Component::drawBoxes(const Component::Properties& p, viz::Client& arviz)
273  {
274  // Draw something in ArViz (requires the armarx::ArVizComponentPluginUser.
275  // See the ArVizExample in RobotAPI for more examples.
276 
277  viz::Layer layer = arviz.layer(p.boxLayerName);
278  for (int i = 0; i < p.numBoxes; ++i)
279  {
280  layer.add(viz::Box("box_" + std::to_string(i))
281  .position(Eigen::Vector3f(i * 100, 0, 0))
282  .size(20).color(simox::Color::blue()));
283  }
284  arviz.commit(layer);
285  }
286  */
287 
288 
290 
291 } // namespace armarx::navigation::components::distance_to_obstacle_costmap_provider
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::createAndStoreCostmap
bool createAndStoreCostmap()
Definition: Component.cpp:117
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::onDisconnectComponent
void onDisconnectComponent() override
Definition: Component.cpp:186
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::GetDefaultName
static std::string GetDefaultName()
Get the component's default name.
Definition: Component.cpp:203
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::onConnectComponent
void onConnectComponent() override
Definition: Component.cpp:75
DateTime.h
CostmapBuilder.h
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:54
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
RunningTask.h
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
Clock.h
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::onInitComponent
void onInitComponent() override
Definition: Component.cpp:65
armarx::navigation::algorithms::Costmap::Parameters::binaryGrid
bool binaryGrid
Definition: Costmap.h:24
util.h
ObjectPose.h
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::run
void run()
Definition: Component.cpp:109
armarx::navigation::util::asSceneObjects
VirtualRobot::SceneObjectSetPtr asSceneObjects(const objpose::ObjectPoseSeq &objectPoses)
Definition: util.cpp:106
armarx::navigation::algorithms::CostmapBuilder
Definition: CostmapBuilder.h:31
armarx::navigation::components::distance_to_obstacle_costmap_provider::ARMARX_REGISTER_COMPONENT_EXECUTABLE
ARMARX_REGISTER_COMPONENT_EXECUTABLE(Component, Component::GetDefaultName())
Component.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component
Definition: Component.h:42
Decoupled.h
forward_declarations.h
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::navigation::algorithms::CostmapBuilder::create
Costmap create(const SceneBounds &init=SceneBounds())
Definition: CostmapBuilder.cpp:58
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::onExitComponent
void onExitComponent() override
Definition: Component.cpp:192
armarx::navigation::util::staticObjects
objpose::ObjectPoseSeq staticObjects(objpose::ObjectPoseSeq objects)
Definition: util.cpp:64
armarx::navigation::algorithms::Costmap::Parameters
Definition: Costmap.h:21
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::ObjectPoseClientPluginUser::getClient
objpose::ObjectPoseClient getClient() const
Definition: ObjectPoseClientPlugin.cpp:64
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::Component
Component()
Definition: Component.cpp:45
armarx::core::time::DateTime::Invalid
static DateTime Invalid()
Definition: DateTime.cpp:60
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
armarx::navigation::components::distance_to_obstacle_costmap_provider
Definition: Component.cpp:43
armarx::navigation::components::distance_to_obstacle_costmap_provider::Component::getDefaultName
std::string getDefaultName() const override
Definition: Component.cpp:197
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36