ObstacleAvoidingPlatformUnit.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::ObstacleAvoidingPlatformUnit
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 <deque>
29 #include <string>
30 #include <tuple>
31 #include <mutex>
32 #include <vector>
33 
34 // Eigen
35 #include <Eigen/Core>
36 
37 // Ice
38 #include <IceUtil/Time.h>
39 
40 // Simox
41 #include <VirtualRobot/Nodes/RobotNode.h>
42 
43 // ArmarX
45 #include <ArmarXCore/util/tasks.h>
46 #include <ArmarXCore/util/time.h>
47 
48 // RobotAPI
50 #include <RobotAPI/interface/components/ObstacleAvoidance/ObstacleAvoidanceInterface.h>
54 
55 
56 namespace armarx
57 {
58 
60  virtual public PlatformUnit,
61  virtual public RobotStateComponentPluginUser,
62  virtual public ArVizComponentPluginUser,
64  {
65 
66  public:
67 
68  enum class control_mode
69  {
70  position,
71  velocity,
72  none
73  };
74 
75  private:
76 
77  struct control_loop
78  {
79  std::mutex mutex;
82  IceUtil::Time cycle_time = IceUtil::Time::milliSeconds(10);
83  };
84 
85  struct control_data
86  {
87  std::mutex mutex;
88  Eigen::Vector2f target_pos;
89  float target_ori;
90  Eigen::Vector2f target_vel;
91  float target_rot_vel;
92  float target_dist;
93  float target_angular_dist;
94  Eigen::Vector2f agent_pos;
95  float agent_ori;
96  double agent_safety_margin = 0;
97  float min_vel_near_target = 50;
98  float min_vel_general = 100;
99  float max_vel = 200;
100  float max_rot_vel = 0.3;
101  float pos_near_threshold = 250;
102  float pos_reached_threshold = 8;
103  float ori_reached_threshold = 0.1;
104  float kp = 3.5;
105  std::deque<std::tuple<Eigen::Vector2f, Eigen::Vector2f>> vel_history;
106  unsigned amount_smoothing = 50;
107  unsigned amount_max_vel = 3;
108  float adaptive_max_vel_exp = 0;
109  };
110 
111  struct visualization
112  {
113  Eigen::Vector3f start;
114  std::vector<Eigen::Vector3f> path;
115  bool enabled = true;
116  };
117 
118  struct velocities
119  {
120  Eigen::Vector2f target_local;
121  Eigen::Vector2f modulated_local;
122  Eigen::Vector2f target_global;
123  Eigen::Vector2f modulated_global;
124  float target_rot;
125  float err_dist;
126  float err_angular_dist;
127  };
128 
129  public:
130 
132 
133  virtual
135  override;
136 
137  virtual
138  std::string
140  const override;
141 
142  virtual
143  void
144  moveTo(
145  float target_pos_x,
146  float target_pos_y,
147  float target_ori,
148  float pos_reached_threshold,
149  float ori_reached_threshold,
150  const Ice::Current& = Ice::Current{})
151  override;
152 
153  virtual
154  void
155  move(
156  float target_vel_x,
157  float target_vel_y,
158  float target_rot_vel,
159  const Ice::Current& = Ice::Current{})
160  override;
161 
162  virtual
163  void
164  moveRelative(
165  float target_pos_delta_x,
166  float target_pos_delta_y,
167  float target_delta_ori,
168  float pos_reached_threshold,
169  float ori_reached_threshold,
170  const Ice::Current& = Ice::Current{})
171  override;
172 
173  virtual
174  void
176  float max_pos_vel,
177  float max_rot_vel,
178  const Ice::Current& = Ice::Current{})
179  override;
180 
181  virtual
182  void
183  stopPlatform(const Ice::Current& = Ice::Current{})
184  override;
185 
186  protected:
187 
188  virtual
189  void
191  override;
192 
193  virtual
194  void
196  override;
197 
198  virtual
199  void
201  override;
202 
203  virtual
206  override;
207 
208  private:
209 
210  void
211  schedule_high_level_control_loop(control_mode mode);
212 
213  void
214  high_level_control_loop();
215 
216  velocities
217  get_velocities();
218 
219  void
220  update_agent_dependent_values();
221 
222  Eigen::Vector2f
223  get_target_velocity()
224  const;
225 
226  float
227  get_target_rotational_velocity()
228  const;
229 
230  bool
231  target_position_reached()
232  const;
233 
234  bool
235  target_orientation_reached()
236  const;
237 
238  bool
239  target_reached()
240  const;
241 
242  Eigen::Vector2f
243  post_process_vel(const Eigen::Vector2f& target_vel, const Eigen::Vector2f& modulated_vel);
244 
245  Eigen::Vector2f
246  calculate_mean_modulated_vel()
247  const;
248 
249  unsigned
250  calculate_adaptive_smoothing_buffer_size()
251  const;
252 
253  float
254  calculate_adaptive_max_vel()
255  const;
256 
257  void
258  visualize();
259 
260  void
261  visualize(const velocities& vels);
262 
263  bool
264  is_near_target(const Eigen::Vector2f& control_velocity)
265  const
266  noexcept;
267 
268  public:
269 
270  static const std::string default_name;
271 
272  private:
273 
274  PlatformUnitInterfacePrx m_platform;
275  ObstacleAvoidanceInterfacePrx m_obstacle_avoidance;
276  VirtualRobot::RobotPtr m_robot;
277 
278  ObstacleAvoidingPlatformUnit::control_loop m_control_loop;
279  ObstacleAvoidingPlatformUnit::control_data m_control_data;
280 
281  mutable PIDController m_rot_pid_controller{1.0, 0.00009, 0.0};
282 
283  visualization m_viz;
284 
285  };
286 
287 }
armarx::ObstacleAvoidingPlatformUnit::control_mode
control_mode
Definition: ObstacleAvoidingPlatformUnit.h:68
ArVizComponentPlugin.h
time.h
RobotStateComponentPlugin.h
armarx::ObstacleAvoidingPlatformUnit::onInitPlatformUnit
virtual void onInitPlatformUnit() override
Definition: ObstacleAvoidingPlatformUnit.cpp:106
armarx::ObstacleAvoidingPlatformUnit::getDefaultName
virtual std::string getDefaultName() const override
Retrieve default name of component.
Definition: ObstacleAvoidingPlatformUnit.cpp:147
armarx::ObstacleAvoidingPlatformUnit::moveRelative
virtual void moveRelative(float target_pos_delta_x, float target_pos_delta_y, float target_delta_ori, float pos_reached_threshold, float ori_reached_threshold, const Ice::Current &=Ice::Current{}) override
Definition: ObstacleAvoidingPlatformUnit.cpp:207
armarx::ObstacleAvoidingPlatformUnit::move
virtual void move(float target_vel_x, float target_vel_y, float target_rot_vel, const Ice::Current &=Ice::Current{}) override
Definition: ObstacleAvoidingPlatformUnit.cpp:183
armarx::ObstacleAvoidingPlatformUnit::control_mode::none
@ none
armarx::ObstacleAvoidingPlatformUnit::onExitPlatformUnit
virtual void onExitPlatformUnit() override
Definition: ObstacleAvoidingPlatformUnit.cpp:136
armarx::PlatformUnit
The PlatformUnit class.
Definition: PlatformUnit.h:70
armarx::ArVizComponentPluginUser
Provides a ready-to-use ArViz client arviz as member variable.
Definition: ArVizComponentPlugin.h:36
armarx::ObstacleAvoidingPlatformUnit::onStartPlatformUnit
virtual void onStartPlatformUnit() override
Definition: ObstacleAvoidingPlatformUnit.cpp:115
armarx::ObstacleAvoidingPlatformUnit::control_mode::position
@ position
armarx::PIDController
Definition: PIDController.h:43
armarx::ObstacleAvoidingPlatformUnit::setMaxVelocities
virtual void setMaxVelocities(float max_pos_vel, float max_rot_vel, const Ice::Current &=Ice::Current{}) override
Definition: ObstacleAvoidingPlatformUnit.cpp:236
enabled
std::atomic< bool > * enabled
Definition: RemoteGuiWidgetController.cpp:75
DebugObserverComponentPlugin.h
armarx::ObstacleAvoidingPlatformUnit::default_name
static const std::string default_name
Definition: ObstacleAvoidingPlatformUnit.h:270
armarx::ObstacleAvoidingPlatformUnit::~ObstacleAvoidingPlatformUnit
virtual ~ObstacleAvoidingPlatformUnit() override
tasks.h
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
PIDController.h
armarx::ObstacleAvoidingPlatformUnit::ObstacleAvoidingPlatformUnit
ObstacleAvoidingPlatformUnit()
PlatformUnit.h
armarx::ObstacleAvoidingPlatformUnit::moveTo
virtual void moveTo(float target_pos_x, float target_pos_y, float target_ori, float pos_reached_threshold, float ori_reached_threshold, const Ice::Current &=Ice::Current{}) override
Definition: ObstacleAvoidingPlatformUnit.cpp:155
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::DebugObserverComponentPluginUser
Definition: DebugObserverComponentPlugin.h:82
armarx::ObstacleAvoidingPlatformUnit::createPropertyDefinitions
virtual PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ObstacleAvoidingPlatformUnit.cpp:855
armarx::ObstacleAvoidingPlatformUnit::control_mode::velocity
@ velocity
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ObstacleAvoidingPlatformUnit::stopPlatform
virtual void stopPlatform(const Ice::Current &=Ice::Current{}) override
Definition: ObstacleAvoidingPlatformUnit.cpp:249
armarx::RobotStateComponentPluginUser
Definition: RobotStateComponentPlugin.h:167
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::ObstacleAvoidingPlatformUnit
Definition: ObstacleAvoidingPlatformUnit.h:59