blob: 26242f9de2d7c7b3396c3600ee154650f3113b31 [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();
32
33 // Returns the offset between our node and the specified node (or nullopt if
34 // no offset is available). The sign of this will be such that the time on
35 // the remote node = time on our node + ClockOffset().
36 std::optional<aos::monotonic_clock::duration> ClockOffset(
37 std::string_view node);
38
39 private:
40 aos::Fetcher<frc971::control_loops::drivetrain::Output> output_fetcher_;
41 aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_;
42 aos::Fetcher<aos::JoystickState> joystick_state_fetcher_;
43};
44
45// Converts a flatbuffer TransformationMatrix to an Eigen matrix.
46Eigen::Matrix<double, 4, 4> FlatbufferToTransformationMatrix(
47 const frc971::vision::calibration::TransformationMatrix &flatbuffer);
48
49} // namespace frc971::control_loops::drivetrain
50
51#endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATION_UTILS_H_