Component.h
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 RobotComponents::ArmarXObjects::LaserScannerFeatureExtraction
17  * @author Fabian Reister ( fabian dot reister at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <math.h>
26 
27 #include <memory>
28 #include <optional>
29 #include <string>
30 #include <unordered_map>
31 #include <variant>
32 #include <vector>
33 
34 #include <VirtualRobot/MathTools.h>
35 #include <VirtualRobot/VirtualRobot.h>
36 
43 
52 
59 
60 #include "ArVizDrawer.h"
61 #include "FeatureExtractor.h"
62 
64 {
65  using LineSegment2DChain = std::vector<Eigen::Vector2f>;
66  using LineSegment2DChainSeq = std::vector<LineSegment2DChain>;
67 
68  // class FeatureExtractor;
69  // class ArVizDrawer;
70 
71  /**
72  * @defgroup Component-LaserScannerFeatureExtraction LaserScannerFeatureExtraction
73  * @ingroup RobotComponents-Components
74  * A description of the component LaserScannerFeatureExtraction.
75  *
76  * @class LaserScannerFeatureExtraction
77  * @ingroup Component-LaserScannerFeatureExtraction
78  * @brief Brief description of class LaserScannerFeatureExtraction.
79  *
80  * Detailed description of class LaserScannerFeatureExtraction.
81  */
83  virtual public armarx::Component,
85  virtual public armarx::ArVizComponentPluginUser,
86  virtual public armem::ClientPluginUser
87  {
88  public:
90 
91  /// @see armarx::ManagedIceObject::getDefaultName()
92  std::string getDefaultName() const override;
93 
94  /// Get the component's default name.
95  static std::string GetDefaultName();
96 
97  protected:
98  /// @see PropertyUser::createPropertyDefinitions()
100 
101  /// @see armarx::ManagedIceObject::onInitComponent()
102  void onInitComponent() override;
103 
104  /// @see armarx::ManagedIceObject::onConnectComponent()
105  void onConnectComponent() override;
106 
107  /// @see armarx::ManagedIceObject::onDisconnectComponent()
108  void onDisconnectComponent() override;
109 
110  /// @see armarx::ManagedIceObject::onExitComponent()
111  void onExitComponent() override;
112 
113  /* (Requires armarx::LightweightRemoteGuiComponentPluginUser.)
114  /// This function should be called once in onConnect() or when you
115  /// need to re-create the Remote GUI tab.
116  void createRemoteGuiTab();
117 
118  /// After calling `RemoteGui_startRunningTask`, this function is
119  /// called periodically in a separate thread. If you update variables,
120  /// make sure to synchronize access to them.
121  void RemoteGui_update() override;
122  */
123 
124  private:
125  struct FramedFeatures
126  {
127  std::vector<Features> features;
128  std::string frame;
129  };
130 
131  void runPeriodically();
132 
133  bool filterLaserScannerData(armem::laser_scans::LaserScanStamped& laserScan);
134 
135  navigation::algorithms::Costmap fetchDistanceToObstacleCostmapSmallMargin(
137 
138 
139  std::vector<Features> onFeatures(const armem::laser_scans::LaserScanStamped& data,
140  const std::vector<Features>& features);
141 
142  Throttler& getOrCreateThrottler(std::string frame);
143  void drawRobot(const armem::Time& timestamp);
144  void drawFeatures(const std::vector<Features>& features,
145  const armem::laser_scans::LaserScanStamped* laserPoints,
146  const std::string frame,
147  const armem::Time& timestamp,
148  const Eigen::Isometry3f& global_T_sensor);
149 
150  void mergeFeatures(const std::vector<FramedFeatures>& framedFeatures,
151  const armem::Time& timestamp);
152 
153  void publishFeatures(const memory::LaserScannerFeatures& features,
154  const armem::Time& timestamp);
155 
156  // Private methods go here.
157 
158  // Forward declare `Properties` if you used it before its defined.
159  // struct Properties;
160 
161  /* (Requires the armarx::ArVizComponentPluginUser.)
162  /// Draw some boxes in ArViz.
163  void drawBoxes(const Properties& p, viz::Client& arviz);
164  */
165 
166  // Private member variables go here.
167  static const std::string defaultName;
168 
169  /// Properties shown in the Scenario GUI.
170  struct Properties
171  {
172  int taskPeriodMs = 100;
173 
174  //
175 
176  std::string robotName;
177 
178  struct RobotHull
179  {
180  enum Shape
181  {
184  };
185 
187  // for shape circle
188  float radius = 500.F; // [mm]
189  // for shape rectangle
191  } robotHull;
192 
193  //
194  struct CableFix
195  {
196  bool enabled = {true};
197  float cableAreaWidth{400};
198  float maxAreaTh{50 * 50};
199  } cableFix;
200 
201  ScanClustering::Params scanClusteringParams{
202  .distanceThreshold = 30.F, // [mm]
203  // we know the scan resolution which produces a bit more than 1000 points
204  .angleThreshold = 2 * M_PIf32 / 1000.F * 5.F, // [rad]
205  .maxDistance = 3'000.F // [mm]
206  };
207 
208  ChainApproximation::Params chainApproximationParams{
209  .distanceTh = 40.F // [mm]
210  };
211 
212  struct ArViz
213  {
214  bool drawRawPoints = {true};
215  bool visualizeSeparateFeatures = {false};
216  bool visualizeMergedFeatures = {true};
217 
218  ArVizDrawer::Parameters arvizDrawer;
219  } arviz;
220 
221  struct Filtering
222  {
223  bool filterLaserScansWithCostmap = false;
224  float distance = 100.0;
225 
226  std::string costmapProviderName = "distance_to_obstacle_costmap_provider";
227  std::string costmapName = "distance_to_obstacles_small_margin";
228  } filtering;
229  };
230 
231  Properties properties;
232  /* Use a mutex if you access variables from different threads
233  * (e.g. ice functions and RemoteGui_update()).
234  std::mutex propertiesMutex;
235  */
236 
237  /* (Requires the armarx::LightweightRemoteGuiComponentPluginUser.)
238  /// Tab shown in the Remote GUI.
239  struct RemoteGuiTab : armarx::RemoteGui::Client::Tab
240  {
241  armarx::RemoteGui::Client::LineEdit boxLayerName;
242  armarx::RemoteGui::Client::IntSpinBox numBoxes;
243 
244  armarx::RemoteGui::Client::Button drawBoxes;
245  };
246  RemoteGuiTab tab;
247  */
248 
249  /* (Requires the armarx::ArVizComponentPluginUser.)
250  * When used from different threads, an ArViz client needs to be synchronized.
251  /// Protects the arviz client inherited from the ArViz plugin.
252  std::mutex arvizMutex;
253  */
254 
255  PeriodicTask<LaserScannerFeatureExtraction>::pointer_type task;
256 
257  std::unique_ptr<FeatureExtractor> featureExtractor;
258  std::unique_ptr<ArVizDrawer> arVizDrawer;
259 
260  std::unique_ptr<FrequencyReporter> frequencyReporterPublish;
261  std::unique_ptr<FrequencyReporter> frequencyReporterRun;
262 
263  std::optional<armarx::navigation::algorithms::Costmap> obstacleDistancesCostmapSmallMargin;
264 
265  std::unordered_map<std::string, Throttler> throttlers;
266 
267  VirtualRobot::RobotPtr robot;
268 
269  std::variant<std::monostate, VirtualRobot::MathTools::ConvexHull2DPtr, Eigen::Vector2f>
270  robotHull;
271  VirtualRobot::MathTools::ConvexHull2DPtr cableArea;
272 
273 
274  armem::client::plugins::ReaderWriterPlugin<armem::robot_state::VirtualRobotReader>*
275  virtualRobotReaderPlugin = nullptr;
276 
277  armem::client::plugins::ReaderWriterPlugin<armem::laser_scans::client::Reader>*
278  laserScannerReaderPlugin = nullptr;
279 
280  armem::client::plugins::ReaderWriterPlugin<memory::client::laser_scanner_features::Writer>*
281  laserScannerFeatureWriterPlugin = nullptr;
282 
283  armem::client::plugins::ReaderWriterPlugin<navigation::memory::client::costmap::Reader>*
284  costmapReaderPlugin = nullptr;
285  };
286 } // namespace armarx::navigation::components::laser_scanner_feature_extraction
armarx::navigation::components::laser_scanner_feature_extraction::detail::ScanClusteringParams
Definition: ScanClustering.h:32
armarx::armem::laser_scans::LaserScanStamped
Definition: types.h:40
ArVizComponentPlugin.h
armarx::navigation::components::laser_scanner_feature_extraction::LineSegment2DChainSeq
std::vector< LineSegment2DChain > LineSegment2DChainSeq
Definition: Component.h:66
armarx::navigation::memory::LaserScannerFeatures
Definition: types.h:61
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction
Brief description of class LaserScannerFeatureExtraction.
Definition: Component.h:82
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::GetDefaultName
static std::string GetDefaultName()
Get the component's default name.
Definition: Component.cpp:644
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::getDefaultName
std::string getDefaultName() const override
Definition: Component.cpp:638
forward_declarations.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::RECTANGLE
@ RECTANGLE
Definition: Component.h:182
Reader.h
PeriodicTask.h
ReaderWriterPlugin.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::robotConvexHullMargin
float robotConvexHullMargin
Definition: Component.h:190
types.h
Reader.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix::enabled
bool enabled
Definition: Component.h:196
Costmap.h
armarx::navigation::components::laser_scanner_feature_extraction::LineSegment2DChain
std::vector< Eigen::Vector2f > LineSegment2DChain
Definition: Component.h:65
FeatureExtractor.h
armarx::Throttler
The Throttler class.
Definition: Throttler.h:34
armarx::armem::client::plugins::PluginUser
Adds the Memory Name System client component plugin.
Definition: PluginUser.h:27
armarx::ArVizComponentPluginUser
Provides a ready-to-use ArViz client arviz as member variable.
Definition: ArVizComponentPlugin.h:35
armarx::navigation::memory::client::costmap::Reader
Definition: Reader.h:36
forward_declarations.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onConnectComponent
void onConnectComponent() override
Definition: Component.cpp:222
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix::maxAreaTh
float maxAreaTh
Definition: Component.h:198
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::CIRCLE
@ CIRCLE
Definition: Component.h:183
ScanClustering.h
DebugObserverComponentPlugin.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::LaserScannerFeatureExtraction
LaserScannerFeatureExtraction()
Definition: Component.cpp:170
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onInitComponent
void onInitComponent() override
Definition: Component.cpp:179
VirtualRobotReader.h
Throttler.h
Component.h
types.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onDisconnectComponent
void onDisconnectComponent() override
Definition: Component.cpp:627
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::radius
float radius
Definition: Component.h:188
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::Shape
Shape
Definition: Component.h:180
ChainApproximation.h
PropertyDefinitionContainer.h
IceUtil::Handle< class PropertyDefinitionContainer >
Writer.h
FrequencyReporter.h
armarx::navigation::components::laser_scanner_feature_extraction::detail::ScanClusteringParams::distanceThreshold
float distanceThreshold
Definition: ScanClustering.h:35
armarx::DebugObserverComponentPluginUser
Definition: DebugObserverComponentPlugin.h:73
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull
Definition: Component.h:178
PluginUser.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix::cableAreaWidth
float cableAreaWidth
Definition: Component.h:197
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix
Definition: Component.h:194
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:81
ArVizDrawer.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::shape
Shape shape
Definition: Component.h:186
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onExitComponent
void onExitComponent() override
Definition: Component.cpp:633
armarx::navigation::components::laser_scanner_feature_extraction
Definition: ArVizDrawer.cpp:28
armarx::navigation::algorithms::Costmap
Definition: Costmap.h:16