blob: d8e2f9a7b0594a1378d22b808f5eb603adb862b9 [file] [log] [blame]
Alex Perry731b4602019-02-02 22:13:01 -08001#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
2
3#include "Eigen/Dense"
4
5#include "frc971/control_loops/drivetrain/drivetrain.q.h"
6#include "frc971/control_loops/drivetrain/drivetrain_config.h"
7
Alex Perry731b4602019-02-02 22:13:01 -08008namespace frc971 {
9namespace control_loops {
10namespace drivetrain {
11
12SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config)
Alex Perrycc3ee4c2019-02-09 21:20:41 -080013 : dt_config_(dt_config), new_goal_(&mutex_) {
14 worker_thread_ = std::thread(&SplineDrivetrain::ComputeTrajectory, this);
15}
Alex Perrye32eabc2019-02-08 19:51:19 -080016
17void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -080018 output_was_capped_ =
19 ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0;
Alex Perrye32eabc2019-02-08 19:51:19 -080020
Alex Perrycc3ee4c2019-02-09 21:20:41 -080021 if (output_was_capped_) {
Alex Perrye32eabc2019-02-08 19:51:19 -080022 *U *= 12.0 / U->lpNorm<Eigen::Infinity>();
23 }
24}
Alex Perry731b4602019-02-02 22:13:01 -080025
Alex Perrycc3ee4c2019-02-09 21:20:41 -080026// TODO(alex): work on setting priority.
27void SplineDrivetrain::ComputeTrajectory() {
28 ::aos::MutexLocker locker(&mutex_);
29 while (run_) {
30 while (goal_.spline.spline_idx == future_spline_idx_) {
31 CHECK(!new_goal_.Wait());
32 if (!run_) {
33 return;
34 }
35 }
36 past_distance_spline_.reset();
37 past_trajectory_.reset();
38
39 plan_state_ = PlanState::kBuildingTrajectory;
40 const ::frc971::MultiSpline &multispline = goal_.spline;
41 future_spline_idx_ = multispline.spline_idx;
Alex Perry731b4602019-02-02 22:13:01 -080042 auto x = multispline.spline_x;
43 auto y = multispline.spline_y;
44 ::std::vector<Spline> splines = ::std::vector<Spline>();
45 for (int i = 0; i < multispline.spline_count; ++i) {
46 ::Eigen::Matrix<double, 2, 6> points =
47 ::Eigen::Matrix<double, 2, 6>::Zero();
48 for (int j = 0; j < 6; ++j) {
49 points(0, j) = x[i * 5 + j];
50 points(1, j) = y[i * 5 + j];
51 }
52 splines.emplace_back(Spline(points));
53 }
54
Alex Perrycc3ee4c2019-02-09 21:20:41 -080055 future_distance_spline_ = ::std::unique_ptr<DistanceSpline>(
Alex Perry731b4602019-02-02 22:13:01 -080056 new DistanceSpline(::std::move(splines)));
57
Alex Perrycc3ee4c2019-02-09 21:20:41 -080058 future_trajectory_ = ::std::unique_ptr<Trajectory>(
59 new Trajectory(future_distance_spline_.get(), dt_config_));
Alex Perry731b4602019-02-02 22:13:01 -080060
Alex Perrycc3ee4c2019-02-09 21:20:41 -080061 for (size_t i = 0; i < multispline.constraints.size(); ++i) {
Alex Perry731b4602019-02-02 22:13:01 -080062 const ::frc971::Constraint &constraint = multispline.constraints[i];
63 switch (constraint.constraint_type) {
64 case 0:
65 break;
66 case 1:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080067 future_trajectory_->set_longitudal_acceleration(constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080068 break;
69 case 2:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080070 future_trajectory_->set_lateral_acceleration(constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080071 break;
72 case 3:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080073 future_trajectory_->set_voltage_limit(constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080074 break;
75 case 4:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080076 future_trajectory_->LimitVelocity(constraint.start_distance,
77 constraint.end_distance,
78 constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080079 break;
80 }
81 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -080082 plan_state_ = PlanState::kPlanningTrajectory;
Alex Perry731b4602019-02-02 22:13:01 -080083
Alex Perrycc3ee4c2019-02-09 21:20:41 -080084 future_trajectory_->Plan();
85 plan_state_ = PlanState::kPlannedTrajectory;
86 }
87}
88
89void SplineDrivetrain::SetGoal(
90 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
91 current_spline_handle_ = goal.spline_handle;
92 // If told to stop, set the executing spline to an invalid index.
93 if (current_spline_handle_ == 0) {
94 current_spline_idx_ = -1;
95 }
96
97 ::aos::Mutex::State mutex_state = mutex_.TryLock();
98 if (mutex_state == ::aos::Mutex::State::kLocked) {
99 if (goal.spline.spline_idx && future_spline_idx_ != goal.spline.spline_idx) {
100 goal_ = goal;
101 new_goal_.Broadcast();
102 }
103 if (future_trajectory_ &&
104 (!current_trajectory_ ||
105 current_trajectory_->is_at_end(current_xva_.block<2, 1>(0, 0)) ||
106 current_spline_idx_ == -1)) {
107 // Move current data to other variables to be cleared by worker.
108 past_trajectory_ = std::move(current_trajectory_);
109 past_distance_spline_ = std::move(current_distance_spline_);
110
111 // Move the computed data to be executed.
112 current_trajectory_ = std::move(future_trajectory_);
113 current_distance_spline_ = std::move(future_distance_spline_);
114 current_spline_idx_ = future_spline_idx_;
115
116 // Reset internal state to a trajectory start position.
117 current_xva_ = current_trajectory_->FFAcceleration(0);
118 current_xva_(1) = 0.0;
119 }
120 mutex_.Unlock();
Alex Perrya71badb2019-02-06 19:40:41 -0800121 }
122}
123
Alex Perrye32eabc2019-02-08 19:51:19 -0800124// TODO(alex): Hold position when done following the spline.
125// TODO(Austin): Compensate for voltage error.
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800126void SplineDrivetrain::Update(bool enable, const ::Eigen::Matrix<double, 5, 1> &state) {
127 next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrye32eabc2019-02-08 19:51:19 -0800128 enable_ = enable;
129 if (enable && current_trajectory_) {
130 ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800131 if (!IsAtEnd() &&
132 current_spline_handle_ == current_spline_idx_) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800133 // TODO(alex): It takes about a cycle for the outputs to propagate to the
134 // motors. Consider delaying the output by a cycle.
135 U_ff = current_trajectory_->FFVoltage(current_xva_(0));
136 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800137
Alex Perrye32eabc2019-02-08 19:51:19 -0800138 ::Eigen::Matrix<double, 2, 5> K =
139 current_trajectory_->KForState(state, dt_config_.dt, Q, R);
James Kuszmaul1057ce82019-02-09 17:58:24 -0800140 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
Alex Perrye32eabc2019-02-08 19:51:19 -0800141 ::Eigen::Matrix<double, 5, 1> state_error = goal_state - state;
142 ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800143
144 ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2,1>(0,0);
145 next_xva_ = current_trajectory_->GetNextXVA(dt_config_.dt, &xv_state);
Alex Perrye32eabc2019-02-08 19:51:19 -0800146 next_U_ = U_ff + U_fb;
147 uncapped_U_ = next_U_;
148 ScaleCapU(&next_U_);
Alex Perry731b4602019-02-02 22:13:01 -0800149 }
150}
151
Alex Perry731b4602019-02-02 22:13:01 -0800152void SplineDrivetrain::SetOutput(
153 ::frc971::control_loops::DrivetrainQueue::Output *output) {
154 if (!output) {
155 return;
156 }
157 if (current_spline_handle_ == current_spline_idx_) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800158 if (!IsAtEnd()) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800159 output->left_voltage = next_U_(0);
160 output->right_voltage = next_U_(1);
Alex Perrya71badb2019-02-06 19:40:41 -0800161 current_xva_ = next_xva_;
Alex Perry731b4602019-02-02 22:13:01 -0800162 }
163 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800164 output->left_voltage = next_U_(0);
165 output->right_voltage = next_U_(1);
Alex Perry731b4602019-02-02 22:13:01 -0800166}
167
Alex Perrye32eabc2019-02-08 19:51:19 -0800168void SplineDrivetrain::PopulateStatus(
169 ::frc971::control_loops::DrivetrainQueue::Status *status) const {
170 if (status && enable_) {
171 status->uncapped_left_voltage = uncapped_U_(0);
172 status->uncapped_right_voltage = uncapped_U_(1);
173 status->robot_speed = current_xva_(1);
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800174 status->output_was_capped = output_was_capped_;
175 }
176
177 if (status) {
178 if (current_distance_spline_) {
179 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
180 status->trajectory_logging.x = goal_state(0);
181 status->trajectory_logging.y = goal_state(1);
182 status->trajectory_logging.theta = goal_state(2);
183 status->trajectory_logging.left_velocity = goal_state(3);
184 status->trajectory_logging.right_velocity = goal_state(4);
185 }
186 status->trajectory_logging.planning_state = static_cast<int8_t>(plan_state_.load());
187 status->trajectory_logging.is_executing = !IsAtEnd();
188 status->trajectory_logging.current_spline_handle = current_spline_handle_;
189 status->trajectory_logging.current_spline_idx = current_spline_idx_;
Alex Perrye32eabc2019-02-08 19:51:19 -0800190 }
191}
192
Alex Perry731b4602019-02-02 22:13:01 -0800193} // namespace drivetrain
194} // namespace control_loops
195} // namespace frc971