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