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