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" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/events/shm_event_loop.h" |
| 9 | #include "frc971/autonomous/auto_generated.h" |
| 10 | #include "frc971/control_loops/control_loops_generated.h" |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 11 | #include "frc971/control_loops/drivetrain/drivetrain_config.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "frc971/control_loops/drivetrain/drivetrain_goal_generated.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
| 14 | #include "y2019/control_loops/drivetrain/target_selector_generated.h" |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 15 | |
| 16 | namespace frc971 { |
| 17 | namespace autonomous { |
| 18 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | class BaseAutonomousActor : public ::aos::common::actions::ActorBase<Goal> { |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 20 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | typedef ::aos::common::actions::TypedActionFactory<Goal> Factory; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 22 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 23 | explicit 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); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 26 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 27 | static Factory MakeFactory(::aos::EventLoop *event_loop) { |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame] | 28 | return Factory(event_loop, "/autonomous"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 31 | protected: |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 32 | class SplineHandle { |
| 33 | public: |
| 34 | bool IsPlanned(); |
| 35 | bool WaitForPlan(); |
| 36 | void Start(); |
| 37 | |
| 38 | bool IsDone(); |
| 39 | bool WaitForDone(); |
| 40 | |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 41 | // Whether there is less than a certain distance, in meters, remaining in |
| 42 | // the current spline. |
| 43 | bool SplineDistanceRemaining(double distance); |
| 44 | bool WaitForSplineDistanceRemaining(double distance); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | friend BaseAutonomousActor; |
| 48 | SplineHandle(int32_t spline_handle, |
| 49 | BaseAutonomousActor *base_autonomous_actor) |
| 50 | : spline_handle_(spline_handle), |
| 51 | base_autonomous_actor_(base_autonomous_actor) {} |
| 52 | |
| 53 | int32_t spline_handle_; |
| 54 | BaseAutonomousActor *base_autonomous_actor_; |
| 55 | }; |
| 56 | |
James Kuszmaul | c73bb22 | 2019-04-07 12:15:35 -0700 | [diff] [blame] | 57 | // Represents the direction that we will drive along a spline. |
| 58 | enum class SplineDirection { |
| 59 | kForward, |
| 60 | kBackward, |
| 61 | }; |
| 62 | |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 63 | // Starts planning the spline, and returns a handle to be used to manipulate |
| 64 | // it. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | SplineHandle PlanSpline( |
| 66 | std::function<flatbuffers::Offset<frc971::MultiSpline>( |
| 67 | aos::Sender<frc971::control_loops::drivetrain::Goal>::Builder |
| 68 | *builder)> &&multispline_builder, |
| 69 | SplineDirection direction); |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 70 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 71 | void ResetDrivetrain(); |
| 72 | void InitializeEncoders(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 73 | void StartDrive(double distance, double angle, ProfileParametersT linear, |
| 74 | ProfileParametersT angular); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 75 | |
| 76 | void WaitUntilDoneOrCanceled( |
| 77 | ::std::unique_ptr<aos::common::actions::Action> action); |
| 78 | // Waits for the drive motion to finish. Returns true if it succeeded, and |
| 79 | // false if it cancels. |
| 80 | bool WaitForDriveDone(); |
| 81 | |
| 82 | // Returns true if the drive has finished. |
| 83 | bool IsDriveDone(); |
| 84 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | void LineFollowAtVelocity( |
| 86 | double velocity, |
| 87 | y2019::control_loops::drivetrain::SelectionHint hint = |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 88 | y2019::control_loops::drivetrain::SelectionHint::NONE); |
James Kuszmaul | 838040b | 2019-04-13 15:51:02 -0700 | [diff] [blame] | 89 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 90 | // Waits until the robot is pitched up above the specified angle, or the move |
| 91 | // finishes. Returns true on success, and false if it cancels. |
| 92 | bool WaitForAboveAngle(double angle); |
| 93 | bool WaitForBelowAngle(double angle); |
| 94 | bool WaitForMaxBy(double angle); |
| 95 | |
| 96 | // Waits until the profile and distance is within distance and angle of the |
| 97 | // goal. Returns true on success, and false when canceled. |
| 98 | bool WaitForDriveNear(double distance, double angle); |
| 99 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 100 | bool WaitForDriveProfileNear(double tolerance); |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 101 | bool WaitForDriveProfileDone(); |
| 102 | |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 103 | bool WaitForTurnProfileNear(double tolerance); |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 104 | bool WaitForTurnProfileDone(); |
| 105 | |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 106 | void set_max_drivetrain_voltage(double max_drivetrain_voltage) { |
| 107 | max_drivetrain_voltage_ = max_drivetrain_voltage; |
| 108 | } |
| 109 | |
Austin Schuh | 546a038 | 2017-04-16 19:10:18 -0700 | [diff] [blame] | 110 | // Returns the distance left to go. |
| 111 | double DriveDistanceLeft(); |
| 112 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 113 | const control_loops::drivetrain::DrivetrainConfig<double> dt_config_; |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 114 | |
| 115 | // Initial drivetrain positions. |
| 116 | struct InitialDrivetrain { |
| 117 | double left; |
| 118 | double right; |
| 119 | }; |
| 120 | InitialDrivetrain initial_drivetrain_; |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 121 | |
Austin Schuh | c087b10 | 2019-05-12 15:33:12 -0700 | [diff] [blame] | 122 | ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint> |
| 123 | target_selector_hint_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 124 | ::aos::Sender<::frc971::control_loops::drivetrain::Goal> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 125 | drivetrain_goal_sender_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 126 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Status> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 127 | drivetrain_status_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Goal> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 129 | drivetrain_goal_fetcher_; |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 130 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 131 | private: |
| 132 | friend class SplineHandle; |
Austin Schuh | 0aae924 | 2018-03-14 19:49:44 -0700 | [diff] [blame] | 133 | double max_drivetrain_voltage_ = 12.0; |
Austin Schuh | 6bcc230 | 2019-03-23 22:28:06 -0700 | [diff] [blame] | 134 | |
| 135 | // Unique counter so we get unique spline handles. |
| 136 | int spline_handle_ = 0; |
| 137 | // Last goal spline handle; |
| 138 | int32_t goal_spline_handle_ = 0; |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Philipp Schrader | 4bd29b1 | 2017-02-22 04:42:27 +0000 | [diff] [blame] | 141 | } // namespace autonomous |
| 142 | } // namespace frc971 |
| 143 | |
| 144 | #endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_ |