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