blob: a6a57cd1ff2a3894ad07c91448c2bc0f9554a3ca [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"
Austin Schuhc087b102019-05-12 15:33:12 -07008#include "aos/events/shm-event-loop.h"
Philipp Schrader4bd29b12017-02-22 04:42:27 +00009#include "frc971/autonomous/auto.q.h"
10#include "frc971/control_loops/drivetrain/drivetrain.q.h"
11#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuhc087b102019-05-12 15:33:12 -070012#include "y2019/control_loops/drivetrain/target_selector.q.h"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000013
14namespace frc971 {
15namespace autonomous {
16
17class BaseAutonomousActor
18 : public ::aos::common::actions::ActorBase<AutonomousActionQueueGroup> {
19 public:
20 explicit BaseAutonomousActor(
21 AutonomousActionQueueGroup *s,
Austin Schuhbcce26a2018-03-26 23:41:24 -070022 const control_loops::drivetrain::DrivetrainConfig<double> &dt_config);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000023
24 protected:
Austin Schuh6bcc2302019-03-23 22:28:06 -070025 class SplineHandle {
26 public:
27 bool IsPlanned();
28 bool WaitForPlan();
29 void Start();
30
31 bool IsDone();
32 bool WaitForDone();
33
James Kuszmaul838040b2019-04-13 15:51:02 -070034 // Whether there is less than a certain distance, in meters, remaining in
35 // the current spline.
36 bool SplineDistanceRemaining(double distance);
37 bool WaitForSplineDistanceRemaining(double distance);
Austin Schuh6bcc2302019-03-23 22:28:06 -070038
39 private:
40 friend BaseAutonomousActor;
41 SplineHandle(int32_t spline_handle,
42 BaseAutonomousActor *base_autonomous_actor)
43 : spline_handle_(spline_handle),
44 base_autonomous_actor_(base_autonomous_actor) {}
45
46 int32_t spline_handle_;
47 BaseAutonomousActor *base_autonomous_actor_;
48 };
49
James Kuszmaulc73bb222019-04-07 12:15:35 -070050 // Represents the direction that we will drive along a spline.
51 enum class SplineDirection {
52 kForward,
53 kBackward,
54 };
55
Austin Schuh6bcc2302019-03-23 22:28:06 -070056 // Starts planning the spline, and returns a handle to be used to manipulate
57 // it.
James Kuszmaulc73bb222019-04-07 12:15:35 -070058 SplineHandle PlanSpline(const ::frc971::MultiSpline &spline,
59 SplineDirection direction);
Austin Schuh6bcc2302019-03-23 22:28:06 -070060
Philipp Schrader4bd29b12017-02-22 04:42:27 +000061 void ResetDrivetrain();
62 void InitializeEncoders();
63 void StartDrive(double distance, double angle, ProfileParameters linear,
64 ProfileParameters angular);
65
66 void WaitUntilDoneOrCanceled(
67 ::std::unique_ptr<aos::common::actions::Action> action);
68 // Waits for the drive motion to finish. Returns true if it succeeded, and
69 // false if it cancels.
70 bool WaitForDriveDone();
71
72 // Returns true if the drive has finished.
73 bool IsDriveDone();
74
James Kuszmaul8bb5df22019-05-01 21:40:08 -050075 void LineFollowAtVelocity(double velocity, int hint = 0);
James Kuszmaul838040b2019-04-13 15:51:02 -070076
Philipp Schrader4bd29b12017-02-22 04:42:27 +000077 // Waits until the robot is pitched up above the specified angle, or the move
78 // finishes. Returns true on success, and false if it cancels.
79 bool WaitForAboveAngle(double angle);
80 bool WaitForBelowAngle(double angle);
81 bool WaitForMaxBy(double angle);
82
83 // Waits until the profile and distance is within distance and angle of the
84 // goal. Returns true on success, and false when canceled.
85 bool WaitForDriveNear(double distance, double angle);
86
Austin Schuh0aae9242018-03-14 19:49:44 -070087 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000088 bool WaitForDriveProfileDone();
89
Austin Schuh0aae9242018-03-14 19:49:44 -070090 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -070091 bool WaitForTurnProfileDone();
92
Austin Schuhc087b102019-05-12 15:33:12 -070093 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
94 max_drivetrain_voltage_ = max_drivetrain_voltage;
95 }
96
Austin Schuh546a0382017-04-16 19:10:18 -070097 // Returns the distance left to go.
98 double DriveDistanceLeft();
99
Austin Schuhbcce26a2018-03-26 23:41:24 -0700100 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000101
102 // Initial drivetrain positions.
103 struct InitialDrivetrain {
104 double left;
105 double right;
106 };
107 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -0700108
Austin Schuh0aae9242018-03-14 19:49:44 -0700109 private:
Austin Schuh6bcc2302019-03-23 22:28:06 -0700110 friend class SplineHandle;
Austin Schuhc087b102019-05-12 15:33:12 -0700111 ::aos::ShmEventLoop event_loop_;
112
113 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
114 target_selector_hint_sender_;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700115
Austin Schuh0aae9242018-03-14 19:49:44 -0700116 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700117
118 // Unique counter so we get unique spline handles.
119 int spline_handle_ = 0;
120 // Last goal spline handle;
121 int32_t goal_spline_handle_ = 0;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000122};
123
124using AutonomousAction =
125 ::aos::common::actions::TypedAction<AutonomousActionQueueGroup>;
126
127// Makes a new AutonomousActor action.
128::std::unique_ptr<AutonomousAction> MakeAutonomousAction(
129 const AutonomousActionParams &params);
130
131} // namespace autonomous
132} // namespace frc971
133
134#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_