Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 1 | #ifndef FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_ |
| 2 | #define FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_ |
| 3 | |
| 4 | #include <memory> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/actions/actions.h" |
| 7 | #include "aos/actions/actor.h" |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 8 | #include "frc971/autonomous/auto.q.h" |
| 9 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 10 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
| 11 | |
| 12 | namespace frc971 { |
| 13 | namespace autonomous { |
| 14 | |
| 15 | class BaseAutonomousActor |
| 16 | : public ::aos::common::actions::ActorBase<AutonomousActionQueueGroup> { |
| 17 | public: |
| 18 | explicit BaseAutonomousActor( |
| 19 | AutonomousActionQueueGroup *s, |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 20 | const control_loops::drivetrain::DrivetrainConfig<double> &dt_config); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 21 | |
| 22 | protected: |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 23 | class SplineHandle { |
| 24 | public: |
| 25 | bool IsPlanned(); |
| 26 | bool WaitForPlan(); |
| 27 | void Start(); |
| 28 | |
| 29 | bool IsDone(); |
| 30 | bool WaitForDone(); |
| 31 | |
| 32 | // Wait for done, wait until X from the end, wait for distance from the end |
| 33 | |
| 34 | private: |
| 35 | friend BaseAutonomousActor; |
| 36 | SplineHandle(int32_t spline_handle, |
| 37 | BaseAutonomousActor *base_autonomous_actor) |
| 38 | : spline_handle_(spline_handle), |
| 39 | base_autonomous_actor_(base_autonomous_actor) {} |
| 40 | |
| 41 | int32_t spline_handle_; |
| 42 | BaseAutonomousActor *base_autonomous_actor_; |
| 43 | }; |
| 44 | |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame^] | 45 | // Represents the direction that we will drive along a spline. |
| 46 | enum class SplineDirection { |
| 47 | kForward, |
| 48 | kBackward, |
| 49 | }; |
| 50 | |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 51 | // Starts planning the spline, and returns a handle to be used to manipulate |
| 52 | // it. |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame^] | 53 | SplineHandle PlanSpline(const ::frc971::MultiSpline &spline, |
| 54 | SplineDirection direction); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 55 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 56 | void ResetDrivetrain(); |
| 57 | void InitializeEncoders(); |
| 58 | void StartDrive(double distance, double angle, ProfileParameters linear, |
| 59 | ProfileParameters angular); |
| 60 | |
| 61 | void WaitUntilDoneOrCanceled( |
| 62 | ::std::unique_ptr<aos::common::actions::Action> action); |
| 63 | // Waits for the drive motion to finish. Returns true if it succeeded, and |
| 64 | // false if it cancels. |
| 65 | bool WaitForDriveDone(); |
| 66 | |
| 67 | // Returns true if the drive has finished. |
| 68 | bool IsDriveDone(); |
| 69 | |
| 70 | // Waits until the robot is pitched up above the specified angle, or the move |
| 71 | // finishes. Returns true on success, and false if it cancels. |
| 72 | bool WaitForAboveAngle(double angle); |
| 73 | bool WaitForBelowAngle(double angle); |
| 74 | bool WaitForMaxBy(double angle); |
| 75 | |
| 76 | // Waits until the profile and distance is within distance and angle of the |
| 77 | // goal. Returns true on success, and false when canceled. |
| 78 | bool WaitForDriveNear(double distance, double angle); |
| 79 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 80 | bool WaitForDriveProfileNear(double tolerance); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 81 | bool WaitForDriveProfileDone(); |
| 82 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 83 | bool WaitForTurnProfileNear(double tolerance); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 84 | bool WaitForTurnProfileDone(); |
| 85 | |
| 86 | // Returns the distance left to go. |
| 87 | double DriveDistanceLeft(); |
| 88 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 89 | const control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 90 | |
| 91 | // Initial drivetrain positions. |
| 92 | struct InitialDrivetrain { |
| 93 | double left; |
| 94 | double right; |
| 95 | }; |
| 96 | InitialDrivetrain initial_drivetrain_; |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 97 | |
| 98 | void set_max_drivetrain_voltage(double max_drivetrain_voltage) { |
| 99 | max_drivetrain_voltage_ = max_drivetrain_voltage; |
| 100 | } |
| 101 | |
| 102 | private: |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 103 | friend class SplineHandle; |
| 104 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 105 | double max_drivetrain_voltage_ = 12.0; |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 106 | |
| 107 | // Unique counter so we get unique spline handles. |
| 108 | int spline_handle_ = 0; |
| 109 | // Last goal spline handle; |
| 110 | int32_t goal_spline_handle_ = 0; |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | using AutonomousAction = |
| 114 | ::aos::common::actions::TypedAction<AutonomousActionQueueGroup>; |
| 115 | |
| 116 | // Makes a new AutonomousActor action. |
| 117 | ::std::unique_ptr<AutonomousAction> MakeAutonomousAction( |
| 118 | const AutonomousActionParams ¶ms); |
| 119 | |
| 120 | } // namespace autonomous |
| 121 | } // namespace frc971 |
| 122 | |
| 123 | #endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_ |