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