blob: aeef38cd52ec5ee68382c8727cef0f8bdf6b8c56 [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
32 // Wait for done, wait until X from the end, wait for distance from the end
33
34 private:
35 friend BaseAutonomousActor;
36 SplineHandle(int32_t spline_handle,
37 BaseAutonomousActor *base_autonomous_actor)
38 : spline_handle_(spline_handle),
39 base_autonomous_actor_(base_autonomous_actor) {}
40
41 int32_t spline_handle_;
42 BaseAutonomousActor *base_autonomous_actor_;
43 };
44
James Kuszmaulc73bb222019-04-07 12:15:35 -070045 // Represents the direction that we will drive along a spline.
46 enum class SplineDirection {
47 kForward,
48 kBackward,
49 };
50
Austin Schuh6bcc2302019-03-23 22:28:06 -070051 // Starts planning the spline, and returns a handle to be used to manipulate
52 // it.
James Kuszmaulc73bb222019-04-07 12:15:35 -070053 SplineHandle PlanSpline(const ::frc971::MultiSpline &spline,
54 SplineDirection direction);
Austin Schuh6bcc2302019-03-23 22:28:06 -070055
Philipp Schrader4bd29b12017-02-22 04:42:27 +000056 void ResetDrivetrain();
57 void InitializeEncoders();
58 void StartDrive(double distance, double angle, ProfileParameters linear,
59 ProfileParameters angular);
60
61 void WaitUntilDoneOrCanceled(
62 ::std::unique_ptr<aos::common::actions::Action> action);
63 // Waits for the drive motion to finish. Returns true if it succeeded, and
64 // false if it cancels.
65 bool WaitForDriveDone();
66
67 // Returns true if the drive has finished.
68 bool IsDriveDone();
69
70 // Waits until the robot is pitched up above the specified angle, or the move
71 // finishes. Returns true on success, and false if it cancels.
72 bool WaitForAboveAngle(double angle);
73 bool WaitForBelowAngle(double angle);
74 bool WaitForMaxBy(double angle);
75
76 // Waits until the profile and distance is within distance and angle of the
77 // goal. Returns true on success, and false when canceled.
78 bool WaitForDriveNear(double distance, double angle);
79
Austin Schuh0aae9242018-03-14 19:49:44 -070080 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000081 bool WaitForDriveProfileDone();
82
Austin Schuh0aae9242018-03-14 19:49:44 -070083 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -070084 bool WaitForTurnProfileDone();
85
86 // Returns the distance left to go.
87 double DriveDistanceLeft();
88
Austin Schuhbcce26a2018-03-26 23:41:24 -070089 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +000090
91 // Initial drivetrain positions.
92 struct InitialDrivetrain {
93 double left;
94 double right;
95 };
96 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -070097
98 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
99 max_drivetrain_voltage_ = max_drivetrain_voltage;
100 }
101
102 private:
Austin Schuh6bcc2302019-03-23 22:28:06 -0700103 friend class SplineHandle;
104
Austin Schuh0aae9242018-03-14 19:49:44 -0700105 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700106
107 // Unique counter so we get unique spline handles.
108 int spline_handle_ = 0;
109 // Last goal spline handle;
110 int32_t goal_spline_handle_ = 0;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000111};
112
113using AutonomousAction =
114 ::aos::common::actions::TypedAction<AutonomousActionQueueGroup>;
115
116// Makes a new AutonomousActor action.
117::std::unique_ptr<AutonomousAction> MakeAutonomousAction(
118 const AutonomousActionParams &params);
119
120} // namespace autonomous
121} // namespace frc971
122
123#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_