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