Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/superstructure/vacuum.h" |
| 2 | |
| 3 | namespace y2019 { |
| 4 | namespace control_loops { |
| 5 | namespace superstructure { |
| 6 | |
| 7 | constexpr double Vacuum::kPumpVoltage; |
| 8 | constexpr double Vacuum::kPumpHasPieceVoltage; |
| 9 | constexpr aos::monotonic_clock::duration Vacuum::kTimeAtHigherVoltage; |
| 10 | constexpr aos::monotonic_clock::duration Vacuum::kTimeToKeepPumpRunning; |
| 11 | |
| 12 | void 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 Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame^] | 23 | const bool new_has_piece = filtered_pressure_ < kVacuumThreshold; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 24 | |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame^] | 25 | if (new_has_piece && !had_piece_) { |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 26 | time_at_last_acquisition_ = monotonic_now; |
| 27 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame^] | 28 | *has_piece = |
| 29 | monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage && |
| 30 | new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 31 | |
| 32 | // if we've had the piece for enought time, go to lower pump_voltage |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame^] | 33 | low_pump_voltage = *has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 34 | 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 Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame^] | 53 | had_piece_ = new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace superstructure |
| 57 | } // namespace control_loops |
| 58 | } // namespace y2019 |