TrajectoryFollowingController.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 
5 #include <Eigen/Geometry>
6 
7 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
8 #include <SimoxUtility/math/convert/mat4f_to_rpy.h>
9 #include <SimoxUtility/math/convert/mat4f_to_xyyaw.h>
10 
15 #include <ArmarXCore/interface/serialization/Eigen/Eigen_fdi.h>
16 
18 
23 #include <armarx/navigation/trajectory_control/local/aron/TrajectoryFollowingControllerParams.aron.generated.h>
26 
28 {
29  // TrajectoryFollowingControllerParams
30 
33  {
35  }
36 
39  {
40  arondto::TrajectoryFollowingControllerParams dto;
41 
44 
45  return dto.toAron();
46  }
47 
50  {
51  arondto::TrajectoryFollowingControllerParams dto;
52  dto.fromAron(dict);
53 
56 
57  return bo;
58  }
59 
60  // TrajectoryFollowingController
61 
63  params(params),
64  pidPos(params.pidPos.Kp,
65  params.pidPos.Ki,
66  params.pidPos.Kd,
67  std::numeric_limits<double>::max(),
68  std::numeric_limits<double>::max(),
69  false,
70  std::vector<bool>{false, false, false}),
71  pidPosTarget(params.pidPos.Kp / 2,
72  params.pidPos.Ki,
73  params.pidPos.Kd,
76  false,
77  std::vector<bool>{false, false, false}),
78  pidOri(params.pidOri.Kp,
79  params.pidOri.Ki,
80  params.pidOri.Kd,
83  false,
84  std::vector<bool>{true, true, true}),
85  pidOriTarget(params.pidOri.Kp,
86  params.pidOri.Ki,
87  params.pidOri.Kd,
90  false,
91  std::vector<bool>{true, true, true})
92  {
93  ARMARX_IMPORTANT << "Trajectory following controller params: "
94  << VAROUT(params.limits.linear) << ", " << VAROUT(params.limits.angular);
95  }
96 
97  core::Twist
99  {
100  const core::Twist limits{.linear = Eigen::Vector3f::Ones() * params.limits.linear,
101  .angular = Eigen::Vector3f::Ones() * params.limits.angular};
102 
103  ARMARX_CHECK((limits.linear.array() > 0).all());
104  ARMARX_CHECK((limits.angular.array() > 0).all());
105  ARMARX_CHECK(limits.linear.allFinite());
106  ARMARX_CHECK(limits.angular.allFinite());
107 
108  // for all entries, scale should be less than 1
109  const auto scalePos = twist.linear.cwiseAbs().cwiseQuotient(limits.linear.cwiseAbs());
110  const auto scaleOri = twist.angular.cwiseAbs().cwiseQuotient(limits.angular.cwiseAbs());
111 
112  ARMARX_CHECK(scalePos.allFinite());
113  ARMARX_CHECK(scaleOri.allFinite());
114 
115  const float scaleMax = std::max(scalePos.maxCoeff(), scaleOri.maxCoeff());
116  if (scaleMax < 1.0F) // both linear and angular velocity in bounds?
117  {
118  return twist;
119  }
120 
121  // scale such that no limit is violated
122  twist.linear /= scaleMax;
123  twist.angular /= scaleMax;
124 
125  constexpr float eps = 0.001;
126 
127  // pedantic checks
128  ARMARX_CHECK_LESS_EQUAL(std::abs(twist.linear.x()), params.limits.linear + eps);
129  ARMARX_CHECK_LESS_EQUAL(std::abs(twist.linear.y()), params.limits.linear + eps);
130  ARMARX_CHECK_LESS_EQUAL(std::abs(twist.linear.z()), params.limits.linear + eps);
131  ARMARX_CHECK_LESS_EQUAL(std::abs(twist.angular.x()), params.limits.angular + eps);
132  ARMARX_CHECK_LESS_EQUAL(std::abs(twist.angular.y()), params.limits.angular + eps);
133  ARMARX_CHECK_LESS_EQUAL(std::abs(twist.angular.z()), params.limits.angular + eps);
134 
135  return twist;
136  }
137 
140  const Eigen::Isometry3f& global_T_robot)
141  {
142  if (trajectory.points().empty())
143  {
144  ARMARX_INFO << "Trajectory is empty.";
146  }
147 
148  // TOOD maybe set via arg?
149  const DateTime timestamp = Clock::Now();
150 
151  const core::Evaluation target = trajectory.evaluate(timestamp);
152 
153  using simox::math::mat4f_to_pos;
154  using simox::math::mat4f_to_rpy;
155 
156  pidPos.update(mat4f_to_pos(global_T_robot.matrix()), mat4f_to_pos(target.pose.matrix()));
157  pidOri.update(mat4f_to_rpy(global_T_robot.matrix()), mat4f_to_rpy(target.pose.matrix()));
158 
159 
160  const core::Twist& twistFeedForward = target.feedforwardTwist;
161 
162  const core::Twist twistError{.linear = pidPos.getControlValue(),
163  .angular = pidOri.getControlValue()};
164 
165  const core::Twist twist{.linear = twistFeedForward.linear + twistError.linear,
166  .angular = twistFeedForward.angular + twistError.angular};
167 
168  const auto twistLimited = applyTwistLimits(twist);
169  ARMARX_VERBOSE << deactivateSpam(1) << "Twist limited " << twistLimited.linear.transpose();
170  ARMARX_VERBOSE << deactivateSpam(1) << "Twist angular " << twistLimited.angular.transpose();
171 
172  // convert to the robot's base frame
173  const auto& twistGlobal = twistLimited;
174 
175  const core::Twist twistLocal
176  {
177  .linear = global_T_robot.linear().inverse() * twistGlobal.linear,
178  .angular = twistGlobal.angular
179  };
180 
181  return TrajectoryControllerResult{.twist = twistLocal};
182  }
183 
184 } // namespace armarx::navigation::traj_ctrl::local
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams::algorithm
Algorithms algorithm() const override
Definition: TrajectoryFollowingController.cpp:32
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams
Definition: TrajectoryFollowingController.h:35
armarx::navigation::traj_ctrl::local::TrajectoryFollowingController::TrajectoryFollowingController
TrajectoryFollowingController(const Params &params)
Definition: TrajectoryFollowingController.cpp:62
TrajectoryController.h
basic_types.h
armarx::navigation::core::Twist::Zero
static Twist Zero()
Definition: basic_types.cpp:13
DateTime.h
armarx::MultiDimPIDControllerTemplate::update
void update(const double deltaSec, const PIDVectorX &measuredValue, const PIDVectorX &targetValue)
Definition: MultiDimPIDController.h:75
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::navigation::traj_ctrl::local::TrajectoryControllerResult::twist
core::Twist twist
Definition: TrajectoryController.h:45
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams::FromAron
static TrajectoryFollowingControllerParams FromAron(const aron::data::DictPtr &dict)
Definition: TrajectoryFollowingController.cpp:49
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::navigation::core::TwistLimits::linear
float linear
Definition: types.h:93
armarx::navigation::traj_ctrl::local::fromAron
void fromAron(const arondto::TrajectoryFollowingControllerParams &dto, TrajectoryFollowingControllerParams &bo)
Definition: aron_conversions.cpp:27
core.h
armarx::navigation::core::LocalTrajectory::points
const std::vector< LocalTrajectoryPoint > & points() const
Definition: Trajectory.cpp:837
armarx::navigation::core::TwistLimits::angular
float angular
Definition: types.h:94
armarx::navigation::traj_ctrl::local::TrajectoryControllerParams::limits
core::TwistLimits limits
Definition: TrajectoryController.h:50
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
Clock.h
armarx::navigation::core::Twist
Definition: basic_types.h:53
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
MultiDimPIDController.h
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::navigation::traj_ctrl::local::toAron
void toAron(arondto::TrajectoryFollowingControllerParams &dto, const TrajectoryFollowingControllerParams &bo)
Definition: aron_conversions.cpp:17
TrajectoryFollowingController.h
armarx::navigation::core::LocalTrajectory
Definition: Trajectory.h:167
max
T max(T t1, T t2)
Definition: gdiam.h:48
ARMARX_CHECK_LESS_EQUAL
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
Definition: ExpressionException.h:109
ExpressionException.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::navigation::core::Twist::linear
LinearVelocity linear
Definition: basic_types.h:55
armarx::navigation::traj_ctrl::local::Algorithms
Algorithms
Definition: core.h:29
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::navigation::traj_ctrl::local::TrajectoryFollowingController::applyTwistLimits
core::Twist applyTwistLimits(core::Twist twist)
Definition: TrajectoryFollowingController.cpp:98
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::navigation::traj_ctrl::local::TrajectoryFollowingController::control
TrajectoryControllerResult control(const core::LocalTrajectory &trajectory, const Eigen::Isometry3f &global_T_robot) override
Definition: TrajectoryFollowingController.cpp:139
std
Definition: Application.h:66
armarx::MultiDimPIDControllerTemplate::getControlValue
const PIDVectorX & getControlValue() const
Definition: MultiDimPIDController.h:186
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::navigation::core::LocalTrajectory::evaluate
Evaluation evaluate(const DateTime &timestamp) const
Definition: Trajectory.cpp:849
armarx::navigation::traj_ctrl::local::TrajectoryFollowingControllerParams::toAron
aron::data::DictPtr toAron() const override
Definition: TrajectoryFollowingController.cpp:38
F
Definition: ExportDialogControllerTest.cpp:16
armarx::navigation::core::Twist::angular
AngularVelocity angular
Definition: basic_types.h:56
aron_conversions.h
armarx::navigation::traj_ctrl::local::TrajectoryControllerResult
Definition: TrajectoryController.h:43
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
Logging.h
Trajectory.h
armarx::navigation::core::Evaluation
Definition: Trajectory.h:160
armarx::navigation::traj_ctrl::local::Algorithms::TrajectoryFollowingController
@ TrajectoryFollowingController
types.h
armarx::navigation::traj_ctrl::local
This file is part of ArmarX.
Definition: aron_conversions.cpp:12
armarx::aron::bo
const std::optional< BoT > & bo
Definition: aron_conversions.h:168