LookForObjects.cpp
Go to the documentation of this file.
1#include "LookForObjects.h"
2
3#include <Eigen/Core>
4
8
12
14
15#include "ScanLocation.h"
16
18{
20 {
21 srv_.emplace(srv);
22 }
23
25 LookForObjects::init(const Base::SpecializedInitInput& in)
26 {
27 return ::armarx::skills::Skill::InitResult{
29 }
30
32 LookForObjects::main(const Base::SpecializedMainInput& in)
33 {
34 const auto tsStart = armarx::Clock::Now();
35
36 std::set<armarx::ObjectID> requiredObjectClassIDs;
37 for (const auto& objectID : in.parameters.requiredObjectClassIDs)
38 {
39 requiredObjectClassIDs.insert(armarx::aron::fromAron<armarx::ObjectID>(objectID));
40 }
41
42 const size_t numberOfRequiredObjectClassIDs = requiredObjectClassIDs.size();
43
44 const std::vector<Eigen::Vector3f> gazePositions =
45 ScanLocation::MakeGazeTargetPositions(in.parameters.scanLocation);
46
47 const Duration duration =
48 Duration::SecondsDouble(in.parameters.scanLocation.durationPerViewSeconds);
49
51
52 Result result;
53
54 DateTime nextTargetDueTime = DateTime::Now();
55 size_t nextTargetIndex = 0;
56
57 Metronome metronome(Frequency::HertzDouble(5));
58 while (true)
59 {
60 // Check object positions.
61 {
62 const objpose::ObjectPoseSeq objectPoses =
63 srv_->objectPoseClient.fetchObjectPoses();
64
65 for (const objpose::ObjectPose& objectPose : objectPoses)
66 {
67 // check if objectPose is too old
68 if (objectPose.timestamp < tsStart)
69 {
70 continue;
71 }
72
73 ARMARX_INFO << deactivateSpam(10., objectPose.objectID.str())
74 << "Detected object " << objectPose.objectID.str();
75
76 const ObjectID objectPoseClassID = objectPose.objectID.getClassID();
77 if (requiredObjectClassIDs.erase(objectPoseClassID) > 0)
78 {
79 toAron(result.foundObjectInstanceIDs.emplace_back(), objectPose.objectID);
80 }
81 }
82
83 if (requiredObjectClassIDs.empty())
84 {
85 // All required object classes found - we are done here.
86 ARMARX_INFO << "All required objects found";
87 break;
88 }
89 }
90
91 // Control head.
92 const DateTime now = DateTime::Now();
93 if (now > nextTargetDueTime)
94 {
95 if (nextTargetIndex >= gazePositions.size())
96 {
97 // "Timeout" - viewed all positions.
98 break;
99 }
100
101 // Move to next target.
102 const Eigen::Vector3f& position = gazePositions.at(nextTargetIndex);
103
104 ARMARX_INFO << "Looking to position #" << (nextTargetIndex + 1) << "/"
105 << gazePositions.size() << " (" << position.transpose() << ")";
106
107 const gaze_targets::GazeTarget gazeTarget =
108 ScanLocation::MakeGazeTarget(position, in.parameters.scanLocation);
109
110 srv_->viewSelectionClient.commitGazeTarget(gazeTarget);
111 this->latestGazeTarget_ = gazeTarget;
112
113 ++nextTargetIndex;
114 nextTargetDueTime = now + duration;
115 }
116 metronome.waitForNextTick();
118 }
119
120 // Hotfix: Wait some time before proceeding to allow the localization to settle.
121 const Duration waitDuration = Duration::SecondsDouble(2.0);
122 {
123 const DateTime start = DateTime::Now();
124
125 Metronome metronome(Frequency::HertzDouble(5.0));
126 while ((DateTime::Now() - start) < waitDuration)
127 {
128 metronome.waitForNextTick();
130 }
131 }
132
134
135 result.allRequiredObjectClassesFound =
136 result.foundObjectInstanceIDs.size() == numberOfRequiredObjectClassIDs;
137
138 return MakeSucceededResult(result.toAron());
139 }
140
141 void
142 LookForObjects::onStopRequested()
143 {
144 if (latestGazeTarget_.has_value())
145 {
146 gaze_targets::GazeTarget deletedTarget = latestGazeTarget_.value();
147 deletedTarget.priority =
148 gaze_targets::TargetPriority(gaze_targets::AttentionType::TaskDriven, 0.0);
149 deletedTarget.keepInQueue = false;
150 deletedTarget.duration = Duration::Seconds(0);
151 deletedTarget.status = gaze_targets::TargetStatus::Aborted;
152 srv_->viewSelectionClient.commitGazeTarget(deletedTarget);
153 }
154 }
155
156 ::armarx::skills::SkillDescription
158 {
159 Params defaults;
160 toAron(defaults.requiredObjectClassIDs.emplace_back(),
161 armarx::ObjectID("Kitchen", "bio-milk"));
162
163 defaults.scanLocation = arondto::ScanLocationParams::FromAron(
164 ScanLocation::DefaultSkillDescription().rootProfileDefaults);
165
167 .skillId = skill_ids::LookForObjects,
168 .description = "",
169 .rootProfileDefaults = defaults.toAron(),
170 .timeout = armarx::Duration::Minutes(10),
171 .parametersType = Params::ToAronType(),
172 .resultType = Result::ToAronType(),
173 };
174 }
175
176} // namespace armarx::view_selection::skills
static DateTime Now()
Current time on the virtual clock.
Definition Clock.cpp:93
static DateTime Now()
Definition DateTime.cpp:51
static Duration Minutes(std::int64_t minutes)
Constructs a duration in minutes.
Definition Duration.cpp:96
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
static Duration SecondsDouble(double seconds)
Constructs a duration in seconds.
Definition Duration.cpp:78
static Frequency HertzDouble(double hertz)
Definition Frequency.cpp:30
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition Logging.cpp:99
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
Definition Skill.cpp:413
void throwIfSkillShouldTerminate(const std::string &abortedMessage="") const
Definition Skill.cpp:389
::armarx::skills::SimpleSpecializedSkill< Params > Base
static armarx::skills::SkillDescription DefaultSkillDescription()
static gaze_targets::GazeTarget MakeGazeTarget(const Eigen::Vector3f &position, const arondto::ScanLocationParams &parameters)
static std::vector< Eigen::Vector3f > MakeGazeTargetPositions(const arondto::ScanLocationParams &parameters)
static armarx::skills::SkillDescription DefaultSkillDescription()
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
armem::MemoryID ObjectID
Definition types.h:79
armarx::core::time::Duration Duration
void fromAron(const T &dto, T &bo)
std::vector< ObjectPose > ObjectPoseSeq
@ TaskDriven
Task-Driven attention has highest priority.
@ Aborted
target was out of reach and thus deactivated by controller
const armarx::skills::SkillID LookForObjects
Definition skill_ids.cpp:32
This file is part of ArmarX.
Definition constants.cpp:26
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
A result struct for skill initialization.
Definition Skill.h:50
A result struct for th main method of a skill.
Definition Skill.h:62