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