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