blob: d4c1c6b70a084f1c33863724a4f130225536b887 [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
Austin Schuh830fc862019-02-22 20:46:56 -080023 const bool new_has_piece = filtered_pressure_ < kVacuumThreshold;
Theo Bafrali3274a182019-02-17 20:01:38 -080024
Austin Schuh830fc862019-02-22 20:46:56 -080025 if (new_has_piece && !had_piece_) {
Theo Bafrali3274a182019-02-17 20:01:38 -080026 time_at_last_acquisition_ = monotonic_now;
27 }
Austin Schuh830fc862019-02-22 20:46:56 -080028 *has_piece =
29 monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage &&
30 new_has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080031
32 // if we've had the piece for enought time, go to lower pump_voltage
Austin Schuh830fc862019-02-22 20:46:56 -080033 low_pump_voltage = *has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080034 no_goal_for_a_bit =
35 monotonic_now > time_at_last_evacuate_goal_ + kTimeToKeepPumpRunning;
36
37 if (unsafe_goal && output) {
38 const bool evacuate = unsafe_goal->top || unsafe_goal->bottom;
39 if (evacuate) {
40 time_at_last_evacuate_goal_ = monotonic_now;
41 }
42
43 // Once the vacuum evacuates, the pump speeds up because there is no
44 // resistance. So, we want to turn it down to save the pump from
45 // overheating.
46 output->pump_voltage =
47 (no_goal_for_a_bit) ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage
48 : kPumpVoltage);
49
50 output->intake_suction_top = unsafe_goal->top;
51 output->intake_suction_bottom = unsafe_goal->bottom;
52 }
Austin Schuh830fc862019-02-22 20:46:56 -080053 had_piece_ = new_has_piece;
Theo Bafrali3274a182019-02-17 20:01:38 -080054}
55
56} // namespace superstructure
57} // namespace control_loops
58} // namespace y2019