GuideHumanToRoom.cpp
Go to the documentation of this file.
1 #include "GuideHumanToRoom.h"
2 
3 #include <cmath>
4 #include <memory>
5 
6 #include <Eigen/Core>
7 #include <Eigen/Geometry>
8 
22 
24 {
25  GuideHumanToRoom::GuideHumanToRoom(const Services& srv) : Base(DefaultSkillDescription())
26  {
27  srv_.emplace(srv);
28  }
29 
31  GuideHumanToRoom::init(const Base::SpecializedInitInput& in)
32  {
33  ARMARX_CHECK(srv_.has_value());
34 
35  // parameterize the navigation stack
41 
42  const std::string configId = DefaultSkillDescription().skillId.skillName;
43 
44  // configure the `navigator` which provides a simplified and typed interface to the navigation server
45  memorySubscriber.emplace(configId, srv_->memoryNameSystem);
46 
47  // register our config
48  ARMARX_INFO << "Registering config";
49  srv_->iceNavigator.createConfig(cfg, configId);
50 
51  navigator.emplace(client::Navigator::InjectedServices{
52  .navigator = &srv_->iceNavigator, .subscriber = &memorySubscriber.value()});
53 
54  return ::armarx::skills::Skill::InitResult{
55  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
56  }
57 
59  GuideHumanToRoom::main(const Base::SpecializedMainInput& in)
60  {
61  const core::Pose globalTarget = getTargetInFrontOfRoom(in.parameters.room);
62 
63  ARMARX_INFO << "moving to target " << VAROUT(globalTarget.matrix());
64 
65  // execute
66  ARMARX_INFO << "Sending navigation request";
67  navigator->moveTo(globalTarget, core::NavigationFrame::Absolute);
68 
69  // Wait until goal is reached
70  ARMARX_INFO << "Waiting until goal is reached.";
71  client::StopEvent se = navigator->waitForStop();
72  if (se)
73  {
74  ARMARX_INFO << "Goal `" << globalTarget.translation() << "`reached.";
75  }
76  else
77  {
78  if (se.isSafetyStopTriggeredEvent())
79  {
80  ARMARX_ERROR << "Safety stop was triggered!";
81 
82  return ::armarx::skills::Skill::MainResult{
83  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
84  }
85  if (se.isUserAbortTriggeredEvent())
86  {
87  ARMARX_ERROR << "Aborted by user!";
88 
89  return ::armarx::skills::Skill::MainResult{
90  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
91  }
92  if (se.isInternalErrorEvent())
93  {
94  ARMARX_ERROR << "Unknown internal error occured! "
95  << se.toInternalErrorEvent().message;
96 
97  return ::armarx::skills::Skill::MainResult{
98  .status = ::armarx::skills::TerminatedSkillStatus::Failed};
99  }
100  }
101 
102 
103  return ::armarx::skills::Skill::MainResult{
104  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
105  }
106 
107  void
108  GuideHumanToRoom::onStopRequested()
109  {
110  if (navigator.has_value())
111  {
112  ARMARX_CHECK(navigator.has_value());
113  navigator->stop();
114  }
115  }
116 
117  core::Pose
118  GuideHumanToRoom::getTargetInFrontOfRoom(const std::string& roomName) const
119  {
120  const algorithms::Costmap costmap = [&]
121  {
122  const memory::client::costmap::Reader::Query costmapQuery{
123  .providerName = properties.distanceToObstacleCostmapProvider,
124  .name = "distance_to_obstacles",
125  .timestamp = Clock::Now()};
126 
127  const auto result = srv_->costmapReader.query(costmapQuery);
128  ARMARX_CHECK(result) << result.errorMessage;
129  ARMARX_CHECK(result.costmap.has_value());
130  return result.costmap.value();
131  }();
132 
133  const armarx::navigation::rooms::Room room = [&]
134  {
135  const memory::client::rooms::Reader::Query roomQuery{.providerName =
136  properties.roomsProvider,
137  .name = roomName,
138  .timestamp = Clock::Now()};
139 
140  const auto result = srv_->roomsReader.query(roomQuery);
141  ARMARX_CHECK(result) << "Failed to query room `" << roomName << "` from provider `"
142  << properties.roomsProvider
143  << "`. Reason: " << result.errorMessage;
144  ARMARX_CHECK(result.room.has_value());
145 
146  return result.room.value();
147  }();
148 
149  const VirtualRobot::RobotPtr robot =
150  srv_->virtualRobotReader.getRobotWaiting(properties.robotName);
151  ARMARX_CHECK_NOT_NULL(robot);
152  ARMARX_CHECK(srv_->virtualRobotReader.synchronizeRobot(*robot, Clock::Now()));
153 
154  const core::Pose global_T_robot(robot->getGlobalPose());
155 
156  rooms::RoomNavigationTargetCreator::Params algoParams;
157  rooms::RoomNavigationTargetCreator algo(algoParams);
158  auto result = algo.getClosestPositionOutsideOfRoom(
159  costmap, room, global_T_robot.translation(), &srv_->arviz);
160 
161  const core::Direction dirToRoom = result.global_P_roomEntry - result.global_P_robot;
162 
163  // the robot should point to the room entry
164  const float yaw = std::atan2(dirToRoom.y(), dirToRoom.x()) - M_PI_2f32;
165 
166  // TODO maybe rotate robot a bit towards the human?
167 
168  core::Pose global_T_robot_target = core::Pose::Identity();
169  global_T_robot_target.translation() = result.global_P_robot;
170  global_T_robot_target.linear() =
171  Eigen::AngleAxisf(yaw, Eigen::Vector3f::UnitZ()).toRotationMatrix();
172 
173  return global_T_robot_target;
174  }
175 
176 
177 } // namespace armarx::navigation::skills
armarx::skills::SimpleSpecializedSkill< arondto::GuideHumanToRoomParams >::init
Skill::InitResult init() final
Definition: SimpleSpecializedSkill.h:62
SPFA.h
Navigator.h
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams
Definition: TrajectoryFollowingController.h:35
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
types.h
basic_types.h
armarx::skills::SkillID::skillName
std::string skillName
Definition: SkillID.h:60
armarx::navigation::skills::GuideHumanToRoom::GuideHumanToRoom
GuideHumanToRoom(const Services &srv)
Definition: GuideHumanToRoom.cpp:25
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
RoomNavigationTargetCreator.h
armarx::navigation::client::NavigationStackConfig
Definition: NavigationStackConfig.h:55
armarx::navigation::client::NavigationStackConfig::globalPlanner
NavigationStackConfig & globalPlanner(const global_planning::GlobalPlannerParams &params)
Definition: NavigationStackConfig.cpp:63
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Costmap.h
GuideHumanToRoom.h
MemorySubscriber.h
armarx::navigation::core::NavigationFrame::Absolute
@ Absolute
armarx::navigation::skills::GuideHumanToRoom::DefaultSkillDescription
static armarx::skills::SkillDescription DefaultSkillDescription()
Definition: GuideHumanToRoom.h:85
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
NavigationStackConfig.h
PathBuilder.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::navigation::skills::GuideHumanToRoom::Properties::roomsProvider
std::string roomsProvider
Definition: GuideHumanToRoom.h:67
armarx::navigation::skills::GuideHumanToRoom::Properties::distanceToObstacleCostmapProvider
std::string distanceToObstacleCostmapProvider
Definition: GuideHumanToRoom.h:65
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::GuideHumanToRoomParams >::main
Skill::MainResult main() final
Definition: SimpleSpecializedSkill.h:71
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::navigation::core::Direction
Eigen::Vector3f Direction
Definition: basic_types.h:39
armarx::skills::Skill::InitResult
A result struct for skill initialization.
Definition: Skill.h:36
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::navigation::skills::GuideHumanToRoom::Properties::robotName
std::string robotName
Definition: GuideHumanToRoom.h:68
armarx::navigation::rooms::Room
Definition: types.h:31
armarx::navigation::client::GeneralConfig
Definition: NavigationStackConfig.h:42
armarx::navigation::skills::GuideHumanToRoom::Services
Definition: GuideHumanToRoom.h:48
armarx::navigation::skills
Definition: constants.cpp:25
armarx::navigation::global_planning::SPFAParams
Parameters for AStar.
Definition: SPFA.h:41
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::navigation::client::NavigationStackConfig::trajectoryController
NavigationStackConfig & trajectoryController(const traj_ctrl::local::TrajectoryControllerParams &params)
Definition: NavigationStackConfig.cpp:91
armarx::navigation::client::NavigationStackConfig::general
NavigationStackConfig & general(const GeneralConfig &cfg)
Definition: NavigationStackConfig.cpp:56
AStar.h