As a part of my senior project on autonomous vehicles, I was tasked with finding out how to detect and track AR tags using a camera. The specific camera we used was the ZED stereo camera, but prior to testing it on that camera that I did not have immediate access to, I wanted to see if it would work on a regular webcam. I found that many resources online were incomplete, outdated, or didn’t specify how to use the package with RViz, the 3D visualizer for ROS. I wrote a short guide on how to do so for my other group members, and thought it would be helpful to post here.

We used the melodic distrubution of ROS which is compatible with Ubuntu 18.0.4 on Linux.

To start, install the package for your distribution:

sudo apt-get install ros-melodic-ar-track-alvar

Create your first marker. Here I created a marker with the id ‘123’

rosrun ar_track_alvar createMarker 123

I’m using catkin workspaces which could be read about here. In catkin_ws/src I ran:

git clone https://github.com/atomoclast/ar_tag_toolbox

Back in /catkin_ws I ran

catkin_make

At this point you need to calibrate your camera. I followed this tutorial on how to calibrate a monocular camera. It would be a slightly different procedure if you were using a stereo camera.

For the calibration I didn’t have access to a larger paper size than a regular 8.5x11 so I simply printed out the checkerboard on that and measured the squares myself. My squares came out to be 0.025m long.

Before you run the calibration program made sure to run the two following commands: (Note: if usb_cam isn’t installed already, install it now!)

roscore
rosrun usb_cam usb_cam_node

Now run the calibration program:

rosrun camera_calibration cameracalibrator.py --size 8x6 --square 0.025 image:=/usb_cam/image_raw camera:=/usb_cam

Once the GUI is open, move your sheet of paper around in front of the webcam, at different angles and distances. Do this until the calibrate button is highlighted and then after it finishes calibrating save and commit it. Now point the camera at your printed AR tags and run the following: (My marker size was 5cm and my webcam was #2 but change if you need)

roslaunch ar_tag_toolbox usb_cam.launch cam_id:=2
roslaunch ar_tag_toolbox ar_track_usb_cam.launch marker_size:=5

This next command will let you see the id of the AR Tag being detected.

rostopic echo /ar_pose_marker

Here’s the part most tutorials left out. I needed to see if the camera could see where the tags were in relation to itself. This is done in RViz. To open up Rviz I ran:

rosrun rviz rviz

Once there, make sure to point the camera at an AR tag.

  • Add the Camera display to the sidebar, the camera stream should pop up
  • Under Global Options, change the Fixed Frame to usb_cam
  • Add the TF display to the sidebar
  • You can now see the positions of the AR tags in relation to the camera!