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