MCPcopy Create free account
hub / github.com/RoboJackets/software-training / PIDController

Class PIDController

controllers/src/pid_controller.cpp:35–230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33{
34
35class PIDController : public nav2_core::Controller
36{
37public:
38 void configure(
39 const rclcpp_lifecycle::LifecycleNode::WeakPtr & node,
40 std::string name,
41 std::shared_ptr<tf2_ros::Buffer> tf_buffer,
42 std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap) override
43 {
44 node_ = node;
45
46 auto node_shared = node_.lock();
47 if (!node_shared) {
48 throw std::runtime_error{"Could not acquire node."};
49 }
50
51 traj_viz_pub_ = node_shared->create_publisher<nav_msgs::msg::Path>(
52 "~/tracking_traj",
53 rclcpp::SystemDefaultsQoS());
54
55 // BEGIN STUDENT CODE
56 // END STUDENT CODE
57 }
58
59 Eigen::Vector3d interpolateState(const rclcpp::Time time)
60 {
61 if (time.seconds() > path_start_time_.seconds() + (time_between_states_ * trajectory_.size())) {
62 return trajectory_.back();
63 }
64 if (time < path_start_time_) {
65 return trajectory_.front();
66 }
67
68 double rel_time = (time - path_start_time_).seconds();
69 int lower_idx = std::floor(rel_time / time_between_states_);
70 int upper_idx = lower_idx + 1;
71 double alpha = (rel_time - lower_idx * time_between_states_) / time_between_states_;
72
73 Eigen::Vector3d interpolated = (1 - alpha) * trajectory_[lower_idx] + alpha *
74 trajectory_[upper_idx];
75
76 // correct the angle average
77 if ((trajectory_[lower_idx](2) > 0 && trajectory_[upper_idx](2) < 0 ||
78 trajectory_[lower_idx](2) < 0 && trajectory_[upper_idx](2) > 0) &&
79 (std::abs(trajectory_[lower_idx](2)) > M_PI_2 && std::abs(
80 trajectory_[upper_idx](2)) > M_PI_2))
81 {
82 double angle_diff = angles::shortest_angular_distance(
83 trajectory_[lower_idx](2), trajectory_[upper_idx](2));
84 interpolated(2) = trajectory_[lower_idx](2) + alpha * angle_diff;
85 interpolated(2) = angles::normalize_angle(interpolated(2));
86 }
87
88 return interpolated;
89 }
90
91 void activate() override
92 {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected