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