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
24
26{
27
28 template <typename R>
29 bool
30 is_ready(std::future<R> const& f)
31 {
32 if (!f.valid())
33 {
34 return true;
35 }
36 std::future_status status = f.wait_for(std::chrono::seconds(0));
37 if (status == std::future_status::ready)
38 {
39 return true;
40 }
41 else if (status == std::future_status::deferred)
42 {
43 // if the task has not been started --> start the task
44 f.wait();
45 return true;
46 }
47 else
48 {
49 return false;
50 }
51 }
52
53 // abstract base class of a relative movement skill
54 template <class AronT>
56 {
57
58 public:
60
62
66 Base(desc), helper(properties, srv)
67 {
68 }
69
70 virtual ~NavigateRelativeSkill() = default;
71
72 private:
73 // implemented by children returning the relative movement to execute
74 virtual Eigen::Isometry3f relativeTarget(const typename Base::SpecializedMainInput& in) = 0;
75
77 init(const typename Base::SpecializedInitInput& in) override
78 {
79 helper.init(in.parameters.navigatingSkillParams, Base::getSkillId().skillName);
80
81 return ::armarx::skills::Skill::InitResult{
83 }
84
86 main(const typename Base::SpecializedMainInput& in) override
87 {
88 const Eigen::Isometry3f relativeTarget = this->relativeTarget(in);
89
90 ARMARX_INFO << "Moving to relative target " << VAROUT(relativeTarget.matrix());
91
92 // excecute
93 ARMARX_INFO << "Sending navigation request";
94 helper.getNavigator()->moveTo(relativeTarget, core::NavigationFrame::Relative);
95
96 // Wait until goal is reached
97 ARMARX_INFO << "Waiting until goal is reached.";
98
99 auto future = std::async(std::launch::async,
100 [this]() { return helper.getNavigator()->waitForStop(); });
101 while (not is_ready(future))
102 {
104 {
105 ARMARX_INFO << "Skill was aborted by user " << Base::stopped << ", "
107 helper.getNavigator()->stop();
108 break;
109 }
110 }
111
112 auto se = future.get();
113 if (se)
114 {
115 ARMARX_INFO << "Goal " << QUOTED(relativeTarget.matrix()) << "reached.";
116 }
117 else
118 {
119 if (se.isSafetyStopTriggeredEvent())
120 {
121 ARMARX_ERROR << "Safety stop was triggered!";
122
123 return armarx::skills::Skill::MainResult{
125 }
126 if (se.isUserAbortTriggeredEvent())
127 {
128 ARMARX_ERROR << "Aborted by user!";
129
130 return armarx::skills::Skill::MainResult{
132 }
133 if (se.isInternalErrorEvent())
134 {
135 ARMARX_ERROR << "Unknown internal error occured! "
136 << se.toInternalErrorEvent().message;
137
138 return armarx::skills::Skill::MainResult{
140 }
141 if (se.isGlobalPlanningFailedEvent())
142 {
143 ARMARX_ERROR << "Global planning failed! "
144 << se.toGlobalPlanningFailedEvent().message;
145
146 return ::armarx::skills::Skill::MainResult{
148 }
149
150 ARMARX_ERROR << "Unknown event!";
151 return ::armarx::skills::Skill::MainResult{
153 }
154
155 return armarx::skills::Skill::MainResult{
157 }
158
159 void
160 onStopRequested() override
161 {
162 if (helper.getNavigator().has_value())
163 {
164 ARMARX_CHECK(helper.getNavigator().has_value());
165 helper.getNavigator()->stop();
166 }
167 }
168
169
170 private:
171 NavigatingSkillHelper helper;
172 };
173
174} // namespace armarx::navigation::skills
#define VAROUT(x)
#define QUOTED(x)
NavigateRelativeSkill(const NavigatingSkillHelper::Properties &properties, const NavigatingSkillHelper::Services &srv, const armarx::skills::SkillDescription &desc)
armarx::skills::SimpleSpecializedSkill< arondto::MoveRelativePlanarParams > Base
std::optional< client::Navigator > & getNavigator()
Skill::MainResult main() final
Override this method with the actual implementation.
Skill::InitResult init() final
Override this method with the actual implementation.
std::atomic_bool timeoutReached
Definition Skill.h:387
std::atomic_bool stopped
Definition Skill.h:386
SkillID getSkillId() const
Get the id of the skill.
Definition Skill.cpp:587
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
bool is_ready(std::future< R > const &f)
A result struct for skill initialization.
Definition Skill.h:50
A result struct for th main method of a skill.
Definition Skill.h:62