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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "frc971/control_loops/control_loops_generated.h" |
| 6 | #include "frc971/control_loops/profiled_subsystem_generated.h" |
| 7 | #include "y2019/control_loops/superstructure/superstructure_goal_generated.h" |
| 8 | #include "y2019/control_loops/superstructure/superstructure_output_generated.h" |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 9 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 10 | namespace y2019 { |
| 11 | namespace control_loops { |
| 12 | namespace superstructure { |
| 13 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 14 | namespace chrono = ::std::chrono; |
| 15 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 16 | constexpr double Vacuum::kPumpVoltage; |
| 17 | constexpr double Vacuum::kPumpHasPieceVoltage; |
| 18 | constexpr aos::monotonic_clock::duration Vacuum::kTimeAtHigherVoltage; |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 19 | constexpr aos::monotonic_clock::duration Vacuum::kReleaseTime; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 20 | |
| 21 | void Vacuum::Iterate(const SuctionGoal *unsafe_goal, float suction_pressure, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | OutputT *output, bool *has_piece, |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 23 | aos::EventLoop *event_loop) { |
| 24 | auto monotonic_now = event_loop->monotonic_now(); |
| 25 | bool low_pump_voltage = false; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 26 | |
| 27 | // implement a simple low-pass filter on the pressure |
| 28 | filtered_pressure_ = kSuctionAlpha * suction_pressure + |
| 29 | (1 - kSuctionAlpha) * filtered_pressure_; |
| 30 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 31 | const bool new_has_piece = |
| 32 | filtered_pressure_ < (filtered_had_piece_near_disabled_ |
| 33 | ? kVacuumOffThreshold |
| 34 | : kVacuumOnThreshold); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 36 | if (new_has_piece && !had_piece_) { |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 37 | time_at_last_acquisition_ = monotonic_now; |
| 38 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 39 | *has_piece = |
| 40 | monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage && |
| 41 | new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 43 | if (!output && *has_piece) { |
| 44 | last_disable_has_piece_time_ = monotonic_now; |
| 45 | } |
| 46 | |
| 47 | |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 48 | // 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] | 49 | low_pump_voltage = *has_piece && filtered_pressure_ < kVacuumOnThreshold; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 50 | |
| 51 | if (unsafe_goal && output) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 52 | const bool release = !unsafe_goal->grab_piece(); |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 53 | |
| 54 | if (release) { |
| 55 | last_release_time_ = monotonic_now; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Once the vacuum evacuates, the pump speeds up because there is no |
| 59 | // resistance. So, we want to turn it down to save the pump from |
| 60 | // overheating. |
| 61 | output->pump_voltage = |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 62 | release ? 0 : (low_pump_voltage ? kPumpHasPieceVoltage : kPumpVoltage); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 63 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 64 | if (unsafe_goal->grab_piece() && unsafe_goal->gamepiece_mode() == 0) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 65 | output->intake_suction_top = false; |
| 66 | output->intake_suction_bottom = true; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | } else if (unsafe_goal->grab_piece() && unsafe_goal->gamepiece_mode() == 1) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 68 | output->intake_suction_top = true; |
| 69 | output->intake_suction_bottom = true; |
| 70 | } else { |
| 71 | output->intake_suction_top = false; |
| 72 | output->intake_suction_bottom = false; |
| 73 | } |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 74 | |
| 75 | // If we intend to release, or recently released, set has_piece to false so |
| 76 | // that we give the part of the vacuum circuit with the pressure sensor time |
| 77 | // to equilibrate with the rest of the suction cup. |
| 78 | if (release || monotonic_now < last_release_time_ + kReleaseTime) { |
| 79 | *has_piece = false; |
| 80 | } |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 81 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 82 | had_piece_ = new_has_piece; |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 83 | |
| 84 | filtered_had_piece_near_disabled_ = |
| 85 | *has_piece && |
| 86 | monotonic_now < last_disable_has_piece_time_ + chrono::milliseconds(250); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace superstructure |
| 90 | } // namespace control_loops |
| 91 | } // namespace y2019 |