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 <variant>
26 
31 
39 
44 
45 #include <RobotComponents/interface/components/LaserScannerFeatureExtraction/LaserScannerFeatureExtraction.h>
46 
47 #include "ArVizDrawer.h"
48 #include "FeatureExtractor.h"
49 
51 {
52 
57 
58  // class FeatureExtractor;
59  // class ArVizDrawer;
60 
61  /**
62  * @defgroup Component-LaserScannerFeatureExtraction LaserScannerFeatureExtraction
63  * @ingroup RobotComponents-Components
64  * A description of the component LaserScannerFeatureExtraction.
65  *
66  * @class LaserScannerFeatureExtraction
67  * @ingroup Component-LaserScannerFeatureExtraction
68  * @brief Brief description of class LaserScannerFeatureExtraction.
69  *
70  * Detailed description of class LaserScannerFeatureExtraction.
71  */
73  virtual public armarx::Component,
75  // , virtual public armarx::LightweightRemoteGuiComponentPluginUser
76  ,
77  virtual public armarx::ArVizComponentPluginUser,
78  virtual public RobotStateComponentPluginUser,
79  virtual public armem::ClientPluginUser
80 
81  {
82  public:
84 
85  /// @see armarx::ManagedIceObject::getDefaultName()
86  std::string getDefaultName() const override;
87 
88  /// Get the component's default name.
89  static std::string GetDefaultName();
90 
91  protected:
92  /// @see PropertyUser::createPropertyDefinitions()
94 
95  /// @see armarx::ManagedIceObject::onInitComponent()
96  void onInitComponent() override;
97 
98  /// @see armarx::ManagedIceObject::onConnectComponent()
99  void onConnectComponent() override;
100 
101  /// @see armarx::ManagedIceObject::onDisconnectComponent()
102  void onDisconnectComponent() override;
103 
104  /// @see armarx::ManagedIceObject::onExitComponent()
105  void onExitComponent() override;
106 
107  /* (Requires armarx::LightweightRemoteGuiComponentPluginUser.)
108  /// This function should be called once in onConnect() or when you
109  /// need to re-create the Remote GUI tab.
110  void createRemoteGuiTab();
111 
112  /// After calling `RemoteGui_startRunningTask`, this function is
113  /// called periodically in a separate thread. If you update variables,
114  /// make sure to synchronize access to them.
115  void RemoteGui_update() override;
116  */
117 
118  private:
119  struct FramedFeatures
120  {
121  std::vector<Features> features;
122  std::string frame;
123  };
124 
125  void runPeriodically();
126 
127  std::vector<Features> onFeatures(const armem::laser_scans::LaserScanStamped& data,
128  const std::vector<Features>& features);
129 
130  Throttler& getOrCreateThrottler(std::string frame);
131  void drawRobot(const armem::Time& timestamp);
132  void drawFeatures(const std::vector<Features>& features,
133  const armem::laser_scans::LaserScanStamped* laserPoints,
134  const std::string frame,
135  const armem::Time& timestamp,
136  const Eigen::Isometry3f& global_T_sensor);
137 
138  void mergeFeatures(const std::vector<FramedFeatures>& framedFeatures,
139  const armem::Time& timestamp);
140 
141  void publishFeatures(const memory::LaserScannerFeatures& features,
142  const armem::Time& timestamp);
143 
144  // Private methods go here.
145 
146  // Forward declare `Properties` if you used it before its defined.
147  // struct Properties;
148 
149  /* (Requires the armarx::ArVizComponentPluginUser.)
150  /// Draw some boxes in ArViz.
151  void drawBoxes(const Properties& p, viz::Client& arviz);
152  */
153 
154  // Private member variables go here.
155  static const std::string defaultName;
156 
157  /// Properties shown in the Scenario GUI.
158  struct Properties
159  {
160  int taskPeriodMs = 100;
161 
162  //
163  struct RobotHull
164  {
165  enum Shape
166  {
169  };
170 
172  // for shape circle
173  float radius = 500.F; // [mm]
174  // for shape rectangle
176  } robotHull;
177 
178  //
179  struct CableFix
180  {
181  bool enabled = {true};
182  float cableAreaWidth{400};
183  float maxAreaTh{50 * 50};
184  } cableFix;
185 
186  ScanClustering::Params scanClusteringParams{
187  .distanceThreshold = 30.F, // [mm]
188  // we know the scan resolution which produces a bit more than 1000 points
189  .angleThreshold = 2 * M_PIf32 / 1000.F * 5.F, // [rad]
190  .maxDistance = 3'000.F // [mm]
191  };
192 
193  ChainApproximation::Params chainApproximationParams{
194  .distanceTh = 40.F // [mm]
195  };
196 
197  struct ArViz
198  {
199  bool drawRawPoints = {true};
200  bool visualizeSeparateFeatures = {false};
201  bool visualizeMergedFeatures = {true};
202  } arviz;
203  };
204 
205  Properties properties;
206  /* Use a mutex if you access variables from different threads
207  * (e.g. ice functions and RemoteGui_update()).
208  std::mutex propertiesMutex;
209  */
210 
211  /* (Requires the armarx::LightweightRemoteGuiComponentPluginUser.)
212  /// Tab shown in the Remote GUI.
213  struct RemoteGuiTab : armarx::RemoteGui::Client::Tab
214  {
215  armarx::RemoteGui::Client::LineEdit boxLayerName;
216  armarx::RemoteGui::Client::IntSpinBox numBoxes;
217 
218  armarx::RemoteGui::Client::Button drawBoxes;
219  };
220  RemoteGuiTab tab;
221  */
222 
223  /* (Requires the armarx::ArVizComponentPluginUser.)
224  * When used from different threads, an ArViz client needs to be synchronized.
225  /// Protects the arviz client inherited from the ArViz plugin.
226  std::mutex arvizMutex;
227  */
228 
229  PeriodicTask<LaserScannerFeatureExtraction>::pointer_type task;
230 
231  std::unique_ptr<FeatureExtractor> featureExtractor;
232  std::unique_ptr<ArVizDrawer> arVizDrawer;
233 
234  std::unique_ptr<FrequencyReporter> frequencyReporterPublish;
235  std::unique_ptr<FrequencyReporter> frequencyReporterRun;
236 
237  std::unordered_map<std::string, Throttler> throttlers;
238 
239  VirtualRobot::RobotPtr robot;
240 
241  LaserScannerFeaturesListenerPrx featuresTopic;
242 
243  std::variant<std::monostate, VirtualRobot::MathTools::ConvexHull2DPtr, Eigen::Vector2f>
244  robotHull;
245  VirtualRobot::MathTools::ConvexHull2DPtr cableArea;
246 
247 
248  armem::client::plugins::ReaderWriterPlugin<armem::laser_scans::client::Reader>*
249  laserScannerReaderPlugin = nullptr;
250 
251 
252  armem::client::plugins::ReaderWriterPlugin<memory::client::laser_scanner_features::Writer>*
253  laserScannerFeatureWriterPlugin = nullptr;
254  };
255 } // namespace armarx::navigation::components::laser_scanner_feature_extraction
armarx::navigation::components::laser_scanner_feature_extraction::detail::ScanClusteringParams
Definition: ScanClustering.h:30
armarx::armem::laser_scans::LaserScanStamped
Definition: types.h:40
ArVizComponentPlugin.h
armarx::navigation::memory::LaserScannerFeatures
Definition: types.h:62
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction
Brief description of class LaserScannerFeatureExtraction.
Definition: Component.h:72
RobotStateComponentPlugin.h
geometry.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::GetDefaultName
static std::string GetDefaultName()
Get the component's default name.
Definition: Component.cpp:553
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::getDefaultName
std::string getDefaultName() const override
Definition: Component.cpp:547
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::RECTANGLE
@ RECTANGLE
Definition: Component.h:167
ReaderWriterPlugin.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::robotConvexHullMargin
float robotConvexHullMargin
Definition: Component.h:175
Reader.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix::enabled
bool enabled
Definition: Component.h:181
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:29
armarx::ArVizComponentPluginUser
Provides a ready-to-use ArViz client arviz as member variable.
Definition: ArVizComponentPlugin.h:36
forward_declarations.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onConnectComponent
void onConnectComponent() override
Definition: Component.cpp:143
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix::maxAreaTh
float maxAreaTh
Definition: Component.h:183
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::CIRCLE
@ CIRCLE
Definition: Component.h:168
ScanClustering.h
DebugObserverComponentPlugin.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::LaserScannerFeatureExtraction
LaserScannerFeatureExtraction()
Definition: Component.cpp:126
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onInitComponent
void onInitComponent() override
Definition: Component.cpp:133
Throttler.h
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::navigation::components::laser_scanner_feature_extraction::LineSegment2DChainSeq
armarx::laser_scanner_feature_extraction::LineSegment2DChainSeq LineSegment2DChainSeq
Definition: Component.h:56
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onDisconnectComponent
void onDisconnectComponent() override
Definition: Component.cpp:536
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:173
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::Shape
Shape
Definition: Component.h:165
ChainApproximation.h
IceUtil::Handle< class PropertyDefinitionContainer >
Writer.h
FrequencyReporter.h
armarx::navigation::components::laser_scanner_feature_extraction::detail::ScanClusteringParams::distanceThreshold
float distanceThreshold
Definition: ScanClustering.h:33
armarx::DebugObserverComponentPluginUser
Definition: DebugObserverComponentPlugin.h:82
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull
Definition: Component.h:163
PluginUser.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix::cableAreaWidth
float cableAreaWidth
Definition: Component.h:182
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::CableFix
Definition: Component.h:179
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: Component.cpp:60
armarx::navigation::components::laser_scanner_feature_extraction::LineSegment2DChain
armarx::laser_scanner_feature_extraction::LineSegment2DChain LineSegment2DChain
Definition: Component.h:55
ArVizDrawer.h
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::Properties::RobotHull::shape
Shape shape
Definition: Component.h:171
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeatureExtraction::onExitComponent
void onExitComponent() override
Definition: Component.cpp:542
armarx::navigation::components::laser_scanner_feature_extraction
Definition: ArVizDrawer.cpp:28
armarx::RobotStateComponentPluginUser
Definition: RobotStateComponentPlugin.h:167
armarx::navigation::components::laser_scanner_feature_extraction::LaserScannerFeaturesListenerPrx
armarx::laser_scanner_feature_extraction::LaserScannerFeaturesListenerPrx LaserScannerFeaturesListenerPrx
Definition: Component.h:54