Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/splinedrivetrain.h" |
| 2 | |
| 3 | #include "Eigen/Dense" |
| 4 | |
Alex Perry | 1ec3452 | 2019-02-17 22:44:10 -0800 | [diff] [blame] | 5 | #include "aos/init.h" |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 6 | #include "aos/util/math.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 7 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 8 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 9 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 10 | namespace frc971 { |
| 11 | namespace control_loops { |
| 12 | namespace drivetrain { |
| 13 | |
| 14 | SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config) |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 15 | : dt_config_(dt_config), new_goal_(&mutex_) { |
| 16 | worker_thread_ = std::thread(&SplineDrivetrain::ComputeTrajectory, this); |
| 17 | } |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 18 | |
| 19 | void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) { |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 20 | output_was_capped_ = |
| 21 | ::std::abs((*U)(0, 0)) > 12.0 || ::std::abs((*U)(1, 0)) > 12.0; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 22 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 23 | if (output_was_capped_) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 24 | *U *= 12.0 / U->lpNorm<Eigen::Infinity>(); |
| 25 | } |
| 26 | } |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 27 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 28 | void SplineDrivetrain::ComputeTrajectory() { |
Alex Perry | 1ec3452 | 2019-02-17 22:44:10 -0800 | [diff] [blame] | 29 | ::aos::SetCurrentThreadRealtimePriority(1); |
| 30 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 31 | ::aos::MutexLocker locker(&mutex_); |
| 32 | while (run_) { |
| 33 | while (goal_.spline.spline_idx == future_spline_idx_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 34 | AOS_CHECK(!new_goal_.Wait()); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 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 Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 45 | planning_spline_idx_ = future_spline_idx_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 46 | 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 | |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 59 | future_drive_spline_backwards_ = goal_.spline.drive_spline_backwards; |
| 60 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 61 | future_distance_spline_ = ::std::unique_ptr<DistanceSpline>( |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 62 | new DistanceSpline(::std::move(splines))); |
| 63 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 64 | future_trajectory_ = ::std::unique_ptr<Trajectory>( |
| 65 | new Trajectory(future_distance_spline_.get(), dt_config_)); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 66 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 67 | for (size_t i = 0; i < multispline.constraints.size(); ++i) { |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 68 | const ::frc971::Constraint &constraint = multispline.constraints[i]; |
| 69 | switch (constraint.constraint_type) { |
| 70 | case 0: |
| 71 | break; |
| 72 | case 1: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 73 | future_trajectory_->set_longitudal_acceleration(constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 74 | break; |
| 75 | case 2: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 76 | future_trajectory_->set_lateral_acceleration(constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 77 | break; |
| 78 | case 3: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 79 | future_trajectory_->set_voltage_limit(constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 80 | break; |
| 81 | case 4: |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 82 | future_trajectory_->LimitVelocity(constraint.start_distance, |
| 83 | constraint.end_distance, |
| 84 | constraint.value); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 88 | plan_state_ = PlanState::kPlanningTrajectory; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 89 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 90 | future_trajectory_->Plan(); |
| 91 | plan_state_ = PlanState::kPlannedTrajectory; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void SplineDrivetrain::SetGoal( |
| 96 | const ::frc971::control_loops::DrivetrainQueue::Goal &goal) { |
| 97 | current_spline_handle_ = goal.spline_handle; |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 98 | // If told to stop, set the executing spline to an invalid index and clear out |
| 99 | // its plan: |
| 100 | if (current_spline_handle_ == 0 && |
| 101 | current_spline_idx_ != goal.spline.spline_idx) { |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 102 | current_spline_idx_ = -1; |
| 103 | } |
| 104 | |
| 105 | ::aos::Mutex::State mutex_state = mutex_.TryLock(); |
| 106 | if (mutex_state == ::aos::Mutex::State::kLocked) { |
| 107 | if (goal.spline.spline_idx && future_spline_idx_ != goal.spline.spline_idx) { |
| 108 | goal_ = goal; |
| 109 | new_goal_.Broadcast(); |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 110 | if (current_spline_handle_ != current_spline_idx_) { |
| 111 | // If we aren't going to actively execute the current spline, evict it's |
| 112 | // plan. |
| 113 | past_trajectory_ = std::move(current_trajectory_); |
| 114 | past_distance_spline_ = std::move(current_distance_spline_); |
| 115 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 116 | } |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 117 | // If you never started executing the previous spline, you're screwed. |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 118 | if (future_trajectory_ && |
| 119 | (!current_trajectory_ || |
| 120 | current_trajectory_->is_at_end(current_xva_.block<2, 1>(0, 0)) || |
| 121 | current_spline_idx_ == -1)) { |
| 122 | // Move current data to other variables to be cleared by worker. |
| 123 | past_trajectory_ = std::move(current_trajectory_); |
| 124 | past_distance_spline_ = std::move(current_distance_spline_); |
| 125 | |
| 126 | // Move the computed data to be executed. |
| 127 | current_trajectory_ = std::move(future_trajectory_); |
| 128 | current_distance_spline_ = std::move(future_distance_spline_); |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 129 | current_drive_spline_backwards_ = future_drive_spline_backwards_; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 130 | current_spline_idx_ = future_spline_idx_; |
| 131 | |
| 132 | // Reset internal state to a trajectory start position. |
| 133 | current_xva_ = current_trajectory_->FFAcceleration(0); |
| 134 | current_xva_(1) = 0.0; |
Alex Perry | 4b502a9 | 2019-04-06 22:00:38 -0700 | [diff] [blame] | 135 | has_started_execution_ = false; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 136 | } |
| 137 | mutex_.Unlock(); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 141 | // TODO(alex): Hold position when done following the spline. |
| 142 | // TODO(Austin): Compensate for voltage error. |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 143 | void SplineDrivetrain::Update(bool enable, const ::Eigen::Matrix<double, 5, 1> &state) { |
| 144 | next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero(); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 145 | enable_ = enable; |
| 146 | if (enable && current_trajectory_) { |
| 147 | ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero(); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 148 | if (!IsAtEnd() && |
| 149 | current_spline_handle_ == current_spline_idx_) { |
Alex Perry | 4b502a9 | 2019-04-06 22:00:38 -0700 | [diff] [blame] | 150 | has_started_execution_ = true; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 151 | // TODO(alex): It takes about a cycle for the outputs to propagate to the |
| 152 | // motors. Consider delaying the output by a cycle. |
| 153 | U_ff = current_trajectory_->FFVoltage(current_xva_(0)); |
| 154 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 155 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 156 | ::Eigen::Matrix<double, 2, 5> K = |
| 157 | current_trajectory_->KForState(state, dt_config_.dt, Q, R); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 158 | ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState(); |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 159 | if (current_drive_spline_backwards_) { |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 160 | ::Eigen::Matrix<double, 2, 1> swapU(U_ff(1, 0), U_ff(0, 0)); |
| 161 | U_ff = -swapU; |
| 162 | goal_state(2, 0) += M_PI; |
| 163 | double left_goal = goal_state(3, 0); |
| 164 | double right_goal = goal_state(4, 0); |
| 165 | goal_state(3, 0) = -right_goal; |
| 166 | goal_state(4, 0) = -left_goal; |
| 167 | } |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 168 | ::Eigen::Matrix<double, 5, 1> state_error = goal_state - state; |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 169 | state_error(2, 0) = ::aos::math::NormalizeAngle(state_error(2, 0)); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 170 | ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 171 | |
| 172 | ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2,1>(0,0); |
| 173 | next_xva_ = current_trajectory_->GetNextXVA(dt_config_.dt, &xv_state); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 174 | next_U_ = U_ff + U_fb; |
| 175 | uncapped_U_ = next_U_; |
| 176 | ScaleCapU(&next_U_); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 180 | void SplineDrivetrain::SetOutput( |
| 181 | ::frc971::control_loops::DrivetrainQueue::Output *output) { |
| 182 | if (!output) { |
| 183 | return; |
| 184 | } |
| 185 | if (current_spline_handle_ == current_spline_idx_) { |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 186 | if (!IsAtEnd()) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 187 | output->left_voltage = next_U_(0); |
| 188 | output->right_voltage = next_U_(1); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 189 | current_xva_ = next_xva_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 192 | output->left_voltage = next_U_(0); |
| 193 | output->right_voltage = next_U_(1); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 196 | void SplineDrivetrain::PopulateStatus( |
| 197 | ::frc971::control_loops::DrivetrainQueue::Status *status) const { |
| 198 | if (status && enable_) { |
| 199 | status->uncapped_left_voltage = uncapped_U_(0); |
| 200 | status->uncapped_right_voltage = uncapped_U_(1); |
| 201 | status->robot_speed = current_xva_(1); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 202 | status->output_was_capped = output_was_capped_; |
| 203 | } |
| 204 | |
| 205 | if (status) { |
| 206 | if (current_distance_spline_) { |
| 207 | ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState(); |
| 208 | status->trajectory_logging.x = goal_state(0); |
| 209 | status->trajectory_logging.y = goal_state(1); |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 210 | status->trajectory_logging.theta = ::aos::math::NormalizeAngle( |
| 211 | goal_state(2) + (current_drive_spline_backwards_ ? M_PI : 0.0)); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 212 | status->trajectory_logging.left_velocity = goal_state(3); |
| 213 | status->trajectory_logging.right_velocity = goal_state(4); |
| 214 | } |
| 215 | status->trajectory_logging.planning_state = static_cast<int8_t>(plan_state_.load()); |
Alex Perry | 4b502a9 | 2019-04-06 22:00:38 -0700 | [diff] [blame] | 216 | status->trajectory_logging.is_executing = !IsAtEnd() && has_started_execution_; |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 217 | status->trajectory_logging.is_executed = |
| 218 | (current_spline_idx_ != -1) && IsAtEnd(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 219 | status->trajectory_logging.goal_spline_handle = current_spline_handle_; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 220 | status->trajectory_logging.current_spline_idx = current_spline_idx_; |
James Kuszmaul | 5a54341 | 2019-04-13 15:49:42 -0700 | [diff] [blame] | 221 | status->trajectory_logging.distance_remaining = |
| 222 | current_trajectory_ ? current_trajectory_->length() - current_xva_.x() |
| 223 | : 0.0; |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 224 | |
| 225 | int32_t planning_spline_idx = planning_spline_idx_; |
| 226 | if (current_spline_idx_ == planning_spline_idx) { |
| 227 | status->trajectory_logging.planning_spline_idx = 0; |
| 228 | } else { |
| 229 | status->trajectory_logging.planning_spline_idx = planning_spline_idx_; |
| 230 | } |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 234 | } // namespace drivetrain |
| 235 | } // namespace control_loops |
| 236 | } // namespace frc971 |