blob: cfd443aa4c35f1e18bf1efaafc4d994c3a982448 [file] [log] [blame]
James Kuszmaul9a1733a2023-02-19 16:51:47 -08001#ifndef FRC971_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATION_UTILS_H_
2#define FRC971_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATION_UTILS_H_
3#include <Eigen/Dense>
4
5#include "aos/events/event_loop.h"
6#include "aos/network/message_bridge_server_generated.h"
7#include "frc971/control_loops/drivetrain/drivetrain_output_generated.h"
8#include "frc971/input/joystick_state_generated.h"
9#include "frc971/vision/calibration_generated.h"
10
11namespace frc971::control_loops::drivetrain {
12// This class provides a variety of checks that have generally proved useful for
13// the localizer but which have no clear place to live otherwise.
14// Specifically, it tracks:
15// * Drivetrain voltages, including checks for whether the Output message
16// has timed out.
17// * Offsets between monotonic clocks on different devices.
18// * Whether we are in autonomous mode.
19class LocalizationUtils {
20 public:
21 LocalizationUtils(aos::EventLoop *event_loop);
22
23 // Returns the latest drivetrain output voltage, or zero if no output is
24 // available (which happens when the robot is disabled; when the robot is
25 // disabled, the voltage is functionally zero). Return value will be
26 // [left_voltage, right_voltage]
27 Eigen::Vector2d VoltageOrZero(aos::monotonic_clock::time_point now);
28
29 // Returns true if either there is no JoystickState message available or if
30 // we are currently in autonomous mode.
31 bool MaybeInAutonomous();
James Kuszmaul4fe845a2023-03-26 12:57:30 -070032 aos::Alliance Alliance();
James Kuszmaul9a1733a2023-02-19 16:51:47 -080033
34 // Returns the offset between our node and the specified node (or nullopt if
35 // no offset is available). The sign of this will be such that the time on
36 // the remote node = time on our node + ClockOffset().
37 std::optional<aos::monotonic_clock::duration> ClockOffset(
38 std::string_view node);
39
40 private:
41 aos::Fetcher<frc971::control_loops::drivetrain::Output> output_fetcher_;
42 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
43 aos::Fetcher<aos::JoystickState> joystick_state_fetcher_;
44};
45
46// Converts a flatbuffer TransformationMatrix to an Eigen matrix.
47Eigen::Matrix<double, 4, 4> FlatbufferToTransformationMatrix(
48 const frc971::vision::calibration::TransformationMatrix &flatbuffer);
49
50} // namespace frc971::control_loops::drivetrain
51
52#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATION_UTILS_H_