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>
27
29{
30
33 armarx::skills::SkillDescription skillDescription) :
34 Base(skillDescription), srv_(services), stateModFn(stateModFn)
35 {
36 }
37
38 void
40 {
41 srv_.emplace(srv);
42 }
43
45 LookDirection::init(const Base::SpecializedInitInput& in)
46 {
47 ARMARX_INFO << "LookDirection:init";
48 gazeTarget.emplace(); // populate optional
49 const std::string frame = in.parameters.frame;
50 const std::string agent = in.parameters.agent;
51
52 // Read current direction of this skill from memory
53 const auto previousTarget = srv_->viewSelectionClient.readGazeTarget("LookDirection");
54
56 if (previousTarget.has_value())
57 {
58 const Eigen::Vector3f directionRead = previousTarget->position.toEigen();
59 state = direction::state::from(directionRead);
60 }
61
62 // Update state
63 state = stateModFn(state);
64
65 const Eigen::Vector3f targetPosition = direction::state::generateTargetPosition(state);
66
67 const FramedPosition targetPos{targetPosition, frame, agent};
68 const gaze_targets::TargetPriority priority{gaze_targets::AttentionType::TaskDriven,
69 in.parameters.priority};
71 const gaze_targets::GazeTarget target{
72 "LookDirection", targetPos, priority, duration, false};
73 gazeTarget.emplace(target);
74
75 return ::armarx::skills::Skill::InitResult{
77 }
78
79 ::armarx::skills::Skill::MainResult
80 LookDirection::main(const Base::SpecializedMainInput& in)
81 {
82 ARMARX_CHECK(gazeTarget.has_value());
83
84 ARMARX_INFO << "Committing gaze target " << gazeTarget.value();
85 srv_->viewSelectionClient.commitGazeTarget(gazeTarget.value());
86
87 return ::armarx::skills::Skill::MainResult{
89 }
90
91 void
92 LookDirection::onStopRequested()
93 {
94 if (gazeTarget.has_value())
95 {
96 srv_->viewSelectionClient.commitGazeTarget(gazeTarget.value());
97 }
98 }
99
100 armarx::skills::SkillDescription
101 LookDirection::generateDefaultSkillDescription(std::string name, std::string directionText)
102 {
103 ParamType defaultParameters;
104 defaultParameters.frame = "root";
105 defaultParameters.agent = "Armar7";
106 defaultParameters.priority = 1;
108 .skillId = armarx::skills::SkillID{.skillName = std::move(name)},
109 .description = "Let the robot look " + directionText +
110 ". Multiple directions can be combined but stacking the same direction "
111 "does not move the head further.",
112 .rootProfileDefaults = defaultParameters.toAron(),
114 .parametersType = Params::ToAronType(),
115 };
116 }
117
118 LookUp::LookUp(const Context& services) :
119 /* very strange problem here: The SpecializedSkill base class is constructed before
120 * LookDirection. This is a problem, because usually the default constructor would be
121 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
122 */
123 armarx::skills::SimpleSpecializedSkill<
124 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
125 LookDirection(services, direction::state::up, DefaultSkillDescription())
126 {
127 }
128
134
135 LookDown::LookDown(const Context& services) :
136 /* very strange problem here: The SpecializedSkill base class is constructed before
137 * LookDirection. This is a problem, because usually the default constructor would be
138 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
139 */
140 armarx::skills::SimpleSpecializedSkill<
141 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
142 LookDirection(services, direction::state::down, DefaultSkillDescription())
143 {
144 }
145
147 /* very strange problem here: The SpecializedSkill base class is constructed before
148 * LookDirection. This is a problem, because usually the default constructor would be
149 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
150 */
151 armarx::skills::SimpleSpecializedSkill<
152 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
153 LookDirection(services, direction::state::downstraight, DefaultSkillDescription())
154 {
155 }
156
162
169
170 LookLeft::LookLeft(const Context& services) :
171 /* very strange problem here: The SpecializedSkill base class is constructed before
172 * LookDirection. This is a problem, because usually the default constructor would be
173 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
174 */
175 armarx::skills::SimpleSpecializedSkill<
176 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
177 LookDirection(services, direction::state::left, DefaultSkillDescription())
178 {
179 }
180
186
187 LookRight::LookRight(const Context& services) :
188 /* very strange problem here: The SpecializedSkill base class is constructed before
189 * LookDirection. This is a problem, because usually the default constructor would be
190 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
191 */
192 armarx::skills::SimpleSpecializedSkill<
193 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
194 LookDirection(services, direction::state::right, DefaultSkillDescription())
195 {
196 }
197
203
204 LookAhead::LookAhead(const Context& services) :
205 /* very strange problem here: The SpecializedSkill base class is constructed before
206 * LookDirection. This is a problem, because usually the default constructor would be
207 * called, which is deleted. Therefore, we need to call it twice with the correct parameter.
208 */
209 armarx::skills::SimpleSpecializedSkill<
210 arondto::LookDirectionParams>::SimpleSpecializedSkill(DefaultSkillDescription()),
211 LookDirection(services, direction::state::center, DefaultSkillDescription())
212 {
213 }
214
220
222 direction::state::from(Eigen::Vector3f targetPosition)
223 {
225
226 // Check for each direction separately and set State bit
227 float epsilon = 10.0;
228 if (std::abs(centerPosition.z() + DeltaUp.z() - targetPosition.z()) < epsilon)
229 state |= state::Up;
230 if (std::abs(centerPosition.z() + deltaDown.z() - targetPosition.z()) < epsilon)
232 if (std::abs(centerPosition.x() + deltaLeft.x() - targetPosition.x()) < epsilon)
234 if (std::abs(centerPosition.x() + deltaRight.x() - targetPosition.x()) < epsilon)
236
237 return state;
238 }
239
240 Eigen::Vector3f
242 {
243 Eigen::Vector3f targetPosition = centerPosition;
244
245 if ((state & state::Up) != 0)
246 targetPosition += DeltaUp;
247 if ((state & state::Down) != 0)
248 targetPosition += deltaDown;
249 if ((state & state::Left) != 0)
250 targetPosition += deltaLeft;
251 if ((state & state::Right) != 0)
252 targetPosition += deltaRight;
253 if ((state & state::Downstraight) != 0)
254 targetPosition = downstraightPosition;
255 // ARMARX_INFO << "setting position to " << targetPosition;
256 // ARMARX_INFO << "State is " << static_cast<std::uint64_t>(state);
257 return targetPosition;
258 }
259
262 {
263 (void)previous; // Unused.
264 return state::Center;
265 }
266
269 {
270 return (previous & ~state::Down) | state::Up;
271 }
272
275 {
276 return (previous & ~state::Up) | state::Down;
277 }
278
281 {
282 return (previous & ~state::Downstraight) | state::Downstraight;
283 }
284
287 {
288 return (previous & ~state::Right) | state::Left;
289 }
290
293 {
294 return (previous & ~state::Left) | state::Right;
295 }
296
297} // 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:181
@ 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.
Definition constants.cpp:26
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