blob: 8c61596735d19b2444bdd19462c942d6a62578ae [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;
Austin Schuh13379ba2019-03-12 21:06:46 -070020
Austin Schuhb5b79a52019-05-08 20:32:07 -070021struct ElevatorWristPosition {
22 double elevator;
23 double wrist;
24};
25
Austin Schuh13379ba2019-03-12 21:06:46 -070026class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
27 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070028 explicit AutonomousActor(::aos::EventLoop *event_loop);
Austin Schuh13379ba2019-03-12 21:06:46 -070029
30 bool RunAction(
31 const ::frc971::autonomous::AutonomousActionParams &params) override;
32
33 private:
Austin Schuha9644062019-03-28 14:31:52 -070034 void Reset(bool is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070035
36 double elevator_goal_ = 0.0;
37 double wrist_goal_ = 0.0;
38 double intake_goal_ = 0.0;
39
40 bool suction_on_ = true;
41 int suction_gamepiece_ = 1;
42
43 double elevator_max_velocity_ = 0.0;
44 double elevator_max_acceleration_ = 0.0;
45 double wrist_max_velocity_ = 0.0;
46 double wrist_max_acceleration_ = 0.0;
47
48 void set_elevator_goal(double elevator_goal) {
49 elevator_goal_ = elevator_goal;
50 }
51 void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; }
52 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
53
54 void set_suction_goal(bool on, int gamepiece_mode) {
55 suction_on_ = on;
56 suction_gamepiece_ = gamepiece_mode;
57 }
58
59 void set_elevator_max_velocity(double elevator_max_velocity) {
60 elevator_max_velocity_ = elevator_max_velocity;
61 }
62 void set_elevator_max_acceleration(double elevator_max_acceleration) {
63 elevator_max_acceleration_ = elevator_max_acceleration;
64 }
65 void set_wrist_max_velocity(double wrist_max_velocity) {
66 wrist_max_velocity_ = wrist_max_velocity;
67 }
68 void set_wrist_max_acceleration(double wrist_max_acceleration) {
69 wrist_max_acceleration_ = wrist_max_acceleration;
70 }
71
Austin Schuhb5b79a52019-05-08 20:32:07 -070072 void set_elevator_wrist_goal(ElevatorWristPosition goal) {
73 set_elevator_goal(goal.elevator);
74 set_wrist_goal(goal.wrist);
75 }
76
Austin Schuh13379ba2019-03-12 21:06:46 -070077 void SendSuperstructureGoal() {
Austin Schuh170f4952019-06-29 18:58:30 -070078 auto new_superstructure_goal = superstructure_goal_sender_.MakeMessage();
Austin Schuh13379ba2019-03-12 21:06:46 -070079 new_superstructure_goal->elevator.unsafe_goal = elevator_goal_;
80 new_superstructure_goal->wrist.unsafe_goal = wrist_goal_;
81 new_superstructure_goal->intake.unsafe_goal = intake_goal_;
82
83 new_superstructure_goal->suction.grab_piece = suction_on_;
84 new_superstructure_goal->suction.gamepiece_mode = suction_gamepiece_;
85
86 new_superstructure_goal->elevator.profile_params.max_velocity =
87 elevator_max_velocity_;
88 new_superstructure_goal->elevator.profile_params.max_acceleration =
89 elevator_max_acceleration_;
90
91 new_superstructure_goal->wrist.profile_params.max_velocity =
92 wrist_max_velocity_;
93 new_superstructure_goal->wrist.profile_params.max_acceleration =
94 wrist_max_acceleration_;
95
96 if (!new_superstructure_goal.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070097 AOS_LOG(ERROR, "Sending superstructure goal failed.\n");
Austin Schuh13379ba2019-03-12 21:06:46 -070098 }
99 }
100
101 bool IsSucked() {
Austin Schuh170f4952019-06-29 18:58:30 -0700102 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700103
Austin Schuh170f4952019-06-29 18:58:30 -0700104 if (superstructure_status_fetcher_.get()) {
105 return superstructure_status_fetcher_->has_piece;
Austin Schuh13379ba2019-03-12 21:06:46 -0700106 }
107 return false;
108 }
109
110 bool WaitForGamePiece() {
111 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700112 event_loop()->monotonic_now(),
Austin Schuh13379ba2019-03-12 21:06:46 -0700113 ::std::chrono::milliseconds(5) / 2);
114
115 while (true) {
116 if (ShouldCancel()) {
117 return false;
118 }
119 phased_loop.SleepUntilNext();
120 if (IsSucked()) {
121 return true;
122 }
123 }
124 }
125
Austin Schuhb5b79a52019-05-08 20:32:07 -0700126 bool WaitForMilliseconds(std::chrono::milliseconds wait) {
Austin Schuh77ed5432019-07-07 20:41:36 -0700127 ::aos::monotonic_clock::time_point end_time = monotonic_now() + wait;
Austin Schuhb5b79a52019-05-08 20:32:07 -0700128
Austin Schuh77ed5432019-07-07 20:41:36 -0700129 while (monotonic_now() < end_time) {
Austin Schuhb5b79a52019-05-08 20:32:07 -0700130 if (ShouldCancel()) {
131 return false;
132 }
133 // TODO(james): Allow non-multiples of 5.
134 ::std::this_thread::sleep_for(::std::chrono::milliseconds(5));
135 }
136 return true;
137 }
138
Austin Schuh13379ba2019-03-12 21:06:46 -0700139 bool IsSuperstructureDone() {
Austin Schuh170f4952019-06-29 18:58:30 -0700140 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700141
142 double kElevatorTolerance = 0.01;
143 double kWristTolerance = 0.05;
144
Austin Schuh170f4952019-06-29 18:58:30 -0700145 if (superstructure_status_fetcher_.get()) {
Austin Schuh13379ba2019-03-12 21:06:46 -0700146 const bool elevator_at_goal =
147 ::std::abs(elevator_goal_ -
Austin Schuh170f4952019-06-29 18:58:30 -0700148 superstructure_status_fetcher_->elevator.position) <
Austin Schuh13379ba2019-03-12 21:06:46 -0700149 kElevatorTolerance;
150
151 const bool wrist_at_goal =
152 ::std::abs(wrist_goal_ -
Austin Schuh170f4952019-06-29 18:58:30 -0700153 superstructure_status_fetcher_->wrist.position) <
Austin Schuh13379ba2019-03-12 21:06:46 -0700154 kWristTolerance;
155
156 return elevator_at_goal && wrist_at_goal;
157 }
158 return false;
159 }
160
161 bool WaitForSuperstructureDone() {
162 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
Austin Schuhd32b3622019-06-23 18:49:06 -0700163 event_loop()->monotonic_now(),
Austin Schuh13379ba2019-03-12 21:06:46 -0700164 ::std::chrono::milliseconds(5) / 2);
165
166 while (true) {
167 if (ShouldCancel()) {
168 return false;
169 }
170 phased_loop.SleepUntilNext();
Austin Schuh170f4952019-06-29 18:58:30 -0700171 superstructure_status_fetcher_.Fetch();
Austin Schuh13379ba2019-03-12 21:06:46 -0700172 if (IsSuperstructureDone()) {
173 return true;
174 }
175 }
176 }
Austin Schuhb5b79a52019-05-08 20:32:07 -0700177
178 // Waits until the robot's x > x.
179 bool WaitForDriveXGreater(double x);
180
181 // Waits until y is within y of zero.
182 bool WaitForDriveYCloseToZero(double y);
Austin Schuheb99d072019-05-12 21:03:38 -0700183
184 ::aos::Sender<::frc971::control_loops::drivetrain::LocalizerControl>
185 localizer_control_sender_;
Austin Schuh170f4952019-06-29 18:58:30 -0700186 ::aos::Sender<
187 ::y2019::control_loops::superstructure::SuperstructureQueue::Goal>
188 superstructure_goal_sender_;
189 ::aos::Fetcher<
190 ::y2019::control_loops::superstructure::SuperstructureQueue::Status>
191 superstructure_status_fetcher_;
Austin Schuh13379ba2019-03-12 21:06:46 -0700192};
193
194} // namespace actors
195} // namespace y2019
196
197#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_