DSObstacleAvoidance.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 RobotAPI::ArmarXObjects::DSObstacleAvoidance
17  * @author Christian R. G. Dreher <c.dreher@kit.edu>
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #pragma once
25 
26 
27 // STD/STL
28 #include <array>
29 #include <deque>
30 #include <map>
31 #include <mutex>
32 #include <shared_mutex>
33 #include <string>
34 #include <vector>
35 
36 // Eigen
37 #include <Eigen/Geometry>
38 
39 // Ice
40 #include <Ice/Current.h>
41 
42 // DynamicObstacleAvoidance
43 #include <DynamicObstacleAvoidance/Agent.hpp>
44 #include <DynamicObstacleAvoidance/Environment.hpp>
45 
46 // ArmarX
48 #include <ArmarXCore/util/tasks.h>
49 #include <ArmarXCore/util/time.h>
50 
51 // RobotAPI
52 #include <RobotAPI/interface/components/ObstacleAvoidance/DSObstacleAvoidanceInterface.h>
54 
55 namespace armarx
56 {
57 
59  virtual public Component,
60  virtual public DSObstacleAvoidanceInterface,
61  virtual public ArVizComponentPluginUser
62  {
63 
64  public:
66 
67  /**
68  * @see armarx::ManagedIceObject::getDefaultName()
69  */
70  std::string getDefaultName() const override;
71 
72  dsobstacleavoidance::Config getConfig(const Ice::Current& = Ice::emptyCurrent) override;
73 
74  void updateObstacle(const obstacledetection::Obstacle& obstacle,
75  const Ice::Current& = Ice::emptyCurrent) override;
76 
77  std::vector<obstacledetection::Obstacle>
78  getObstacles(const Ice::Current& = Ice::emptyCurrent) override;
79 
80  void removeObstacle(const std::string& obstacle_name,
81  const Ice::Current& = Ice::emptyCurrent) override;
82 
83  Eigen::Vector3f modulateVelocity(const obstacleavoidance::Agent& agent,
84  const Ice::Current& = Ice::emptyCurrent) override;
85 
86  void updateEnvironment(const Ice::Current& = Ice::emptyCurrent) override;
87 
88  void writeDebugPlots(const std::string& filename,
89  const Ice::Current& = Ice::emptyCurrent) override;
90 
91  protected:
92  /**
93  * @see armarx::ManagedIceObject::onInitComponent()
94  */
95  void onInitComponent() override;
96 
97  /**
98  * @see armarx::ManagedIceObject::onConnectComponent()
99  */
100  void onConnectComponent() override;
101 
102  /**
103  * @see armarx::ManagedIceObject::onDisconnectComponent()
104  */
105  void onDisconnectComponent() override;
106 
107  /**
108  * @see armarx::ManagedIceObject::onExitComponent()
109  */
110  void onExitComponent() override;
111 
112  /**
113  * @see PropertyUser::createPropertyDefinitions()
114  */
116 
117  private:
118  void sanity_check_obstacle(const obstacledetection::Obstacle& obstacle) const;
119 
120  void run_watchdog();
121 
122  void run_visualization();
123 
124  public:
125  static const std::string default_name;
126 
127  private:
128  struct visualization
129  {
130  bool enabled = true;
132  unsigned task_interval = 20;
133  mutable std::mutex mutex;
134  bool needs_update = true;
135  };
136 
137  struct watchdog
138  {
139  bool enabled = true;
141  unsigned task_interval = 100;
142  IceUtil::Time threshold = IceUtil::Time::milliSeconds(500);
143  };
144 
145  struct dynamic_obstacle_avoidance
146  {
147  dsobstacleavoidance::Config cfg;
148  DynamicObstacleAvoidance::Environment env;
149  mutable std::shared_mutex mutex;
150  std::string load_obstacles_from_file =
151  "RobotAPI/obstacle_avoidance/r034_2d_scene_obstacles.json";
152  };
153 
154  struct buffer
155  {
156  enum class update_type
157  {
158  update,
159  removal
160  };
161 
162  struct update
163  {
165  std::string name;
166  DSObstacleAvoidance::buffer::update_type type;
167  };
168 
169  mutable std::mutex mutex;
170  std::vector<DSObstacleAvoidance::buffer::update> update_log;
171  std::map<std::string, obstacledetection::Obstacle> obstacles;
172  bool needs_env_update;
173  IceUtil::Time last_env_update;
174  };
175 
176  DSObstacleAvoidance::visualization m_vis;
177 
178  DSObstacleAvoidance::watchdog m_watchdog;
179 
180  DSObstacleAvoidance::dynamic_obstacle_avoidance m_doa;
181 
182  DSObstacleAvoidance::buffer m_buf;
183  };
184 
185 } // namespace armarx
armarx::DSObstacleAvoidance::removeObstacle
void removeObstacle(const std::string &obstacle_name, const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:279
armarx::DSObstacleAvoidance::getObstacles
std::vector< obstacledetection::Obstacle > getObstacles(const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:297
armarx::DSObstacleAvoidance::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: DSObstacleAvoidance.cpp:621
ArVizComponentPlugin.h
time.h
armarx::DSObstacleAvoidance::buffer::update
Definition: DSObstacleAvoidance.h:162
armarx::DSObstacleAvoidance::buffer::update::type
DSObstacleAvoidance::buffer::update_type type
Definition: DSObstacleAvoidance.h:166
armarx::DSObstacleAvoidance::onDisconnectComponent
void onDisconnectComponent() override
Definition: DSObstacleAvoidance.cpp:219
armarx::DSObstacleAvoidance::DSObstacleAvoidance
DSObstacleAvoidance()
Definition: DSObstacleAvoidance.cpp:142
armarx::DSObstacleAvoidance::buffer::update::time
IceUtil::Time time
Definition: DSObstacleAvoidance.h:164
armarx::DSObstacleAvoidance::getDefaultName
std::string getDefaultName() const override
Definition: DSObstacleAvoidance.cpp:245
armarx::DSObstacleAvoidance::default_name
static const std::string default_name
Definition: DSObstacleAvoidance.h:125
armarx::ArVizComponentPluginUser
Provides a ready-to-use ArViz client arviz as member variable.
Definition: ArVizComponentPlugin.h:35
armarx::DSObstacleAvoidance::modulateVelocity
Eigen::Vector3f modulateVelocity(const obstacleavoidance::Agent &agent, const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:340
armarx::DSObstacleAvoidance::updateEnvironment
void updateEnvironment(const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:387
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::DSObstacleAvoidance::updateObstacle
void updateObstacle(const obstacledetection::Obstacle &obstacle, const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:257
armarx::DSObstacleAvoidance::onConnectComponent
void onConnectComponent() override
Definition: DSObstacleAvoidance.cpp:197
tasks.h
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::DSObstacleAvoidance::buffer::update::name
std::string name
Definition: DSObstacleAvoidance.h:165
armarx::DSObstacleAvoidance
Definition: DSObstacleAvoidance.h:58
armarx::DSObstacleAvoidance::writeDebugPlots
void writeDebugPlots(const std::string &filename, const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:465
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::DSObstacleAvoidance::onInitComponent
void onInitComponent() override
Definition: DSObstacleAvoidance.cpp:156
armarx::DSObstacleAvoidance::onExitComponent
void onExitComponent() override
Definition: DSObstacleAvoidance.cpp:237
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::DSObstacleAvoidance::getConfig
dsobstacleavoidance::Config getConfig(const Ice::Current &=Ice::emptyCurrent) override
Definition: DSObstacleAvoidance.cpp:251