Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 1 | #include "frc971/autonomous/base_autonomous_actor.h" |
| 2 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 3 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cinttypes> |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 5 | #include <cmath> |
| 6 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include "aos/logging/logging.h" |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 8 | #include "aos/util/math.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 9 | #include "aos/util/phased_loop.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "frc971/control_loops/control_loops_generated.h" |
| 11 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 12 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 13 | #include "frc971/control_loops/drivetrain/spline.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "y2019/control_loops/drivetrain/target_selector_generated.h" |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 15 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 16 | using ::aos::monotonic_clock; |
| 17 | namespace chrono = ::std::chrono; |
| 18 | namespace this_thread = ::std::this_thread; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | namespace drivetrain = frc971::control_loops::drivetrain; |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 20 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 21 | namespace frc971::autonomous { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 22 | |
| 23 | BaseAutonomousActor::BaseAutonomousActor( |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 24 | ::aos::EventLoop *event_loop, |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 25 | const control_loops::drivetrain::DrivetrainConfig<double> &dt_config) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | : aos::common::actions::ActorBase<Goal>(event_loop, "/autonomous"), |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 27 | dt_config_(dt_config), |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 28 | initial_drivetrain_({0.0, 0.0}), |
| 29 | target_selector_hint_sender_( |
James Kuszmaul | 202e438 | 2023-03-05 14:56:55 -0800 | [diff] [blame] | 30 | event_loop->TryMakeSender< |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 31 | ::y2019::control_loops::drivetrain::TargetSelectorHint>( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | "/drivetrain")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 33 | drivetrain_goal_sender_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | event_loop->MakeSender<drivetrain::Goal>("/drivetrain")), |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 35 | spline_goal_sender_( |
| 36 | event_loop->MakeSender<drivetrain::SplineGoal>("/drivetrain")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 37 | drivetrain_status_fetcher_( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | event_loop->MakeFetcher<drivetrain::Status>("/drivetrain")), |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 39 | drivetrain_goal_fetcher_( |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 40 | event_loop->MakeFetcher<drivetrain::Goal>("/drivetrain")) { |
| 41 | event_loop->SetRuntimeRealtimePriority(29); |
| 42 | } |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 43 | |
Austin Schuh | 9aa78b1 | 2021-11-12 11:53:33 -0800 | [diff] [blame] | 44 | void BaseAutonomousActor::ApplyThrottle(double throttle) { |
| 45 | goal_spline_handle_ = 0; |
| 46 | |
| 47 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
| 48 | |
| 49 | drivetrain::Goal::Builder goal_builder = |
| 50 | builder.MakeBuilder<drivetrain::Goal>(); |
| 51 | goal_builder.add_controller_type(drivetrain::ControllerType::POLYDRIVE); |
| 52 | goal_builder.add_highgear(true); |
| 53 | goal_builder.add_wheel(0.0); |
| 54 | goal_builder.add_throttle(throttle); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 55 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Austin Schuh | 9aa78b1 | 2021-11-12 11:53:33 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 58 | void BaseAutonomousActor::ResetDrivetrain() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 59 | AOS_LOG(INFO, "resetting the drivetrain\n"); |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 60 | max_drivetrain_voltage_ = 12.0; |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 61 | goal_spline_handle_ = 0; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 62 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
| 64 | |
| 65 | drivetrain::Goal::Builder goal_builder = |
| 66 | builder.MakeBuilder<drivetrain::Goal>(); |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 67 | goal_builder.add_controller_type(drivetrain::ControllerType::POLYDRIVE); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | goal_builder.add_highgear(true); |
| 69 | goal_builder.add_wheel(0.0); |
| 70 | goal_builder.add_throttle(0.0); |
| 71 | goal_builder.add_left_goal(initial_drivetrain_.left); |
| 72 | goal_builder.add_right_goal(initial_drivetrain_.right); |
| 73 | goal_builder.add_max_ss_voltage(max_drivetrain_voltage_); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 74 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void BaseAutonomousActor::InitializeEncoders() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 78 | // Spin until we get a message. |
| 79 | WaitUntil([this]() { return drivetrain_status_fetcher_.Fetch(); }); |
| 80 | |
| 81 | initial_drivetrain_.left = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | drivetrain_status_fetcher_->estimated_left_position(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 83 | initial_drivetrain_.right = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | drivetrain_status_fetcher_->estimated_right_position(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void BaseAutonomousActor::StartDrive(double distance, double angle, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | ProfileParametersT linear, |
| 89 | ProfileParametersT angular) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 90 | AOS_LOG(INFO, "Driving distance %f, angle %f\n", distance, angle); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 91 | { |
| 92 | const double dangle = angle * dt_config_.robot_radius; |
| 93 | initial_drivetrain_.left += distance - dangle; |
| 94 | initial_drivetrain_.right += distance + dangle; |
| 95 | } |
| 96 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 98 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 99 | auto linear_offset = ProfileParameters::Pack(*builder.fbb(), &linear); |
| 100 | auto angular_offset = ProfileParameters::Pack(*builder.fbb(), &angular); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 101 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 102 | drivetrain::Goal::Builder goal_builder = |
| 103 | builder.MakeBuilder<drivetrain::Goal>(); |
| 104 | |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 105 | goal_builder.add_controller_type(drivetrain::ControllerType::MOTION_PROFILE); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | goal_builder.add_highgear(true); |
| 107 | goal_builder.add_wheel(0.0); |
| 108 | goal_builder.add_throttle(0.0); |
| 109 | goal_builder.add_left_goal(initial_drivetrain_.left); |
| 110 | goal_builder.add_right_goal(initial_drivetrain_.right); |
| 111 | goal_builder.add_max_ss_voltage(max_drivetrain_voltage_); |
| 112 | goal_builder.add_linear(linear_offset); |
| 113 | goal_builder.add_angular(angular_offset); |
| 114 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 115 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void BaseAutonomousActor::WaitUntilDoneOrCanceled( |
| 119 | ::std::unique_ptr<aos::common::actions::Action> action) { |
| 120 | if (!action) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 121 | AOS_LOG(ERROR, "No action, not waiting\n"); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 125 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 126 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 127 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 128 | while (true) { |
| 129 | // Poll the running bit and see if we should cancel. |
| 130 | phased_loop.SleepUntilNext(); |
| 131 | if (!action->Running() || ShouldCancel()) { |
| 132 | return; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | bool BaseAutonomousActor::WaitForDriveDone() { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 138 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 139 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 140 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 141 | |
| 142 | while (true) { |
| 143 | if (ShouldCancel()) { |
| 144 | return false; |
| 145 | } |
| 146 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 147 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 148 | if (IsDriveDone()) { |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | bool BaseAutonomousActor::IsDriveDone() { |
Brian Silverman | f83678c | 2017-03-11 14:02:26 -0800 | [diff] [blame] | 155 | static constexpr double kPositionTolerance = 0.02; |
| 156 | static constexpr double kVelocityTolerance = 0.10; |
| 157 | static constexpr double kProfileTolerance = 0.001; |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 158 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 159 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 160 | if (::std::abs(drivetrain_status_fetcher_->profiled_left_position_goal() - |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 161 | initial_drivetrain_.left) < kProfileTolerance && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 162 | ::std::abs(drivetrain_status_fetcher_->profiled_right_position_goal() - |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 163 | initial_drivetrain_.right) < kProfileTolerance && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | ::std::abs(drivetrain_status_fetcher_->estimated_left_position() - |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 165 | initial_drivetrain_.left) < kPositionTolerance && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | ::std::abs(drivetrain_status_fetcher_->estimated_right_position() - |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 167 | initial_drivetrain_.right) < kPositionTolerance && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | ::std::abs(drivetrain_status_fetcher_->estimated_left_velocity()) < |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 169 | kVelocityTolerance && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 170 | ::std::abs(drivetrain_status_fetcher_->estimated_right_velocity()) < |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 171 | kVelocityTolerance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 172 | AOS_LOG(INFO, "Finished drive\n"); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
Austin Schuh | a5fefcd | 2023-03-22 20:23:42 -0700 | [diff] [blame] | 179 | double BaseAutonomousActor::X() { |
| 180 | drivetrain_status_fetcher_.Fetch(); |
| 181 | CHECK(drivetrain_status_fetcher_.get()); |
| 182 | return drivetrain_status_fetcher_->x(); |
| 183 | } |
| 184 | |
| 185 | double BaseAutonomousActor::Y() { |
| 186 | drivetrain_status_fetcher_.Fetch(); |
| 187 | CHECK(drivetrain_status_fetcher_.get()); |
| 188 | return drivetrain_status_fetcher_->y(); |
| 189 | } |
| 190 | |
| 191 | double BaseAutonomousActor::Theta() { |
| 192 | drivetrain_status_fetcher_.Fetch(); |
| 193 | CHECK(drivetrain_status_fetcher_.get()); |
| 194 | return drivetrain_status_fetcher_->theta(); |
| 195 | } |
| 196 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 197 | bool BaseAutonomousActor::WaitForAboveAngle(double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 198 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 199 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 200 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 201 | while (true) { |
| 202 | if (ShouldCancel()) { |
| 203 | return false; |
| 204 | } |
| 205 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 206 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 207 | if (IsDriveDone()) { |
| 208 | return true; |
| 209 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 210 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | if (drivetrain_status_fetcher_->ground_angle() > angle) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | bool BaseAutonomousActor::WaitForBelowAngle(double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 219 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 220 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 221 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 222 | while (true) { |
| 223 | if (ShouldCancel()) { |
| 224 | return false; |
| 225 | } |
| 226 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 227 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 228 | if (IsDriveDone()) { |
| 229 | return true; |
| 230 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 231 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 232 | if (drivetrain_status_fetcher_->ground_angle() < angle) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | bool BaseAutonomousActor::WaitForMaxBy(double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 240 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 241 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 242 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 243 | double max_angle = -M_PI; |
| 244 | while (true) { |
| 245 | if (ShouldCancel()) { |
| 246 | return false; |
| 247 | } |
| 248 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 249 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 250 | if (IsDriveDone()) { |
| 251 | return true; |
| 252 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 253 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 254 | if (drivetrain_status_fetcher_->ground_angle() > max_angle) { |
| 255 | max_angle = drivetrain_status_fetcher_->ground_angle(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 256 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | if (drivetrain_status_fetcher_->ground_angle() < max_angle - angle) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | bool BaseAutonomousActor::WaitForDriveNear(double distance, double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 265 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 266 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 267 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 268 | constexpr double kPositionTolerance = 0.02; |
| 269 | constexpr double kProfileTolerance = 0.001; |
| 270 | |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 271 | bool drive_has_been_close = false; |
| 272 | bool turn_has_been_close = false; |
| 273 | bool printed_first = false; |
| 274 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 275 | while (true) { |
| 276 | if (ShouldCancel()) { |
| 277 | return false; |
| 278 | } |
| 279 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 280 | drivetrain_status_fetcher_.Fetch(); |
| 281 | if (drivetrain_status_fetcher_.get()) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 282 | const double left_profile_error = |
| 283 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 284 | drivetrain_status_fetcher_->profiled_left_position_goal()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 285 | const double right_profile_error = |
| 286 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 287 | drivetrain_status_fetcher_->profiled_right_position_goal()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 288 | |
| 289 | const double left_error = |
| 290 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 291 | drivetrain_status_fetcher_->estimated_left_position()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 292 | const double right_error = |
| 293 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 294 | drivetrain_status_fetcher_->estimated_right_position()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 295 | |
| 296 | const double profile_distance_to_go = |
| 297 | (left_profile_error + right_profile_error) / 2.0; |
| 298 | const double profile_angle_to_go = |
| 299 | (right_profile_error - left_profile_error) / |
| 300 | (dt_config_.robot_radius * 2.0); |
| 301 | |
| 302 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 303 | const double angle_to_go = |
| 304 | (right_error - left_error) / (dt_config_.robot_radius * 2.0); |
| 305 | |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 306 | const bool drive_close = |
| 307 | ::std::abs(profile_distance_to_go) < distance + kProfileTolerance && |
| 308 | ::std::abs(distance_to_go) < distance + kPositionTolerance; |
| 309 | const bool turn_close = |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 310 | ::std::abs(profile_angle_to_go) < angle + kProfileTolerance && |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 311 | ::std::abs(angle_to_go) < angle + kPositionTolerance; |
| 312 | |
| 313 | drive_has_been_close |= drive_close; |
| 314 | turn_has_been_close |= turn_close; |
| 315 | if (drive_has_been_close && !turn_has_been_close && !printed_first) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 316 | AOS_LOG(INFO, "Drive finished first\n"); |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 317 | printed_first = true; |
| 318 | } else if (!drive_has_been_close && turn_has_been_close && |
| 319 | !printed_first) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 320 | AOS_LOG(INFO, "Turn finished first\n"); |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 321 | printed_first = true; |
| 322 | } |
| 323 | |
| 324 | if (drive_close && turn_close) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 325 | AOS_LOG(INFO, "Closer than %f < %f distance, %f < %f angle\n", |
| 326 | distance_to_go, distance, angle_to_go, angle); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 333 | bool BaseAutonomousActor::WaitForDriveProfileNear(double tolerance) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 334 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 335 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 336 | aos::common::actions::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 337 | while (true) { |
| 338 | if (ShouldCancel()) { |
| 339 | return false; |
| 340 | } |
| 341 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 342 | drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 343 | |
| 344 | const Eigen::Matrix<double, 7, 1> current_error = |
| 345 | (Eigen::Matrix<double, 7, 1>() |
| 346 | << initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 347 | drivetrain_status_fetcher_->profiled_left_position_goal(), |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 348 | 0.0, |
| 349 | initial_drivetrain_.right - |
| 350 | drivetrain_status_fetcher_->profiled_right_position_goal(), |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 351 | 0.0, 0.0, 0.0, 0.0) |
| 352 | .finished(); |
| 353 | const Eigen::Matrix<double, 2, 1> linear_error = |
| 354 | dt_config_.LeftRightToLinear(current_error); |
| 355 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 356 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 357 | if (::std::abs(linear_error(0)) < tolerance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 358 | AOS_LOG(INFO, "Finished drive\n"); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 365 | bool BaseAutonomousActor::WaitForDriveProfileDone() { |
| 366 | constexpr double kProfileTolerance = 0.001; |
| 367 | return WaitForDriveProfileNear(kProfileTolerance); |
| 368 | } |
| 369 | |
| 370 | bool BaseAutonomousActor::WaitForTurnProfileNear(double tolerance) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 371 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 372 | event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 373 | aos::common::actions::kLoopOffset); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 374 | while (true) { |
| 375 | if (ShouldCancel()) { |
| 376 | return false; |
| 377 | } |
| 378 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 379 | drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 380 | |
| 381 | const Eigen::Matrix<double, 7, 1> current_error = |
| 382 | (Eigen::Matrix<double, 7, 1>() |
| 383 | << initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 384 | drivetrain_status_fetcher_->profiled_left_position_goal(), |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 385 | 0.0, |
| 386 | initial_drivetrain_.right - |
| 387 | drivetrain_status_fetcher_->profiled_right_position_goal(), |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 388 | 0.0, 0.0, 0.0, 0.0) |
| 389 | .finished(); |
| 390 | const Eigen::Matrix<double, 2, 1> angular_error = |
| 391 | dt_config_.LeftRightToAngular(current_error); |
| 392 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 393 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 394 | if (::std::abs(angular_error(0)) < tolerance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 395 | AOS_LOG(INFO, "Finished turn\n"); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 396 | return true; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 402 | bool BaseAutonomousActor::WaitForTurnProfileDone() { |
| 403 | constexpr double kProfileTolerance = 0.001; |
| 404 | return WaitForTurnProfileNear(kProfileTolerance); |
| 405 | } |
| 406 | |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 407 | double BaseAutonomousActor::DriveDistanceLeft() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 408 | drivetrain_status_fetcher_.Fetch(); |
| 409 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 410 | const double left_error = |
| 411 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 412 | drivetrain_status_fetcher_->estimated_left_position()); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 413 | const double right_error = |
| 414 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 415 | drivetrain_status_fetcher_->estimated_right_position()); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 416 | |
| 417 | return (left_error + right_error) / 2.0; |
| 418 | } else { |
| 419 | return 0; |
| 420 | } |
| 421 | } |
| 422 | |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 423 | bool BaseAutonomousActor::SplineHandle::SplineDistanceRemaining( |
| 424 | double distance) { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 425 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
| 426 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get()) { |
James Kuszmaul | 04e8d6c | 2021-03-27 18:12:13 -0700 | [diff] [blame] | 427 | // Confirm that: |
| 428 | // (a) The spline has started executiong (is_executing remains true even |
| 429 | // when we reach the end of the spline). |
| 430 | // (b) The spline that we are executing is the correct one. |
| 431 | // (c) There is less than distance distance remaining. |
Austin Schuh | 034c34e | 2023-03-22 20:24:06 -0700 | [diff] [blame] | 432 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 433 | ->goal_spline_handle() != spline_handle_) { |
| 434 | // Never done if we aren't the active spline. |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 439 | ->is_executed()) { |
| 440 | return true; |
| 441 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 442 | return base_autonomous_actor_->drivetrain_status_fetcher_ |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 443 | ->trajectory_logging() |
| 444 | ->is_executing() && |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 445 | base_autonomous_actor_->drivetrain_status_fetcher_ |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 446 | ->trajectory_logging() |
| 447 | ->distance_remaining() < distance; |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 448 | } |
| 449 | return false; |
| 450 | } |
Austin Schuh | 034c34e | 2023-03-22 20:24:06 -0700 | [diff] [blame] | 451 | |
Austin Schuh | 1a84377 | 2023-04-09 22:18:05 -0700 | [diff] [blame] | 452 | bool BaseAutonomousActor::SplineHandle::SplineDistanceTraveled( |
| 453 | double distance) { |
| 454 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
| 455 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get()) { |
| 456 | // Confirm that: |
| 457 | // (a) The spline has started executiong (is_executing remains true even |
| 458 | // when we reach the end of the spline). |
| 459 | // (b) The spline that we are executing is the correct one. |
| 460 | // (c) There is less than distance distance remaining. |
| 461 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 462 | ->goal_spline_handle() != spline_handle_) { |
| 463 | // Never done if we aren't the active spline. |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 468 | ->is_executed()) { |
| 469 | return true; |
| 470 | } |
| 471 | return base_autonomous_actor_->drivetrain_status_fetcher_ |
| 472 | ->trajectory_logging() |
| 473 | ->is_executing() && |
| 474 | base_autonomous_actor_->drivetrain_status_fetcher_ |
| 475 | ->trajectory_logging() |
| 476 | ->distance_traveled() > distance; |
| 477 | } |
| 478 | return false; |
| 479 | } |
| 480 | |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 481 | bool BaseAutonomousActor::SplineHandle::WaitForSplineDistanceRemaining( |
| 482 | double distance) { |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 483 | ::aos::time::PhasedLoop phased_loop( |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 484 | frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 485 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 486 | aos::common::actions::kLoopOffset); |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 487 | while (true) { |
| 488 | if (base_autonomous_actor_->ShouldCancel()) { |
| 489 | return false; |
| 490 | } |
| 491 | phased_loop.SleepUntilNext(); |
| 492 | if (SplineDistanceRemaining(distance)) { |
| 493 | return true; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Austin Schuh | 1a84377 | 2023-04-09 22:18:05 -0700 | [diff] [blame] | 498 | bool BaseAutonomousActor::SplineHandle::WaitForSplineDistanceTraveled( |
| 499 | double distance) { |
| 500 | ::aos::time::PhasedLoop phased_loop( |
| 501 | frc971::controls::kLoopFrequency, |
| 502 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 503 | aos::common::actions::kLoopOffset); |
Austin Schuh | 1a84377 | 2023-04-09 22:18:05 -0700 | [diff] [blame] | 504 | while (true) { |
| 505 | if (base_autonomous_actor_->ShouldCancel()) { |
| 506 | return false; |
| 507 | } |
| 508 | phased_loop.SleepUntilNext(); |
| 509 | if (SplineDistanceTraveled(distance)) { |
| 510 | return true; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 515 | void BaseAutonomousActor::LineFollowAtVelocity( |
| 516 | double velocity, y2019::control_loops::drivetrain::SelectionHint hint) { |
| 517 | { |
| 518 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
| 519 | drivetrain::Goal::Builder goal_builder = |
| 520 | builder.MakeBuilder<drivetrain::Goal>(); |
| 521 | goal_builder.add_controller_type( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 522 | drivetrain::ControllerType::SPLINE_FOLLOWER); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 523 | // TODO(james): Currently the 4.0 is copied from the |
| 524 | // line_follow_drivetrain.cc, but it is somewhat year-specific, so we should |
| 525 | // factor it out in some way. |
| 526 | goal_builder.add_throttle(velocity / 4.0); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 527 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 528 | } |
| 529 | |
James Kuszmaul | 202e438 | 2023-03-05 14:56:55 -0800 | [diff] [blame] | 530 | if (target_selector_hint_sender_) { |
| 531 | // TODO(james): 2019? Seriously? |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 532 | auto builder = target_selector_hint_sender_.MakeBuilder(); |
| 533 | ::y2019::control_loops::drivetrain::TargetSelectorHint::Builder |
| 534 | target_hint_builder = builder.MakeBuilder< |
| 535 | ::y2019::control_loops::drivetrain::TargetSelectorHint>(); |
| 536 | |
| 537 | target_hint_builder.add_suggested_target(hint); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 538 | builder.CheckOk(builder.Send(target_hint_builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 539 | } |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 542 | BaseAutonomousActor::SplineHandle BaseAutonomousActor::PlanSpline( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 543 | std::function<flatbuffers::Offset<frc971::MultiSpline>( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 544 | aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder |
| 545 | *builder)> &&multispline_builder, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 546 | SplineDirection direction) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 547 | AOS_LOG(INFO, "Planning spline\n"); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 548 | |
| 549 | int32_t spline_handle = (++spline_handle_) | ((getpid() & 0xFFFF) << 15); |
| 550 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 551 | drivetrain_goal_fetcher_.Fetch(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 552 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 553 | auto builder = spline_goal_sender_.MakeBuilder(); |
James Kuszmaul | c864bcf | 2019-05-01 20:17:34 -0700 | [diff] [blame] | 554 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 555 | flatbuffers::Offset<frc971::MultiSpline> multispline_offset = |
| 556 | multispline_builder(&builder); |
| 557 | |
| 558 | drivetrain::SplineGoal::Builder spline_builder = |
| 559 | builder.MakeBuilder<drivetrain::SplineGoal>(); |
| 560 | spline_builder.add_spline_idx(spline_handle); |
| 561 | spline_builder.add_drive_spline_backwards(direction == |
| 562 | SplineDirection::kBackward); |
| 563 | spline_builder.add_spline(multispline_offset); |
| 564 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 565 | // Calculate the starting position and yaw. |
| 566 | Eigen::Vector3d start; |
| 567 | { |
| 568 | const frc971::MultiSpline *const spline = |
| 569 | flatbuffers::GetTemporaryPointer(*builder.fbb(), multispline_offset); |
| 570 | |
| 571 | start(0) = spline->spline_x()->Get(0); |
| 572 | start(1) = spline->spline_y()->Get(0); |
| 573 | |
| 574 | Eigen::Matrix<double, 2, 6> control_points; |
| 575 | for (size_t ii = 0; ii < 6; ++ii) { |
| 576 | control_points(0, ii) = spline->spline_x()->Get(ii); |
| 577 | control_points(1, ii) = spline->spline_y()->Get(ii); |
| 578 | } |
| 579 | |
| 580 | frc971::control_loops::drivetrain::Spline spline_object(control_points); |
| 581 | start(2) = spline_object.Theta(0); |
| 582 | if (direction == SplineDirection::kBackward) { |
| 583 | start(2) = aos::math::NormalizeAngle(start(2) + M_PI); |
| 584 | } |
| 585 | } |
| 586 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 587 | builder.CheckOk(builder.Send(spline_builder.Finish())); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 588 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 589 | return BaseAutonomousActor::SplineHandle(spline_handle, this, start); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | bool BaseAutonomousActor::SplineHandle::IsPlanned() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 593 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 594 | VLOG(1) << aos::FlatbufferToJson( |
| 595 | base_autonomous_actor_->drivetrain_status_fetcher_.get()); |
| 596 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 597 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get() && |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 598 | base_autonomous_actor_->drivetrain_status_fetcher_.get() |
| 599 | ->has_trajectory_logging() && |
| 600 | base_autonomous_actor_->drivetrain_status_fetcher_.get() |
| 601 | ->trajectory_logging() |
| 602 | ->has_available_splines()) { |
| 603 | const flatbuffers::Vector<int> *splines = |
| 604 | base_autonomous_actor_->drivetrain_status_fetcher_.get() |
| 605 | ->trajectory_logging() |
| 606 | ->available_splines(); |
| 607 | |
| 608 | return std::find(splines->begin(), splines->end(), spline_handle_) != |
| 609 | splines->end(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 610 | } |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | bool BaseAutonomousActor::SplineHandle::WaitForPlan() { |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 615 | ::aos::time::PhasedLoop phased_loop( |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 616 | frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 617 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 618 | aos::common::actions::kLoopOffset); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 619 | while (true) { |
| 620 | if (base_autonomous_actor_->ShouldCancel()) { |
| 621 | return false; |
| 622 | } |
| 623 | phased_loop.SleepUntilNext(); |
| 624 | if (IsPlanned()) { |
| 625 | return true; |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | void BaseAutonomousActor::SplineHandle::Start() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 631 | auto builder = base_autonomous_actor_->drivetrain_goal_sender_.MakeBuilder(); |
| 632 | drivetrain::Goal::Builder goal_builder = |
| 633 | builder.MakeBuilder<drivetrain::Goal>(); |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 634 | goal_builder.add_controller_type(drivetrain::ControllerType::SPLINE_FOLLOWER); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 635 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 636 | AOS_LOG(INFO, "Starting spline\n"); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 637 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 638 | goal_builder.add_spline_handle(spline_handle_); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 639 | base_autonomous_actor_->goal_spline_handle_ = spline_handle_; |
| 640 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 641 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | bool BaseAutonomousActor::SplineHandle::IsDone() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 645 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 646 | |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 647 | // We check that the spline we are waiting on is neither currently planning |
James Kuszmaul | 29e417d | 2019-04-13 10:03:35 -0700 | [diff] [blame] | 648 | // nor executing (we check is_executed because it is possible to receive |
| 649 | // status messages with is_executing false before the execution has started). |
| 650 | // We check for planning so that the user can go straight from starting the |
| 651 | // planner to executing without a WaitForPlan in between. |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 652 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get() && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 653 | ((!base_autonomous_actor_->drivetrain_status_fetcher_ |
| 654 | ->trajectory_logging() |
| 655 | ->is_executed() && |
| 656 | base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 657 | ->current_spline_idx() == spline_handle_) || |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 658 | IsPlanned())) { |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 659 | return false; |
| 660 | } |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | bool BaseAutonomousActor::SplineHandle::WaitForDone() { |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 665 | ::aos::time::PhasedLoop phased_loop( |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 666 | frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 667 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Stephan Pleines | 743f83a | 2024-02-02 18:32:09 -0800 | [diff] [blame] | 668 | aos::common::actions::kLoopOffset); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 669 | while (true) { |
| 670 | if (base_autonomous_actor_->ShouldCancel()) { |
| 671 | return false; |
| 672 | } |
| 673 | phased_loop.SleepUntilNext(); |
| 674 | if (IsDone()) { |
| 675 | return true; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 680 | } // namespace frc971::autonomous |