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