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