blob: 497ae5b0092b2a3d8dce5458fc909e5de3f16b4c [file] [log] [blame]
Theo Bafrali3274a182019-02-17 20:01:38 -08001#include "y2019/control_loops/superstructure/vacuum.h"
2
3namespace y2019 {
4namespace control_loops {
5namespace superstructure {
6
7constexpr double Vacuum::kPumpVoltage;
8constexpr double Vacuum::kPumpHasPieceVoltage;
9constexpr aos::monotonic_clock::duration Vacuum::kTimeAtHigherVoltage;
Tyler Chatow7db827f2019-02-24 00:10:13 -080010constexpr aos::monotonic_clock::duration Vacuum::kReleaseTime;
Theo Bafrali3274a182019-02-17 20:01:38 -080011
12void Vacuum::Iterate(const SuctionGoal *unsafe_goal, float suction_pressure,
13 SuperstructureQueue::Output *output, bool *has_piece,
14 aos::EventLoop *event_loop) {
15 auto monotonic_now = event_loop->monotonic_now();
16 bool low_pump_voltage = false;
Theo Bafrali3274a182019-02-17 20:01:38 -080017
18 // implement a simple low-pass filter on the pressure
19 filtered_pressure_ = kSuctionAlpha * suction_pressure +
20 (1 - kSuctionAlpha) * filtered_pressure_;
21
Austin Schuh830fc862019-02-22 20:46:56 -080022 const bool new_has_piece = filtered_pressure_ < kVacuumThreshold;
Theo Bafrali3274a182019-02-17 20:01:38 -080023
Austin Schuh830fc862019-02-22 20:46:56 -080024 if (new_has_piece && !had_piece_) {
Theo Bafrali3274a182019-02-17 20:01:38 -080025 time_at_last_acquisition_ = monotonic_now;
26 }
Austin Schuh830fc862019-02-22 20:46:56 -080027 *has_piece =
28 monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage &&
29 new_has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080030
Tyler Chatow7db827f2019-02-24 00:10:13 -080031 // If we've had the piece for enough time, go to lower pump_voltage
Austin Schuh830fc862019-02-22 20:46:56 -080032 low_pump_voltage = *has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080033
34 if (unsafe_goal && output) {
Sabina Davisc6329342019-03-01 20:44:42 -080035 const bool release = !unsafe_goal->grab_piece;
Tyler Chatow7db827f2019-02-24 00:10:13 -080036
37 if (release) {
38 last_release_time_ = monotonic_now;
Theo Bafrali3274a182019-02-17 20:01:38 -080039 }
40
41 // Once the vacuum evacuates, the pump speeds up because there is no
42 // resistance. So, we want to turn it down to save the pump from
43 // overheating.
44 output->pump_voltage =
Tyler Chatow7db827f2019-02-24 00:10:13 -080045 release ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage : kPumpVoltage);
Theo Bafrali3274a182019-02-17 20:01:38 -080046
Sabina Davisc6329342019-03-01 20:44:42 -080047 if (unsafe_goal->grab_piece && unsafe_goal->gamepiece_mode == 0) {
48 output->intake_suction_top = false;
49 output->intake_suction_bottom = true;
50 } else if (unsafe_goal->grab_piece && unsafe_goal->gamepiece_mode == 1) {
51 output->intake_suction_top = true;
52 output->intake_suction_bottom = true;
53 } else {
54 output->intake_suction_top = false;
55 output->intake_suction_bottom = false;
56 }
Tyler Chatow7db827f2019-02-24 00:10:13 -080057
58 // If we intend to release, or recently released, set has_piece to false so
59 // that we give the part of the vacuum circuit with the pressure sensor time
60 // to equilibrate with the rest of the suction cup.
61 if (release || monotonic_now < last_release_time_ + kReleaseTime) {
62 *has_piece = false;
63 }
Theo Bafrali3274a182019-02-17 20:01:38 -080064 }
Austin Schuh830fc862019-02-22 20:46:56 -080065 had_piece_ = new_has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080066}
67
68} // namespace superstructure
69} // namespace control_loops
70} // namespace y2019