blob: 7e3ef35fc96e6b4171cca365bc0a57f0fb3c6b3e [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
45 // Starts planning the spline, and returns a handle to be used to manipulate
46 // it.
47 SplineHandle PlanSpline(const ::frc971::MultiSpline &spline);
48
Philipp Schrader4bd29b12017-02-22 04:42:27 +000049 void ResetDrivetrain();
50 void InitializeEncoders();
51 void StartDrive(double distance, double angle, ProfileParameters linear,
52 ProfileParameters angular);
53
54 void WaitUntilDoneOrCanceled(
55 ::std::unique_ptr<aos::common::actions::Action> action);
56 // Waits for the drive motion to finish. Returns true if it succeeded, and
57 // false if it cancels.
58 bool WaitForDriveDone();
59
60 // Returns true if the drive has finished.
61 bool IsDriveDone();
62
63 // Waits until the robot is pitched up above the specified angle, or the move
64 // finishes. Returns true on success, and false if it cancels.
65 bool WaitForAboveAngle(double angle);
66 bool WaitForBelowAngle(double angle);
67 bool WaitForMaxBy(double angle);
68
69 // Waits until the profile and distance is within distance and angle of the
70 // goal. Returns true on success, and false when canceled.
71 bool WaitForDriveNear(double distance, double angle);
72
Austin Schuh0aae9242018-03-14 19:49:44 -070073 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000074 bool WaitForDriveProfileDone();
75
Austin Schuh0aae9242018-03-14 19:49:44 -070076 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -070077 bool WaitForTurnProfileDone();
78
79 // Returns the distance left to go.
80 double DriveDistanceLeft();
81
Austin Schuhbcce26a2018-03-26 23:41:24 -070082 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +000083
84 // Initial drivetrain positions.
85 struct InitialDrivetrain {
86 double left;
87 double right;
88 };
89 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -070090
91 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
92 max_drivetrain_voltage_ = max_drivetrain_voltage;
93 }
94
95 private:
Austin Schuh6bcc2302019-03-23 22:28:06 -070096 friend class SplineHandle;
97
Austin Schuh0aae9242018-03-14 19:49:44 -070098 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -070099
100 // Unique counter so we get unique spline handles.
101 int spline_handle_ = 0;
102 // Last goal spline handle;
103 int32_t goal_spline_handle_ = 0;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000104};
105
106using AutonomousAction =
107 ::aos::common::actions::TypedAction<AutonomousActionQueueGroup>;
108
109// Makes a new AutonomousActor action.
110::std::unique_ptr<AutonomousAction> MakeAutonomousAction(
111 const AutonomousActionParams &params);
112
113} // namespace autonomous
114} // namespace frc971
115
116#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_