James Kuszmaul | e3df1ed | 2023-02-20 16:21:17 -0800 | [diff] [blame] | 1 | #include "frc971/control_loops/drivetrain/localization/utils.h" |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 2 | |
| 3 | namespace frc971::control_loops::drivetrain { |
| 4 | |
| 5 | LocalizationUtils::LocalizationUtils(aos::EventLoop *event_loop) |
James Kuszmaul | 19da323 | 2024-03-01 21:28:43 -0800 | [diff] [blame] | 6 | : event_loop_(event_loop), |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 7 | output_fetcher_(event_loop->TryMakeFetcher<Output>("/drivetrain")), |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame] | 8 | position_fetcher_(event_loop->TryMakeFetcher<Position>("/drivetrain")), |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 9 | combined_fetcher_( |
| 10 | event_loop->TryMakeFetcher<RioLocalizerInputs>("/drivetrain")), |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 11 | clock_offset_fetcher_( |
| 12 | event_loop->MakeFetcher<aos::message_bridge::ServerStatistics>( |
| 13 | "/aos")), |
| 14 | joystick_state_fetcher_( |
| 15 | event_loop->MakeFetcher<aos::JoystickState>("/roborio/aos")) {} |
| 16 | |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 17 | namespace { |
| 18 | template <typename T> |
| 19 | Eigen::Vector2d GetVoltage(T &fetcher, aos::monotonic_clock::time_point now) { |
| 20 | fetcher.Fetch(); |
| 21 | // Determine if the robot is likely to be disabled currently. |
| 22 | const bool disabled = |
| 23 | (fetcher.get() == nullptr) || |
James Kuszmaul | 8ca5dc1 | 2024-04-05 17:31:15 -0700 | [diff] [blame] | 24 | (fetcher.context().monotonic_event_time + std::chrono::milliseconds(500) < |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 25 | now); |
| 26 | return disabled ? Eigen::Vector2d::Zero() |
| 27 | : Eigen::Vector2d{fetcher->left_voltage(), |
| 28 | fetcher->right_voltage()}; |
| 29 | } |
| 30 | } // namespace |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 31 | Eigen::Vector2d LocalizationUtils::VoltageOrZero( |
| 32 | aos::monotonic_clock::time_point now) { |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 33 | if (output_fetcher_.valid()) { |
| 34 | return GetVoltage(output_fetcher_, now); |
| 35 | } else { |
| 36 | CHECK(combined_fetcher_.valid()); |
| 37 | return GetVoltage(combined_fetcher_, now); |
| 38 | } |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 39 | } |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 40 | namespace { |
| 41 | template <typename T> |
| 42 | std::optional<Eigen::Vector2d> GetPosition( |
| 43 | T &fetcher, aos::monotonic_clock::time_point now) { |
James Kuszmaul | 2700e0f | 2024-03-16 16:45:48 -0700 | [diff] [blame] | 44 | if (!fetcher.Fetch()) { |
| 45 | return std::nullopt; |
| 46 | } |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 47 | const bool stale = |
| 48 | (fetcher.get() == nullptr) || |
| 49 | (fetcher.context().monotonic_event_time + std::chrono::milliseconds(10) < |
| 50 | now); |
| 51 | return stale ? std::nullopt |
| 52 | : std::make_optional<Eigen::Vector2d>(fetcher->left_encoder(), |
| 53 | fetcher->right_encoder()); |
| 54 | } |
| 55 | } // namespace |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 56 | |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame] | 57 | std::optional<Eigen::Vector2d> LocalizationUtils::Encoders( |
| 58 | aos::monotonic_clock::time_point now) { |
James Kuszmaul | f6aa038 | 2024-03-01 19:46:05 -0800 | [diff] [blame] | 59 | if (position_fetcher_.valid()) { |
| 60 | return GetPosition(position_fetcher_, now); |
| 61 | } else { |
| 62 | CHECK(combined_fetcher_.valid()); |
| 63 | return GetPosition(combined_fetcher_, now); |
| 64 | } |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame] | 65 | } |
| 66 | |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 67 | bool LocalizationUtils::MaybeInAutonomous() { |
| 68 | joystick_state_fetcher_.Fetch(); |
| 69 | return (joystick_state_fetcher_.get() != nullptr) |
| 70 | ? joystick_state_fetcher_->autonomous() |
| 71 | : true; |
| 72 | } |
| 73 | |
James Kuszmaul | 4fe845a | 2023-03-26 12:57:30 -0700 | [diff] [blame] | 74 | aos::Alliance LocalizationUtils::Alliance() { |
| 75 | joystick_state_fetcher_.Fetch(); |
| 76 | return (joystick_state_fetcher_.get() != nullptr) |
| 77 | ? joystick_state_fetcher_->alliance() |
| 78 | : aos::Alliance::kInvalid; |
James Kuszmaul | 4fe845a | 2023-03-26 12:57:30 -0700 | [diff] [blame] | 79 | } |
| 80 | |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 81 | std::optional<aos::monotonic_clock::duration> LocalizationUtils::ClockOffset( |
| 82 | std::string_view node) { |
James Kuszmaul | 19da323 | 2024-03-01 21:28:43 -0800 | [diff] [blame] | 83 | if (node == event_loop_->node()->name()->string_view()) { |
| 84 | return std::chrono::seconds(0); |
| 85 | } |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 86 | std::optional<aos::monotonic_clock::duration> monotonic_offset; |
| 87 | clock_offset_fetcher_.Fetch(); |
| 88 | if (clock_offset_fetcher_.get() != nullptr) { |
| 89 | for (const auto connection : *clock_offset_fetcher_->connections()) { |
| 90 | if (connection->has_node() && connection->node()->has_name() && |
| 91 | connection->node()->name()->string_view() == node) { |
| 92 | if (connection->has_monotonic_offset()) { |
| 93 | monotonic_offset = |
| 94 | std::chrono::nanoseconds(connection->monotonic_offset()); |
| 95 | } else { |
| 96 | // If we don't have a monotonic offset, that means we aren't |
| 97 | // connected. |
| 98 | return std::nullopt; |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | CHECK(monotonic_offset.has_value()); |
| 105 | return monotonic_offset; |
| 106 | } |
| 107 | |
| 108 | // Technically, this should be able to do a single memcpy, but the extra |
| 109 | // verbosity here seems appropriate. |
| 110 | Eigen::Matrix<double, 4, 4> FlatbufferToTransformationMatrix( |
| 111 | const frc971::vision::calibration::TransformationMatrix &flatbuffer) { |
| 112 | CHECK_EQ(16u, CHECK_NOTNULL(flatbuffer.data())->size()); |
| 113 | Eigen::Matrix<double, 4, 4> result; |
| 114 | result.setIdentity(); |
| 115 | for (int row = 0; row < 4; ++row) { |
| 116 | for (int col = 0; col < 4; ++col) { |
| 117 | result(row, col) = (*flatbuffer.data())[row * 4 + col]; |
| 118 | } |
| 119 | } |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | } // namespace frc971::control_loops::drivetrain |