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