blob: 21acb2e97e800deceb3d992b14fb65b80f448151 [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;
10constexpr aos::monotonic_clock::duration Vacuum::kTimeToKeepPumpRunning;
11
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;
17 bool no_goal_for_a_bit = false;
18
19 // implement a simple low-pass filter on the pressure
20 filtered_pressure_ = kSuctionAlpha * suction_pressure +
21 (1 - kSuctionAlpha) * filtered_pressure_;
22
23 *has_piece = filtered_pressure_ < kVacuumThreshold;
24
25 if (*has_piece && !had_piece_) {
26 time_at_last_acquisition_ = monotonic_now;
27 }
28
29 // if we've had the piece for enought time, go to lower pump_voltage
30 low_pump_voltage =
31 *has_piece &&
32 monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage;
33 no_goal_for_a_bit =
34 monotonic_now > time_at_last_evacuate_goal_ + kTimeToKeepPumpRunning;
35
36 if (unsafe_goal && output) {
37 const bool evacuate = unsafe_goal->top || unsafe_goal->bottom;
38 if (evacuate) {
39 time_at_last_evacuate_goal_ = monotonic_now;
40 }
41
42 // Once the vacuum evacuates, the pump speeds up because there is no
43 // resistance. So, we want to turn it down to save the pump from
44 // overheating.
45 output->pump_voltage =
46 (no_goal_for_a_bit) ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage
47 : kPumpVoltage);
48
49 output->intake_suction_top = unsafe_goal->top;
50 output->intake_suction_bottom = unsafe_goal->bottom;
51 }
52 had_piece_ = *has_piece;
53}
54
55} // namespace superstructure
56} // namespace control_loops
57} // namespace y2019