blob: 5a5bcea6c8f084f9636f42969e766f0d2c7c5f28 [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:
Austin Schuh1bf8a212019-05-26 22:13:14 -070020 typedef ::aos::common::actions::TypedActionFactory<AutonomousActionQueueGroup>
21 Factory;
22
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) {
28 return Factory(event_loop, ".frc971.autonomous.autonomous_action");
29 }
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
46 private:
47 friend BaseAutonomousActor;
48 SplineHandle(int32_t spline_handle,
49 BaseAutonomousActor *base_autonomous_actor)
50 : spline_handle_(spline_handle),
51 base_autonomous_actor_(base_autonomous_actor) {}
52
53 int32_t spline_handle_;
54 BaseAutonomousActor *base_autonomous_actor_;
55 };
56
James Kuszmaulc73bb222019-04-07 12:15:35 -070057 // Represents the direction that we will drive along a spline.
58 enum class SplineDirection {
59 kForward,
60 kBackward,
61 };
62
Austin Schuh6bcc2302019-03-23 22:28:06 -070063 // Starts planning the spline, and returns a handle to be used to manipulate
64 // it.
James Kuszmaulc73bb222019-04-07 12:15:35 -070065 SplineHandle PlanSpline(const ::frc971::MultiSpline &spline,
66 SplineDirection direction);
Austin Schuh6bcc2302019-03-23 22:28:06 -070067
Philipp Schrader4bd29b12017-02-22 04:42:27 +000068 void ResetDrivetrain();
69 void InitializeEncoders();
70 void StartDrive(double distance, double angle, ProfileParameters linear,
71 ProfileParameters angular);
72
73 void WaitUntilDoneOrCanceled(
74 ::std::unique_ptr<aos::common::actions::Action> action);
75 // Waits for the drive motion to finish. Returns true if it succeeded, and
76 // false if it cancels.
77 bool WaitForDriveDone();
78
79 // Returns true if the drive has finished.
80 bool IsDriveDone();
81
James Kuszmaul8bb5df22019-05-01 21:40:08 -050082 void LineFollowAtVelocity(double velocity, int hint = 0);
James Kuszmaul838040b2019-04-13 15:51:02 -070083
Philipp Schrader4bd29b12017-02-22 04:42:27 +000084 // Waits until the robot is pitched up above the specified angle, or the move
85 // finishes. Returns true on success, and false if it cancels.
86 bool WaitForAboveAngle(double angle);
87 bool WaitForBelowAngle(double angle);
88 bool WaitForMaxBy(double angle);
89
90 // Waits until the profile and distance is within distance and angle of the
91 // goal. Returns true on success, and false when canceled.
92 bool WaitForDriveNear(double distance, double angle);
93
Austin Schuh0aae9242018-03-14 19:49:44 -070094 bool WaitForDriveProfileNear(double tolerance);
Philipp Schrader4bd29b12017-02-22 04:42:27 +000095 bool WaitForDriveProfileDone();
96
Austin Schuh0aae9242018-03-14 19:49:44 -070097 bool WaitForTurnProfileNear(double tolerance);
Austin Schuh546a0382017-04-16 19:10:18 -070098 bool WaitForTurnProfileDone();
99
Austin Schuhc087b102019-05-12 15:33:12 -0700100 void set_max_drivetrain_voltage(double max_drivetrain_voltage) {
101 max_drivetrain_voltage_ = max_drivetrain_voltage;
102 }
103
Austin Schuh546a0382017-04-16 19:10:18 -0700104 // Returns the distance left to go.
105 double DriveDistanceLeft();
106
Austin Schuhbcce26a2018-03-26 23:41:24 -0700107 const control_loops::drivetrain::DrivetrainConfig<double> dt_config_;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000108
109 // Initial drivetrain positions.
110 struct InitialDrivetrain {
111 double left;
112 double right;
113 };
114 InitialDrivetrain initial_drivetrain_;
Austin Schuh0aae9242018-03-14 19:49:44 -0700115
Austin Schuhc087b102019-05-12 15:33:12 -0700116 ::aos::Sender<::y2019::control_loops::drivetrain::TargetSelectorHint>
117 target_selector_hint_sender_;
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700118 ::aos::Sender<::frc971::control_loops::DrivetrainQueue::Goal>
119 drivetrain_goal_sender_;
120 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Status>
121 drivetrain_status_fetcher_;
122 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Goal>
123 drivetrain_goal_fetcher_;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700124
Austin Schuhbd0a40f2019-06-30 14:56:31 -0700125 private:
126 friend class SplineHandle;
Austin Schuh0aae9242018-03-14 19:49:44 -0700127 double max_drivetrain_voltage_ = 12.0;
Austin Schuh6bcc2302019-03-23 22:28:06 -0700128
129 // Unique counter so we get unique spline handles.
130 int spline_handle_ = 0;
131 // Last goal spline handle;
132 int32_t goal_spline_handle_ = 0;
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000133};
134
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000135} // namespace autonomous
136} // namespace frc971
137
138#endif // FRC971_AUTONOMOUS_BASE_AUTONOMOUS_ACTOR_H_