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