> For the complete documentation index, see [llms.txt](https://nxp.gitbook.io/nxp-cup/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nxp.gitbook.io/nxp-cup/gazebo/milestone-3-simple-reaction/apriltag-example-code-overview.md).

# AprilTag Example Code Overview

## Subscribing to the `/apriltag/detections` topic

The AprilTag detection node exposes a new topic called `/apriltag/detections`. This topic contains data detailing the ID of the AprilTag, the location within the image, and more. In NXP Gazebo, you will really only need the ID data.

If you want to see the full detail of the AprilTagDetection message, see below:

{% embed url="<https://github.com/christianrauch/apriltag_msgs/blob/master/msg/AprilTagDetection.msg>" %}

{% hint style="info" %}
The `/apriltag/detections` topic publishes a message called AprilTagDetectionArray - this message is an array of the message linked above. If more than one AprilTag is detected, you can use the array index to find each detection. In NXP Gazebo milestone 3, you will always use detection 0, as there is no case in which more than one detection will be found at any given time.
{% endhint %}

To subscribe to the `/apriltag/detections` topic, we need to create a new subscriber. In the updated example code, you will find the following lines:

```
apriltag_subscription_ = this->create_subscription<apriltag_msgs::msg::AprilTagDetectionArray>(
                              "/apriltag/detections", 10, std::bind(&LineFollower::apriltag_callback,
                              this, std::placeholders::_1));
```

This section of code is similar to the simulated Pixy camera subscription, just adapted to the AprilTagDetectionArray message in the `/apriltag/detections` topic. You can see within this section of code we passed a new callback function called `LineFollower::apriltag_callback`. This function will be run each time an AprilTagDetectionArray is published.

## Using the new callback function

The new `LineFollower::apriltag_callback` function will allow you to use the data from the `/apriltag/detections` topic at your disposal. An example function which prints out the ID of the detected AprilTag is included in the new example code.

```
  /*
   * NXP SUMMER CAMP PARTICIPANTS:
   * This code just shows you how to retrieve the data from the
   * AprilTag node. You will need to use this data to perform actions
   * with the car within listener_callback.
   */
  void apriltag_callback(apriltag_msgs::msg::AprilTagDetectionArray::SharedPtr msg)
  {
    // Check to see if the detections object is empty. If so, then no detections found.
    if(!msg->detections.empty())
    {
      // Store the 1st detection in an AprilTagDetection object
      apriltag_msgs::msg::AprilTagDetection detection = msg->detections[0];
      // Print the detection ID to the ROS INFO stream
      RCLCPP_INFO(this->get_logger(), "Detection found! ID: %d\n Count: %d\n", detection.id, stop_count);
    }
  }
```

Note that the function looks for the first AprilTag detected. Within NXP Gazebo, only one AprilTag will be shown at any given time, so you won't need to worry about using a detection other than detection 0.

## Questions?

If you have questions, please don't hesitate to ask Landon in the NXP Gazebo Summer Camp Teams channel!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nxp.gitbook.io/nxp-cup/gazebo/milestone-3-simple-reaction/apriltag-example-code-overview.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
