blob: 13b336db05de611bcc57ccf4c7db9bc5a734c3eb [file] [log] [blame]
Austin Schuh13379ba2019-03-12 21:06:46 -07001#ifndef Y2019_ACTORS_AUTONOMOUS_ACTOR_H_
2#define Y2019_ACTORS_AUTONOMOUS_ACTOR_H_
3
4#include <chrono>
5#include <memory>
6
7#include "aos/actions/actions.h"
8#include "aos/actions/actor.h"
9#include "frc971/autonomous/base_autonomous_actor.h"
10#include "frc971/control_loops/control_loops.q.h"
11#include "frc971/control_loops/drivetrain/drivetrain.q.h"
12#include "frc971/control_loops/drivetrain/drivetrain_config.h"
Austin Schuheb99d072019-05-12 21:03:38 -070013#include "frc971/control_loops/drivetrain/localizer.q.h"
Austin Schuh13379ba2019-03-12 21:06:46 -070014#include "y2019/control_loops/superstructure/superstructure.q.h"
15
16namespace y2019 {
17namespace actors {
18
19using ::frc971::ProfileParameters;
20using ::y2019::control_loops::superstructure::superstructure_queue;
21
Austin Schuhb5b79a52019-05-08 20:32:07 -070022struct ElevatorWristPosition {
23 double elevator;
24 double wrist;
25};
26
Austin Schuh13379ba2019-03-12 21:06:46 -070027class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
28 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070029 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuh13379ba2019-03-12 21:06:46 -070030
31 bool RunAction(
32 const ::frc971::autonomous::AutonomousActionParams &params) override;
33
34 private:
Austin Schuha9644062019-03-28 14:31:52 -070035 void Reset(bool is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070036
37 double elevator_goal_ = 0.0;
38 double wrist_goal_ = 0.0;
39 double intake_goal_ = 0.0;
40
41 bool suction_on_ = true;
42 int suction_gamepiece_ = 1;
43
44 double elevator_max_velocity_ = 0.0;
45 double elevator_max_acceleration_ = 0.0;
46 double wrist_max_velocity_ = 0.0;
47 double wrist_max_acceleration_ = 0.0;
48
49 void set_elevator_goal(double elevator_goal) {
50 elevator_goal_ = elevator_goal;
51 }
52 void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; }
53 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
54
55 void set_suction_goal(bool on, int gamepiece_mode) {
56 suction_on_ = on;
57 suction_gamepiece_ = gamepiece_mode;
58 }
59
60 void set_elevator_max_velocity(double elevator_max_velocity) {
61 elevator_max_velocity_ = elevator_max_velocity;
62 }
63 void set_elevator_max_acceleration(double elevator_max_acceleration) {
64 elevator_max_acceleration_ = elevator_max_acceleration;
65 }
66 void set_wrist_max_velocity(double wrist_max_velocity) {
67 wrist_max_velocity_ = wrist_max_velocity;
68 }
69 void set_wrist_max_acceleration(double wrist_max_acceleration) {
70 wrist_max_acceleration_ = wrist_max_acceleration;
71 }
72
Austin Schuhb5b79a52019-05-08 20:32:07 -070073 void set_elevator_wrist_goal(ElevatorWristPosition goal) {
74 set_elevator_goal(goal.elevator);
75 set_wrist_goal(goal.wrist);
76 }
77
Austin Schuh13379ba2019-03-12 21:06:46 -070078 void SendSuperstructureGoal() {
79 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
80 new_superstructure_goal->elevator.unsafe_goal = elevator_goal_;
81 new_superstructure_goal->wrist.unsafe_goal = wrist_goal_;
82 new_superstructure_goal->intake.unsafe_goal = intake_goal_;
83
84 new_superstructure_goal->suction.grab_piece = suction_on_;
85 new_superstructure_goal->suction.gamepiece_mode = suction_gamepiece_;
86
87 new_superstructure_goal->elevator.profile_params.max_velocity =
88 elevator_max_velocity_;
89 new_superstructure_goal->elevator.profile_params.max_acceleration =
90 elevator_max_acceleration_;
91
92 new_superstructure_goal->wrist.profile_params.max_velocity =
93 wrist_max_velocity_;
94 new_superstructure_goal->wrist.profile_params.max_acceleration =
95 wrist_max_acceleration_;
96
97 if (!new_superstructure_goal.Send()) {
98 LOG(ERROR, "Sending superstructure goal failed.\n");
99 }
100 }
101
102 bool IsSucked() {
103 superstructure_queue.status.FetchLatest();
104
105 if (superstructure_queue.status.get()) {
106 return superstructure_queue.status->has_piece;
107 }
108 return false;
109 }
110
111 bool WaitForGamePiece() {
112 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700113 event_loop()->monotonic_now(),
Austin Schuh13379ba2019-03-12 21:06:46 -0700114 ::std::chrono::milliseconds(5) / 2);
115
116 while (true) {
117 if (ShouldCancel()) {
118 return false;
119 }
120 phased_loop.SleepUntilNext();
121 if (IsSucked()) {
122 return true;
123 }
124 }
125 }
126
Austin Schuhb5b79a52019-05-08 20:32:07 -0700127 bool WaitForMilliseconds(std::chrono::milliseconds wait) {
128 ::aos::monotonic_clock::time_point end_time =
129 ::aos::monotonic_clock::now() + wait;
130
131 while (::aos::monotonic_clock::now() < end_time) {
132 if (ShouldCancel()) {
133 return false;
134 }
135 // TODO(james): Allow non-multiples of 5.
136 ::std::this_thread::sleep_for(::std::chrono::milliseconds(5));
137 }
138 return true;
139 }
140
Austin Schuh13379ba2019-03-12 21:06:46 -0700141 bool IsSuperstructureDone() {
142 superstructure_queue.status.FetchLatest();
143
144 double kElevatorTolerance = 0.01;
145 double kWristTolerance = 0.05;
146
147 if (superstructure_queue.status.get()) {
148 const bool elevator_at_goal =
149 ::std::abs(elevator_goal_ -
150 superstructure_queue.status->elevator.position) <
151 kElevatorTolerance;
152
153 const bool wrist_at_goal =
154 ::std::abs(wrist_goal_ -
155 superstructure_queue.status->wrist.position) <
156 kWristTolerance;
157
158 return elevator_at_goal && wrist_at_goal;
159 }
160 return false;
161 }
162
163 bool WaitForSuperstructureDone() {
164 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700165 event_loop()->monotonic_now(),
Austin Schuh13379ba2019-03-12 21:06:46 -0700166 ::std::chrono::milliseconds(5) / 2);
167
168 while (true) {
169 if (ShouldCancel()) {
170 return false;
171 }
172 phased_loop.SleepUntilNext();
173 superstructure_queue.status.FetchLatest();
174 superstructure_queue.goal.FetchLatest();
175 if (IsSuperstructureDone()) {
176 return true;
177 }
178 }
179 }
Austin Schuhb5b79a52019-05-08 20:32:07 -0700180
181 // Waits until the robot's x > x.
182 bool WaitForDriveXGreater(double x);
183
184 // Waits until y is within y of zero.
185 bool WaitForDriveYCloseToZero(double y);
Austin Schuheb99d072019-05-12 21:03:38 -0700186
187 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
188 localizer_control_sender_;
Austin Schuh13379ba2019-03-12 21:06:46 -0700189};
190
191} // namespace actors
192} // namespace y2019
193
194#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_