blob: 15c88d4c2063548e16a99670667f0fd55515314d [file] [log] [blame]
Philipp Schrader4bd29b12017-02-22 04:42:27 +00001#ifndef FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_
2#define FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_
3
4#include <memory>
5
John Park33858a32018-09-28 23:05:48 -07006#include "aos/actions/actions.h"
7#include "aos/actions/actor.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/events/shm_event_loop.h"
9#include "frc971/autonomous/auto_generated.h"
10#include "frc971/control_loops/control_loops_generated.h"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000011#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#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 Schrader4bd29b12017-02-22 04:42:27 +000015
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080016namespace frc971::autonomous {
Philipp Schrader4bd29b12017-02-22 04:42:27 +000017
Alex Perrycb7da4b2019-08-28 19:35:56 -070018class BaseAutonomousActor : public ::aos::common::actions::ActorBase<Goal> {
Philipp Schrader4bd29b12017-02-22 04:42:27 +000019 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 typedef ::aos::common::actions::TypedActionFactory<Goal> Factory;
Austin Schuh1bf8a212019-05-26 22:13:14 -070021
Philipp Schrader4bd29b12017-02-22 04:42:27 +000022 explicit BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070023 ::aos::EventLoop *event_loop,
Austin Schuhbcce26a2018-03-26 23:41:24 -070024 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000025
Austin Schuh1bf8a212019-05-26 22:13:14 -070026 static Factory MakeFactory(::aos::EventLoop *event_loop) {
Austin Schuhed5b26d2019-12-05 20:51:59 -080027 return Factory(event_loop, "/autonomous");
Austin Schuh1bf8a212019-05-26 22:13:14 -070028 }
29
Philipp Schrader4bd29b12017-02-22 04:42:27 +000030 protected:
Austin Schuh6bcc2302019-03-23 22:28:06 -070031 class SplineHandle {
32 public:
33 bool IsPlanned();
34 bool WaitForPlan();
35 void Start();
36
37 bool IsDone();
38 bool WaitForDone();
39
James Kuszmaul838040b2019-04-13 15:51:02 -070040 // Whether there is less than a certain distance, in meters, remaining in
41 // the current spline.
42 bool SplineDistanceRemaining(double distance);
Austin Schuh1a843772023-04-09 22:18:05 -070043 bool SplineDistanceTraveled(double distance);
James Kuszmaul838040b2019-04-13 15:51:02 -070044 bool WaitForSplineDistanceRemaining(double distance);
Austin Schuh1a843772023-04-09 22:18:05 -070045 bool WaitForSplineDistanceTraveled(double distance);
Austin Schuh6bcc2302019-03-23 22:28:06 -070046
James Kuszmaul99af8b52021-03-28 10:50:15 -070047 // Returns [x, y, theta] position of the start.
48 const Eigen::Vector3d &starting_position() const { return spline_start_; }
49
Austin Schuh6bcc2302019-03-23 22:28:06 -070050 private:
51 friend BaseAutonomousActor;
52 SplineHandle(int32_t spline_handle,
James Kuszmaul99af8b52021-03-28 10:50:15 -070053 BaseAutonomousActor *base_autonomous_actor,
54 const Eigen::Vector3d &start)
Austin Schuh6bcc2302019-03-23 22:28:06 -070055 : spline_handle_(spline_handle),
James Kuszmaul99af8b52021-03-28 10:50:15 -070056 base_autonomous_actor_(base_autonomous_actor),
57 spline_start_(start) {}
Austin Schuh6bcc2302019-03-23 22:28:06 -070058
59 int32_t spline_handle_;
60 BaseAutonomousActor *base_autonomous_actor_;
James Kuszmaul99af8b52021-03-28 10:50:15 -070061
62 // Starting [x, y, theta] position of the spline.
63 Eigen::Vector3d spline_start_;
Austin Schuh6bcc2302019-03-23 22:28:06 -070064 };
65
James Kuszmaulc73bb222019-04-07 12:15:35 -070066 // Represents the direction that we will drive along a spline.
67 enum class SplineDirection {
68 kForward,
69 kBackward,
70 };
71
Austin Schuh6bcc2302019-03-23 22:28:06 -070072 // Starts planning the spline, and returns a handle to be used to manipulate
73 // it.
Alex Perrycb7da4b2019-08-28 19:35:56 -070074 SplineHandle PlanSpline(
75 std::function<flatbuffers::Offset<frc971::MultiSpline>(
James Kuszmaul75a18c52021-03-10 22:02:07 -080076 aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 *builder)> &&multispline_builder,
78 SplineDirection direction);
Austin Schuh6bcc2302019-03-23 22:28:06 -070079
Philipp Schrader4bd29b12017-02-22 04:42:27 +000080 void ResetDrivetrain();
81 void InitializeEncoders();
Alex Perrycb7da4b2019-08-28 19:35:56 -070082 void StartDrive(double distance, double angle, ProfileParametersT linear,
83 ProfileParametersT angular);
Austin Schuh9aa78b12021-11-12 11:53:33 -080084 void ApplyThrottle(double throttle);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000085
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 Schuha5fefcd2023-03-22 20:23:42 -070095 // Returns the current x, y, theta of the robot on the field.
96 double X();
97 double Y();
98 double Theta();
99
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100 void LineFollowAtVelocity(
101 double velocity,
102 y2019::control_loops::drivetrain::SelectionHint hint =
Austin Schuh872723c2019-12-25 14:38:09 -0800103 y2019::control_loops::drivetrain::SelectionHint::NONE);
James Kuszmaul838040b2019-04-13 15:51:02 -0700104
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000105 // 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 Schuh0aae9242018-03-14 19:49:44 -0700115 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000116 bool WaitForDriveProfileDone();
117
Austin Schuh0aae9242018-03-14 19:49:44 -0700118 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -0700119 bool WaitForTurnProfileDone();
120
Austin Schuhc087b102019-05-12 15:33:12 -0700121 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
122 max_drivetrain_voltage_ = max_drivetrain_voltage;
123 }
124
Austin Schuh546a0382017-04-16 19:10:18 -0700125 // Returns the distance left to go.
126 double DriveDistanceLeft();
127
Austin Schuhbcce26a2018-03-26 23:41:24 -0700128 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000129
130 // Initial drivetrain positions.
131 struct InitialDrivetrain {
132 double left;
133 double right;
134 };
135 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -0700136
Austin Schuhc087b102019-05-12 15:33:12 -0700137 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
138 target_selector_hint_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 ::aos::Sender<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700140 drivetrain_goal_sender_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800141 ::aos::Sender<::frc971::control_loops::drivetrain::SplineGoal>
142 spline_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700144 drivetrain_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700145 ::aos::Fetcher<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700146 drivetrain_goal_fetcher_;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700147
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700148 private:
149 friend class SplineHandle;
Austin Schuh0aae9242018-03-14 19:49:44 -0700150 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700151
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 Schrader4bd29b12017-02-22 04:42:27 +0000156};
157
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800158} // namespace frc971::autonomous
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000159
160#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_