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 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 10 | namespace y2019::control_loops::superstructure { |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 12 | namespace chrono = ::std::chrono; |
| 13 | |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 14 | constexpr double Vacuum::kPumpVoltage; |
| 15 | constexpr double Vacuum::kPumpHasPieceVoltage; |
| 16 | constexpr aos::monotonic_clock::duration Vacuum::kTimeAtHigherVoltage; |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 17 | constexpr aos::monotonic_clock::duration Vacuum::kReleaseTime; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 18 | |
| 19 | void Vacuum::Iterate(const SuctionGoal *unsafe_goal, float suction_pressure, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | OutputT *output, bool *has_piece, |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 21 | aos::EventLoop *event_loop) { |
| 22 | auto monotonic_now = event_loop->monotonic_now(); |
| 23 | bool low_pump_voltage = false; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 24 | |
| 25 | // implement a simple low-pass filter on the pressure |
| 26 | filtered_pressure_ = kSuctionAlpha * suction_pressure + |
| 27 | (1 - kSuctionAlpha) * filtered_pressure_; |
| 28 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 29 | const bool new_has_piece = |
| 30 | filtered_pressure_ < (filtered_had_piece_near_disabled_ |
| 31 | ? kVacuumOffThreshold |
| 32 | : kVacuumOnThreshold); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 33 | |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 34 | if (new_has_piece && !had_piece_) { |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 35 | time_at_last_acquisition_ = monotonic_now; |
| 36 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 37 | *has_piece = |
| 38 | monotonic_now > time_at_last_acquisition_ + kTimeAtHigherVoltage && |
| 39 | new_has_piece; |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 41 | if (!output && *has_piece) { |
| 42 | last_disable_has_piece_time_ = monotonic_now; |
| 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) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | if (unsafe_goal->grab_piece() && unsafe_goal->gamepiece_mode() == 0) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 62 | output->intake_suction_top = false; |
| 63 | output->intake_suction_bottom = true; |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 64 | } else if (unsafe_goal->grab_piece() && |
| 65 | unsafe_goal->gamepiece_mode() == 1) { |
Sabina Davis | c632934 | 2019-03-01 20:44:42 -0800 | [diff] [blame] | 66 | output->intake_suction_top = true; |
| 67 | output->intake_suction_bottom = true; |
| 68 | } else { |
| 69 | output->intake_suction_top = false; |
| 70 | output->intake_suction_bottom = false; |
| 71 | } |
Tyler Chatow | 7db827f | 2019-02-24 00:10:13 -0800 | [diff] [blame] | 72 | |
| 73 | // If we intend to release, or recently released, set has_piece to false so |
| 74 | // that we give the part of the vacuum circuit with the pressure sensor time |
| 75 | // to equilibrate with the rest of the suction cup. |
| 76 | if (release || monotonic_now < last_release_time_ + kReleaseTime) { |
| 77 | *has_piece = false; |
| 78 | } |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 79 | } |
Austin Schuh | 830fc86 | 2019-02-22 20:46:56 -0800 | [diff] [blame] | 80 | had_piece_ = new_has_piece; |
Austin Schuh | 5462941 | 2019-04-14 19:53:59 -0700 | [diff] [blame] | 81 | |
| 82 | filtered_had_piece_near_disabled_ = |
| 83 | *has_piece && |
| 84 | monotonic_now < last_disable_has_piece_time_ + chrono::milliseconds(250); |
Theo Bafrali | 3274a18 | 2019-02-17 20:01:38 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 87 | } // namespace y2019::control_loops::superstructure |