MoveRelativePlanar.cpp
Go to the documentation of this file.
1 #include "MoveRelativePlanar.h"
2 
3 #include <optional>
4 #include <sstream>
5 #include <string>
6 
7 #include <SimoxUtility/math/convert/deg_to_rad.h>
8 #include <VirtualRobot/VirtualRobot.h>
9 
14 
16 #include <RobotAPI/libraries/aron/common/aron/framed.aron.generated.h>
19 
20 #include <armarx/navigation/skills/aron/MoveRelativePlanar.aron.generated.h>
22 
24 {
25 
28  {
29  ParamType defaultParams;
30  defaultParams.translationCartesian = std::nullopt;
31  defaultParams.translationPolar = std::nullopt;
32  defaultParams.rotationRadians = std::nullopt;
33  defaultParams.rotationDegrees = std::nullopt;
34 
35  static const std::string nl = "\n\n";
36 
37  std::stringstream desc;
38  desc << "**Move the robot relatively to its current pose in the horizontal plane.**";
39 
40  desc << nl
41  << "The parameters `translationCartesian` and `translationPolar` specify the "
42  "translation of the robot. (If both are given, `translationCartesian` takes "
43  "precedence over `translationPolar`.)";
44 
45  desc << nl
46  << "`translationCartesian` specifies the relative translation in Cartesian"
47  " coordinates (x, y). The corresponding coordinate system is specified by its "
48  "`frameID`. If `frameID` is not given, the robot's root coordinate system is used."
49  "To define a translation in the global frame, set `frame` to `Global` (and leave "
50  "`agent` empty).";
51 
52  desc << nl
53  << "`translationPolar` specifies the relative translation in polar coordinates"
54  " (direction, distance), where"
55  << nl << "- `direction = 0` moves the robot forward," << nl
56  << "- `direction > 0` moves the robot to the left," << nl
57  << "- `direction < 0` moves the robot to the right." << nl
58  << " (Generally, `forward` is defined as the +y axis of the robot root frame.)";
59 
60  desc << nl
61  << "`rotationRadians` and `rotationDegrees` specify the rotation around the +z axis"
62  " (up). "
63  << nl
64  << "(If both are given, `rotationRadians` takes precedence over `rotationDegrees`.)"
65  << nl << "- `directionDegrees = 0` does not turn the robot," << nl
66  << "- `directionDegrees > 0` turns the robot to the left," << nl
67  << "- `directionDegrees < 0` turns the robot to the right.";
68 
71  .description = desc.str(),
72  .rootProfileDefaults = defaultParams.toAron(),
74  .parametersType = ParamType::ToAronType(),
75  };
76  }
77 
79  Base(
80  Base::Services{
82  .memoryNameSystem = srv.memoryNameSystem,
83  },
84  DefaultSkillDescription()),
85  properties{properties},
86  services{.robotReader = srv.robotReader}
87  {
88  }
89 
90  Eigen::Vector3f
91  makeTranslation(const arondto::FramedCartesian2D& cartesian,
93  const std::string& defaultRobotName)
94  {
95  Eigen::Vector3f translation;
96  translation << cartesian.translationCartesian.cast<float>(), 0;
97 
98  // Target translation must be given in root frame of robot.
99  if (cartesian.frameID.has_value())
100  {
101  armarx::arondto::FrameID frameID = cartesian.frameID.value();
102 
103  if (frameID.agent.empty())
104  {
105  // If frame == armarx::GlobalFrame, the agent is ignored, so we can set it anyway.
106  frameID.agent = defaultRobotName;
107  }
108 
109  VirtualRobot::RobotPtr robot = robotReader.getRobot(frameID.agent);
110  ARMARX_CHECK_NOT_NULL(robot)
111  << "Robot " << QUOTED(frameID.agent) << " not found in robot reader";
112 
113  ARMARX_CHECK(robotReader.synchronizeRobot(*robot, armarx::Clock::Now()))
114  << "Failed to synchronize robot " << QUOTED(frameID.agent);
115 
116  if (frameID.frame.empty())
117  {
118  frameID.frame = robot->getRootNode()->getName();
119  }
120 
121  armarx::FramedDirection dir(translation, frameID.frame, frameID.agent);
122 
123  return dir.toRootFrame(robot)->toEigen();
124  }
125  else
126  {
127  // If not given, we fall back to the robot root frame => no transformation necessary.
128  return translation;
129  }
130  }
131 
132  Eigen::Vector3f
133  makeTranslation(const arondto::Polar& polar,
134  const Eigen::Vector3f forward,
135  const Eigen::Vector3f up)
136  {
137  const Eigen::AngleAxisf rotation(simox::math::deg_to_rad(polar.directionDegrees), up);
138  const Eigen::Vector3f translation = polar.distanceMillimeters * (rotation * forward);
139 
140  return translation;
141  }
142 
143  Eigen::AngleAxisf
144  makeRotation(float rotationRadians, const Eigen::Vector3f up)
145  {
146  return Eigen::AngleAxisf(rotationRadians, up);
147  }
148 
149  Eigen::Isometry3f
150  MoveRelativePlanar::relativeTarget(const Base::SpecializedMainInput& in)
151  {
152  // Note: In theory, it depends on the robot.
153  static const Eigen::Vector3f forward = Eigen::Vector3f::UnitY();
154  static const Eigen::Vector3f up = Eigen::Vector3f::UnitZ();
155 
156  const ParamType& parameters = in.parameters;
157 
158  Eigen::Isometry3f relativeTarget = Eigen::Isometry3f::Identity();
159 
160  // Translation.
161  if (parameters.translationCartesian.has_value())
162  {
163  relativeTarget.translation() = makeTranslation(parameters.translationCartesian.value(),
164  services.robotReader,
165  properties.robotName);
166  }
167  else if (parameters.translationPolar.has_value())
168  {
169  relativeTarget.translation() =
170  makeTranslation(parameters.translationPolar.value(), forward, up);
171  }
172 
173  // Rotation.
174  std::optional<float> rotationRadians = parameters.rotationRadians;
175  if (not rotationRadians.has_value() and parameters.rotationDegrees.has_value())
176  {
177  rotationRadians = simox::math::deg_to_rad(parameters.rotationDegrees.value());
178  }
179  if (rotationRadians.has_value())
180  {
181  relativeTarget.linear() =
182  relativeTarget.linear() * makeRotation(rotationRadians.value(), up);
183  }
184 
185  return relativeTarget;
186  }
187 
188 } // namespace armarx::navigation::skills
armarx::view_selection::skills::direction::state::up
state::Type up(state::Type previous)
Definition: LookDirection.cpp:240
constants.h
armarx::armem::robot_state::VirtualRobotReader
The VirtualRobotReader class.
Definition: VirtualRobotReader.h:43
armarx::navigation::skills::MoveRelativePlanar::DefaultSkillDescription
static armarx::skills::SkillDescription DefaultSkillDescription()
Definition: MoveRelativePlanar.cpp:27
armarx::armem::robot_state::VirtualRobotReader::getRobot
VirtualRobot::RobotPtr getRobot(const std::string &name, const armem::Time &timestamp=armem::Time::Invalid(), const VirtualRobot::RobotIO::RobotDescription &loadMode=VirtualRobot::RobotIO::RobotDescription::eStructure)
Definition: VirtualRobotReader.cpp:79
armarx::navigation::skills::MoveRelativePlanar::Properties::robotName
std::string robotName
Definition: MoveRelativePlanar.h:31
QUOTED
#define QUOTED(x)
Definition: StringHelpers.h:199
armarx::navigation::skills::constants::skill_names::MoveRelativePlanar
const std::string MoveRelativePlanar
Definition: constants.h:36
armarx::navigation::skills::MoveRelativePlanar::Services::iceNavigator
client::IceNavigator & iceNavigator
Definition: MoveRelativePlanar.h:24
armarx::skills::SkillID::skillName
std::string skillName
Definition: SkillID.h:41
armarx::skills::SkillDescription
Definition: SkillDescription.h:17
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::navigation::skills::NavigateRelativeSkill< arondto::MoveRelativePlanarParams >
Duration.h
armarx::armem::robot_state::VirtualRobotReader::synchronizeRobot
bool synchronizeRobot(VirtualRobot::Robot &robot, const armem::Time &timestamp) const
Synchronize both the platform pose and the joint values of a virtual robot, according to the robot st...
Definition: VirtualRobotReader.cpp:26
StringHelpers.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
armarx::FramedDirection::toRootFrame
FramedDirectionPtr toRootFrame(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:262
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::navigation::skills::MoveRelativePlanar::Properties
Definition: MoveRelativePlanar.h:29
armarx::navigation::skills::makeRotation
Eigen::AngleAxisf makeRotation(float rotationRadians, const Eigen::Vector3f up)
Definition: MoveRelativePlanar.cpp:144
FramedPose.h
armarx::skills::SkillDescription::skillId
SkillID skillId
Definition: SkillDescription.h:19
VirtualRobotReader.h
armarx::core::time::Duration::Minutes
static Duration Minutes(std::int64_t minutes)
Constructs a duration in minutes.
Definition: Duration.cpp:96
ExpressionException.h
armarx::FramedDirection
FramedDirection is a 3 dimensional direction vector with a reference frame. The reference frame can b...
Definition: FramedPose.h:86
MoveRelativePlanar.h
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:93
armarx::navigation::skills::MoveRelativePlanar::Services
Definition: MoveRelativePlanar.h:22
armarx::navigation::skills::MoveRelativePlanar::Services::memoryNameSystem
armem::client::MemoryNameSystem & memoryNameSystem
Definition: MoveRelativePlanar.h:25
armarx::navigation::skills::makeTranslation
Eigen::Vector3f makeTranslation(const arondto::FramedCartesian2D &cartesian, armem::robot_state::VirtualRobotReader &robotReader, const std::string &defaultRobotName)
Definition: MoveRelativePlanar.cpp:91
armarx::navigation::skills
Definition: constants.cpp:25
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
SkillDescription.h
armarx::navigation::skills::MoveRelativePlanar::MoveRelativePlanar
MoveRelativePlanar(const Properties &properties, const Services &srv)
Definition: MoveRelativePlanar.cpp:78