ScanLocationsForObject.cpp
Go to the documentation of this file.
2
3#include <chrono>
4
6
10
11#include <armarx/view_selection/skills/aron/LookAtArticulatedObjectFrame.aron.generated.h>
14
16{
17
18 ::armarx::skills::SkillDescription
20 {
21 ParamType defaults;
22
23 defaults.objectToSearch.dataset = "Kitchen-interior";
24 defaults.objectToSearch.className = "fridge";
25 defaults.objectToSearch.instanceName = "0";
26
27 defaults.locationsToScan = {"FridgeDoorShelf_node"};
28
29 defaults.durationPerLocationSeconds = 10;
30
31 defaults.objectToSearchFor.dataset = "Kitchen-interior";
32 defaults.objectToSearchFor.className = "bio-milk";
33 defaults.objectToSearchFor.instanceName = "0";
34
35 defaults.matchInstanceName = false;
36
37 return ::armarx::skills::SkillDescription{
39 .description = "Scan locations of an articulated object looking for another object.",
40 .rootProfileDefaults = defaults.toAron(),
41 .timeout = ::armarx::Duration::Minutes(10),
42 .parametersType = ParamType::ToAronType(),
43 };
44 }
45
48 .providerId =
49 ::armarx::skills::ProviderID{
50 .providerName =
51 armarx::view_selection::skills::constants::ViewSelectionSkillProviderName},
52 .skillName =
54
55 }
56 {
57 }
58
60 const Properties& properties) :
61 ::armarx::skills::SimpleSpecializedSkill<ParamType>(GetSkillDescription()),
62 services(services),
63 properties(properties)
64 {
65 }
66
68 ScanLocationsForObject::main(const SpecializedMainInput& in)
69 {
70 armarx::DateTime skillStartTime = armarx::DateTime::Now();
71
72 armarx::ObjectID objectToSearchFor;
73 armarx::fromAron(in.parameters.objectToSearchFor, objectToSearchFor);
74
75 ARMARX_INFO << "Scanning " << in.parameters.objectToSearch.dataset << "/"
76 << in.parameters.objectToSearch.className << "/"
77 << in.parameters.objectToSearch.instanceName << ". Looking for "
78 << objectToSearchFor;
79 for (const auto& location : in.parameters.locationsToScan)
80 {
82 ARMARX_VERBOSE << "Facing " << location;
83
84 using Parameters =
85 armarx::view_selection::skills::arondto::LookAtArticulatedObjectFrameParams;
86
87 // call subskill by id and provide a parameter-update lambda
88 const auto update =
90 [&](Parameters& params)
91 {
92 params.object = in.parameters.objectToSearch;
93 params.node = location; // node to face for searching
94 params.objectPoseProvider = std::nullopt; // "any"
95 params.durationSeconds =
96 in.parameters.durationPerLocationSeconds;
97 params.priority = 100;
98 });
99
101 {
102 ARMARX_WARNING << "View selection failed";
103 return MakeFailedResult();
104 }
105
106 armarx::DateTime timeout =
108 armarx::Duration::Seconds(in.parameters.durationPerLocationSeconds);
109
110 // wait until the timeout has expired and periodically check whether the object has been found already.
111 Metronome metronome(Frequency::Hertz(5));
112 while (armarx::DateTime::Now() < timeout)
113 {
115
116
117 const auto object = [&]() -> std::optional<objpose::ObjectPose>
118 {
119 if (in.parameters.matchInstanceName)
120 {
121 // match object including instance name
122 return services.objectReader.queryLatestObjectInstance(objectToSearchFor);
123 }
124
125 // get all objects of the desired class
126 for (const auto& [_, obj] :
127 services.objectReader.queryLatestObjectInstances(objectToSearchFor))
128 {
129 // we count an object as found, if it was seen after the skill was started
130 if (obj.timestamp > skillStartTime)
131 {
132 return obj;
133 }
134 }
135 return std::nullopt;
136 }();
137
138 if (object.has_value() && object->timestamp > skillStartTime)
139 {
140 // object has been found
141 ARMARX_INFO << "Object " << object->objectID << " found at "
142 << object->timestamp;
143 return MakeSucceededResult();
144 }
145 metronome.waitForNextTick();
146 }
147 }
148
149 ARMARX_WARNING << "Object not found in any location";
150 return MakeFailedResult();
151 }
152
153
154} // namespace armarx::view_selection::skills
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 Frequency Hertz(std::int64_t hertz)
Definition Frequency.cpp:20
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
std::optional< objpose::ObjectPose > queryLatestObjectInstance(const ObjectID &instanceId)
std::map< std::string, objpose::ObjectPose > queryLatestObjectInstances(const ObjectID &classId)
Represents a point in time.
Definition DateTime.h:25
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition Metronome.h:57
std::optional< TerminatedSkillStatusUpdate > callSubskill(const SkillID &skillId)
Call a subskill with the given ID and its default parameters.
Definition Skill.cpp:119
static MainResult MakeSucceededResult(aron::data::DictPtr data=nullptr)
Definition Skill.cpp:413
virtual MainResult main()
Override this method with the actual implementation.
Definition Skill.cpp:542
void throwIfSkillShouldTerminate(const std::string &abortedMessage="") const
Definition Skill.cpp:389
static MainResult MakeFailedResult(aron::data::DictPtr data=nullptr)
Definition Skill.cpp:422
ScanLocationsForObject(const Services &services, const Properties &properties)
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
bool skillExecutionFailed(const std::optional< armarx::skills::TerminatedSkillStatusUpdate > &update)
This file is part of ArmarX.
Definition constants.cpp:31
const armarx::skills::SkillID ScanLocationsForObject
Definition skill_ids.cpp:60
This file is part of ArmarX.
Definition constants.cpp:26
This file offers overloads of toIce() and fromIce() functions for STL container types.
void fromAron(const arondto::PackagePath &dto, PackageFileLocation &bo)
This file is part of ArmarX.
This file is part of ArmarX.
A result struct for th main method of a skill.
Definition Skill.h:62