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