blob: 956bb790ea7ef29fb72b1fae03842cb099d54ea9 [file] [log] [blame]
Theo Bafrali3274a182019-02-17 20:01:38 -08001#include "y2019/control_loops/superstructure/vacuum.h"
2
Austin Schuh54629412019-04-14 19:53:59 -07003#include <chrono>
4
5#include "y2019/control_loops/superstructure/superstructure.q.h"
6
Theo Bafrali3274a182019-02-17 20:01:38 -08007namespace y2019 {
8namespace control_loops {
9namespace superstructure {
10
Austin Schuh54629412019-04-14 19:53:59 -070011namespace chrono = ::std::chrono;
12
Theo Bafrali3274a182019-02-17 20:01:38 -080013constexpr double Vacuum::kPumpVoltage;
14constexpr double Vacuum::kPumpHasPieceVoltage;
15constexpr aos::monotonic_clock::duration Vacuum::kTimeAtHigherVoltage;
Tyler Chatow7db827f2019-02-24 00:10:13 -080016constexpr aos::monotonic_clock::duration Vacuum::kReleaseTime;
Theo Bafrali3274a182019-02-17 20:01:38 -080017
18void Vacuum::Iterate(const SuctionGoal *unsafe_goal, float suction_pressure,
19 SuperstructureQueue::Output *output, bool *has_piece,
20 aos::EventLoop *event_loop) {
21 auto monotonic_now = event_loop->monotonic_now();
22 bool low_pump_voltage = false;
Theo Bafrali3274a182019-02-17 20:01:38 -080023
24 // implement a simple low-pass filter on the pressure
25 filtered_pressure_ = kSuctionAlpha * suction_pressure +
26 (1 - kSuctionAlpha) * filtered_pressure_;
27
Austin Schuh54629412019-04-14 19:53:59 -070028 const bool new_has_piece =
29 filtered_pressure_ < (filtered_had_piece_near_disabled_
30 ? kVacuumOffThreshold
31 : kVacuumOnThreshold);
Theo Bafrali3274a182019-02-17 20:01:38 -080032
Austin Schuh830fc862019-02-22 20:46:56 -080033 if (new_has_piece && !had_piece_) {
Theo Bafrali3274a182019-02-17 20:01:38 -080034 time_at_last_acquisition_ = monotonic_now;
35 }
Austin Schuh830fc862019-02-22 20:46:56 -080036 *has_piece =
37 monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage &&
38 new_has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080039
Austin Schuh54629412019-04-14 19:53:59 -070040 if (!output && *has_piece) {
41 last_disable_has_piece_time_ = monotonic_now;
42 }
43
44
Tyler Chatow7db827f2019-02-24 00:10:13 -080045 // If we've had the piece for enough time, go to lower pump_voltage
Austin Schuh54629412019-04-14 19:53:59 -070046 low_pump_voltage = *has_piece && filtered_pressure_ < kVacuumOnThreshold;
Theo Bafrali3274a182019-02-17 20:01:38 -080047
48 if (unsafe_goal && output) {
Sabina Davisc6329342019-03-01 20:44:42 -080049 const bool release = !unsafe_goal->grab_piece;
Tyler Chatow7db827f2019-02-24 00:10:13 -080050
51 if (release) {
52 last_release_time_ = monotonic_now;
Theo Bafrali3274a182019-02-17 20:01:38 -080053 }
54
55 // Once the vacuum evacuates, the pump speeds up because there is no
56 // resistance. So, we want to turn it down to save the pump from
57 // overheating.
58 output->pump_voltage =
Tyler Chatow7db827f2019-02-24 00:10:13 -080059 release ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage : kPumpVoltage);
Theo Bafrali3274a182019-02-17 20:01:38 -080060
Sabina Davisc6329342019-03-01 20:44:42 -080061 if (unsafe_goal->grab_piece && unsafe_goal->gamepiece_mode == 0) {
62 output->intake_suction_top = false;
63 output->intake_suction_bottom = true;
64 } else if (unsafe_goal->grab_piece && unsafe_goal->gamepiece_mode == 1) {
65 output->intake_suction_top = true;
66 output->intake_suction_bottom = true;
67 } else {
68 output->intake_suction_top = false;
69 output->intake_suction_bottom = false;
70 }
Tyler Chatow7db827f2019-02-24 00:10:13 -080071
72 // If we intend to release, or recently released, set has_piece to false so
73 // that we give the part of the vacuum circuit with the pressure sensor time
74 // to equilibrate with the rest of the suction cup.
75 if (release || monotonic_now < last_release_time_ + kReleaseTime) {
76 *has_piece = false;
77 }
Theo Bafrali3274a182019-02-17 20:01:38 -080078 }
Austin Schuh830fc862019-02-22 20:46:56 -080079 had_piece_ = new_has_piece;
Austin Schuh54629412019-04-14 19:53:59 -070080
81 filtered_had_piece_near_disabled_ =
82 *has_piece &&
83 monotonic_now < last_disable_has_piece_time_ + chrono::milliseconds(250);
Theo Bafrali3274a182019-02-17 20:01:38 -080084}
85
86} // namespace superstructure
87} // namespace control_loops
88} // namespace y2019