forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trajectory_follower.cc
65 lines (55 loc) · 2.38 KB
/
trajectory_follower.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "drake/automotive/trajectory_follower.h"
#include "drake/common/default_scalars.h"
namespace drake {
namespace automotive {
using multibody::SpatialVelocity;
using systems::rendering::FrameVelocity;
using systems::rendering::PoseVector;
template <typename T>
TrajectoryFollower<T>::TrajectoryFollower(const Trajectory& trajectory,
double sampling_time_sec)
: systems::LeafSystem<T>(
systems::SystemTypeTag<automotive::TrajectoryFollower>{}),
trajectory_(trajectory) {
this->DeclarePeriodicUnrestrictedUpdate(sampling_time_sec, 0.);
this->DeclareVectorOutputPort(&TrajectoryFollower::CalcStateOutput);
this->DeclareVectorOutputPort(&TrajectoryFollower::CalcPoseOutput);
this->DeclareVectorOutputPort(&TrajectoryFollower::CalcVelocityOutput);
}
template <typename T>
void TrajectoryFollower<T>::CalcStateOutput(
const systems::Context<T>& context,
SimpleCarState<T>* output_vector) const {
const PoseVelocity values = GetValues(context);
output_vector->set_x(T{values.pose3().x()});
output_vector->set_y(T{values.pose3().y()});
output_vector->set_heading(T{values.pose3().z()});
output_vector->set_velocity(T{values.speed()});
}
template <typename T>
void TrajectoryFollower<T>::CalcPoseOutput(const systems::Context<T>& context,
PoseVector<T>* pose) const {
const PoseVelocity values = GetValues(context);
pose->set_translation(Eigen::Translation<T, 3>{values.translation()});
const Eigen::Quaternion<double>& q =
math::RotationMatrix<double>(values.rotation()).ToQuaternion();
pose->set_rotation(Eigen::Quaternion<T>{q});
}
template <typename T>
void TrajectoryFollower<T>::CalcVelocityOutput(
const systems::Context<T>& context,
FrameVelocity<T>* velocity) const {
const PoseVelocity values = GetValues(context);
const Eigen::Vector3d& v = values.velocity().translational();
const Eigen::Vector3d& w = values.velocity().rotational();
velocity->set_velocity(SpatialVelocity<T>{Vector3<T>{w}, Vector3<T>{v}});
}
template <typename T>
PoseVelocity TrajectoryFollower<T>::GetValues(
const systems::Context<T>& context) const {
return trajectory_.value(ExtractDoubleOrThrow(context.get_time()));
}
} // namespace automotive
} // namespace drake
DRAKE_DEFINE_CLASS_TEMPLATE_INSTANTIATIONS_ON_DEFAULT_SCALARS(
class ::drake::automotive::TrajectoryFollower)