PlanMotionBase.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 RobotSkillTemplates::MotionPlanningGroup
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2016
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "PlanMotionBase.h"
24 
29 
30 using namespace armarx;
31 using namespace MotionPlanningGroup;
32 
33 // DO NOT EDIT NEXT LINE
34 PlanMotionBase::SubClassRegistry PlanMotionBase::Registry(PlanMotionBase::GetName(), &PlanMotionBase::CreateInstance);
35 
37 {
38  resetTask();
39  //called prior to any locking of a mutex -> no deadlock
41  {
42  resetTask();
43  };
44 
45  //setup cspace
46  SimoxCSpacePtr cspaceSimox;
47  if (in.getPlanPlatformMovement())
48  {
49  SimoxCSpaceWith2DPosePtr cspaceSimox2d = new SimoxCSpaceWith2DPose {getWorkingMemory()->getCommonStorage()};
50  if (!in.isCSpacePlatformMovementBoundsMinSet())
51  {
52  ARMARX_ERROR_S << "platform movement should be planned, but the min movement bounds were not set.";
53  emitFailure();
54  return;
55  }
56  if (!in.isCSpacePlatformMovementBoundsMaxSet())
57  {
58  ARMARX_ERROR_S << "platform movement should be planned, but the max movement bounds were not set.";
59  emitFailure();
60  return;
61  }
62  cspaceSimox = cspaceSimox2d;
63  Vector3fRange movBounds;
64  movBounds.min.e0 = in.getCSpacePlatformMovementBoundsMin()->x;
65  movBounds.min.e1 = in.getCSpacePlatformMovementBoundsMin()->y;
66  movBounds.min.e2 = in.getCSpacePlatformMovementBoundsMin()->z;
67  movBounds.max.e0 = in.getCSpacePlatformMovementBoundsMax()->x;
68  movBounds.max.e1 = in.getCSpacePlatformMovementBoundsMax()->y;
69  movBounds.max.e2 = in.getCSpacePlatformMovementBoundsMax()->z;
70  cspaceSimox2d->setPoseBounds(movBounds);
71  }
72  else
73  {
74  cspaceSimox = new SimoxCSpace {getWorkingMemory()->getCommonStorage()};
75  }
76 
77  //agent
78  AgentPlanningInformation agent;
79  agent.agentProjectNames = in.getAgentProjectName();
80  agent.agentRelativeFilePath = in.getAgentRelativeFilePath();
81 
82  if (in.isCSpaceKinemaicChainNamesSet())
83  {
84  agent.kinemaicChainNames = in.getCSpaceKinemaicChainNames();
85  }
86  if (in.isCSpaceAdditionalJointsForPlanningSet())
87  {
88  agent.additionalJointsForPlanning = in.getCSpaceAdditionalJointsForPlanning();
89  }
90  if (in.isCSpaceJointsExcludedFromPlanningSet())
91  {
92  agent.jointsExcludedFromPlanning = in.getCSpaceJointsExcludedFromPlanning();
93  }
94 
95  agent.collisionSetNames = in.getCollisionSetNames();
96  if (in.isInitialJointValuesSet())
97  {
98  agent.initialJointValues = in.getInitialJointValues();
99  }
100  agent.agentPose = in.getInitialAgentPose();
101 
102  if (agent.kinemaicChainNames.empty() && agent.additionalJointsForPlanning.empty())
103  {
104  ARMARX_ERROR_S << "no joints set for planning! (define CSpaceAdditionalJointsForPlanning and/or CSpaceKinemaicChainNames)";
105  emitFailure();
106  return;
107  }
108 
109  ARMARX_VERBOSE_S << "Loading agent from project '" << agent.agentProjectName
110  << "' and path '" << agent.agentRelativeFilePath << "'";
111 
112  cspaceSimox->setAgent(agent);
113  //objects
114  cspaceSimox->addObjectsFromWorkingMemory(getWorkingMemory());
115  cspaceSimox->setStationaryObjectMargin(in.getStationaryObjectMargin());
116  VectorXf start = cspaceSimox->jointMapToVector(in.getConfigStart());
117  if (start.size() != static_cast<std::size_t>(cspaceSimox->getDimensionality()))
118  {
119  ARMARX_ERROR_S << "the start config has " << start.size() << "demensions, but the cspace has " << cspaceSimox->getDimensionality() << " dimensions.";
120  emitFailure();
121  return;
122  }
123 
124  VectorXf stop = cspaceSimox->jointMapToVector(in.getConfigStop());
125  if (stop.size() != static_cast<std::size_t>(cspaceSimox->getDimensionality()))
126  {
127  ARMARX_ERROR_S << "the stop config has " << stop.size() << "demensions, but the cspace has " << cspaceSimox->getDimensionality() << " dimensions.";
128  emitFailure();
129  return;
130  }
131  ARMARX_IMPORTANT_S << "planned joints: " << cspaceSimox->getAgentJointNames();
132 
133  CSpaceBasePtr cspace = cspaceSimox;
134  ScaledCSpacePtr scaledCspace;
135  if (in.getPlanPlatformMovement())
136  {
137  //we need scaling
138  VectorXf scale = {0.005, 0.005, 1};
139  scale.resize(cspaceSimox->getDimensionality(), 1);
140  scaledCspace = new ScaledCSpace {cspaceSimox, scale};
141  scaledCspace->scaleConfig(start);
142  scaledCspace->scaleConfig(stop);
143  cspace = scaledCspace;
144  }
145 
146  float dcdStepSize = 0.1;
147 
148  std::lock_guard< std::mutex> lock {taskMutex};
149  assert(!task);
150  task =
151  getMotionPlanningServer()->enqueueTask(
153  {
154  new RRTConnectTask
155  {
156  cspace,
157  start,
158  stop,
159  "Statechart.PlanMotionBase.RRT",
160  in.getPlanningTimeInSeconds(),
161  dcdStepSize
162  },
163  "Statechart.PlanMotionBase.PostProcess",
164  in.getTrajectoryOptimizationTimeout()//max time spend for postprocessing
165  }
166  );
167 
168  //wait for the task to finish (we can't call wait, since we may want to abort)
169  while (!isRunningTaskStopped() && !task->finishedRunning())
170  {
171  //time does not need to respect the time server (this is just polling for a result)
172  std::this_thread::sleep_for(std::chrono::milliseconds(20));
173  }
174  if (TaskStatus::eDone == task->getTaskStatus())
175  {
176  //found a path!
177  PathWithCost p = task->getPathWithCost();
178  if (scaledCspace)
179  {
180  //we planned movement->we need to unscale the path
181  scaledCspace->unscalePath(p.nodes);
182  }
183  TrajectoryPtr trajectory = cspaceSimox->pathToTrajectory(p);// new Trajectory{p.nodes, Ice::DoubleSeq{}, cspaceSimox->getAgentJointNames()};
184  ARMARX_INFO_S << "Traj: " << trajectory->output();
185  setOutput("Trajectory", trajectory);
186  emitSuccess();
187  return;
188  }
189  //no path was found!
190  emitFailure();
191 }
192 
194 {
195  std::lock_guard< std::mutex> lock {taskMutex};
196  if (task)
197  {
198  task->abortTask();
199  task = nullptr;
200  }
201 }
202 
203 // DO NOT EDIT NEXT FUNCTION
205 {
206  return XMLStateFactoryBasePtr(new PlanMotionBase(stateData));
207 }
208 
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:203
OnScopeExit.h
ScaledCSpace.h
armarx::SimoxCSpaceWith2DPose
Similar to armarx::SimoxCSpace, but prepends dimensions for translation in x and y and a rotation aro...
Definition: SimoxCSpace.h:366
armarx::XMLStateConstructorParams
Definition: XMLState.h:50
ARMARX_ON_SCOPE_EXIT
#define ARMARX_ON_SCOPE_EXIT
Executes given code when the enclosing scope is left.
Definition: OnScopeExit.h:112
armarx::MotionPlanningGroup::PlanMotionBase::Registry
static SubClassRegistry Registry
Definition: PlanMotionBase.h:72
IceInternal::Handle< SimoxCSpace >
PlanMotionBase.h
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::MotionPlanningGroup::PlanMotionBase::run
void run() override
Definition: PlanMotionBase.cpp:36
armarx::MotionPlanningGroup::PlanMotionBase::CreateInstance
static XMLStateFactoryBasePtr CreateInstance(XMLStateConstructorParams stateData)
Definition: PlanMotionBase.cpp:204
Task.h
armarx::rrtconnect::Task
Definition: Task.h:47
armarx::MotionPlanningGroup::PlanMotionBase::taskMutex
std::mutex taskMutex
Definition: PlanMotionBase.h:78
armarx::ScaledCSpace
Takes an other cspace and scales its' dimensions.
Definition: ScaledCSpace.h:43
armarx::MotionPlanningGroup::PlanMotionBase::PlanMotionBase
PlanMotionBase(const XMLStateConstructorParams &stateData)
Definition: PlanMotionBase.h:35
armarx::XMLStateFactoryBasePtr
IceInternal::Handle< XMLStateFactoryBase > XMLStateFactoryBasePtr
Definition: XMLState.h:65
SimoxCSpace.h
armarx::MotionPlanningGroup::PlanMotionBase::resetTask
void resetTask()
Definition: PlanMotionBase.cpp:193
armarx::rngshortcut::Task
Definition: Task.h:46
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::MotionPlanningGroup::PlanMotionBase::task
RandomShortcutPostprocessorTaskHandle task
Definition: PlanMotionBase.h:77
armarx::SimoxCSpace
The SimoxCSpace contains a set of stationary obstacles and an agent.
Definition: SimoxCSpace.h:67
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28