WPILibC++ 2024.3.2
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
18#include "frc/geometry/Pose3d.h"
19
20namespace frc {
21/**
22 * Class for representing a layout of AprilTags on a field and reading them from
23 * a JSON format.
24 *
25 * The JSON format contains two top-level objects, "tags" and "field".
26 * The "tags" object is a list of all AprilTags contained within a layout. Each
27 * AprilTag serializes to a JSON object containing an ID and a Pose3d. The
28 * "field" object is a descriptor of the size of the field in meters with
29 * "width" and "length" values. This is to account for arbitrary field sizes
30 * when transforming the poses.
31 *
32 * Pose3ds in the JSON are measured using the normal FRC coordinate system, NWU
33 * with the origin at the bottom-right corner of the blue alliance wall.
34 * SetOrigin(OriginPosition) can be used to change the poses returned from
35 * GetTagPose(int) to be from the perspective of a specific alliance.
36 *
37 * Tag poses represent the center of the tag, with a zero rotation representing
38 * a tag that is upright and facing away from the (blue) alliance wall (that is,
39 * towards the opposing alliance). */
41 public:
42 /**
43 * Common origin positions for the AprilTag coordinate system.
44 */
45 enum class OriginPosition {
46 /// Blue alliance wall, right side.
47 kBlueAllianceWallRightSide,
48 /// Red alliance wall, right side.
49 kRedAllianceWallRightSide,
50 };
51
52 /**
53 * Loads an AprilTagFieldLayout from a predefined field
54 *
55 * @param field The predefined field
56 * @return AprilTagFieldLayout of the field
57 */
59
61
62 /**
63 * Construct a new AprilTagFieldLayout with values imported from a JSON file.
64 *
65 * @param path Path of the JSON file to import from.
66 */
68
69 /**
70 * Construct a new AprilTagFieldLayout from a vector of AprilTag objects.
71 *
72 * @param apriltags Vector of AprilTags.
73 * @param fieldLength Length of field the layout is representing.
74 * @param fieldWidth Width of field the layout is representing.
75 */
76 AprilTagFieldLayout(std::vector<AprilTag> apriltags,
77 units::meter_t fieldLength, units::meter_t fieldWidth);
78
79 /**
80 * Returns the length of the field the layout is representing.
81 * @return length
82 */
83 units::meter_t GetFieldLength() const;
84
85 /**
86 * Returns the length of the field the layout is representing.
87 * @return width
88 */
89 units::meter_t GetFieldWidth() const;
90
91 /**
92 * Returns a vector of all the april tags used in this layout.
93 * @return list of tags
94 */
95 std::vector<AprilTag> GetTags() const;
96
97 /**
98 * Sets the origin based on a predefined enumeration of coordinate frame
99 * origins. The origins are calculated from the field dimensions.
100 *
101 * This transforms the Pose3ds returned by GetTagPose(int) to return the
102 * correct pose relative to a predefined coordinate frame.
103 *
104 * @param origin The predefined origin
105 */
107
108 /**
109 * Sets the origin for tag pose transformation.
110 *
111 * This transforms the Pose3ds returned by GetTagPose(int) to return the
112 * correct pose relative to the provided origin.
113 *
114 * @param origin The new origin for tag transformations
115 */
116 void SetOrigin(const Pose3d& origin);
117
118 /**
119 * Returns the origin used for tag pose transformation.
120 * @return the origin
121 */
123
124 /**
125 * Gets an AprilTag pose by its ID.
126 *
127 * @param ID The ID of the tag.
128 * @return The pose corresponding to the ID that was passed in or an empty
129 * optional if a tag with that ID is not found.
130 */
131 std::optional<Pose3d> GetTagPose(int ID) const;
132
133 /**
134 * Serializes an AprilTagFieldLayout to a JSON file.
135 *
136 * @param path The path to write the JSON file to.
137 */
139
140 /*
141 * Checks equality between this AprilTagFieldLayout and another object.
142 */
143 bool operator==(const AprilTagFieldLayout&) const = default;
144
145 private:
146 std::unordered_map<int, AprilTag> m_apriltags;
147 units::meter_t m_fieldLength;
148 units::meter_t m_fieldWidth;
149 Pose3d m_origin;
150
151 friend WPILIB_DLLEXPORT void to_json(wpi::json& json,
152 const AprilTagFieldLayout& layout);
153
154 friend WPILIB_DLLEXPORT void from_json(const wpi::json& json,
155 AprilTagFieldLayout& layout);
156};
157
159void to_json(wpi::json& json, const AprilTagFieldLayout& layout);
160
162void from_json(const wpi::json& json, AprilTagFieldLayout& layout);
163
164/**
165 * Loads an AprilTagFieldLayout from a predefined field
166 *
167 * @param field The predefined field
168 * @return AprilTagFieldLayout of the field
169 */
172
173} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
a class to store JSON values
Definition: json.h:96
Class for representing a layout of AprilTags on a field and reading them from a JSON format.
Definition: AprilTagFieldLayout.h:40
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
Common origin positions for the AprilTag coordinate system.
Definition: AprilTagFieldLayout.h:45
static AprilTagFieldLayout LoadField(AprilTagField field)
Loads an AprilTagFieldLayout from a predefined field.
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)
AprilTagField
Loadable AprilTag field layouts.
Definition: AprilTagFields.h:16
WPILIB_DLLEXPORT void to_json(wpi::json &json, const AprilTagFieldLayout &layout)
WPILIB_DLLEXPORT AprilTagFieldLayout LoadAprilTagLayoutField(AprilTagField field)
Loads an AprilTagFieldLayout from a predefined field.