blob: 941129f311084cfd79cab98c3fa8efea5d76b00a [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 Schuheb99d072019-05-12 21:03:38 -070029 explicit AutonomousActor(::aos::EventLoop *event_loop,
30 ::frc971::autonomous::AutonomousActionQueueGroup *s);
Austin Schuh13379ba2019-03-12 21:06:46 -070031
32 bool RunAction(
33 const ::frc971::autonomous::AutonomousActionParams &params) override;
34
35 private:
Austin Schuha9644062019-03-28 14:31:52 -070036 void Reset(bool is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070037
38 double elevator_goal_ = 0.0;
39 double wrist_goal_ = 0.0;
40 double intake_goal_ = 0.0;
41
42 bool suction_on_ = true;
43 int suction_gamepiece_ = 1;
44
45 double elevator_max_velocity_ = 0.0;
46 double elevator_max_acceleration_ = 0.0;
47 double wrist_max_velocity_ = 0.0;
48 double wrist_max_acceleration_ = 0.0;
49
50 void set_elevator_goal(double elevator_goal) {
51 elevator_goal_ = elevator_goal;
52 }
53 void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; }
54 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
55
56 void set_suction_goal(bool on, int gamepiece_mode) {
57 suction_on_ = on;
58 suction_gamepiece_ = gamepiece_mode;
59 }
60
61 void set_elevator_max_velocity(double elevator_max_velocity) {
62 elevator_max_velocity_ = elevator_max_velocity;
63 }
64 void set_elevator_max_acceleration(double elevator_max_acceleration) {
65 elevator_max_acceleration_ = elevator_max_acceleration;
66 }
67 void set_wrist_max_velocity(double wrist_max_velocity) {
68 wrist_max_velocity_ = wrist_max_velocity;
69 }
70 void set_wrist_max_acceleration(double wrist_max_acceleration) {
71 wrist_max_acceleration_ = wrist_max_acceleration;
72 }
73
Austin Schuhb5b79a52019-05-08 20:32:07 -070074 void set_elevator_wrist_goal(ElevatorWristPosition goal) {
75 set_elevator_goal(goal.elevator);
76 set_wrist_goal(goal.wrist);
77 }
78
Austin Schuh13379ba2019-03-12 21:06:46 -070079 void SendSuperstructureGoal() {
80 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
81 new_superstructure_goal->elevator.unsafe_goal = elevator_goal_;
82 new_superstructure_goal->wrist.unsafe_goal = wrist_goal_;
83 new_superstructure_goal->intake.unsafe_goal = intake_goal_;
84
85 new_superstructure_goal->suction.grab_piece = suction_on_;
86 new_superstructure_goal->suction.gamepiece_mode = suction_gamepiece_;
87
88 new_superstructure_goal->elevator.profile_params.max_velocity =
89 elevator_max_velocity_;
90 new_superstructure_goal->elevator.profile_params.max_acceleration =
91 elevator_max_acceleration_;
92
93 new_superstructure_goal->wrist.profile_params.max_velocity =
94 wrist_max_velocity_;
95 new_superstructure_goal->wrist.profile_params.max_acceleration =
96 wrist_max_acceleration_;
97
98 if (!new_superstructure_goal.Send()) {
99 LOG(ERROR, "Sending superstructure goal failed.\n");
100 }
101 }
102
103 bool IsSucked() {
104 superstructure_queue.status.FetchLatest();
105
106 if (superstructure_queue.status.get()) {
107 return superstructure_queue.status->has_piece;
108 }
109 return false;
110 }
111
112 bool WaitForGamePiece() {
113 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
114 ::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),
165 ::std::chrono::milliseconds(5) / 2);
166
167 while (true) {
168 if (ShouldCancel()) {
169 return false;
170 }
171 phased_loop.SleepUntilNext();
172 superstructure_queue.status.FetchLatest();
173 superstructure_queue.goal.FetchLatest();
174 if (IsSuperstructureDone()) {
175 return true;
176 }
177 }
178 }
Austin Schuhb5b79a52019-05-08 20:32:07 -0700179
180 // Waits until the robot's x > x.
181 bool WaitForDriveXGreater(double x);
182
183 // Waits until y is within y of zero.
184 bool WaitForDriveYCloseToZero(double y);
Austin Schuheb99d072019-05-12 21:03:38 -0700185
186 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
187 localizer_control_sender_;
Austin Schuh13379ba2019-03-12 21:06:46 -0700188};
189
190} // namespace actors
191} // namespace y2019
192
193#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_