blob: 18c8381a87c6c1535890045063643755abdca8a9 [file] [log] [blame]
Alex Perry731b4602019-02-02 22:13:01 -08001#include "frc971/control_loops/drivetrain/splinedrivetrain.h"
2
3#include "Eigen/Dense"
4
Alex Perry1ec34522019-02-17 22:44:10 -08005#include "aos/init.h"
James Kuszmaulc73bb222019-04-07 12:15:35 -07006#include "aos/util/math.h"
Alex Perry731b4602019-02-02 22:13:01 -08007#include "frc971/control_loops/drivetrain/drivetrain.q.h"
8#include "frc971/control_loops/drivetrain/drivetrain_config.h"
9
Alex Perry731b4602019-02-02 22:13:01 -080010namespace frc971 {
11namespace control_loops {
12namespace drivetrain {
13
14SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config)
Alex Perrycc3ee4c2019-02-09 21:20:41 -080015 : dt_config_(dt_config), new_goal_(&mutex_) {
16 worker_thread_ = std::thread(&SplineDrivetrain::ComputeTrajectory, this);
17}
Alex Perrye32eabc2019-02-08 19:51:19 -080018
19void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -080020 output_was_capped_ =
21 ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0;
Alex Perrye32eabc2019-02-08 19:51:19 -080022
Alex Perrycc3ee4c2019-02-09 21:20:41 -080023 if (output_was_capped_) {
Alex Perrye32eabc2019-02-08 19:51:19 -080024 *U *= 12.0 / U->lpNorm<Eigen::Infinity>();
25 }
26}
Alex Perry731b4602019-02-02 22:13:01 -080027
Alex Perrycc3ee4c2019-02-09 21:20:41 -080028void SplineDrivetrain::ComputeTrajectory() {
Alex Perry1ec34522019-02-17 22:44:10 -080029 ::aos::SetCurrentThreadRealtimePriority(1);
30
Alex Perrycc3ee4c2019-02-09 21:20:41 -080031 ::aos::MutexLocker locker(&mutex_);
32 while (run_) {
33 while (goal_.spline.spline_idx == future_spline_idx_) {
34 CHECK(!new_goal_.Wait());
35 if (!run_) {
36 return;
37 }
38 }
39 past_distance_spline_.reset();
40 past_trajectory_.reset();
41
42 plan_state_ = PlanState::kBuildingTrajectory;
43 const ::frc971::MultiSpline &multispline = goal_.spline;
44 future_spline_idx_ = multispline.spline_idx;
Austin Schuh6bcc2302019-03-23 22:28:06 -070045 planning_spline_idx_ = future_spline_idx_;
Alex Perry731b4602019-02-02 22:13:01 -080046 auto x = multispline.spline_x;
47 auto y = multispline.spline_y;
48 ::std::vector<Spline> splines = ::std::vector<Spline>();
49 for (int i = 0; i < multispline.spline_count; ++i) {
50 ::Eigen::Matrix<double, 2, 6> points =
51 ::Eigen::Matrix<double, 2, 6>::Zero();
52 for (int j = 0; j < 6; ++j) {
53 points(0, j) = x[i * 5 + j];
54 points(1, j) = y[i * 5 + j];
55 }
56 splines.emplace_back(Spline(points));
57 }
58
Alex Perrycc3ee4c2019-02-09 21:20:41 -080059 future_distance_spline_ = ::std::unique_ptr<DistanceSpline>(
Alex Perry731b4602019-02-02 22:13:01 -080060 new DistanceSpline(::std::move(splines)));
61
Alex Perrycc3ee4c2019-02-09 21:20:41 -080062 future_trajectory_ = ::std::unique_ptr<Trajectory>(
63 new Trajectory(future_distance_spline_.get(), dt_config_));
Alex Perry731b4602019-02-02 22:13:01 -080064
Alex Perrycc3ee4c2019-02-09 21:20:41 -080065 for (size_t i = 0; i < multispline.constraints.size(); ++i) {
Alex Perry731b4602019-02-02 22:13:01 -080066 const ::frc971::Constraint &constraint = multispline.constraints[i];
67 switch (constraint.constraint_type) {
68 case 0:
69 break;
70 case 1:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080071 future_trajectory_->set_longitudal_acceleration(constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080072 break;
73 case 2:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080074 future_trajectory_->set_lateral_acceleration(constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080075 break;
76 case 3:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080077 future_trajectory_->set_voltage_limit(constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080078 break;
79 case 4:
Alex Perrycc3ee4c2019-02-09 21:20:41 -080080 future_trajectory_->LimitVelocity(constraint.start_distance,
81 constraint.end_distance,
82 constraint.value);
Alex Perry731b4602019-02-02 22:13:01 -080083 break;
84 }
85 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -080086 plan_state_ = PlanState::kPlanningTrajectory;
Alex Perry731b4602019-02-02 22:13:01 -080087
Alex Perrycc3ee4c2019-02-09 21:20:41 -080088 future_trajectory_->Plan();
89 plan_state_ = PlanState::kPlannedTrajectory;
90 }
91}
92
93void SplineDrivetrain::SetGoal(
94 const ::frc971::control_loops::DrivetrainQueue::Goal &goal) {
95 current_spline_handle_ = goal.spline_handle;
96 // If told to stop, set the executing spline to an invalid index.
Alex Perry4b502a92019-04-06 22:00:38 -070097 if (current_spline_handle_ == 0 && has_started_execution_) {
Alex Perrycc3ee4c2019-02-09 21:20:41 -080098 current_spline_idx_ = -1;
99 }
100
101 ::aos::Mutex::State mutex_state = mutex_.TryLock();
102 if (mutex_state == ::aos::Mutex::State::kLocked) {
James Kuszmaulc73bb222019-04-07 12:15:35 -0700103 drive_spline_backwards_ = goal_.drive_spline_backwards;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800104 if (goal.spline.spline_idx && future_spline_idx_ != goal.spline.spline_idx) {
105 goal_ = goal;
106 new_goal_.Broadcast();
107 }
108 if (future_trajectory_ &&
109 (!current_trajectory_ ||
110 current_trajectory_->is_at_end(current_xva_.block<2, 1>(0, 0)) ||
111 current_spline_idx_ == -1)) {
112 // Move current data to other variables to be cleared by worker.
113 past_trajectory_ = std::move(current_trajectory_);
114 past_distance_spline_ = std::move(current_distance_spline_);
115
116 // Move the computed data to be executed.
117 current_trajectory_ = std::move(future_trajectory_);
118 current_distance_spline_ = std::move(future_distance_spline_);
119 current_spline_idx_ = future_spline_idx_;
120
121 // Reset internal state to a trajectory start position.
122 current_xva_ = current_trajectory_->FFAcceleration(0);
123 current_xva_(1) = 0.0;
Alex Perry4b502a92019-04-06 22:00:38 -0700124 has_started_execution_ = false;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800125 }
126 mutex_.Unlock();
Alex Perrya71badb2019-02-06 19:40:41 -0800127 }
128}
129
Alex Perrye32eabc2019-02-08 19:51:19 -0800130// TODO(alex): Hold position when done following the spline.
131// TODO(Austin): Compensate for voltage error.
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800132void SplineDrivetrain::Update(bool enable, const ::Eigen::Matrix<double, 5, 1> &state) {
133 next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrye32eabc2019-02-08 19:51:19 -0800134 enable_ = enable;
135 if (enable && current_trajectory_) {
136 ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero();
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800137 if (!IsAtEnd() &&
138 current_spline_handle_ == current_spline_idx_) {
Alex Perry4b502a92019-04-06 22:00:38 -0700139 has_started_execution_ = true;
Alex Perrye32eabc2019-02-08 19:51:19 -0800140 // TODO(alex): It takes about a cycle for the outputs to propagate to the
141 // motors. Consider delaying the output by a cycle.
142 U_ff = current_trajectory_->FFVoltage(current_xva_(0));
143 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800144
Alex Perrye32eabc2019-02-08 19:51:19 -0800145 ::Eigen::Matrix<double, 2, 5> K =
146 current_trajectory_->KForState(state, dt_config_.dt, Q, R);
James Kuszmaul1057ce82019-02-09 17:58:24 -0800147 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
James Kuszmaulc73bb222019-04-07 12:15:35 -0700148 if (drive_spline_backwards_) {
149 ::Eigen::Matrix<double, 2, 1> swapU(U_ff(1, 0), U_ff(0, 0));
150 U_ff = -swapU;
151 goal_state(2, 0) += M_PI;
152 double left_goal = goal_state(3, 0);
153 double right_goal = goal_state(4, 0);
154 goal_state(3, 0) = -right_goal;
155 goal_state(4, 0) = -left_goal;
156 }
Alex Perrye32eabc2019-02-08 19:51:19 -0800157 ::Eigen::Matrix<double, 5, 1> state_error = goal_state - state;
James Kuszmaulc73bb222019-04-07 12:15:35 -0700158 state_error(2, 0) = ::aos::math::NormalizeAngle(state_error(2, 0));
Alex Perrye32eabc2019-02-08 19:51:19 -0800159 ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800160
161 ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2,1>(0,0);
162 next_xva_ = current_trajectory_->GetNextXVA(dt_config_.dt, &xv_state);
Alex Perrye32eabc2019-02-08 19:51:19 -0800163 next_U_ = U_ff + U_fb;
164 uncapped_U_ = next_U_;
165 ScaleCapU(&next_U_);
Alex Perry731b4602019-02-02 22:13:01 -0800166 }
167}
168
Alex Perry731b4602019-02-02 22:13:01 -0800169void SplineDrivetrain::SetOutput(
170 ::frc971::control_loops::DrivetrainQueue::Output *output) {
171 if (!output) {
172 return;
173 }
174 if (current_spline_handle_ == current_spline_idx_) {
James Kuszmaul1057ce82019-02-09 17:58:24 -0800175 if (!IsAtEnd()) {
Alex Perrye32eabc2019-02-08 19:51:19 -0800176 output->left_voltage = next_U_(0);
177 output->right_voltage = next_U_(1);
Alex Perrya71badb2019-02-06 19:40:41 -0800178 current_xva_ = next_xva_;
Alex Perry731b4602019-02-02 22:13:01 -0800179 }
180 }
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800181 output->left_voltage = next_U_(0);
182 output->right_voltage = next_U_(1);
Alex Perry731b4602019-02-02 22:13:01 -0800183}
184
Alex Perrye32eabc2019-02-08 19:51:19 -0800185void SplineDrivetrain::PopulateStatus(
186 ::frc971::control_loops::DrivetrainQueue::Status *status) const {
187 if (status && enable_) {
188 status->uncapped_left_voltage = uncapped_U_(0);
189 status->uncapped_right_voltage = uncapped_U_(1);
190 status->robot_speed = current_xva_(1);
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800191 status->output_was_capped = output_was_capped_;
192 }
193
194 if (status) {
195 if (current_distance_spline_) {
196 ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState();
197 status->trajectory_logging.x = goal_state(0);
198 status->trajectory_logging.y = goal_state(1);
199 status->trajectory_logging.theta = goal_state(2);
200 status->trajectory_logging.left_velocity = goal_state(3);
201 status->trajectory_logging.right_velocity = goal_state(4);
202 }
203 status->trajectory_logging.planning_state = static_cast<int8_t>(plan_state_.load());
Alex Perry4b502a92019-04-06 22:00:38 -0700204 status->trajectory_logging.is_executing = !IsAtEnd() && has_started_execution_;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700205 status->trajectory_logging.goal_spline_handle = current_spline_handle_;
Alex Perrycc3ee4c2019-02-09 21:20:41 -0800206 status->trajectory_logging.current_spline_idx = current_spline_idx_;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700207
208 int32_t planning_spline_idx = planning_spline_idx_;
209 if (current_spline_idx_ == planning_spline_idx) {
210 status->trajectory_logging.planning_spline_idx = 0;
211 } else {
212 status->trajectory_logging.planning_spline_idx = planning_spline_idx_;
213 }
Alex Perrye32eabc2019-02-08 19:51:19 -0800214 }
215}
216
Alex Perry731b4602019-02-02 22:13:01 -0800217} // namespace drivetrain
218} // namespace control_loops
219} // namespace frc971