blob: 38db070c37eb5f724187c6fa65b9eb25dd747d7a [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"
13#include "y2019/control_loops/superstructure/superstructure.q.h"
14
15namespace y2019 {
16namespace actors {
17
18using ::frc971::ProfileParameters;
19using ::y2019::control_loops::superstructure::superstructure_queue;
20
21class AutonomousActor : public ::frc971::autonomous::BaseAutonomousActor {
22 public:
23 explicit AutonomousActor(::frc971::autonomous::AutonomousActionQueueGroup *s);
24
25 bool RunAction(
26 const ::frc971::autonomous::AutonomousActionParams &params) override;
27
28 private:
Austin Schuha9644062019-03-28 14:31:52 -070029 void Reset(bool is_left);
Austin Schuh13379ba2019-03-12 21:06:46 -070030
31 double elevator_goal_ = 0.0;
32 double wrist_goal_ = 0.0;
33 double intake_goal_ = 0.0;
34
35 bool suction_on_ = true;
36 int suction_gamepiece_ = 1;
37
38 double elevator_max_velocity_ = 0.0;
39 double elevator_max_acceleration_ = 0.0;
40 double wrist_max_velocity_ = 0.0;
41 double wrist_max_acceleration_ = 0.0;
42
43 void set_elevator_goal(double elevator_goal) {
44 elevator_goal_ = elevator_goal;
45 }
46 void set_wrist_goal(double wrist_goal) { wrist_goal_ = wrist_goal; }
47 void set_intake_goal(double intake_goal) { intake_goal_ = intake_goal; }
48
49 void set_suction_goal(bool on, int gamepiece_mode) {
50 suction_on_ = on;
51 suction_gamepiece_ = gamepiece_mode;
52 }
53
54 void set_elevator_max_velocity(double elevator_max_velocity) {
55 elevator_max_velocity_ = elevator_max_velocity;
56 }
57 void set_elevator_max_acceleration(double elevator_max_acceleration) {
58 elevator_max_acceleration_ = elevator_max_acceleration;
59 }
60 void set_wrist_max_velocity(double wrist_max_velocity) {
61 wrist_max_velocity_ = wrist_max_velocity;
62 }
63 void set_wrist_max_acceleration(double wrist_max_acceleration) {
64 wrist_max_acceleration_ = wrist_max_acceleration;
65 }
66
67 void SendSuperstructureGoal() {
68 auto new_superstructure_goal = superstructure_queue.goal.MakeMessage();
69 new_superstructure_goal->elevator.unsafe_goal = elevator_goal_;
70 new_superstructure_goal->wrist.unsafe_goal = wrist_goal_;
71 new_superstructure_goal->intake.unsafe_goal = intake_goal_;
72
73 new_superstructure_goal->suction.grab_piece = suction_on_;
74 new_superstructure_goal->suction.gamepiece_mode = suction_gamepiece_;
75
76 new_superstructure_goal->elevator.profile_params.max_velocity =
77 elevator_max_velocity_;
78 new_superstructure_goal->elevator.profile_params.max_acceleration =
79 elevator_max_acceleration_;
80
81 new_superstructure_goal->wrist.profile_params.max_velocity =
82 wrist_max_velocity_;
83 new_superstructure_goal->wrist.profile_params.max_acceleration =
84 wrist_max_acceleration_;
85
86 if (!new_superstructure_goal.Send()) {
87 LOG(ERROR, "Sending superstructure goal failed.\n");
88 }
89 }
90
91 bool IsSucked() {
92 superstructure_queue.status.FetchLatest();
93
94 if (superstructure_queue.status.get()) {
95 return superstructure_queue.status->has_piece;
96 }
97 return false;
98 }
99
100 bool WaitForGamePiece() {
101 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
102 ::std::chrono::milliseconds(5) / 2);
103
104 while (true) {
105 if (ShouldCancel()) {
106 return false;
107 }
108 phased_loop.SleepUntilNext();
109 if (IsSucked()) {
110 return true;
111 }
112 }
113 }
114
115 bool IsSuperstructureDone() {
116 superstructure_queue.status.FetchLatest();
117
118 double kElevatorTolerance = 0.01;
119 double kWristTolerance = 0.05;
120
121 if (superstructure_queue.status.get()) {
122 const bool elevator_at_goal =
123 ::std::abs(elevator_goal_ -
124 superstructure_queue.status->elevator.position) <
125 kElevatorTolerance;
126
127 const bool wrist_at_goal =
128 ::std::abs(wrist_goal_ -
129 superstructure_queue.status->wrist.position) <
130 kWristTolerance;
131
132 return elevator_at_goal && wrist_at_goal;
133 }
134 return false;
135 }
136
137 bool WaitForSuperstructureDone() {
138 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5),
139 ::std::chrono::milliseconds(5) / 2);
140
141 while (true) {
142 if (ShouldCancel()) {
143 return false;
144 }
145 phased_loop.SleepUntilNext();
146 superstructure_queue.status.FetchLatest();
147 superstructure_queue.goal.FetchLatest();
148 if (IsSuperstructureDone()) {
149 return true;
150 }
151 }
152 }
153};
154
155} // namespace actors
156} // namespace y2019
157
158#endif // Y2019_ACTORS_AUTONOMOUS_ACTOR_H_