LookDirection.cpp
Go to the documentation of this file.
1#include "LookDirection.h"
2
3#include <cstdlib>
4#include <string>
5#include <utility>
6
7#include <Eigen/Core>
8#include <Eigen/Geometry>
9
13
20
24#include <armarx/view_selection/skills/aron/LookDirection.aron.generated.h>
28
30{
31
34 armarx::skills::SkillDescription skillDescription) :
35 Base(skillDescription), srv_(services), stateModFn(stateModFn)
36 {
37 }
38
39 void
41 {
42 srv_.emplace(srv);
43 }
44
46 LookDirection::init(const Base::SpecializedInitInput& in)
47 {
48 ARMARX_INFO << "LookDirection:init";
49 gazeTarget.emplace(); // populate optional
50 const std::string frame = in.parameters.frame;
51 const std::string agent = in.parameters.agent;
52
53 // Read current direction of this skill from memory
54 const auto previousTarget = srv_->viewSelectionClient.readGazeTarget("LookDirection");
55
57 if (previousTarget.has_value())
58 {
59 const Eigen::Vector3f directionRead = previousTarget->position.toEigen();
60 state = direction::state::from(directionRead);
61 }
62
63 // Update state
64 state = stateModFn(state);
65
66 const Eigen::Vector3f targetPosition = direction::state::generateTargetPosition(state);
67
68 const FramedPosition targetPos{targetPosition, frame, agent};
69 const gaze_targets::TargetPriority priority{gaze_targets::AttentionType::TaskDriven,
70 in.parameters.priority};
72 const gaze_targets::GazeTarget target{
73 "LookDirection", targetPos, priority, duration, false};
74 gazeTarget.emplace(target);
75
76 return ::armarx::skills::Skill::InitResult{
78 }
79
80 ::armarx::skills::Skill::MainResult
81 LookDirection::main(const Base::SpecializedMainInput& in)
82 {
83 ARMARX_CHECK(gazeTarget.has_value());
84
85 return commitAndMaybeWait(srv_->viewSelectionClient,
86 gazeTarget.value(),
87 in.parameters.waitUntilReached,
88 in.parameters.waitTimeout_ms,
89 [this]() { return shouldSkillTerminate(); });
90 }
91
92 void
93 LookDirection::onStopRequested()
94 {
95 if (gazeTarget.has_value())
96 {
97 srv_->viewSelectionClient.commitGazeTarget(gazeTarget.value());
98 }
99 }
100
101 armarx::skills::SkillDescription
102 LookDirection::generateDefaultSkillDescription(std::string name, std::string directionText)
103 {
104 ParamType defaultParameters;
105 defaultParameters.frame = "root";
106 defaultParameters.agent = "Armar7";
107 defaultParameters.priority = 1;
109 .skillId = armarx::skills::SkillID{.skillName = std::move(name)},
110 .description = "Let the robot look " + directionText +
111 ". Multiple directions can be combined but stacking the same direction "
112 "does not move the head further.",
113 .rootProfileDefaults = defaultParameters.toAron(),
115 .parametersType = Params::ToAronType(),
116 };
117 }
118
119 LookUp::LookUp(const Context& services) :
120 /* very strange problem here: The SpecializedSkill base class is constructed before
121 * LookDirection. This is a problem, because usually the default constructor would be
122 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
123 */
124 armarx::skills::SimpleSpecializedSkill<
125 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
126 LookDirection(services, direction::state::up, DefaultSkillDescription())
127 {
128 }
129
135
136 LookDown::LookDown(const Context& services) :
137 /* very strange problem here: The SpecializedSkill base class is constructed before
138 * LookDirection. This is a problem, because usually the default constructor would be
139 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
140 */
141 armarx::skills::SimpleSpecializedSkill<
142 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
143 LookDirection(services, direction::state::down, DefaultSkillDescription())
144 {
145 }
146
148 /* very strange problem here: The SpecializedSkill base class is constructed before
149 * LookDirection. This is a problem, because usually the default constructor would be
150 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
151 */
152 armarx::skills::SimpleSpecializedSkill<
153 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
154 LookDirection(services, direction::state::downstraight, DefaultSkillDescription())
155 {
156 }
157
163
170
171 LookLeft::LookLeft(const Context& services) :
172 /* very strange problem here: The SpecializedSkill base class is constructed before
173 * LookDirection. This is a problem, because usually the default constructor would be
174 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
175 */
176 armarx::skills::SimpleSpecializedSkill<
177 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
178 LookDirection(services, direction::state::left, DefaultSkillDescription())
179 {
180 }
181
187
188 LookRight::LookRight(const Context& services) :
189 /* very strange problem here: The SpecializedSkill base class is constructed before
190 * LookDirection. This is a problem, because usually the default constructor would be
191 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
192 */
193 armarx::skills::SimpleSpecializedSkill<
194 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
195 LookDirection(services, direction::state::right, DefaultSkillDescription())
196 {
197 }
198
204
205 LookAhead::LookAhead(const Context& services) :
206 /* very strange problem here: The SpecializedSkill base class is constructed before
207 * LookDirection. This is a problem, because usually the default constructor would be
208 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
209 */
210 armarx::skills::SimpleSpecializedSkill<
211 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
212 LookDirection(services, direction::state::center, DefaultSkillDescription())
213 {
214 }
215
221
223 direction::state::from(Eigen::Vector3f targetPosition)
224 {
226
227 // Check for each direction separately and set State bit
228 float epsilon = 10.0;
229 if (std::abs(centerPosition.z() + DeltaUp.z() - targetPosition.z()) < epsilon)
230 state |= state::Up;
231 if (std::abs(centerPosition.z() + deltaDown.z() - targetPosition.z()) < epsilon)
233 if (std::abs(centerPosition.x() + deltaLeft.x() - targetPosition.x()) < epsilon)
235 if (std::abs(centerPosition.x() + deltaRight.x() - targetPosition.x()) < epsilon)
237
238 return state;
239 }
240
241 Eigen::Vector3f
243 {
244 Eigen::Vector3f targetPosition = centerPosition;
245
246 if ((state & state::Up) != 0)
247 targetPosition += DeltaUp;
248 if ((state & state::Down) != 0)
249 targetPosition += deltaDown;
250 if ((state & state::Left) != 0)
251 targetPosition += deltaLeft;
252 if ((state & state::Right) != 0)
253 targetPosition += deltaRight;
254 if ((state & state::Downstraight) != 0)
255 targetPosition = downstraightPosition;
256 // ARMARX_INFO << "setting position to " << targetPosition;
257 // ARMARX_INFO << "State is " << static_cast<std::uint64_t>(state);
258 return targetPosition;
259 }
260
263 {
264 (void)previous; // Unused.
265 return state::Center;
266 }
267
270 {
271 return (previous & ~state::Down) | state::Up;
272 }
273
276 {
277 return (previous & ~state::Up) | state::Down;
278 }
279
282 {
283 return (previous & ~state::Downstraight) | state::Downstraight;
284 }
285
288 {
289 return (previous & ~state::Right) | state::Left;
290 }
291
294 {
295 return (previous & ~state::Left) | state::Right;
296 }
297
298} // namespace armarx::view_selection::skills
The FramedPosition class.
Definition FramedPose.h:158
Represents a duration.
Definition Duration.h:17
static Duration Seconds(std::int64_t seconds)
Constructs a duration in seconds.
Definition Duration.cpp:72
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
static armarx::skills::SkillDescription DefaultSkillDescription()
::armarx::skills::SimpleSpecializedSkill< Params > Base
static armarx::skills::SkillDescription generateDefaultSkillDescription(std::string name, std::string directionText)
LookDirection(const Context &services, direction::state::StateModFn *stateModFn, armarx::skills::SkillDescription skillDescription)
static armarx::skills::SkillDescription DefaultSkillDescription()
static armarx::skills::SkillDescription DefaultSkillDescription()
static armarx::skills::SkillDescription DefaultSkillDescription()
static armarx::skills::SkillDescription DefaultSkillDescription()
static armarx::skills::SkillDescription DefaultSkillDescription()
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:179
@ TaskDriven
Task-Driven attention has highest priority.
The namespace state contains the states of allowed directions.
state::Type downstraight(state::Type previous)
Eigen::Vector3f generateTargetPosition(state::Type state)
state::Type from(Eigen::Vector3f targetPosition)
This file is part of ArmarX.
::armarx::skills::Skill::MainResult commitAndMaybeWait(client::ViewSelection &viewSelection, const gaze_targets::GazeTarget &target, const bool waitUntilReached, const std::int64_t waitTimeout_ms, const std::function< bool()> &shouldAbort)
Commit a gaze target, optionally blocking until the robot's gaze is fixed on it.
This file offers overloads of toIce() and fromIce() functions for STL container types.
This file is part of ArmarX.
A result struct for skill initialization.
Definition Skill.h:50