blob: dd332b891ef5f26832a1f06a7fb2f65e620922cc [file] [log] [blame]
Tyler Chatowe51334a2019-01-20 16:58:16 -08001#include "y2019/control_loops/superstructure/superstructure.h"
2
3#include "aos/controls/control_loops.q.h"
4#include "frc971/control_loops/control_loops.q.h"
Theo Bafrali00e42272019-02-12 01:07:46 -08005#include "frc971/control_loops/static_zeroing_single_dof_profiled_subsystem.h"
Tyler Chatowe51334a2019-01-20 16:58:16 -08006
Sabina Davisc6329342019-03-01 20:44:42 -08007#include "y2019/status_light.q.h"
8
Tyler Chatowe51334a2019-01-20 16:58:16 -08009namespace y2019 {
10namespace control_loops {
11namespace superstructure {
12
Sabina Davisc6329342019-03-01 20:44:42 -080013namespace {
14
15void SendColors(float red, float green, float blue) {
16 auto new_status_light = status_light.MakeMessage();
17 new_status_light->red = red;
18 new_status_light->green = green;
19 new_status_light->blue = blue;
20
21 if (!new_status_light.Send()) {
22 LOG(ERROR, "Failed to send lights.\n");
23 }
24}
25} // namespace
26
Austin Schuh55a13dc2019-01-27 22:39:03 -080027Superstructure::Superstructure(::aos::EventLoop *event_loop,
28 const ::std::string &name)
Theo Bafrali00e42272019-02-12 01:07:46 -080029 : aos::controls::ControlLoop<SuperstructureQueue>(event_loop, name),
30 elevator_(constants::GetValues().elevator.subsystem_params),
31 wrist_(constants::GetValues().wrist.subsystem_params),
32 intake_(constants::GetValues().intake),
33 stilts_(constants::GetValues().stilts.subsystem_params) {}
Tyler Chatowe51334a2019-01-20 16:58:16 -080034
Theo Bafrali00e42272019-02-12 01:07:46 -080035void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal,
36 const SuperstructureQueue::Position *position,
37 SuperstructureQueue::Output *output,
38 SuperstructureQueue::Status *status) {
Tyler Chatowe51334a2019-01-20 16:58:16 -080039 if (WasReset()) {
40 LOG(ERROR, "WPILib reset, restarting\n");
Theo Bafrali00e42272019-02-12 01:07:46 -080041 elevator_.Reset();
42 wrist_.Reset();
43 intake_.Reset();
44 stilts_.Reset();
Tyler Chatowe51334a2019-01-20 16:58:16 -080045 }
Theo Bafrali00e42272019-02-12 01:07:46 -080046
47 elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr,
48 &(position->elevator),
49 output != nullptr ? &(output->elevator_voltage) : nullptr,
50 &(status->elevator));
51
52 wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr,
53 &(position->wrist),
54 output != nullptr ? &(output->wrist_voltage) : nullptr,
55 &(status->wrist));
56
Theo Bafrali09517b92019-02-16 15:59:17 -080057 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
58 &(position->intake_joint),
59 output != nullptr ? &(output->intake_joint_voltage) : nullptr,
60 &(status->intake));
Theo Bafrali00e42272019-02-12 01:07:46 -080061
62 stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr,
63 &(position->stilts),
64 output != nullptr ? &(output->stilts_voltage) : nullptr,
65 &(status->stilts));
66
Theo Bafrali3274a182019-02-17 20:01:38 -080067 vacuum_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr,
68 position->suction_pressure, output, &(status->has_piece),
69 event_loop());
Austin Schuhe8354752019-02-17 14:59:05 -080070
Theo Bafrali00e42272019-02-12 01:07:46 -080071 status->zeroed = status->elevator.zeroed && status->wrist.zeroed &&
72 status->intake.zeroed && status->stilts.zeroed;
73
74 status->estopped = status->elevator.estopped || status->wrist.estopped ||
75 status->intake.estopped || status->stilts.estopped;
76
Theo Bafrali09517b92019-02-16 15:59:17 -080077 if (output) {
Austin Schuh85e2e912019-02-17 15:04:29 -080078 if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) {
79 output->intake_roller_voltage = unsafe_goal->roller_voltage;
Theo Bafrali09517b92019-02-16 15:59:17 -080080 } else {
81 output->intake_roller_voltage = 0.0;
82 }
83 }
84
Theo Bafrali00e42272019-02-12 01:07:46 -080085 // TODO(theo) move these up when Iterate() is split
86 // update the goals
87 collision_avoidance_.UpdateGoal(status, unsafe_goal);
88
89 elevator_.set_min_position(collision_avoidance_.min_elevator_goal());
90 wrist_.set_min_position(collision_avoidance_.min_wrist_goal());
91 wrist_.set_max_position(collision_avoidance_.max_wrist_goal());
92 intake_.set_min_position(collision_avoidance_.min_intake_goal());
93 intake_.set_max_position(collision_avoidance_.max_intake_goal());
Sabina Davisc6329342019-03-01 20:44:42 -080094
95 if (status && unsafe_goal) {
96 // Light Logic
97 if (status->estopped) {
98 // Estop is red
99 SendColors(0.5, 0.0, 0.0);
100 } else if (unsafe_goal->suction.gamepiece_mode == 0) {
101 // Ball mode is blue
102 SendColors(0.0, 0.0, 0.5);
103 } else if (unsafe_goal->suction.gamepiece_mode == 1) {
104 // Disk mode is yellow
105 SendColors(0.5, 0.5, 0.0);
106 } else {
107 SendColors(0.0, 0.0, 0.0);
108 }
109 }
Tyler Chatowe51334a2019-01-20 16:58:16 -0800110}
111
112} // namespace superstructure
113} // namespace control_loops
Austin Schuh55a13dc2019-01-27 22:39:03 -0800114} // namespace y2019