James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 1 | #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" |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame^] | 8 | #include "frc971/control_loops/drivetrain/drivetrain_position_generated.h" |
| 9 | #include "frc971/control_loops/drivetrain/hybrid_ekf.h" |
| 10 | #include "frc971/control_loops/pose.h" |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 11 | #include "frc971/input/joystick_state_generated.h" |
| 12 | #include "frc971/vision/calibration_generated.h" |
| 13 | |
| 14 | namespace frc971::control_loops::drivetrain { |
| 15 | // This class provides a variety of checks that have generally proved useful for |
| 16 | // the localizer but which have no clear place to live otherwise. |
| 17 | // Specifically, it tracks: |
| 18 | // * Drivetrain voltages, including checks for whether the Output message |
| 19 | // has timed out. |
| 20 | // * Offsets between monotonic clocks on different devices. |
| 21 | // * Whether we are in autonomous mode. |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame^] | 22 | // * Drivetrain encoder voltages, as reported by the roborio. |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 23 | class LocalizationUtils { |
| 24 | public: |
| 25 | LocalizationUtils(aos::EventLoop *event_loop); |
| 26 | |
| 27 | // Returns the latest drivetrain output voltage, or zero if no output is |
| 28 | // available (which happens when the robot is disabled; when the robot is |
| 29 | // disabled, the voltage is functionally zero). Return value will be |
| 30 | // [left_voltage, right_voltage] |
| 31 | Eigen::Vector2d VoltageOrZero(aos::monotonic_clock::time_point now); |
| 32 | |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame^] | 33 | // Returns the latest drivetrain encoder values (in meters), or nullopt if no |
| 34 | // position message is available (or if the message is stale). |
| 35 | // Returns encoders as [left_position, right_position] |
| 36 | std::optional<Eigen::Vector2d> Encoders(aos::monotonic_clock::time_point now); |
| 37 | |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 38 | // Returns true if either there is no JoystickState message available or if |
| 39 | // we are currently in autonomous mode. |
| 40 | bool MaybeInAutonomous(); |
James Kuszmaul | 4fe845a | 2023-03-26 12:57:30 -0700 | [diff] [blame] | 41 | aos::Alliance Alliance(); |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 42 | |
| 43 | // Returns the offset between our node and the specified node (or nullopt if |
| 44 | // no offset is available). The sign of this will be such that the time on |
| 45 | // the remote node = time on our node + ClockOffset(). |
| 46 | std::optional<aos::monotonic_clock::duration> ClockOffset( |
| 47 | std::string_view node); |
| 48 | |
| 49 | private: |
| 50 | aos::Fetcher<frc971::control_loops::drivetrain::Output> output_fetcher_; |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame^] | 51 | aos::Fetcher<frc971::control_loops::drivetrain::Position> position_fetcher_; |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 52 | aos::Fetcher<aos::message_bridge::ServerStatistics> clock_offset_fetcher_; |
| 53 | aos::Fetcher<aos::JoystickState> joystick_state_fetcher_; |
| 54 | }; |
| 55 | |
| 56 | // Converts a flatbuffer TransformationMatrix to an Eigen matrix. |
| 57 | Eigen::Matrix<double, 4, 4> FlatbufferToTransformationMatrix( |
| 58 | const frc971::vision::calibration::TransformationMatrix &flatbuffer); |
| 59 | |
James Kuszmaul | 19612ab | 2024-02-17 20:45:58 -0800 | [diff] [blame^] | 60 | // This approximates the Jacobian of a vector of [heading, distance, skew] |
| 61 | // of a target with respect to the full state of a drivetrain EKF. |
| 62 | // Note that the only nonzero values in the returned matrix will be in the |
| 63 | // columns corresponding to the X, Y, and Theta components of the state. |
| 64 | // This is suitable for use as the H matrix in the kalman updates of the EKF, |
| 65 | // although due to the approximation it should not be used to actually |
| 66 | // calculate the expected measurement. |
| 67 | // target_pose is the global pose of the target that we have identified. |
| 68 | // camera_pose is the current estimate of the global pose of |
| 69 | // the camera that can see the target. |
| 70 | template <typename Scalar> |
| 71 | Eigen::Matrix<double, 3, HybridEkf<Scalar>::kNStates> |
| 72 | HMatrixForCameraHeadingDistanceSkew(const TypedPose<Scalar> &target_pose, |
| 73 | const TypedPose<Scalar> &camera_pose) { |
| 74 | // For all of the below calculations, we will assume to a first |
| 75 | // approximation that: |
| 76 | // |
| 77 | // dcamera_theta / dtheta ~= 1 |
| 78 | // dcamera_x / dx ~= 1 |
| 79 | // dcamera_y / dy ~= 1 |
| 80 | // |
| 81 | // For cameras sufficiently far from the robot's origin, or if the robot were |
| 82 | // spinning extremely rapidly, this would not hold. |
| 83 | |
| 84 | // To calculate dheading/d{x,y,theta}: |
| 85 | // heading = arctan2(target_pos - camera_pos) - camera_theta |
| 86 | Eigen::Matrix<Scalar, 3, 1> target_pos = target_pose.abs_pos(); |
| 87 | Eigen::Matrix<Scalar, 3, 1> camera_pos = camera_pose.abs_pos(); |
| 88 | Scalar diffx = target_pos.x() - camera_pos.x(); |
| 89 | Scalar diffy = target_pos.y() - camera_pos.y(); |
| 90 | Scalar norm2 = diffx * diffx + diffy * diffy; |
| 91 | Scalar dheadingdx = diffy / norm2; |
| 92 | Scalar dheadingdy = -diffx / norm2; |
| 93 | Scalar dheadingdtheta = -1.0; |
| 94 | |
| 95 | // To calculate ddistance/d{x,y}: |
| 96 | // distance = sqrt(diffx^2 + diffy^2) |
| 97 | Scalar distance = ::std::sqrt(norm2); |
| 98 | Scalar ddistdx = -diffx / distance; |
| 99 | Scalar ddistdy = -diffy / distance; |
| 100 | |
| 101 | // Skew = target.theta - camera.theta - heading |
| 102 | // = target.theta - arctan2(target_pos - camera_pos) |
| 103 | Scalar dskewdx = -dheadingdx; |
| 104 | Scalar dskewdy = -dheadingdy; |
| 105 | Eigen::Matrix<Scalar, 3, HybridEkf<Scalar>::kNStates> H; |
| 106 | H.setZero(); |
| 107 | H(0, HybridEkf<Scalar>::kX) = dheadingdx; |
| 108 | H(0, HybridEkf<Scalar>::kY) = dheadingdy; |
| 109 | H(0, HybridEkf<Scalar>::kTheta) = dheadingdtheta; |
| 110 | H(1, HybridEkf<Scalar>::kX) = ddistdx; |
| 111 | H(1, HybridEkf<Scalar>::kY) = ddistdy; |
| 112 | H(2, HybridEkf<Scalar>::kX) = dskewdx; |
| 113 | H(2, HybridEkf<Scalar>::kY) = dskewdy; |
| 114 | return H; |
| 115 | } |
| 116 | |
James Kuszmaul | 9a1733a | 2023-02-19 16:51:47 -0800 | [diff] [blame] | 117 | } // namespace frc971::control_loops::drivetrain |
| 118 | |
| 119 | #endif // FRC971_CONTROL_LOOPS_DRIVETRAIN_LOCALIZATION_UTILS_H_ |