blob: a70a4407448ab59429bd82714f94cd107253d72f [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
16namespace frc971 {
17namespace autonomous {
18
Alex Perrycb7da4b2019-08-28 19:35:56 -070019class BaseAutonomousActor : public ::aos::common::actions::ActorBase<Goal> {
Philipp Schrader4bd29b12017-02-22 04:42:27 +000020 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070021 typedef ::aos::common::actions::TypedActionFactory<Goal> Factory;
Austin Schuh1bf8a212019-05-26 22:13:14 -070022
Philipp Schrader4bd29b12017-02-22 04:42:27 +000023 explicit BaseAutonomousActor(
Austin Schuh1bf8a212019-05-26 22:13:14 -070024 ::aos::EventLoop *event_loop,
Austin Schuhbcce26a2018-03-26 23:41:24 -070025 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000026
Austin Schuh1bf8a212019-05-26 22:13:14 -070027 static Factory MakeFactory(::aos::EventLoop *event_loop) {
Austin Schuhed5b26d2019-12-05 20:51:59 -080028 return Factory(event_loop, "/autonomous");
Austin Schuh1bf8a212019-05-26 22:13:14 -070029 }
30
Philipp Schrader4bd29b12017-02-22 04:42:27 +000031 protected:
Austin Schuh6bcc2302019-03-23 22:28:06 -070032 class SplineHandle {
33 public:
34 bool IsPlanned();
35 bool WaitForPlan();
36 void Start();
37
38 bool IsDone();
39 bool WaitForDone();
40
James Kuszmaul838040b2019-04-13 15:51:02 -070041 // 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 Schuh6bcc2302019-03-23 22:28:06 -070045
James Kuszmaul99af8b52021-03-28 10:50:15 -070046 // Returns [x, y, theta] position of the start.
47 const Eigen::Vector3d &starting_position() const { return spline_start_; }
48
Austin Schuh6bcc2302019-03-23 22:28:06 -070049 private:
50 friend BaseAutonomousActor;
51 SplineHandle(int32_t spline_handle,
James Kuszmaul99af8b52021-03-28 10:50:15 -070052 BaseAutonomousActor *base_autonomous_actor,
53 const Eigen::Vector3d &start)
Austin Schuh6bcc2302019-03-23 22:28:06 -070054 : spline_handle_(spline_handle),
James Kuszmaul99af8b52021-03-28 10:50:15 -070055 base_autonomous_actor_(base_autonomous_actor),
56 spline_start_(start) {}
Austin Schuh6bcc2302019-03-23 22:28:06 -070057
58 int32_t spline_handle_;
59 BaseAutonomousActor *base_autonomous_actor_;
James Kuszmaul99af8b52021-03-28 10:50:15 -070060
61 // Starting [x, y, theta] position of the spline.
62 Eigen::Vector3d spline_start_;
Austin Schuh6bcc2302019-03-23 22:28:06 -070063 };
64
James Kuszmaulc73bb222019-04-07 12:15:35 -070065 // Represents the direction that we will drive along a spline.
66 enum class SplineDirection {
67 kForward,
68 kBackward,
69 };
70
Austin Schuh6bcc2302019-03-23 22:28:06 -070071 // Starts planning the spline, and returns a handle to be used to manipulate
72 // it.
Alex Perrycb7da4b2019-08-28 19:35:56 -070073 SplineHandle PlanSpline(
74 std::function<flatbuffers::Offset<frc971::MultiSpline>(
James Kuszmaul75a18c52021-03-10 22:02:07 -080075 aos::Sender<frc971::control_loops::drivetrain::SplineGoal>::Builder
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 *builder)> &&multispline_builder,
77 SplineDirection direction);
Austin Schuh6bcc2302019-03-23 22:28:06 -070078
Philipp Schrader4bd29b12017-02-22 04:42:27 +000079 void ResetDrivetrain();
80 void InitializeEncoders();
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 void StartDrive(double distance, double angle, ProfileParametersT linear,
82 ProfileParametersT angular);
Austin Schuh9aa78b12021-11-12 11:53:33 -080083 void ApplyThrottle(double throttle);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000084
85 void WaitUntilDoneOrCanceled(
86 ::std::unique_ptr<aos::common::actions::Action> action);
87 // Waits for the drive motion to finish. Returns true if it succeeded, and
88 // false if it cancels.
89 bool WaitForDriveDone();
90
91 // Returns true if the drive has finished.
92 bool IsDriveDone();
93
Alex Perrycb7da4b2019-08-28 19:35:56 -070094 void LineFollowAtVelocity(
95 double velocity,
96 y2019::control_loops::drivetrain::SelectionHint hint =
Austin Schuh872723c2019-12-25 14:38:09 -080097 y2019::control_loops::drivetrain::SelectionHint::NONE);
James Kuszmaul838040b2019-04-13 15:51:02 -070098
Philipp Schrader4bd29b12017-02-22 04:42:27 +000099 // Waits until the robot is pitched up above the specified angle, or the move
100 // finishes. Returns true on success, and false if it cancels.
101 bool WaitForAboveAngle(double angle);
102 bool WaitForBelowAngle(double angle);
103 bool WaitForMaxBy(double angle);
104
105 // Waits until the profile and distance is within distance and angle of the
106 // goal. Returns true on success, and false when canceled.
107 bool WaitForDriveNear(double distance, double angle);
108
Austin Schuh0aae9242018-03-14 19:49:44 -0700109 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000110 bool WaitForDriveProfileDone();
111
Austin Schuh0aae9242018-03-14 19:49:44 -0700112 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -0700113 bool WaitForTurnProfileDone();
114
Austin Schuhc087b102019-05-12 15:33:12 -0700115 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
116 max_drivetrain_voltage_ = max_drivetrain_voltage;
117 }
118
Austin Schuh546a0382017-04-16 19:10:18 -0700119 // Returns the distance left to go.
120 double DriveDistanceLeft();
121
Austin Schuhbcce26a2018-03-26 23:41:24 -0700122 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000123
124 // Initial drivetrain positions.
125 struct InitialDrivetrain {
126 double left;
127 double right;
128 };
129 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -0700130
Austin Schuhc087b102019-05-12 15:33:12 -0700131 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
132 target_selector_hint_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133 ::aos::Sender<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700134 drivetrain_goal_sender_;
James Kuszmaul75a18c52021-03-10 22:02:07 -0800135 ::aos::Sender<::frc971::control_loops::drivetrain::SplineGoal>
136 spline_goal_sender_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 ::aos::Fetcher<::frc971::control_loops::drivetrain::Status>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700138 drivetrain_status_fetcher_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 ::aos::Fetcher<::frc971::control_loops::drivetrain::Goal>
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700140 drivetrain_goal_fetcher_;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700141
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700142 private:
143 friend class SplineHandle;
Austin Schuh0aae9242018-03-14 19:49:44 -0700144 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700145
146 // Unique counter so we get unique spline handles.
147 int spline_handle_ = 0;
148 // Last goal spline handle;
149 int32_t goal_spline_handle_ = 0;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000150};
151
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000152} // namespace autonomous
153} // namespace frc971
154
155#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_