NavigateToLocation.cpp
Go to the documentation of this file.
1 #include "NavigateToLocation.h"
2 
3 #include <future>
4 #include <memory>
5 
6 #include <Eigen/Core>
7 #include <Eigen/Geometry>
8 
10 
22 
24 {
25 
26  template <typename R>
27  bool
28  is_ready(std::future<R> const& f)
29  {
30  if (!f.valid())
31  {
32  return true;
33  }
34  std::future_status status = f.wait_for(std::chrono::seconds(0));
35  if (status == std::future_status::ready)
36  {
37  return true;
38  }
39  else if (status == std::future_status::deferred)
40  {
41  // if the task has not been started --> start the task
42  f.wait();
43  return true;
44  }
45  else
46  {
47  return false;
48  }
49  }
50 
53  {
54  ParamType defaultParameters;
55  defaultParameters.location = "";
56  defaultParameters.velocityLimitAngular = std::nullopt;
57  defaultParameters.velocityLimitLinear = std::nullopt;
58 
59  std::stringstream description;
61  << "Retrieve the location `location` from the memory and navigate to it."
62  << "\n\nThe `location` is specified by its provider segment name and entity name"
63  " in the format `providerSegmentName/entityName` .";
64 
67  .description = description.str(),
68  .rootProfileDefaults = defaultParameters.toAron(),
70  .parametersType = Params::ToAronType(),
71  };
72  }
73 
74  NavigateToLocation::NavigateToLocation(const Services& srv) : Base(DefaultSkillDescription())
75  {
76  srv_.emplace(srv);
77  }
78 
80  NavigateToLocation::init(const Base::SpecializedInitInput& in)
81  {
83  trajControllerParams{};
85 
86  client::GeneralConfig generalParams{};
87 
88  if (in.parameters.velocityLimitLinear.has_value())
89  {
90  trajControllerParams.limits.linear = in.parameters.velocityLimitLinear.value();
91  globalPlannerParams.linearVelocity = in.parameters.velocityLimitLinear.value();
92  generalParams.maxVel.linear = in.parameters.velocityLimitLinear.value();
93  }
94 
95  if (in.parameters.velocityLimitAngular.has_value())
96  {
97  trajControllerParams.limits.angular = in.parameters.velocityLimitAngular.value();
98  globalPlannerParams.angularVelocity = in.parameters.velocityLimitAngular.value();
99  generalParams.maxVel.angular = in.parameters.velocityLimitAngular.value();
100  }
101 
102  ARMARX_INFO << "Configuring navigator to respect the velocity limits:";
103  ARMARX_INFO << "linear: " << trajControllerParams.limits.linear;
104  ARMARX_INFO << "angular: " << trajControllerParams.limits.angular;
105 
106  // parameterize the navigation stack
107  client::NavigationStackConfig cfg;
108  cfg.general(generalParams);
109  cfg.globalPlanner(globalPlannerParams);
110  cfg.trajectoryController(trajControllerParams);
111 
112  const std::string configId = DefaultSkillDescription().skillId.skillName;
113 
114  // configure the `navigator` which provides a simplified and typed interface to the navigation server
115  ARMARX_TRACE;
116  memorySubscriber.reset();
117 
118  ARMARX_TRACE;
119  memorySubscriber.emplace(configId, srv_->memoryNameSystem);
120 
121  // register our config
122  ARMARX_INFO << "Registering config";
123  srv_->iceNavigator.createConfig(cfg, configId);
124 
125  navigator.emplace(client::Navigator::InjectedServices{
126  .navigator = &srv_->iceNavigator, .subscriber = &memorySubscriber.value()});
127 
128  return ::armarx::skills::Skill::InitResult{
129  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
130  }
131 
133  NavigateToLocation::main(const Base::SpecializedMainInput& in)
134  {
135  const std::string& location = in.parameters.location;
136 
137  ARMARX_INFO << "moving to target " << VAROUT(location);
138 
139  // execute
140  ARMARX_INFO << "Sending navigation request";
141 
142  // client::PathBuilder builder;
143  // builder.add(location, client::GlobalPlanningStrategy::Free);
144 
145  navigator->moveToLocation(location);
146 
147  // Wait until goal is reached
148  ARMARX_INFO << "Waiting until goal is reached.";
149  auto future = std::async(std::launch::async, [this]() { return navigator->waitForStop(); });
150  while (not is_ready(future))
151  {
152  if (shouldSkillTerminate())
153  {
154  ARMARX_INFO << "Skill was aborted by user";
155  navigator->stop();
156  break;
157  }
158  }
159 
160  auto se = future.get();
161  if (se)
162  {
163  ARMARX_INFO << "Goal `" << location << "`reached.";
164  return ::armarx::skills::Skill::MainResult{
165  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
166  }
167  else
168  {
169  if (se.isSafetyStopTriggeredEvent())
170  {
171  ARMARX_ERROR << "Safety stop was triggered!";
172 
173  return ::armarx::skills::Skill::MainResult{
174  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
175  }
176  if (se.isUserAbortTriggeredEvent())
177  {
178  ARMARX_ERROR << "Aborted by user!";
179 
180  return ::armarx::skills::Skill::MainResult{
181  .status = ::armarx::skills::TerminatedSkillStatus::Aborted};
182  }
183  if (se.isInternalErrorEvent())
184  {
185  ARMARX_ERROR << "Unknown internal error occured! "
186  << se.toInternalErrorEvent().message;
187 
188  return ::armarx::skills::Skill::MainResult{
189  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
190  }
191  }
192 
193  return ::armarx::skills::Skill::MainResult{
194  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
195  }
196 
197  void
198  NavigateToLocation::onStopRequested()
199  {
200  if (navigator.has_value())
201  {
202  ARMARX_CHECK(navigator.has_value());
203  navigator->stop();
204  }
205  }
206 
207 } // namespace armarx::navigation::skills
armarx::skills::SimpleSpecializedSkill< arondto::NavigateToLocationParams >::init
Skill::InitResult init() final
Definition: SimpleSpecializedSkill.h:62
SPFA.h
Navigator.h
armarx::skills::SimpleSpecializedSkill< arondto::NavigateToLocationParams >::ParamType
arondto::NavigateToLocationParams ParamType
Definition: SimpleSpecializedSkill.h:14
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::SkillDescription
Definition: SkillDescription.h:18
Duration.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
MemorySubscriber.h
NavigateToLocation.h
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::status
status
Definition: FiniteStateMachine.h:259
armarx::core::time::Duration::Seconds
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:83
armarx::navigation::skills::NavigateToLocation::NavigateToLocation
NavigateToLocation(const Services &srv)
Definition: NavigateToLocation.cpp:74
NavigationStackConfig.h
PathBuilder.h
armarx::navigation::skills::NavigateToLocation::DefaultSkillDescription
static armarx::skills::SkillDescription DefaultSkillDescription()
Definition: NavigateToLocation.cpp:52
skill_ids.h
IceNavigator.h
armarx::skills::Skill::description
SkillDescription description
Definition: Skill.h:336
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::navigation::skills::skill_ids::NavigateToLocation
const armarx::skills::SkillID NavigateToLocation
Definition: skill_ids.cpp:58
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:48
armarx::skills::SkillDescription::rootProfileDefaults
aron::data::DictPtr rootProfileDefaults
Definition: SkillDescription.h:22
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::skills::SimpleSpecializedSkill< arondto::NavigateToLocationParams >::main
Skill::MainResult main() final
Definition: SimpleSpecializedSkill.h:71
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::navigation::skills::NavigateToLocation::Services
Definition: NavigateToLocation.h:39
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::global_planning::SPFAParams::linearVelocity
float linearVelocity
Definition: SPFA.h:43
armarx::navigation::skills::is_ready
bool is_ready(std::future< R > const &f)
Definition: NavigateRelativeSkill.h:17
armarx::navigation::skills
Definition: constants.cpp:25
armarx::navigation::global_planning::SPFAParams
Parameters for AStar.
Definition: SPFA.h:41
AStar.h