blob: 187379b26b1f87cd30cc9efd042ad39343294310 [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
7namespace y2019 {
8namespace control_loops {
9namespace superstructure {
10
Austin Schuhe8354752019-02-17 14:59:05 -080011void Superstructure::HandleSuction(const SuctionGoal *unsafe_goal,
12 float suction_pressure,
13 SuperstructureQueue::Output *output,
14 bool *has_piece) {
15 constexpr double kPumpVoltage = 12.0;
16 constexpr double kPumpHasPieceVoltage = 8.0;
John Parkbdc86122019-02-10 12:35:25 -080017
Austin Schuhe8354752019-02-17 14:59:05 -080018 // TODO(austin): Low pass filter on pressure.
19 *has_piece = suction_pressure < 0.70;
20
21 if (unsafe_goal && output) {
22 const bool evacuate = unsafe_goal->top || unsafe_goal->bottom;
23 if (evacuate) {
24 vacuum_count_ = 200;
John Parkbdc86122019-02-10 12:35:25 -080025 }
Austin Schuhe8354752019-02-17 14:59:05 -080026 // TODO(austin): High speed pump a bit longer after we detect we have the
27 // game piece.
28 // Once the vacuum evacuates, the pump speeds up because there is no
29 // resistance. So, we want to turn it down to save the pump from
30 // overheating.
31 output->pump_voltage =
32 (vacuum_count_ > 0) ? (*has_piece ? kPumpHasPieceVoltage : kPumpVoltage)
33 : 0.0;
34 output->intake_suction_top = unsafe_goal->top;
35 output->intake_suction_bottom = unsafe_goal->bottom;
John Parkbdc86122019-02-10 12:35:25 -080036 }
Austin Schuhe8354752019-02-17 14:59:05 -080037 vacuum_count_ = ::std::max(0, vacuum_count_ - 1);
John Parkbdc86122019-02-10 12:35:25 -080038}
39
Austin Schuh55a13dc2019-01-27 22:39:03 -080040Superstructure::Superstructure(::aos::EventLoop *event_loop,
41 const ::std::string &name)
Theo Bafrali00e42272019-02-12 01:07:46 -080042 : aos::controls::ControlLoop<SuperstructureQueue>(event_loop, name),
43 elevator_(constants::GetValues().elevator.subsystem_params),
44 wrist_(constants::GetValues().wrist.subsystem_params),
45 intake_(constants::GetValues().intake),
46 stilts_(constants::GetValues().stilts.subsystem_params) {}
Tyler Chatowe51334a2019-01-20 16:58:16 -080047
Theo Bafrali00e42272019-02-12 01:07:46 -080048void Superstructure::RunIteration(const SuperstructureQueue::Goal *unsafe_goal,
49 const SuperstructureQueue::Position *position,
50 SuperstructureQueue::Output *output,
51 SuperstructureQueue::Status *status) {
Tyler Chatowe51334a2019-01-20 16:58:16 -080052 if (WasReset()) {
53 LOG(ERROR, "WPILib reset, restarting\n");
Theo Bafrali00e42272019-02-12 01:07:46 -080054 elevator_.Reset();
55 wrist_.Reset();
56 intake_.Reset();
57 stilts_.Reset();
Tyler Chatowe51334a2019-01-20 16:58:16 -080058 }
Theo Bafrali00e42272019-02-12 01:07:46 -080059
60 elevator_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->elevator) : nullptr,
61 &(position->elevator),
62 output != nullptr ? &(output->elevator_voltage) : nullptr,
63 &(status->elevator));
64
65 wrist_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->wrist) : nullptr,
66 &(position->wrist),
67 output != nullptr ? &(output->wrist_voltage) : nullptr,
68 &(status->wrist));
69
Theo Bafrali09517b92019-02-16 15:59:17 -080070 intake_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->intake) : nullptr,
71 &(position->intake_joint),
72 output != nullptr ? &(output->intake_joint_voltage) : nullptr,
73 &(status->intake));
Theo Bafrali00e42272019-02-12 01:07:46 -080074
75 stilts_.Iterate(unsafe_goal != nullptr ? &(unsafe_goal->stilts) : nullptr,
76 &(position->stilts),
77 output != nullptr ? &(output->stilts_voltage) : nullptr,
78 &(status->stilts));
79
Austin Schuhe8354752019-02-17 14:59:05 -080080 HandleSuction(unsafe_goal != nullptr ? &(unsafe_goal->suction) : nullptr,
81 position->suction_pressure, output, &(status->has_piece));
82
Theo Bafrali00e42272019-02-12 01:07:46 -080083 status->zeroed = status->elevator.zeroed && status->wrist.zeroed &&
84 status->intake.zeroed && status->stilts.zeroed;
85
86 status->estopped = status->elevator.estopped || status->wrist.estopped ||
87 status->intake.estopped || status->stilts.estopped;
88
Theo Bafrali09517b92019-02-16 15:59:17 -080089 if (output) {
Austin Schuh85e2e912019-02-17 15:04:29 -080090 if (unsafe_goal && status->intake.position > kMinIntakeAngleForRollers) {
91 output->intake_roller_voltage = unsafe_goal->roller_voltage;
Theo Bafrali09517b92019-02-16 15:59:17 -080092 } else {
93 output->intake_roller_voltage = 0.0;
94 }
95 }
96
Theo Bafrali00e42272019-02-12 01:07:46 -080097 // TODO(theo) move these up when Iterate() is split
98 // update the goals
99 collision_avoidance_.UpdateGoal(status, unsafe_goal);
100
101 elevator_.set_min_position(collision_avoidance_.min_elevator_goal());
102 wrist_.set_min_position(collision_avoidance_.min_wrist_goal());
103 wrist_.set_max_position(collision_avoidance_.max_wrist_goal());
104 intake_.set_min_position(collision_avoidance_.min_intake_goal());
105 intake_.set_max_position(collision_avoidance_.max_intake_goal());
Tyler Chatowe51334a2019-01-20 16:58:16 -0800106}
107
108} // namespace superstructure
109} // namespace control_loops
Austin Schuh55a13dc2019-01-27 22:39:03 -0800110} // namespace y2019