RobotUnitModuleSelfCollisionChecker.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::RobotUnit
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <thread>
26 
27 #include <VirtualRobot/Robot.h>
28 
30 
31 #include <RobotAPI/interface/units/RobotUnit/RobotUnitInterface.h>
32 
33 #include "RobotUnitModuleBase.h"
34 
36 {
38  {
39  public:
42  {
43  defineOptionalProperty<float>("SelfCollisionCheck_Frequency",
44  10,
45  "Frequency [Hz] of self collision checking (default = "
46  "10). If set to 0, no cllisions will be checked.",
48  defineOptionalProperty<float>("SelfCollisionCheck_MinSelfDistance",
49  20,
50  "Minimum allowed distance (mm) between each collision "
51  "model before entering emergency stop (default = 20).",
53  defineOptionalProperty<std::string>(
54  "SelfCollisionCheck_ModelGroupsToCheck",
55  "",
56  "Comma seperated list of groups (two or more) of collision models (RobotNodeSets "
57  "or RobotNodes) that "
58  "should be checked against each other by collision avoidance \n"
59  "(e.g. {rnsColModel1,rnsColModel2};{rnsColModel3,rnsColModel4}; .... "
60  "says that rnsColModel1 will be only checked against rnsColModel2 and rnsColModel3 "
61  "will only be checked against rnsColModel4). \n"
62  "Following predefined descriptors can be used: 'FLOOR' which represents a virtual "
63  "collision model of the floor.");
64  }
65  };
66 
67  /**
68  * @ingroup Library-RobotUnit-Modules
69  * @brief This \ref ModuleBase "Module" manages self collision avoidance.
70  * If the distance between a pair of bodies falls below a threshold, the the emergency stop is activated.
71  *
72  * @see ModuleBase
73  */
75  virtual public ModuleBase,
76  virtual public RobotUnitSelfCollisionCheckerInterface
77  {
78  friend class ModuleBase;
79 
80  public:
81  /**
82  * @brief Returns the singleton instance of this class
83  * @return The singleton instance of this class
84  */
85  static SelfCollisionChecker&
87  {
88  return ModuleBase::Instance<SelfCollisionChecker>();
89  }
90 
91  // //////////////////////////////////////////////////////////////////////////////////////// //
92  // ///////////////////////////////// RobotUnitModule hooks //////////////////////////////// //
93  // //////////////////////////////////////////////////////////////////////////////////////// //
94  private:
95  /// @see ModuleBase::_preOnInitRobotUnit
96  void _preOnInitRobotUnit();
97  /// @see ModuleBase::_postFinishControlThreadInitialization
98  void _postFinishControlThreadInitialization();
99  /// @see ModuleBase::_preFinishRunning
100  void _preFinishRunning();
101  /// @see ModuleBase::_componentPropertiesUpdated
102  void _componentPropertiesUpdated(const std::set<std::string>& changed);
103  // //////////////////////////////////////////////////////////////////////////////////////// //
104  // ///////////////////////////////////// ice interface //////////////////////////////////// //
105  // //////////////////////////////////////////////////////////////////////////////////////// //
106  public:
107  /**
108  * @brief Sets the minimal distance (mm) between a configured pair of bodies to 'distance'.
109  * @param distance The minimal distance (mm) between a pair of bodies.
110  */
112  const Ice::Current& = Ice::emptyCurrent) override;
113  /**
114  * @brief Sets the frequency of self collision checks.
115  * @param freq The frequency of self collision checks.
116  */
118  const Ice::Current& = Ice::emptyCurrent) override;
119 
120  /**
121  * @brief Returns whether the frequency of self collision checks is above 0.
122  * @return Whether the frequency of self collision checks is above 0.
123  */
124  bool isSelfCollisionCheckEnabled(const Ice::Current& = Ice::emptyCurrent) const override;
125  /**
126  * @brief Returns the frequency of self collision checks.
127  * @return The frequency of self collision checks.
128  */
129  float
130  getSelfCollisionAvoidanceFrequency(const Ice::Current& = Ice::emptyCurrent) const override;
131  /**
132  * @brief Returns the minimal distance (mm) between a pair of bodies.
133  * @return The minimal distance (mm) between a pair of bodies.
134  */
135  float
136  getSelfCollisionAvoidanceDistance(const Ice::Current& = Ice::emptyCurrent) const override;
137  // //////////////////////////////////////////////////////////////////////////////////////// //
138  // //////////////////////////////////// implementation //////////////////////////////////// //
139  // //////////////////////////////////////////////////////////////////////////////////////// //
140  private:
141  /// @brief The Loop executing the self collision check
142  void selfCollisionAvoidanceTask();
143  // //////////////////////////////////////////////////////////////////////////////////////// //
144  // ///////////////////////////////////////// Data ///////////////////////////////////////// //
145  // //////////////////////////////////////////////////////////////////////////////////////// //
146  private:
147  /// @brief The thread executing \ref selfCollisionAvoidanceTask
148  std::thread selfCollisionAvoidanceThread;
149  // //////////////////////////////////////////////////////////////////////////////////////// //
150  // //////////////////////////////////////// config //////////////////////////////////////// //
151  // //////////////////////////////////////////////////////////////////////////////////////// //
152  private:
153  /// @brief The list of pairs of collision models that should be checked agaisnt each other (names only)
154  std::set<std::pair<std::string, std::string>> namePairsToCheck;
155  /// @brief The frequency of self collision checks.
156  std::atomic<float> checkFrequency{0};
157  // //////////////////////////////////////////////////////////////////////////////////////// //
158  // /////////////////////////////////////// col data /////////////////////////////////////// //
159  // //////////////////////////////////////////////////////////////////////////////////////// //
160  private:
161  ///@ brief The mutex guarding data used for self collision checks.
162  std::mutex selfCollisionDataMutex;
163  /// @brief Min allowed distance (mm) between each pair of collision models
164  std::atomic<float> minSelfDistance{0};
165  /// @brief The list of pairs of collision models that should be checked agaisnt each other (model nodes only)
166  std::vector<std::pair<VirtualRobot::SceneObjectPtr, VirtualRobot::SceneObjectPtr>>
167  nodePairsToCheck;
168  /// @brief The Robot used for self collison checks
169  VirtualRobot::RobotPtr selfCollisionAvoidanceRobot;
170  /// @brief The Robot Nodes of the Robot used for collision checks
171  std::vector<VirtualRobot::RobotNodePtr> selfCollisionAvoidanceRobotNodes;
172  /// @brief A Collision object for the floor
173  VirtualRobot::SceneObjectSetPtr floor;
174  /// @brief The pair of nodes causing the last detected collision (as index in \ref nodePairsToCheck or -1)
175  std::atomic_size_t lastCollisionPairIndex{std::numeric_limits<std::size_t>::max()};
176  };
177 } // namespace armarx::RobotUnitModule
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::RobotUnitModule::SelfCollisionChecker::setSelfCollisionAvoidanceDistance
void setSelfCollisionAvoidanceDistance(Ice::Float distance, const Ice::Current &=Ice::emptyCurrent) override
Sets the minimal distance (mm) between a configured pair of bodies to 'distance'.
Definition: RobotUnitModuleSelfCollisionChecker.cpp:183
RobotUnitModuleBase.h
armarx::RobotUnitModule::SelfCollisionChecker::Instance
static SelfCollisionChecker & Instance()
Returns the singleton instance of this class.
Definition: RobotUnitModuleSelfCollisionChecker.h:86
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
PeriodicTask.h
armarx::RobotUnitModule::SelfCollisionChecker::isSelfCollisionCheckEnabled
bool isSelfCollisionCheckEnabled(const Ice::Current &=Ice::emptyCurrent) const override
Returns whether the frequency of self collision checks is above 0.
Definition: RobotUnitModuleSelfCollisionChecker.cpp:314
armarx::RobotUnitModule::SelfCollisionChecker
This Module manages self collision avoidance. If the distance between a pair of bodies falls below a ...
Definition: RobotUnitModuleSelfCollisionChecker.h:74
armarx::RobotUnitModule::SelfCollisionChecker::getSelfCollisionAvoidanceDistance
float getSelfCollisionAvoidanceDistance(const Ice::Current &=Ice::emptyCurrent) const override
Returns the minimal distance (mm) between a pair of bodies.
Definition: RobotUnitModuleSelfCollisionChecker.cpp:328
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::RobotUnitModule::SelfCollisionChecker::getSelfCollisionAvoidanceFrequency
float getSelfCollisionAvoidanceFrequency(const Ice::Current &=Ice::emptyCurrent) const override
Returns the frequency of self collision checks.
Definition: RobotUnitModuleSelfCollisionChecker.cpp:321
armarx::RobotUnitModule::SelfCollisionCheckerPropertyDefinitions::SelfCollisionCheckerPropertyDefinitions
SelfCollisionCheckerPropertyDefinitions(std::string prefix)
Definition: RobotUnitModuleSelfCollisionChecker.h:40
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
armarx::RobotUnitModule
Definition: ControlDevice.h:34
armarx::RobotUnitModule::SelfCollisionCheckerPropertyDefinitions
Definition: RobotUnitModuleSelfCollisionChecker.h:37
armarx::PropertyDefinitionBase::eModifiable
@ eModifiable
Definition: PropertyDefinitionInterface.h:57
armarx::RobotUnitModule::ModuleBasePropertyDefinitions
Definition: RobotUnitModuleBase.h:107
armarx::RobotUnitModule::ModuleBase
Base class for all RobotUnitModules.
Definition: RobotUnitModuleBase.h:183
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::RobotUnitModule::SelfCollisionChecker::setSelfCollisionAvoidanceFrequency
void setSelfCollisionAvoidanceFrequency(Ice::Float freq, const Ice::Current &=Ice::emptyCurrent) override
Sets the frequency of self collision checks.
Definition: RobotUnitModuleSelfCollisionChecker.cpp:302