PathPlanner.cpp
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::PathPlanner
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2015 Humanoids Group, H2T, KIT
19 * @license http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "PathPlanner.h"
24 
25 
27 
28 #include <MemoryX/interface/memorytypes/MemoryEntities.h>
33 
34 #include <VirtualRobot/XML/RobotIO.h>
35 #include <VirtualRobot/CollisionDetection/CollisionChecker.h>
36 
38  safetyMargin {50.f},
39  objects {}
40 {
41 }
42 
44 {
45  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
46  // offeringTopic(getProperty<std::string>("DebugDrawerName").getValue());
47 }
48 
50 {
51  workingMemoryPrx = getProxy<memoryx::WorkingMemoryInterfacePrx>(getProperty<std::string>("WorkingMemoryName").getValue());
52  commonStoragePrx = workingMemoryPrx->getCommonStorage();
53  fileManager.reset(new memoryx::GridFileManager(commonStoragePrx));
54 
55  // debugDrawer = getTopic<armarx::DebugDrawerInterfacePrx>(getProperty<std::string>("DebugDrawerName").getValue());
56 }
57 
58 void armarx::PathPlanner::setCollisionObjects(const ::armarx::ObjectPositionBaseList& list, const ::Ice::Current&)
59 {
60  clearCollisionObjects();
61  addCollisionObjects(list);
62 }
63 
64 void armarx::PathPlanner::addCollisionObjects(const ::armarx::ObjectPositionBaseList& list, const ::Ice::Current&)
65 {
66  ARMARX_VERBOSE << "Adding " << list.size() << " collision objects";
67 
68  //strong except guarantee
69  std::vector<CollisionObjectData> newObjects;
70 
71  for (const auto& elem : list)
72  {
73  ARMARX_VERBOSE << "Object: " << newObjects.size();
74 
75  CollisionObjectData newObject;
76  newObject.object = elem.objectClassBase;
77 
78  ARMARX_VERBOSE << "Object: name " << newObject.object->getName() << ", id " << newObject.object->getId();
79 
80  memoryx::ObjectClassPtr objectClass = memoryx::ObjectClassPtr::dynamicCast(elem.objectClassBase);
81 
82  if (!objectClass)
83  {
84  std::stringstream s;
85  s << "Can't use object class with ice id " << elem.objectClassBase->ice_id();
86  ARMARX_ERROR_S << s.str();
87  throw armarx::InvalidArgumentException {s.str()};
88  }
89 
90  ARMARX_VERBOSE << "Adding SimoxObjectWrapper";
92  ARMARX_VERBOSE << "Getting ManipulationObject name and object";
93  VirtualRobot::ManipulationObjectPtr orgMo = sw->getManipulationObject();
94  std::string moName = orgMo->getName();
95  ARMARX_VERBOSE << "Cloning ManipulationObject";
96  VirtualRobot::ManipulationObjectPtr mo = orgMo->clone(moName);
97 
98  ARMARX_VERBOSE << "Moving object";
99  //move the object to the given position
100  const auto objectPose = armarx::PosePtr::dynamicCast(elem.objectPose);
101 
102  if (!objectPose)
103  {
104  std::stringstream s;
105  s << "Can't convert pose of " << objectClass->getName() << " to armarx::Pose.";
106  ARMARX_ERROR_S << s.str();
107  throw armarx::InvalidArgumentException {s.str()};
108  }
109 
110  mo->setGlobalPose(objectPose->toEigen());
111 
112  ARMARX_VERBOSE << "Get collision object";
113  VirtualRobot::CollisionModelPtr colModel = mo->getCollisionModel();
114 
115  newObject.colModel = colModel;
116  newObjects.push_back(newObject);
117  ARMARX_VERBOSE << "Object: " << newObjects.size() - 1 << " done.";
118  }
119 
120  //copy
121  std::copy(newObjects.begin(), newObjects.end(), std::back_inserter(objects));
122 
123  ARMARX_VERBOSE << "Added " << newObjects.size() << " collision objects. (Total: " << objects.size() << ")";
124 }
125 
127 {
128  ARMARX_VERBOSE << "Cleared " << objects.size() << " objects.";
129  objects.clear();
130 }
131 
132 void armarx::PathPlanner::setAgent(const ::memoryx::AgentInstanceBasePtr& newAgent, const std::string& agentColModelName, const ::Ice::Current&)
133 {
134  ARMARX_VERBOSE << "Setting agent '" << newAgent->getName() << "' with id " << newAgent->getId();
135  //get agent
136  std::string agentFilePath = newAgent->getAgentFilePath();
137 
138  if (!ArmarXDataPath::getAbsolutePath(agentFilePath, agentFilePath))
139  {
140  ARMARX_ERROR << "Could not find robot file " << agentFilePath << std::flush;
141 
142  ARMARX_VERBOSE << "Searched in " << ArmarXDataPath::getDataPaths().size() << " additional paths";
143 
144  for (const auto& path : ArmarXDataPath::getDataPaths())
145  {
146  ARMARX_VERBOSE << "\t" << path;
147  }
148 
149  return;
150  }
151 
152  //strong except guarantee
153  ARMARX_VERBOSE << "Loading agent: " << agentFilePath;
154  auto agent2 = VirtualRobot::RobotIO::loadRobot(agentFilePath, VirtualRobot::RobotIO::eFull);
155  ARMARX_VERBOSE << "Agent loaded.";
156 
157  if (!agent2)
158  {
159  std::stringstream s;
160  s << "Can't load agent from: " << agentFilePath;
161  ARMARX_ERROR_S << s.str();
162  throw armarx::InvalidArgumentException {s.str()};
163  }
164 
165  //get collision model
166  if (!agent2->hasRobotNode(agentColModelName) || !agent2->getRobotNode(agentColModelName)->getCollisionModel())
167  {
168  std::stringstream s;
169  s << "Agent has no collision model with name " << agentColModelName;
170  ARMARX_ERROR_S << s.str();
171  throw armarx::InvalidArgumentException {s.str()};
172  }
173 
174  agent = agent2;
175  agentCollisionModel = agent->getRobotNode(agentColModelName)->getCollisionModel();//->clone();
176  agentZCoord = agent->getGlobalPose()(2, 3);
177  ARMARX_VERBOSE << "Setting agent...done";
178 }
179 
180 void armarx::PathPlanner::setSafetyMargin(::Ice::Float margin, const ::Ice::Current&)
181 {
182  if (margin < 0)
183  {
184  std::stringstream s;
185  s << "Invalid margin: " << margin << " < 0.0";
186  ARMARX_ERROR_S << s.str();
187  throw armarx::InvalidArgumentException {s.str()};
188  }
189 
190  safetyMargin = margin;
191 }
192 
194 {
195  if (!agentCollisionModel)
196  {
197  throw std::logic_error {"no agent collision model"};
198  }
199 
200  if (objects.empty())
201  {
202  return true;
203  }
204 
205  //set pose
207  pose.block<3, 3>(0, 0) = VirtualRobot::MathTools::axisangle2eigen3f(Eigen::Vector3f {0.f, 0.f, 1.f}, position.z);
208  pose(0, 3) = position.x;
209  pose(1, 3) = position.y;
210  pose(3, 3) = agentZCoord;
211  agent->setGlobalPose(pose);
212 
213  if (safetyMargin > 0.01f)
214  {
215  for (const auto& object : objects)
216  {
217  float dist = VirtualRobot::CollisionChecker::getGlobalCollisionChecker()->calculateDistance(
218  agentCollisionModel,
219  object.colModel);
220 
221  if (dist <= safetyMargin)
222  {
223  return false;
224  }
225  }
226  }
227  else
228  {
229  //the distance is small => only check collision
230  for (const auto& object : objects)
231  {
232  bool col = VirtualRobot::CollisionChecker::getGlobalCollisionChecker()->checkCollision(
233  agentCollisionModel,
234  object.colModel
235  );
236 
237  if (col)
238  {
239  return false;
240  }
241  }
242  }
243 
244  return true;
245 }
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::PathPlanner::isPositionValid
bool isPositionValid(armarx::Vector3 position) const
Definition: PathPlanner.cpp:193
armarx::eFull
@ eFull
Definition: MorphingItem.h:35
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::PathPlanner::onConnectComponent
void onConnectComponent() override
Definition: PathPlanner.cpp:49
armarx::PathPlanner::clearCollisionObjects
void clearCollisionObjects(const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PathPlanner.cpp:126
armarx::PathPlanner::CollisionObjectData::colModel
VirtualRobot::CollisionModelPtr colModel
Definition: PathPlanner.h:127
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::ArmarXDataPath::getDataPaths
static std::vector< std::string > getDataPaths()
Definition: ArmarXDataPath.cpp:620
armarx::PathPlanner::onInitComponent
void onInitComponent() override
Definition: PathPlanner.cpp:43
ObjectClass.h
IceInternal::Handle< ObjectClass >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::PathPlanner::setSafetyMargin
void setSafetyMargin(::Ice::Float margin, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PathPlanner.cpp:180
PathPlanner.h
armarx::PathPlanner::CollisionObjectData::object
memoryx::ObjectClassBasePtr object
Definition: PathPlanner.h:126
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::Vector3
The Vector3 class.
Definition: Pose.h:112
armarx::PathPlanner::PathPlanner
PathPlanner()
Definition: PathPlanner.cpp:37
MemoryXCoreObjectFactories.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::PathPlanner::setCollisionObjects
void setCollisionObjects(const ::armarx::ObjectPositionBaseList &list, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PathPlanner.cpp:58
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
SimoxObjectWrapper.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::PathPlanner::CollisionObjectData
Definition: PathPlanner.h:124
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
MemoryXTypesObjectFactories.h
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:111
ArmarXDataPath.h
armarx::PathPlanner::addCollisionObjects
void addCollisionObjects(const ::armarx::ObjectPositionBaseList &list, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PathPlanner.cpp:64
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::PathPlanner::setAgent
void setAgent(const ::memoryx::AgentInstanceBasePtr &newAgent, const std::string &agentColModelName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: PathPlanner.cpp:132