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; |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame^] | 10 | constexpr aos::monotonic_clock::duration Vacuum::kReleaseTime; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 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; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 17 | |
| 18 | // implement a simple low-pass filter on the pressure |
| 19 | filtered_pressure_ = kSuctionAlpha * suction_pressure + |
| 20 | (1 - kSuctionAlpha) * filtered_pressure_; |
| 21 | |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 22 | const bool new_has_piece = filtered_pressure_ < kVacuumThreshold; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 24 | if (new_has_piece && !had_piece_) { |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 25 | time_at_last_acquisition_ = monotonic_now; |
| 26 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 27 | *has_piece = |
| 28 | monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage && |
| 29 | new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 30 | |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame^] | 31 | // If we've had the piece for enough time, go to lower pump_voltage |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 32 | low_pump_voltage = *has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 33 | |
| 34 | if (unsafe_goal && output) { |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame^] | 35 | const bool release = !unsafe_goal->top && !unsafe_goal->bottom; |
| 36 | |
| 37 | if (release) { |
| 38 | last_release_time_ = monotonic_now; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 39 | } |
| 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 Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame^] | 45 | release ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage : kPumpVoltage); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 46 | |
| 47 | output->intake_suction_top = unsafe_goal->top; |
| 48 | output->intake_suction_bottom = unsafe_goal->bottom; |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame^] | 49 | |
| 50 | // If we intend to release, or recently released, set has_piece to false so |
| 51 | // that we give the part of the vacuum circuit with the pressure sensor time |
| 52 | // to equilibrate with the rest of the suction cup. |
| 53 | if (release || monotonic_now < last_release_time_ + kReleaseTime) { |
| 54 | *has_piece = false; |
| 55 | } |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 56 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 57 | had_piece_ = new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | } // namespace superstructure |
| 61 | } // namespace control_loops |
| 62 | } // namespace y2019 |