NavigateTo.cpp
Go to the documentation of this file.
1 #include "NavigateTo.h"
2 
3 #include <future>
4 #include <memory>
5 
6 #include <Eigen/Core>
7 #include <Eigen/Geometry>
8 
10 
21 
23 {
24  template <typename R>
25  bool
26  is_ready(std::future<R> const& f)
27  {
28  if (!f.valid())
29  {
30  return true;
31  }
32  std::future_status status = f.wait_for(std::chrono::seconds(0));
33  if (status == std::future_status::ready)
34  {
35  return true;
36  }
37  else if (status == std::future_status::deferred)
38  {
39  // if the task has not been started --> start the task
40  f.wait();
41  return true;
42  }
43  else
44  {
45  return false;
46  }
47  }
48 
49  NavigateTo::NavigateTo(const Services& srv) : Base(DefaultSkillDescription())
50  {
51  srv_.emplace(srv);
52  }
53 
55  NavigateTo::init(const Base::SpecializedInitInput& in)
56  {
57 
59  trajControllerParams{};
60 
61  client::GeneralConfig generalParams{};
62 
63  if (in.parameters.velocityLimitLinear.has_value())
64  {
65  trajControllerParams.limits.linear = in.parameters.velocityLimitLinear.value();
66  generalParams.maxVel.linear = in.parameters.velocityLimitLinear.value();
67  }
68 
69  if (in.parameters.velocityLimitAngular.has_value())
70  {
71  trajControllerParams.limits.angular = in.parameters.velocityLimitAngular.value();
72  generalParams.maxVel.angular = in.parameters.velocityLimitAngular.value();
73  }
74 
75  ARMARX_INFO << "Configuring navigator to respect the velocity limits:";
76  ARMARX_INFO << "linear: " << trajControllerParams.limits.linear;
77  ARMARX_INFO << "angular: " << trajControllerParams.limits.angular;
78 
79  // parameterize the navigation stack
80  client::NavigationStackConfig cfg;
81  cfg.general(generalParams);
83  cfg.trajectoryController(trajControllerParams);
84 
85  const std::string configId = DefaultSkillDescription().skillId.skillName;
86 
87  // configure the `navigator` which provides a simplified and typed interface to the navigation server
88  memorySubscriber.emplace(configId, srv_->memoryNameSystem);
89 
90  // register our config
91  ARMARX_INFO << "Registering config";
92  srv_->iceNavigator.createConfig(cfg, configId);
93 
94  navigator.emplace(client::Navigator::InjectedServices{
95  .navigator = &srv_->iceNavigator, .subscriber = &memorySubscriber.value()});
96 
97  return ::armarx::skills::Skill::InitResult{
98  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
99  }
100 
102  NavigateTo::main(const Base::SpecializedMainInput& in)
103  {
104  const Eigen::Isometry3f globalTarget(in.parameters.targetPose);
105 
106  ARMARX_INFO << "moving to target " << VAROUT(globalTarget.matrix());
107 
108  // execute
109  ARMARX_INFO << "Sending navigation request";
110  navigator->moveTo(globalTarget, core::NavigationFrame::Absolute);
111 
112  // Wait until goal is reached
113  ARMARX_INFO << "Waiting until goal is reached.";
114 
115  auto future = std::async(std::launch::async, [this]() { return navigator->waitForStop(); });
116  while (not is_ready(future))
117  {
118  if (shouldSkillTerminate())
119  {
120  ARMARX_INFO << "Skill was aborted by user " << stopped << ", " << timeoutReached;
121  navigator->stop();
122  break;
123  }
124  }
125 
126  auto se = future.get();
127  if (se)
128  {
129  ARMARX_INFO << "Goal `" << Eigen::Isometry3f(in.parameters.targetPose).translation()
130  << "`reached.";
131  }
132  else
133  {
134  if (se.isSafetyStopTriggeredEvent())
135  {
136  ARMARX_ERROR << "Safety stop was triggered!";
137 
138  return ::armarx::skills::Skill::MainResult{
139  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
140  }
141  if (se.isUserAbortTriggeredEvent())
142  {
143  ARMARX_ERROR << "Aborted by user!";
144 
145  return ::armarx::skills::Skill::MainResult{
146  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
147  }
148  if (se.isInternalErrorEvent())
149  {
150  ARMARX_ERROR << "Unknown internal error occured! "
151  << se.toInternalErrorEvent().message;
152 
153  return ::armarx::skills::Skill::MainResult{
154  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
155  }
156  }
157 
158 
159  return ::armarx::skills::Skill::MainResult{
160  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
161  }
162 
163  void
164  NavigateTo::onStopRequested()
165  {
166  if (navigator.has_value())
167  {
168  ARMARX_CHECK(navigator.has_value());
169  navigator->stop();
170  }
171  }
172 
173 
174 } // namespace armarx::navigation::skills
armarx::skills::SimpleSpecializedSkill< arondto::NavigateToParams >::init
Skill::InitResult init() final
Definition: SimpleSpecializedSkill.h:62
SPFA.h
Navigator.h
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams
Definition: TrajectoryFollowingController.h:35
types.h
armarx::skills::SkillID::skillName
std::string skillName
Definition: SkillID.h:60
armarx::skills::Skill::timeoutReached
std::atomic_bool timeoutReached
Definition: Skill.h:348
armarx::navigation::skills::NavigateTo::NavigateTo
NavigateTo(const Services &srv)
Definition: NavigateTo.cpp:49
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::skills::Skill::stopped
std::atomic_bool stopped
Definition: Skill.h:347
armarx::navigation::skills::NavigateTo::Services
Definition: NavigateTo.h:44
MemorySubscriber.h
armarx::navigation::core::NavigationFrame::Absolute
@ Absolute
armarx::status
status
Definition: FiniteStateMachine.h:259
NavigationStackConfig.h
PathBuilder.h
NavigateTo.h
IceNavigator.h
TrajectoryFollowingController.h
armarx::skills::SkillDescription::skillId
SkillID skillId
Definition: SkillDescription.h:20
armarx::skills::SimpleSpecializedSkill
Definition: SimpleSpecializedSkill.h:10
SimpleEventHandler.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:48
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::skills::SimpleSpecializedSkill< arondto::NavigateToParams >::main
Skill::MainResult main() final
Definition: SimpleSpecializedSkill.h:71
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::skills::Skill::shouldSkillTerminate
bool shouldSkillTerminate() const
Returns whether the skill should terminate as soon as possible.
Definition: Skill.cpp:371
armarx::skills::Skill::InitResult
A result struct for skill initialization.
Definition: Skill.h:36
armarx::navigation::core::VelocityLimits::linear
float linear
Definition: types.h:56
armarx::navigation::skills::is_ready
bool is_ready(std::future< R > const &f)
Definition: NavigateRelativeSkill.h:17
Logging.h
armarx::navigation::client::GeneralConfig::maxVel
core::VelocityLimits maxVel
max ...
Definition: NavigationStackConfig.h:48
armarx::navigation::skills::NavigateTo::DefaultSkillDescription
static armarx::skills::SkillDescription DefaultSkillDescription()
Definition: NavigateTo.h:62
armarx::navigation::client::GeneralConfig
Definition: NavigationStackConfig.h:42
armarx::navigation::skills
Definition: constants.cpp:25
armarx::navigation::global_planning::SPFAParams
Parameters for AStar.
Definition: SPFA.h:41
AStar.h