NavigateRelativeSkill.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <future>
5 #include <optional>
6 #include <string>
7 
11 
16 
23 
25 {
26 
27  template <typename R>
28  bool
29  is_ready(std::future<R> const& f)
30  {
31  if (!f.valid())
32  {
33  return true;
34  }
35  std::future_status status = f.wait_for(std::chrono::seconds(0));
36  if (status == std::future_status::ready)
37  {
38  return true;
39  }
40  else if (status == std::future_status::deferred)
41  {
42  // if the task has not been started --> start the task
43  f.wait();
44  return true;
45  }
46  else
47  {
48  return false;
49  }
50  }
51 
52  // abstract base class of a relative movement skill
53  template <class AronT>
54  class NavigateRelativeSkill : public armarx::skills::SimpleSpecializedSkill<AronT>
55  {
56 
57  public:
59 
60  struct Services
61  {
64  };
65 
66  NavigateRelativeSkill() = delete;
67 
68  NavigateRelativeSkill(const Services& srv, const armarx::skills::SkillDescription& desc) :
69  Base(desc)
70  {
71  srv_.emplace(srv);
72  }
73 
74  virtual ~NavigateRelativeSkill() = default;
75 
76  private:
77  // implemented by children returning the relative movement to execute
78  virtual Eigen::Isometry3f relativeTarget(const typename Base::SpecializedMainInput& in) = 0;
79 
81  init(const typename Base::SpecializedInitInput& in) override
82  {
83  // parameterize the navigation stack
89 
90  const std::string configId = this->getSkillId().skillName;
91 
92  // configure the `navigator` which provides a simplified and typed interface to the navigation server
93  memorySubscriber.emplace(configId, srv_->memoryNameSystem);
94 
95  // register our config
96  ARMARX_INFO << "Registering config";
97  srv_->iceNavigator.createConfig(cfg, configId);
98 
99  navigator.emplace(client::Navigator::InjectedServices{
100  .navigator = &srv_->iceNavigator, .subscriber = &memorySubscriber.value()});
101 
104  }
105 
107  main(const typename Base::SpecializedMainInput& in) override
108  {
109  const Eigen::Isometry3f relativeTarget = this->relativeTarget(in);
110 
111  ARMARX_INFO << "Moving to relative target " << VAROUT(relativeTarget.matrix());
112 
113  // excecute
114  ARMARX_INFO << "Sending navigation request";
115  navigator->moveTo(relativeTarget, core::NavigationFrame::Relative);
116 
117  // Wait until goal is reached
118  ARMARX_INFO << "Waiting until goal is reached.";
119 
120  auto future =
121  std::async(std::launch::async, [this]() { return navigator->waitForStop(); });
122  while (not is_ready(future))
123  {
125  {
126  ARMARX_INFO << "Skill was aborted by user " << Base::stopped << ", "
128  navigator->stop();
129  break;
130  }
131  }
132 
133  auto se = future.get();
134  if (se)
135  {
136  ARMARX_INFO << "Goal `" << relativeTarget.matrix() << "`reached.";
137  }
138  else
139  {
140  if (se.isSafetyStopTriggeredEvent())
141  {
142  ARMARX_ERROR << "Safety stop was triggered!";
143 
146  }
147  if (se.isUserAbortTriggeredEvent())
148  {
149  ARMARX_ERROR << "Aborted by user!";
150 
153  }
154  if (se.isInternalErrorEvent())
155  {
156  ARMARX_ERROR << "Unknown internal error occured! "
157  << se.toInternalErrorEvent().message;
158 
161  }
162  }
163 
166  }
167 
168  void
169  onStopRequested() override
170  {
171  if (navigator.has_value())
172  {
173  ARMARX_CHECK(navigator.has_value());
174  navigator->stop();
175  }
176  }
177 
178 
179  private:
180  std::optional<Services> srv_;
181 
182  // will be initialized in "init()"
183  std::optional<client::MemorySubscriber> memorySubscriber;
184  std::optional<client::Navigator> navigator;
185  };
186 
187 } // namespace armarx::navigation::skills
armarx::skills::TerminatedSkillStatus::Succeeded
@ Succeeded
SkillStatusUpdate.h
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams
Definition: TrajectoryFollowingController.h:40
armarx::skills::SkillDescription
Definition: SkillDescription.h:17
armarx::navigation::skills::NavigateRelativeSkill::NavigateRelativeSkill
NavigateRelativeSkill(const Services &srv, const armarx::skills::SkillDescription &desc)
Definition: NavigateRelativeSkill.h:68
armarx::navigation::client::NavigationStackConfig
Definition: NavigationStackConfig.h:49
armarx::navigation::client::IceNavigator
Definition: IceNavigator.h:17
armarx::navigation::client::NavigationStackConfig::globalPlanner
NavigationStackConfig & globalPlanner(const global_planning::GlobalPlannerParams &params)
Definition: NavigationStackConfig.cpp:68
armarx::skills::Skill::timeoutReached
std::atomic_bool timeoutReached
Definition: Skill.h:312
armarx::navigation::skills::NavigateRelativeSkill::NavigateRelativeSkill
NavigateRelativeSkill()=delete
SimpleSpecializedSkill.h
StringHelpers.h
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:311
armarx::navigation::skills::NavigateRelativeSkill::~NavigateRelativeSkill
virtual ~NavigateRelativeSkill()=default
Point2Point.h
MemorySubscriber.h
armarx::status
status
Definition: FiniteStateMachine.h:244
Skill.h
IceNavigator.h
TrajectoryFollowingController.h
armarx::skills::SimpleSpecializedSkill
Definition: SimpleSpecializedSkill.h:10
client.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::navigation::skills::NavigateRelativeSkill::Services
Definition: NavigateRelativeSkill.h:60
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:39
ExpressionException.h
armarx::skills::Skill::MainResult::status
TerminatedSkillStatus status
Definition: Skill.h:41
armarx::skills::TerminatedSkillStatus::Failed
@ Failed
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::navigation::global_planning::Point2PointParams
Parameters for Point2Point.
Definition: Point2Point.h:41
armarx::navigation::core::NavigationFrame::Relative
@ Relative
armarx::skills::Skill::InitResult::status
TerminatedSkillStatus status
Definition: Skill.h:29
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
armarx::skills::Skill::shouldSkillTerminate
bool shouldSkillTerminate() const
Returns whether the skill should terminate as soon as possible.
Definition: Skill.cpp:385
armarx::skills::Skill::InitResult
A result struct for skill initialization.
Definition: Skill.h:27
armarx::navigation::skills::is_ready
bool is_ready(std::future< R > const &f)
Definition: NavigateRelativeSkill.h:29
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:68
Logging.h
armarx::navigation::skills::NavigateRelativeSkill::Services::iceNavigator
client::IceNavigator & iceNavigator
Definition: NavigateRelativeSkill.h:62
armarx::navigation::client::GeneralConfig
Definition: NavigationStackConfig.h:36
types.h
armarx::navigation::skills
Definition: constants.cpp:25
SkillDescription.h
armarx::navigation::skills::NavigateRelativeSkill::Services::memoryNameSystem
armem::client::MemoryNameSystem & memoryNameSystem
Definition: NavigateRelativeSkill.h:63
armarx::navigation::client::NavigationStackConfig::trajectoryController
NavigationStackConfig & trajectoryController(const traj_ctrl::local::TrajectoryControllerParams &params)
Definition: NavigationStackConfig.cpp:97
armarx::navigation::client::NavigationStackConfig::general
NavigationStackConfig & general(const GeneralConfig &cfg)
Definition: NavigationStackConfig.cpp:61