blob: 1f4aadcc33f1f5e56e09cbd3b040a374eac4ca11 [file] [log] [blame]
James Kuszmaule3df1ed2023-02-20 16:21:17 -08001#include "frc971/control_loops/drivetrain/localization/utils.h"
James Kuszmaul9a1733a2023-02-19 16:51:47 -08002
3namespace frc971::control_loops::drivetrain {
4
5LocalizationUtils::LocalizationUtils(aos::EventLoop *event_loop)
James Kuszmaul19da3232024-03-01 21:28:43 -08006 : event_loop_(event_loop),
7 output_fetcher_(event_loop->MakeFetcher<Output>("/drivetrain")),
James Kuszmaul19612ab2024-02-17 20:45:58 -08008 position_fetcher_(event_loop->TryMakeFetcher<Position>("/drivetrain")),
James Kuszmaul9a1733a2023-02-19 16:51:47 -08009 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
15Eigen::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 Kuszmaul19612ab2024-02-17 20:45:58 -080028std::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 Kuszmaul9a1733a2023-02-19 16:51:47 -080042bool LocalizationUtils::MaybeInAutonomous() {
43 joystick_state_fetcher_.Fetch();
44 return (joystick_state_fetcher_.get() != nullptr)
45 ? joystick_state_fetcher_->autonomous()
46 : true;
47}
48
James Kuszmaul4fe845a2023-03-26 12:57:30 -070049aos::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 Kuszmaul4fe845a2023-03-26 12:57:30 -070054}
55
James Kuszmaul9a1733a2023-02-19 16:51:47 -080056std::optional<aos::monotonic_clock::duration> LocalizationUtils::ClockOffset(
57 std::string_view node) {
James Kuszmaul19da3232024-03-01 21:28:43 -080058 if (node == event_loop_->node()->name()->string_view()) {
59 return std::chrono::seconds(0);
60 }
James Kuszmaul9a1733a2023-02-19 16:51:47 -080061 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.
85Eigen::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