NavigateToLocation.cpp
Go to the documentation of this file.
2
3#include <chrono>
4#include <future>
5#include <mutex>
6#include <optional>
7#include <sstream>
8#include <string>
9
10#include <Eigen/Core>
11#include <Eigen/Geometry>
12
18
22
30
32{
33
34 template <typename R>
35 bool
36 is_ready(std::future<R> const& f)
37 {
38 if (!f.valid())
39 {
40 return true;
41 }
42 std::future_status status = f.wait_for(std::chrono::seconds(0));
43 if (status == std::future_status::ready)
44 {
45 return true;
46 }
47 else if (status == std::future_status::deferred)
48 {
49 // if the task has not been started --> start the task
50 f.wait();
51 return true;
52 }
53 else
54 {
55 return false;
56 }
57 }
58
59 armarx::skills::SkillDescription
61 {
62 ParamType defaultParameters;
63 defaultParameters.location = "";
64 defaultParameters.navigatingSkillParams = NavigatingSkillHelper::DefaultSkillDescription();
65
66
67 std::stringstream description;
69 << "Retrieve the location `location` from the memory and navigate to it."
70 << "\n\nThe `location` is specified by its provider segment name and entity name"
71 " in the format `providerSegmentName/entityName` .";
72
75 .description = description.str(),
76 .rootProfileDefaults = defaultParameters.toAron(),
78 .parametersType = Params::ToAronType(),
79 };
80 }
81
87
89 NavigateToLocation::init(const Base::SpecializedInitInput& in)
90 {
91 helper.init(in.parameters.navigatingSkillParams,
92 DefaultSkillDescription().skillId.skillName);
93
94 return ::armarx::skills::Skill::InitResult{
96 }
97
99 NavigateToLocation::main(const Base::SpecializedMainInput& in)
100 {
101 const std::string& location = in.parameters.location;
102 const std::optional<std::string>& locationProvider = in.parameters.locationProvider;
103
104 ARMARX_INFO << "moving to target " << VAROUT(location);
105
106 // execute
107 ARMARX_INFO << "Sending navigation request";
108
109 // client::PathBuilder builder;
110 // builder.add(location, client::GlobalPlanningStrategy::Free);
111
112 helper.getNavigator()->moveToLocation(location, locationProvider);
113
114 // Wait until goal is reached
115 ARMARX_INFO << "Waiting until goal is reached.";
116 auto future = std::async(std::launch::async,
117 [this]() { return helper.getNavigator()->waitForStop(); });
118 while (not is_ready(future))
119 {
121 {
122 ARMARX_INFO << "Skill was aborted by user";
123 helper.getNavigator()->stop();
124 break;
125 }
126 }
127
128 auto se = future.get();
129 if (se)
130 {
131 ARMARX_INFO << "Goal " << QUOTED(location) << "reached.";
132 return ::armarx::skills::Skill::MainResult{
134 }
135 else
136 {
137 if (se.isSafetyStopTriggeredEvent())
138 {
139 ARMARX_INFO << "Safety stop was triggered!";
140
141 return ::armarx::skills::Skill::MainResult{
143 }
144 if (se.isUserAbortTriggeredEvent())
145 {
146 ARMARX_INFO << "Aborted by user!";
147
148 return ::armarx::skills::Skill::MainResult{
150 }
151 if (se.isInternalErrorEvent())
152 {
153 ARMARX_INFO << "Unknown internal error occured! "
154 << se.toInternalErrorEvent().message;
155
156 return ::armarx::skills::Skill::MainResult{
158 }
159 if (se.isGlobalPlanningFailedEvent())
160 {
161 ARMARX_ERROR << "Global planning failed! "
162 << se.toGlobalPlanningFailedEvent().message;
163
164 return ::armarx::skills::Skill::MainResult{
166 }
167
168 ARMARX_ERROR << "Unknown event!";
169 return ::armarx::skills::Skill::MainResult{
171 }
172
173 return ::armarx::skills::Skill::MainResult{
175 }
176
177 void
178 NavigateToLocation::onStopRequested()
179 {
180 if (helper.getNavigator().has_value())
181 {
182 ARMARX_CHECK(helper.getNavigator().has_value());
183 helper.getNavigator()->stop();
184 }
185 }
186
187} // namespace armarx::navigation::skills
#define VAROUT(x)
#define QUOTED(x)
static Duration Hours(std::int64_t hours)
Constructs a duration in hours.
Definition Duration.cpp:120
::armarx::skills::SimpleSpecializedSkill< Params > Base
static armarx::skills::SkillDescription DefaultSkillDescription()
NavigateToLocation(const NavigatingSkillHelper::Properties &properties, const NavigatingSkillHelper::Services &srv)
std::optional< client::Navigator > & getNavigator()
void init(const arondto::NavigatingSkillParams &params, const std::string &id)
static arondto::NavigatingSkillParams DefaultSkillDescription()
SkillDescription description
Definition Skill.h:372
bool shouldSkillTerminate() const override
Returns whether the skill should terminate as soon as possible.
Definition Skill.cpp:469
#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
const armarx::skills::SkillID NavigateToLocation
Definition skill_ids.cpp:61
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