WPILibC++ 2024.1.1-beta-4
AprilTagFieldLayout.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <optional>
8#include <string_view>
9#include <unordered_map>
10#include <vector>
11
12#include <units/length.h>
13#include <wpi/SymbolExports.h>
14#include <wpi/json_fwd.h>
15
17#include "frc/geometry/Pose3d.h"
18
19namespace frc {
20/**
21 * Class for representing a layout of AprilTags on a field and reading them from
22 * a JSON format.
23 *
24 * The JSON format contains two top-level objects, "tags" and "field".
25 * The "tags" object is a list of all AprilTags contained within a layout. Each
26 * AprilTag serializes to a JSON object containing an ID and a Pose3d. The
27 * "field" object is a descriptor of the size of the field in meters with
28 * "width" and "length" values. This is to account for arbitrary field sizes
29 * when transforming the poses.
30 *
31 * Pose3ds in the JSON are measured using the normal FRC coordinate system, NWU
32 * with the origin at the bottom-right corner of the blue alliance wall.
33 * SetOrigin(OriginPosition) can be used to change the poses returned from
34 * GetTagPose(int) to be from the perspective of a specific alliance.
35 *
36 * Tag poses represent the center of the tag, with a zero rotation representing
37 * a tag that is upright and facing away from the (blue) alliance wall (that is,
38 * towards the opposing alliance). */
40 public:
41 enum class OriginPosition {
42 kBlueAllianceWallRightSide,
43 kRedAllianceWallRightSide,
44 };
45
47
48 /**
49 * Construct a new AprilTagFieldLayout with values imported from a JSON file.
50 *
51 * @param path Path of the JSON file to import from.
52 */
54
55 /**
56 * Construct a new AprilTagFieldLayout from a vector of AprilTag objects.
57 *
58 * @param apriltags Vector of AprilTags.
59 * @param fieldLength Length of field the layout is representing.
60 * @param fieldWidth Width of field the layout is representing.
61 */
62 AprilTagFieldLayout(std::vector<AprilTag> apriltags,
63 units::meter_t fieldLength, units::meter_t fieldWidth);
64
65 /**
66 * Returns the length of the field the layout is representing.
67 * @return length
68 */
69 units::meter_t GetFieldLength() const;
70
71 /**
72 * Returns the length of the field the layout is representing.
73 * @return width
74 */
75 units::meter_t GetFieldWidth() const;
76
77 /**
78 * Returns a vector of all the april tags used in this layout.
79 * @return list of tags
80 */
81 std::vector<AprilTag> GetTags() const;
82
83 /**
84 * Sets the origin based on a predefined enumeration of coordinate frame
85 * origins. The origins are calculated from the field dimensions.
86 *
87 * This transforms the Pose3ds returned by GetTagPose(int) to return the
88 * correct pose relative to a predefined coordinate frame.
89 *
90 * @param origin The predefined origin
91 */
93
94 /**
95 * Sets the origin for tag pose transformation.
96 *
97 * This transforms the Pose3ds returned by GetTagPose(int) to return the
98 * correct pose relative to the provided origin.
99 *
100 * @param origin The new origin for tag transformations
101 */
102 void SetOrigin(const Pose3d& origin);
103
104 /**
105 * Returns the origin used for tag pose transformation.
106 * @return the origin
107 */
109
110 /**
111 * Gets an AprilTag pose by its ID.
112 *
113 * @param ID The ID of the tag.
114 * @return The pose corresponding to the ID that was passed in or an empty
115 * optional if a tag with that ID is not found.
116 */
117 std::optional<Pose3d> GetTagPose(int ID) const;
118
119 /**
120 * Serializes an AprilTagFieldLayout to a JSON file.
121 *
122 * @param path The path to write the JSON file to.
123 */
125
126 /*
127 * Checks equality between this AprilTagFieldLayout and another object.
128 */
129 bool operator==(const AprilTagFieldLayout&) const = default;
130
131 private:
132 std::unordered_map<int, AprilTag> m_apriltags;
133 units::meter_t m_fieldLength;
134 units::meter_t m_fieldWidth;
135 Pose3d m_origin;
136
137 friend WPILIB_DLLEXPORT void to_json(wpi::json& json,
138 const AprilTagFieldLayout& layout);
139
140 friend WPILIB_DLLEXPORT void from_json(const wpi::json& json,
141 AprilTagFieldLayout& layout);
142};
143
145void to_json(wpi::json& json, const AprilTagFieldLayout& layout);
146
148void from_json(const wpi::json& json, AprilTagFieldLayout& layout);
149
150} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
namespace for Niels Lohmann
Definition: json.h:96
Class for representing a layout of AprilTags on a field and reading them from a JSON format.
Definition: AprilTagFieldLayout.h:39
friend WPILIB_DLLEXPORT void to_json(wpi::json &json, const AprilTagFieldLayout &layout)
AprilTagFieldLayout(std::vector< AprilTag > apriltags, units::meter_t fieldLength, units::meter_t fieldWidth)
Construct a new AprilTagFieldLayout from a vector of AprilTag objects.
bool operator==(const AprilTagFieldLayout &) const =default
units::meter_t GetFieldLength() const
Returns the length of the field the layout is representing.
OriginPosition
Definition: AprilTagFieldLayout.h:41
std::optional< Pose3d > GetTagPose(int ID) const
Gets an AprilTag pose by its ID.
void Serialize(std::string_view path)
Serializes an AprilTagFieldLayout to a JSON file.
units::meter_t GetFieldWidth() const
Returns the length of the field the layout is representing.
std::vector< AprilTag > GetTags() const
Returns a vector of all the april tags used in this layout.
friend WPILIB_DLLEXPORT void from_json(const wpi::json &json, AprilTagFieldLayout &layout)
Pose3d GetOrigin() const
Returns the origin used for tag pose transformation.
AprilTagFieldLayout(std::string_view path)
Construct a new AprilTagFieldLayout with values imported from a JSON file.
void SetOrigin(const Pose3d &origin)
Sets the origin for tag pose transformation.
void SetOrigin(OriginPosition origin)
Sets the origin based on a predefined enumeration of coordinate frame origins.
Represents a 3D pose containing translational and rotational elements.
Definition: Pose3d.h:21
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15
WPILIB_DLLEXPORT void from_json(const wpi::json &json, AprilTagFieldLayout &layout)
WPILIB_DLLEXPORT void to_json(wpi::json &json, const AprilTagFieldLayout &layout)