Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 1 | #include "y2019/control_loops/superstructure/vacuum.h" |
| 2 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
| 5 | #include "y2019/control_loops/superstructure/superstructure.q.h" |
| 6 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 7 | namespace y2019 { |
| 8 | namespace control_loops { |
| 9 | namespace superstructure { |
| 10 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 11 | namespace chrono = ::std::chrono; |
| 12 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 13 | constexpr double Vacuum::kPumpVoltage; |
| 14 | constexpr double Vacuum::kPumpHasPieceVoltage; |
| 15 | constexpr aos::monotonic_clock::duration Vacuum::kTimeAtHigherVoltage; |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 16 | constexpr aos::monotonic_clock::duration Vacuum::kReleaseTime; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 17 | |
| 18 | void 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 Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 23 | |
| 24 | // implement a simple low-pass filter on the pressure |
| 25 | filtered_pressure_ = kSuctionAlpha * suction_pressure + |
| 26 | (1 - kSuctionAlpha) * filtered_pressure_; |
| 27 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 28 | const bool new_has_piece = |
| 29 | filtered_pressure_ < (filtered_had_piece_near_disabled_ |
| 30 | ? kVacuumOffThreshold |
| 31 | : kVacuumOnThreshold); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 33 | if (new_has_piece && !had_piece_) { |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 34 | time_at_last_acquisition_ = monotonic_now; |
| 35 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 36 | *has_piece = |
| 37 | monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage && |
| 38 | new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 39 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 40 | if (!output && *has_piece) { |
| 41 | last_disable_has_piece_time_ = monotonic_now; |
| 42 | } |
| 43 | |
| 44 | |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 45 | // If we've had the piece for enough time, go to lower pump_voltage |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 46 | low_pump_voltage = *has_piece && filtered_pressure_ < kVacuumOnThreshold; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 47 | |
| 48 | if (unsafe_goal && output) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 49 | const bool release = !unsafe_goal->grab_piece; |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 50 | |
| 51 | if (release) { |
| 52 | last_release_time_ = monotonic_now; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 53 | } |
| 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 Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 59 | release ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage : kPumpVoltage); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 60 | |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 61 | 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 Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 71 | |
| 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 Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 78 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 79 | had_piece_ = new_has_piece; |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 80 | |
| 81 | filtered_had_piece_near_disabled_ = |
| 82 | *has_piece && |
| 83 | monotonic_now < last_disable_has_piece_time_ + chrono::milliseconds(250); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace superstructure |
| 87 | } // namespace control_loops |
| 88 | } // namespace y2019 |