LookAtHumanFace.cpp
Go to the documentation of this file.
1 #include "LookAtHumanFace.h"
2 
3 #include <Eigen/Core>
4 #include <Eigen/Geometry>
5 
7 
9 
11 {
12  LookAtHumanFace::LookAtHumanFace(const Services& srv) : Base(DefaultSkillDescription())
13  {
14  srv_.emplace(srv);
15  }
16 
18  LookAtHumanFace::init(const Base::SpecializedInitInput& in)
19  {
20  // initialize variables; store which human to follow
21 
22  fromAron(in.parameters.humanTrackingId, humanTrackingId);
23 
24  handoverTargetProvider = std::make_unique<target_provider::handover::RobotReceiver>(
25  humanTrackingId, srv_->robot);
26  handoverTargetProvider->setPhase(target_provider::handover::Phase::PostHandover);
27 
28  return ::armarx::skills::Skill::InitResult{
29  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
30  }
31 
33  LookAtHumanFace::main(const Base::SpecializedMainInput& in)
34  {
35  // repeatedly update the robot's gaze target to follow the requested human
36  // (method is blocking, as intended for skills)
37 
38  // setup metronome to control execution frequency
39  const auto metronomeTargetPeriod =
40  armarx::Duration::MilliSeconds(properties.updatePeriodMs);
41  armarx::Metronome metronome(metronomeTargetPeriod);
42 
43  while (not shouldSkillTerminate() and not stop_requested_)
44  {
45  // update the view target based on the current pose of the human
46  ARMARX_INFO << "Before update";
47  update();
48  ARMARX_INFO << "After update";
49 
50  metronome.waitForNextTick();
51  }
52 
53  ARMARX_INFO << "Successfully finished skill";
54 
55  return ::armarx::skills::Skill::MainResult{
56  .status = ::armarx::skills::TerminatedSkillStatus::Succeeded};
57  }
58 
59  void
60  LookAtHumanFace::update()
61  {
62  std::optional<armem::human::HumanPose> latestPoseOfRequestedHuman =
63  getLatestPoseOfRequestedHuman();
64 
65  if (latestPoseOfRequestedHuman.has_value())
66  {
67  // updateTargetsAfterHandover generates targets to look at the head
68  std::vector<gaze_targets::GazeTarget> gazeTargets =
69  handoverTargetProvider->updateTargetsAfterHandover(
70  latestPoseOfRequestedHuman.value());
71  for (const auto& gazeTarget : gazeTargets)
72  {
73  ARMARX_INFO << "Committing gaze target " << gazeTarget;
74  }
75  srv_->viewSelectionClient.commitGazeTargets(gazeTargets);
76  }
77  else
78  {
79  ARMARX_VERBOSE << "Did not find any pose for the requested human tracking ID";
80  }
81  }
82 
83  std::optional<armem::human::HumanPose>
84  LookAtHumanFace::getLatestPoseOfRequestedHuman()
85  {
86  // get the current pose of the human
88  builder.coreSegments()
89  .withName(srv_->humanPoseMemoryID.coreSegmentName)
91  .all()
92  .entities()
93  .all()
94  .snapshots()
95  .latest();
96  armem::client::QueryResult result =
97  srv_->memoryNameSystem.getReader(srv_->humanPoseMemoryID)
98  .query(builder.buildQueryInput());
99 
100  std::optional<armem::human::HumanPose> latestPoseOfRequestedHuman = std::nullopt;
101  if (result.success)
102  {
103  result.memory.forEachInstance(
104  [this, &latestPoseOfRequestedHuman](const armem::wm::EntityInstance& instance)
105  {
106  armem::human::HumanPose humanPose;
107  armarx::human::arondto::HumanPose dto =
108  armarx::human::arondto::HumanPose::FromAron(instance.data());
109  fromAron(dto, humanPose);
110  if (humanPose.humanTrackingId == humanTrackingId &&
111  (!latestPoseOfRequestedHuman.has_value() ||
112  humanPose.timestamp > latestPoseOfRequestedHuman->timestamp))
113  {
114  latestPoseOfRequestedHuman = humanPose;
115  }
116  });
117  }
118  else
119  {
120  ARMARX_WARNING << "Failed to query the human memory: " << result.errorMessage;
121  }
122  return latestPoseOfRequestedHuman;
123  }
124 
125  void
126  LookAtHumanFace::onStopRequested()
127  {
128  ARMARX_INFO << "Stop requested";
129  stop_requested_ = true;
130  }
131 
132 
133 } // namespace armarx::view_selection::skills
armarx::view_selection::skills
This file is part of ArmarX.
Definition: constants.cpp:25
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:104
armarx::skills::SimpleSpecializedSkill< arondto::LookAtHumanFaceParams >::init
Skill::InitResult init() final
Definition: SimpleSpecializedSkill.h:62
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::armem::client::query::SnapshotSelector::latest
SnapshotSelector & latest()
Definition: selectors.cpp:30
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:65
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:135
armarx::armem::client::query::Builder::buildQueryInput
QueryInput buildQueryInput() const
Definition: Builder.cpp:12
armarx::armem::client::query::EntitySelector::snapshots
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition: selectors.cpp:92
armarx::armem::client::query::Builder::coreSegments
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition: Builder.cpp:42
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:198
armarx::skills::SimpleSpecializedSkill
Definition: SimpleSpecializedSkill.h:10
armarx::skills::Skill::MainResult
A result struct for th main method of a skill.
Definition: Skill.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::skills::SimpleSpecializedSkill< arondto::LookAtHumanFaceParams >::main
Skill::MainResult main() final
Definition: SimpleSpecializedSkill.h:71
armarx::view_selection::skills::LookAtHumanFace::Services
Definition: LookAtHumanFace.h:46
aron_conversions.h
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:34
armarx::skills::Skill::shouldSkillTerminate
bool shouldSkillTerminate() const
Returns whether the skill should terminate as soon as possible.
Definition: Skill.cpp:402
armarx::skills::Skill::InitResult
A result struct for skill initialization.
Definition: Skill.h:27
armarx::fromAron
void fromAron(const arondto::PackagePath &dto, PackageFileLocation &bo)
LookAtHumanFace.h
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:21
armarx::armem::client::query::CoreSegmentSelector::providerSegments
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
Definition: selectors.cpp:178
armarx::view_selection::skills::LookAtHumanFace::LookAtHumanFace
LookAtHumanFace(const Services &srv)
Definition: LookAtHumanFace.cpp:12
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::client::query::ProviderSegmentSelector::all
ProviderSegmentSelector & all() override
Definition: selectors.cpp:147
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:48
armarx::view_selection::target_provider::handover::Phase::PostHandover
@ PostHandover