blob: e060a2c5fd0ff081b171e57e0bec1fd80849c485 [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"
Philipp Schrader4bd29b12017-02-22 04:42:27 +00008#include "frc971/autonomous/auto.q.h"
9#include "frc971/control_loops/drivetrain/drivetrain.q.h"
10#include "frc971/control_loops/drivetrain/drivetrain_config.h"
11
12namespace frc971 {
13namespace autonomous {
14
15class BaseAutonomousActor
16 : public ::aos::common::actions::ActorBase<AutonomousActionQueueGroup> {
17 public:
18 explicit BaseAutonomousActor(
19 AutonomousActionQueueGroup *s,
Austin Schuhbcce26a2018-03-26 23:41:24 -070020 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000021
22 protected:
Austin Schuh6bcc2302019-03-23 22:28:06 -070023 class SplineHandle {
24 public:
25 bool IsPlanned();
26 bool WaitForPlan();
27 void Start();
28
29 bool IsDone();
30 bool WaitForDone();
31
James Kuszmaul838040b2019-04-13 15:51:02 -070032 // Whether there is less than a certain distance, in meters, remaining in
33 // the current spline.
34 bool SplineDistanceRemaining(double distance);
35 bool WaitForSplineDistanceRemaining(double distance);
Austin Schuh6bcc2302019-03-23 22:28:06 -070036
37 private:
38 friend BaseAutonomousActor;
39 SplineHandle(int32_t spline_handle,
40 BaseAutonomousActor *base_autonomous_actor)
41 : spline_handle_(spline_handle),
42 base_autonomous_actor_(base_autonomous_actor) {}
43
44 int32_t spline_handle_;
45 BaseAutonomousActor *base_autonomous_actor_;
46 };
47
James Kuszmaulc73bb222019-04-07 12:15:35 -070048 // Represents the direction that we will drive along a spline.
49 enum class SplineDirection {
50 kForward,
51 kBackward,
52 };
53
Austin Schuh6bcc2302019-03-23 22:28:06 -070054 // Starts planning the spline, and returns a handle to be used to manipulate
55 // it.
James Kuszmaulc73bb222019-04-07 12:15:35 -070056 SplineHandle PlanSpline(const ::frc971::MultiSpline &spline,
57 SplineDirection direction);
Austin Schuh6bcc2302019-03-23 22:28:06 -070058
Philipp Schrader4bd29b12017-02-22 04:42:27 +000059 void ResetDrivetrain();
60 void InitializeEncoders();
61 void StartDrive(double distance, double angle, ProfileParameters linear,
62 ProfileParameters angular);
63
64 void WaitUntilDoneOrCanceled(
65 ::std::unique_ptr<aos::common::actions::Action> action);
66 // Waits for the drive motion to finish. Returns true if it succeeded, and
67 // false if it cancels.
68 bool WaitForDriveDone();
69
70 // Returns true if the drive has finished.
71 bool IsDriveDone();
72
James Kuszmaul838040b2019-04-13 15:51:02 -070073 void LineFollowAtVelocity(double velocity);
74
Philipp Schrader4bd29b12017-02-22 04:42:27 +000075 // Waits until the robot is pitched up above the specified angle, or the move
76 // finishes. Returns true on success, and false if it cancels.
77 bool WaitForAboveAngle(double angle);
78 bool WaitForBelowAngle(double angle);
79 bool WaitForMaxBy(double angle);
80
81 // Waits until the profile and distance is within distance and angle of the
82 // goal. Returns true on success, and false when canceled.
83 bool WaitForDriveNear(double distance, double angle);
84
Austin Schuh0aae9242018-03-14 19:49:44 -070085 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000086 bool WaitForDriveProfileDone();
87
Austin Schuh0aae9242018-03-14 19:49:44 -070088 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -070089 bool WaitForTurnProfileDone();
90
91 // Returns the distance left to go.
92 double DriveDistanceLeft();
93
Austin Schuhbcce26a2018-03-26 23:41:24 -070094 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +000095
96 // Initial drivetrain positions.
97 struct InitialDrivetrain {
98 double left;
99 double right;
100 };
101 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -0700102
103 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
104 max_drivetrain_voltage_ = max_drivetrain_voltage;
105 }
106
107 private:
Austin Schuh6bcc2302019-03-23 22:28:06 -0700108 friend class SplineHandle;
109
Austin Schuh0aae9242018-03-14 19:49:44 -0700110 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700111
112 // Unique counter so we get unique spline handles.
113 int spline_handle_ = 0;
114 // Last goal spline handle;
115 int32_t goal_spline_handle_ = 0;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000116};
117
118using AutonomousAction =
119 ::aos::common::actions::TypedAction<AutonomousActionQueueGroup>;
120
121// Makes a new AutonomousActor action.
122::std::unique_ptr<AutonomousAction> MakeAutonomousAction(
123 const AutonomousActionParams &params);
124
125} // namespace autonomous
126} // namespace frc971
127
128#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_