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_( |
James Kuszmaul | 202e438 | 2023-03-05 14:56:55 -0800 | [diff] [blame] | 31 | event_loop->TryMakeSender< |
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 | |
Austin Schuh | a5fefcd | 2023-03-22 20:23:42 -0700 | [diff] [blame] | 180 | double BaseAutonomousActor::X() { |
| 181 | drivetrain_status_fetcher_.Fetch(); |
| 182 | CHECK(drivetrain_status_fetcher_.get()); |
| 183 | return drivetrain_status_fetcher_->x(); |
| 184 | } |
| 185 | |
| 186 | double BaseAutonomousActor::Y() { |
| 187 | drivetrain_status_fetcher_.Fetch(); |
| 188 | CHECK(drivetrain_status_fetcher_.get()); |
| 189 | return drivetrain_status_fetcher_->y(); |
| 190 | } |
| 191 | |
| 192 | double BaseAutonomousActor::Theta() { |
| 193 | drivetrain_status_fetcher_.Fetch(); |
| 194 | CHECK(drivetrain_status_fetcher_.get()); |
| 195 | return drivetrain_status_fetcher_->theta(); |
| 196 | } |
| 197 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 198 | bool BaseAutonomousActor::WaitForAboveAngle(double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 199 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 200 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 201 | ActorBase::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 202 | while (true) { |
| 203 | if (ShouldCancel()) { |
| 204 | return false; |
| 205 | } |
| 206 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 207 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 208 | if (IsDriveDone()) { |
| 209 | return true; |
| 210 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 211 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 212 | if (drivetrain_status_fetcher_->ground_angle() > angle) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | bool BaseAutonomousActor::WaitForBelowAngle(double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 220 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 221 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 222 | ActorBase::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 223 | while (true) { |
| 224 | if (ShouldCancel()) { |
| 225 | return false; |
| 226 | } |
| 227 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 228 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 229 | if (IsDriveDone()) { |
| 230 | return true; |
| 231 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 232 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 233 | if (drivetrain_status_fetcher_->ground_angle() < angle) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | bool BaseAutonomousActor::WaitForMaxBy(double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 241 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 242 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 243 | ActorBase::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 244 | double max_angle = -M_PI; |
| 245 | while (true) { |
| 246 | if (ShouldCancel()) { |
| 247 | return false; |
| 248 | } |
| 249 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 250 | drivetrain_status_fetcher_.Fetch(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 251 | if (IsDriveDone()) { |
| 252 | return true; |
| 253 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 254 | if (drivetrain_status_fetcher_.get()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 255 | if (drivetrain_status_fetcher_->ground_angle() > max_angle) { |
| 256 | max_angle = drivetrain_status_fetcher_->ground_angle(); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 257 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 258 | if (drivetrain_status_fetcher_->ground_angle() < max_angle - angle) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | bool BaseAutonomousActor::WaitForDriveNear(double distance, double angle) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 266 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 267 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 268 | ActorBase::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 269 | constexpr double kPositionTolerance = 0.02; |
| 270 | constexpr double kProfileTolerance = 0.001; |
| 271 | |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 272 | bool drive_has_been_close = false; |
| 273 | bool turn_has_been_close = false; |
| 274 | bool printed_first = false; |
| 275 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 276 | while (true) { |
| 277 | if (ShouldCancel()) { |
| 278 | return false; |
| 279 | } |
| 280 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 281 | drivetrain_status_fetcher_.Fetch(); |
| 282 | if (drivetrain_status_fetcher_.get()) { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 283 | const double left_profile_error = |
| 284 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 285 | drivetrain_status_fetcher_->profiled_left_position_goal()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 286 | const double right_profile_error = |
| 287 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 288 | drivetrain_status_fetcher_->profiled_right_position_goal()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 289 | |
| 290 | const double left_error = |
| 291 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 292 | drivetrain_status_fetcher_->estimated_left_position()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 293 | const double right_error = |
| 294 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 295 | drivetrain_status_fetcher_->estimated_right_position()); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 296 | |
| 297 | const double profile_distance_to_go = |
| 298 | (left_profile_error + right_profile_error) / 2.0; |
| 299 | const double profile_angle_to_go = |
| 300 | (right_profile_error - left_profile_error) / |
| 301 | (dt_config_.robot_radius * 2.0); |
| 302 | |
| 303 | const double distance_to_go = (left_error + right_error) / 2.0; |
| 304 | const double angle_to_go = |
| 305 | (right_error - left_error) / (dt_config_.robot_radius * 2.0); |
| 306 | |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 307 | const bool drive_close = |
| 308 | ::std::abs(profile_distance_to_go) < distance + kProfileTolerance && |
| 309 | ::std::abs(distance_to_go) < distance + kPositionTolerance; |
| 310 | const bool turn_close = |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 311 | ::std::abs(profile_angle_to_go) < angle + kProfileTolerance && |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 312 | ::std::abs(angle_to_go) < angle + kPositionTolerance; |
| 313 | |
| 314 | drive_has_been_close |= drive_close; |
| 315 | turn_has_been_close |= turn_close; |
| 316 | if (drive_has_been_close && !turn_has_been_close && !printed_first) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 317 | AOS_LOG(INFO, "Drive finished first\n"); |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 318 | printed_first = true; |
| 319 | } else if (!drive_has_been_close && turn_has_been_close && |
| 320 | !printed_first) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 321 | AOS_LOG(INFO, "Turn finished first\n"); |
Austin Schuh | e6af999 | 2018-07-08 15:59:14 -0700 | [diff] [blame] | 322 | printed_first = true; |
| 323 | } |
| 324 | |
| 325 | if (drive_close && turn_close) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 326 | AOS_LOG(INFO, "Closer than %f < %f distance, %f < %f angle\n", |
| 327 | distance_to_go, distance, angle_to_go, angle); |
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::WaitForDriveProfileNear(double tolerance) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 335 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 336 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 337 | ActorBase::kLoopOffset); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 338 | while (true) { |
| 339 | if (ShouldCancel()) { |
| 340 | return false; |
| 341 | } |
| 342 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 343 | drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 344 | |
| 345 | const Eigen::Matrix<double, 7, 1> current_error = |
| 346 | (Eigen::Matrix<double, 7, 1>() |
| 347 | << initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 348 | drivetrain_status_fetcher_->profiled_left_position_goal(), |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 349 | 0.0, |
| 350 | initial_drivetrain_.right - |
| 351 | drivetrain_status_fetcher_->profiled_right_position_goal(), |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 352 | 0.0, 0.0, 0.0, 0.0) |
| 353 | .finished(); |
| 354 | const Eigen::Matrix<double, 2, 1> linear_error = |
| 355 | dt_config_.LeftRightToLinear(current_error); |
| 356 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 357 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 358 | if (::std::abs(linear_error(0)) < tolerance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 359 | AOS_LOG(INFO, "Finished drive\n"); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 366 | bool BaseAutonomousActor::WaitForDriveProfileDone() { |
| 367 | constexpr double kProfileTolerance = 0.001; |
| 368 | return WaitForDriveProfileNear(kProfileTolerance); |
| 369 | } |
| 370 | |
| 371 | bool BaseAutonomousActor::WaitForTurnProfileNear(double tolerance) { |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 372 | ::aos::time::PhasedLoop phased_loop(frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 373 | event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 374 | ActorBase::kLoopOffset); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 375 | while (true) { |
| 376 | if (ShouldCancel()) { |
| 377 | return false; |
| 378 | } |
| 379 | phased_loop.SleepUntilNext(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 380 | drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 381 | |
| 382 | const Eigen::Matrix<double, 7, 1> current_error = |
| 383 | (Eigen::Matrix<double, 7, 1>() |
| 384 | << initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 385 | drivetrain_status_fetcher_->profiled_left_position_goal(), |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 386 | 0.0, |
| 387 | initial_drivetrain_.right - |
| 388 | drivetrain_status_fetcher_->profiled_right_position_goal(), |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 389 | 0.0, 0.0, 0.0, 0.0) |
| 390 | .finished(); |
| 391 | const Eigen::Matrix<double, 2, 1> angular_error = |
| 392 | dt_config_.LeftRightToAngular(current_error); |
| 393 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 394 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 395 | if (::std::abs(angular_error(0)) < tolerance) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 396 | AOS_LOG(INFO, "Finished turn\n"); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 403 | bool BaseAutonomousActor::WaitForTurnProfileDone() { |
| 404 | constexpr double kProfileTolerance = 0.001; |
| 405 | return WaitForTurnProfileNear(kProfileTolerance); |
| 406 | } |
| 407 | |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 408 | double BaseAutonomousActor::DriveDistanceLeft() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 409 | drivetrain_status_fetcher_.Fetch(); |
| 410 | if (drivetrain_status_fetcher_.get()) { |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 411 | const double left_error = |
| 412 | (initial_drivetrain_.left - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | drivetrain_status_fetcher_->estimated_left_position()); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 414 | const double right_error = |
| 415 | (initial_drivetrain_.right - |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 416 | drivetrain_status_fetcher_->estimated_right_position()); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 417 | |
| 418 | return (left_error + right_error) / 2.0; |
| 419 | } else { |
| 420 | return 0; |
| 421 | } |
| 422 | } |
| 423 | |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 424 | bool BaseAutonomousActor::SplineHandle::SplineDistanceRemaining( |
| 425 | double distance) { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 426 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
| 427 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get()) { |
James Kuszmaul | 04e8d6c | 2021-03-27 18:12:13 -0700 | [diff] [blame] | 428 | // Confirm that: |
| 429 | // (a) The spline has started executiong (is_executing remains true even |
| 430 | // when we reach the end of the spline). |
| 431 | // (b) The spline that we are executing is the correct one. |
| 432 | // (c) There is less than distance distance remaining. |
Austin Schuh | 034c34e | 2023-03-22 20:24:06 -0700 | [diff] [blame] | 433 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 434 | ->goal_spline_handle() != spline_handle_) { |
| 435 | // Never done if we aren't the active spline. |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 440 | ->is_executed()) { |
| 441 | return true; |
| 442 | } |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 443 | return base_autonomous_actor_->drivetrain_status_fetcher_ |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 444 | ->trajectory_logging() |
| 445 | ->is_executing() && |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 446 | base_autonomous_actor_->drivetrain_status_fetcher_ |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 447 | ->trajectory_logging() |
| 448 | ->distance_remaining() < distance; |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 449 | } |
| 450 | return false; |
| 451 | } |
Austin Schuh | 034c34e | 2023-03-22 20:24:06 -0700 | [diff] [blame] | 452 | |
Austin Schuh | 1a84377 | 2023-04-09 22:18:05 -0700 | [diff] [blame] | 453 | bool BaseAutonomousActor::SplineHandle::SplineDistanceTraveled( |
| 454 | double distance) { |
| 455 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
| 456 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get()) { |
| 457 | // Confirm that: |
| 458 | // (a) The spline has started executiong (is_executing remains true even |
| 459 | // when we reach the end of the spline). |
| 460 | // (b) The spline that we are executing is the correct one. |
| 461 | // (c) There is less than distance distance remaining. |
| 462 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 463 | ->goal_spline_handle() != spline_handle_) { |
| 464 | // Never done if we aren't the active spline. |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | if (base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 469 | ->is_executed()) { |
| 470 | return true; |
| 471 | } |
| 472 | return base_autonomous_actor_->drivetrain_status_fetcher_ |
| 473 | ->trajectory_logging() |
| 474 | ->is_executing() && |
| 475 | base_autonomous_actor_->drivetrain_status_fetcher_ |
| 476 | ->trajectory_logging() |
| 477 | ->distance_traveled() > distance; |
| 478 | } |
| 479 | return false; |
| 480 | } |
| 481 | |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 482 | bool BaseAutonomousActor::SplineHandle::WaitForSplineDistanceRemaining( |
| 483 | double distance) { |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 484 | ::aos::time::PhasedLoop phased_loop( |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 485 | frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 486 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 487 | ActorBase::kLoopOffset); |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 488 | while (true) { |
| 489 | if (base_autonomous_actor_->ShouldCancel()) { |
| 490 | return false; |
| 491 | } |
| 492 | phased_loop.SleepUntilNext(); |
| 493 | if (SplineDistanceRemaining(distance)) { |
| 494 | return true; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
Austin Schuh | 1a84377 | 2023-04-09 22:18:05 -0700 | [diff] [blame] | 499 | bool BaseAutonomousActor::SplineHandle::WaitForSplineDistanceTraveled( |
| 500 | double distance) { |
| 501 | ::aos::time::PhasedLoop phased_loop( |
| 502 | frc971::controls::kLoopFrequency, |
| 503 | base_autonomous_actor_->event_loop()->monotonic_now(), |
| 504 | ActorBase::kLoopOffset); |
| 505 | while (true) { |
| 506 | if (base_autonomous_actor_->ShouldCancel()) { |
| 507 | return false; |
| 508 | } |
| 509 | phased_loop.SleepUntilNext(); |
| 510 | if (SplineDistanceTraveled(distance)) { |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 516 | void BaseAutonomousActor::LineFollowAtVelocity( |
| 517 | double velocity, y2019::control_loops::drivetrain::SelectionHint hint) { |
| 518 | { |
| 519 | auto builder = drivetrain_goal_sender_.MakeBuilder(); |
| 520 | drivetrain::Goal::Builder goal_builder = |
| 521 | builder.MakeBuilder<drivetrain::Goal>(); |
| 522 | goal_builder.add_controller_type( |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 523 | drivetrain::ControllerType::SPLINE_FOLLOWER); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 524 | // TODO(james): Currently the 4.0 is copied from the |
| 525 | // line_follow_drivetrain.cc, but it is somewhat year-specific, so we should |
| 526 | // factor it out in some way. |
| 527 | goal_builder.add_throttle(velocity / 4.0); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 528 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 529 | } |
| 530 | |
James Kuszmaul | 202e438 | 2023-03-05 14:56:55 -0800 | [diff] [blame] | 531 | if (target_selector_hint_sender_) { |
| 532 | // TODO(james): 2019? Seriously? |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 533 | auto builder = target_selector_hint_sender_.MakeBuilder(); |
| 534 | ::y2019::control_loops::drivetrain::TargetSelectorHint::Builder |
| 535 | target_hint_builder = builder.MakeBuilder< |
| 536 | ::y2019::control_loops::drivetrain::TargetSelectorHint>(); |
| 537 | |
| 538 | target_hint_builder.add_suggested_target(hint); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 539 | builder.CheckOk(builder.Send(target_hint_builder.Finish())); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 540 | } |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 543 | BaseAutonomousActor::SplineHandle BaseAutonomousActor::PlanSpline( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 544 | std::function<flatbuffers::Offset<frc971::MultiSpline>( |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 545 | aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder |
| 546 | *builder)> &&multispline_builder, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | SplineDirection direction) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 548 | AOS_LOG(INFO, "Planning spline\n"); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 549 | |
| 550 | int32_t spline_handle = (++spline_handle_) | ((getpid() & 0xFFFF) << 15); |
| 551 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 552 | drivetrain_goal_fetcher_.Fetch(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 553 | |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 554 | auto builder = spline_goal_sender_.MakeBuilder(); |
James Kuszmaul | c864bcf | 2019-05-01 20:17:34 -0700 | [diff] [blame] | 555 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 556 | flatbuffers::Offset<frc971::MultiSpline> multispline_offset = |
| 557 | multispline_builder(&builder); |
| 558 | |
| 559 | drivetrain::SplineGoal::Builder spline_builder = |
| 560 | builder.MakeBuilder<drivetrain::SplineGoal>(); |
| 561 | spline_builder.add_spline_idx(spline_handle); |
| 562 | spline_builder.add_drive_spline_backwards(direction == |
| 563 | SplineDirection::kBackward); |
| 564 | spline_builder.add_spline(multispline_offset); |
| 565 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 566 | // Calculate the starting position and yaw. |
| 567 | Eigen::Vector3d start; |
| 568 | { |
| 569 | const frc971::MultiSpline *const spline = |
| 570 | flatbuffers::GetTemporaryPointer(*builder.fbb(), multispline_offset); |
| 571 | |
| 572 | start(0) = spline->spline_x()->Get(0); |
| 573 | start(1) = spline->spline_y()->Get(0); |
| 574 | |
| 575 | Eigen::Matrix<double, 2, 6> control_points; |
| 576 | for (size_t ii = 0; ii < 6; ++ii) { |
| 577 | control_points(0, ii) = spline->spline_x()->Get(ii); |
| 578 | control_points(1, ii) = spline->spline_y()->Get(ii); |
| 579 | } |
| 580 | |
| 581 | frc971::control_loops::drivetrain::Spline spline_object(control_points); |
| 582 | start(2) = spline_object.Theta(0); |
| 583 | if (direction == SplineDirection::kBackward) { |
| 584 | start(2) = aos::math::NormalizeAngle(start(2) + M_PI); |
| 585 | } |
| 586 | } |
| 587 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 588 | builder.CheckOk(builder.Send(spline_builder.Finish())); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 589 | |
James Kuszmaul | 99af8b5 | 2021-03-28 10:50:15 -0700 | [diff] [blame] | 590 | return BaseAutonomousActor::SplineHandle(spline_handle, this, start); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | bool BaseAutonomousActor::SplineHandle::IsPlanned() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 594 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 595 | VLOG(1) << aos::FlatbufferToJson( |
| 596 | base_autonomous_actor_->drivetrain_status_fetcher_.get()); |
| 597 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 598 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get() && |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 599 | base_autonomous_actor_->drivetrain_status_fetcher_.get() |
| 600 | ->has_trajectory_logging() && |
| 601 | base_autonomous_actor_->drivetrain_status_fetcher_.get() |
| 602 | ->trajectory_logging() |
| 603 | ->has_available_splines()) { |
| 604 | const flatbuffers::Vector<int> *splines = |
| 605 | base_autonomous_actor_->drivetrain_status_fetcher_.get() |
| 606 | ->trajectory_logging() |
| 607 | ->available_splines(); |
| 608 | |
| 609 | return std::find(splines->begin(), splines->end(), spline_handle_) != |
| 610 | splines->end(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 611 | } |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | bool BaseAutonomousActor::SplineHandle::WaitForPlan() { |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 616 | ::aos::time::PhasedLoop phased_loop( |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 617 | frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 618 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 619 | ActorBase::kLoopOffset); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 620 | while (true) { |
| 621 | if (base_autonomous_actor_->ShouldCancel()) { |
| 622 | return false; |
| 623 | } |
| 624 | phased_loop.SleepUntilNext(); |
| 625 | if (IsPlanned()) { |
| 626 | return true; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | void BaseAutonomousActor::SplineHandle::Start() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 632 | auto builder = base_autonomous_actor_->drivetrain_goal_sender_.MakeBuilder(); |
| 633 | drivetrain::Goal::Builder goal_builder = |
| 634 | builder.MakeBuilder<drivetrain::Goal>(); |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 635 | goal_builder.add_controller_type(drivetrain::ControllerType::SPLINE_FOLLOWER); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 636 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 637 | AOS_LOG(INFO, "Starting spline\n"); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 638 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 639 | goal_builder.add_spline_handle(spline_handle_); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 640 | base_autonomous_actor_->goal_spline_handle_ = spline_handle_; |
| 641 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 642 | builder.CheckOk(builder.Send(goal_builder.Finish())); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | bool BaseAutonomousActor::SplineHandle::IsDone() { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 646 | base_autonomous_actor_->drivetrain_status_fetcher_.Fetch(); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 647 | |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 648 | // 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] | 649 | // nor executing (we check is_executed because it is possible to receive |
| 650 | // status messages with is_executing false before the execution has started). |
| 651 | // We check for planning so that the user can go straight from starting the |
| 652 | // planner to executing without a WaitForPlan in between. |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 653 | if (base_autonomous_actor_->drivetrain_status_fetcher_.get() && |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 654 | ((!base_autonomous_actor_->drivetrain_status_fetcher_ |
| 655 | ->trajectory_logging() |
| 656 | ->is_executed() && |
| 657 | base_autonomous_actor_->drivetrain_status_fetcher_->trajectory_logging() |
| 658 | ->current_spline_idx() == spline_handle_) || |
James Kuszmaul | 75a18c5 | 2021-03-10 22:02:07 -0800 | [diff] [blame] | 659 | IsPlanned())) { |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 660 | return false; |
| 661 | } |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | bool BaseAutonomousActor::SplineHandle::WaitForDone() { |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 666 | ::aos::time::PhasedLoop phased_loop( |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 667 | frc971::controls::kLoopFrequency, |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 668 | base_autonomous_actor_->event_loop()->monotonic_now(), |
Yash Chainani | a6fe97b | 2021-12-15 21:01:11 -0800 | [diff] [blame] | 669 | ActorBase::kLoopOffset); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 670 | while (true) { |
| 671 | if (base_autonomous_actor_->ShouldCancel()) { |
| 672 | return false; |
| 673 | } |
| 674 | phased_loop.SleepUntilNext(); |
| 675 | if (IsDone()) { |
| 676 | return true; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 681 | } // namespace autonomous |
| 682 | } // namespace frc971 |