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 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 5 | #include "aos/json_to_flatbuffer.h" |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/realtime.h" |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 8 | #include "aos/util/math.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "frc971/control_loops/control_loops_generated.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 10 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_output_generated.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 14 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 15 | namespace frc971 { |
| 16 | namespace control_loops { |
| 17 | namespace drivetrain { |
| 18 | |
| 19 | SplineDrivetrain::SplineDrivetrain(const DrivetrainConfig<double> &dt_config) |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 20 | : dt_config_(dt_config), |
| 21 | current_xva_(Eigen::Vector3d::Zero()), |
| 22 | next_xva_(Eigen::Vector3d::Zero()), |
| 23 | next_U_(Eigen::Vector2d::Zero()) {} |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 24 | |
| 25 | void SplineDrivetrain::ScaleCapU(Eigen::Matrix<double, 2, 1> *U) { |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 26 | output_was_capped_ = |
| 27 | ::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] | 28 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 29 | if (output_was_capped_) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 30 | *U *= 12.0 / U->lpNorm<Eigen::Infinity>(); |
| 31 | } |
| 32 | } |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 33 | |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 34 | void SplineDrivetrain::SetGoal( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | const ::frc971::control_loops::drivetrain::Goal *goal) { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 36 | if (goal->has_spline_handle()) { |
| 37 | commanded_spline_ = goal->spline_handle(); |
Austin Schuh | b574fe4 | 2019-12-06 23:51:47 -0800 | [diff] [blame] | 38 | } else { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 39 | commanded_spline_.reset(); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 40 | } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 41 | UpdateSplineHandles(); |
| 42 | } |
| 43 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame^] | 44 | bool SplineDrivetrain::IsCurrentTrajectory( |
| 45 | const fb::Trajectory *trajectory) const { |
| 46 | return (current_trajectory_ != nullptr && |
| 47 | current_trajectory().spline_handle() == trajectory->handle()); |
| 48 | } |
| 49 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 50 | bool SplineDrivetrain::HasTrajectory(const fb::Trajectory *trajectory) const { |
| 51 | if (trajectory == nullptr) { |
| 52 | return false; |
| 53 | } |
| 54 | for (size_t ii = 0; ii < trajectories_.size(); ++ii) { |
| 55 | if (trajectories_[ii]->spline_handle() == trajectory->handle()) { |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame^] | 62 | void SplineDrivetrain::DeleteTrajectory(const fb::Trajectory *trajectory) { |
| 63 | CHECK(trajectory != nullptr); |
| 64 | |
| 65 | for (size_t ii = 0; ii < trajectories_.size(); ++ii) { |
| 66 | if (trajectories_[ii]->spline_handle() == trajectory->handle()) { |
| 67 | trajectories_.erase(trajectories_.begin() + ii); |
| 68 | return; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | LOG(FATAL) << "Trying to remove unknown trajectory " << trajectory->handle(); |
| 73 | } |
| 74 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 75 | void SplineDrivetrain::AddTrajectory(const fb::Trajectory *trajectory) { |
| 76 | trajectories_.emplace_back( |
| 77 | std::make_unique<FinishedTrajectory>(dt_config_, trajectory)); |
| 78 | UpdateSplineHandles(); |
| 79 | } |
| 80 | |
| 81 | void SplineDrivetrain::DeleteCurrentSpline() { |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame^] | 82 | DeleteTrajectory(&CHECK_NOTNULL(current_trajectory_)->trajectory()); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 83 | executing_spline_ = false; |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame^] | 84 | current_trajectory_ = nullptr; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 85 | current_xva_.setZero(); |
| 86 | } |
| 87 | |
| 88 | void SplineDrivetrain::UpdateSplineHandles() { |
| 89 | // If we are currently executing a spline and have received a change |
| 90 | if (executing_spline_) { |
| 91 | if (!commanded_spline_) { |
| 92 | // We've been told to stop executing a spline; remove it from our queue, |
| 93 | // and clean up. |
| 94 | DeleteCurrentSpline(); |
| 95 | return; |
| 96 | } else { |
| 97 | if (executing_spline_ && |
| 98 | current_trajectory().spline_handle() != *commanded_spline_) { |
| 99 | // If we are executing a spline, and the handle has changed, garbage |
| 100 | // collect the old spline. |
| 101 | DeleteCurrentSpline(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | // We've now cleaned up the previous state; handle any new commands. |
| 106 | if (!commanded_spline_) { |
| 107 | return; |
| 108 | } |
| 109 | for (size_t ii = 0; ii < trajectories_.size(); ++ii) { |
| 110 | if (trajectories_[ii]->spline_handle() == *commanded_spline_) { |
| 111 | executing_spline_ = true; |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame^] | 112 | current_trajectory_ = trajectories_[ii].get(); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | // If we didn't find the commanded spline in the list of available splines, |
| 116 | // that's fine; it just means, it hasn't been fully planned yet. |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 119 | // TODO(alex): Hold position when done following the spline. |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 120 | void SplineDrivetrain::Update( |
| 121 | bool enable, const ::Eigen::Matrix<double, 5, 1> &state, |
| 122 | const ::Eigen::Matrix<double, 2, 1> &voltage_error) { |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 123 | next_U_ = ::Eigen::Matrix<double, 2, 1>::Zero(); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 124 | enable_ = enable; |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 125 | if (enable && executing_spline_) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 126 | ::Eigen::Matrix<double, 2, 1> U_ff = ::Eigen::Matrix<double, 2, 1>::Zero(); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 127 | if (!IsAtEnd() && executing_spline_) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 128 | // TODO(alex): It takes about a cycle for the outputs to propagate to the |
| 129 | // motors. Consider delaying the output by a cycle. |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 130 | U_ff = current_trajectory().FFVoltage(current_xva_(0)); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 131 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 132 | |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 133 | const double current_distance = current_xva_(0); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 134 | ::Eigen::Matrix<double, 2, 5> K = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 135 | current_trajectory().GainForDistance(current_distance); |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 136 | ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState(); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 137 | if (current_trajectory().drive_spline_backwards()) { |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 138 | ::Eigen::Matrix<double, 2, 1> swapU(U_ff(1, 0), U_ff(0, 0)); |
| 139 | U_ff = -swapU; |
| 140 | goal_state(2, 0) += M_PI; |
| 141 | double left_goal = goal_state(3, 0); |
| 142 | double right_goal = goal_state(4, 0); |
| 143 | goal_state(3, 0) = -right_goal; |
| 144 | goal_state(4, 0) = -left_goal; |
| 145 | } |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 146 | const Eigen::Matrix<double, 5, 1> relative_goal = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 147 | current_trajectory().StateToPathRelativeState(current_distance, |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 148 | goal_state); |
| 149 | const Eigen::Matrix<double, 5, 1> relative_state = |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 150 | current_trajectory().StateToPathRelativeState(current_distance, state); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 151 | Eigen::Matrix<double, 5, 1> state_error = relative_goal - relative_state; |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 152 | state_error(2, 0) = ::aos::math::NormalizeAngle(state_error(2, 0)); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 153 | ::Eigen::Matrix<double, 2, 1> U_fb = K * state_error; |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 154 | |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 155 | ::Eigen::Matrix<double, 2, 1> xv_state = current_xva_.block<2, 1>(0, 0); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 156 | next_xva_ = current_trajectory().GetNextXVA(dt_config_.dt, &xv_state); |
James Kuszmaul | aa2499d | 2020-06-02 21:31:19 -0700 | [diff] [blame] | 157 | next_U_ = U_ff + U_fb - voltage_error; |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 158 | uncapped_U_ = next_U_; |
| 159 | ScaleCapU(&next_U_); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 163 | void SplineDrivetrain::SetOutput( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | ::frc971::control_loops::drivetrain::OutputT *output) { |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 165 | if (!output) { |
| 166 | return; |
| 167 | } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 168 | if (executing_spline_) { |
James Kuszmaul | 1057ce8 | 2019-02-09 17:58:24 -0800 | [diff] [blame] | 169 | if (!IsAtEnd()) { |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 170 | output->left_voltage = next_U_(0); |
| 171 | output->right_voltage = next_U_(1); |
Alex Perry | a71badb | 2019-02-06 19:40:41 -0800 | [diff] [blame] | 172 | current_xva_ = next_xva_; |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 175 | output->left_voltage = next_U_(0); |
| 176 | output->right_voltage = next_U_(1); |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 179 | void SplineDrivetrain::PopulateStatus( |
Austin Schuh | d749d93 | 2020-12-30 21:38:40 -0800 | [diff] [blame] | 180 | drivetrain::Status::Builder *builder) const { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | if (builder && enable_) { |
| 182 | builder->add_uncapped_left_voltage(uncapped_U_(0)); |
| 183 | builder->add_uncapped_right_voltage(uncapped_U_(1)); |
| 184 | builder->add_robot_speed(current_xva_(1)); |
| 185 | builder->add_output_was_capped(output_was_capped_); |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 186 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 187 | } |
Alex Perry | cc3ee4c | 2019-02-09 21:20:41 -0800 | [diff] [blame] | 188 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 189 | flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging( |
| 190 | flatbuffers::FlatBufferBuilder *builder) const { |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 191 | int *spline_handles; |
| 192 | const flatbuffers::Offset<flatbuffers::Vector<int>> handles_vector = |
| 193 | builder->CreateUninitializedVector(trajectories_.size(), &spline_handles); |
| 194 | |
| 195 | for (size_t ii = 0; ii < trajectories_.size(); ++ii) { |
| 196 | spline_handles[ii] = trajectories_[ii]->spline_handle(); |
| 197 | } |
| 198 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 199 | drivetrain::TrajectoryLogging::Builder trajectory_logging_builder(*builder); |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 200 | if (executing_spline_) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 201 | ::Eigen::Matrix<double, 5, 1> goal_state = CurrentGoalState(); |
| 202 | trajectory_logging_builder.add_x(goal_state(0)); |
| 203 | trajectory_logging_builder.add_y(goal_state(1)); |
| 204 | trajectory_logging_builder.add_theta(::aos::math::NormalizeAngle( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 205 | goal_state(2) + |
| 206 | (current_trajectory().drive_spline_backwards() ? M_PI : 0.0))); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 207 | trajectory_logging_builder.add_left_velocity(goal_state(3)); |
| 208 | trajectory_logging_builder.add_right_velocity(goal_state(4)); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 209 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 210 | trajectory_logging_builder.add_is_executing(!IsAtEnd() && |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 211 | executing_spline_); |
| 212 | trajectory_logging_builder.add_is_executed(executing_spline_ && IsAtEnd()); |
| 213 | if (commanded_spline_) { |
| 214 | trajectory_logging_builder.add_goal_spline_handle(*commanded_spline_); |
| 215 | if (executing_spline_) { |
| 216 | trajectory_logging_builder.add_current_spline_idx(*commanded_spline_); |
| 217 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 218 | } |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 219 | trajectory_logging_builder.add_distance_remaining( |
| 220 | executing_spline_ ? current_trajectory().length() - current_xva_.x() |
| 221 | : 0.0); |
| 222 | trajectory_logging_builder.add_available_splines(handles_vector); |
| 223 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 224 | return trajectory_logging_builder.Finish(); |
| 225 | } |
| 226 | |
| 227 | flatbuffers::Offset<TrajectoryLogging> SplineDrivetrain::MakeTrajectoryLogging( |
| 228 | aos::Sender<drivetrain::Status>::Builder *builder) const { |
| 229 | return MakeTrajectoryLogging(builder->fbb()); |
Alex Perry | e32eabc | 2019-02-08 19:51:19 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Alex Perry | 731b460 | 2019-02-02 22:13:01 -0800 | [diff] [blame] | 232 | } // namespace drivetrain |
| 233 | } // namespace control_loops |
| 234 | } // namespace frc971 |