[專案]Getting Started Guide for AprilTag === >[name=鍾鎧煜][time=2022_08_25] [TOC] --- ###### tags: `專案` ## Camera Inialization **硬體** logitech C930e webcam 1080p 30fps 但本文方法適用任何相機設備(不適用RealSense) ![](https://i.imgur.com/hy6AHOa.png =300x200) **下載並編譯** [官方網站](https://github.com/ros-drivers/usb_cam) [實驗室使用版本](https://github.com/mike55688/usb_cam) :::danger :fire: **因為官方在持續更新中,因此後續教學將無法吻合,建議使用"實驗室使用版本"** ::: **啟動相機** > $ roslaunch usb_cam usb_cam-test.launch ```xml <launch> <node name="usb_cam" pkg="usb_cam" type="usb_cam_node" output="screen" > <param name="video_device" value="/dev/video4" /> <param name="camera_info_url" value="file:///home/ec404/catkin_ws/src/usb_cam/calibrationdata_logitech_black/ost.yaml"/> <param name="image_width" value="1280" /> <param name="image_height" value="720" /> <param name="framerate" value="30" /> <param name="pixel_format" value="yuyv" /> <param name="camera_frame_id" value="usb_cam" /> <param name="io_method" value="mmap"/> </node> <node name="image_view" pkg="image_view" type="image_view" respawn="false" output="screen"> <remap from="image" to="/usb_cam/image_raw"/> <param name="autosize" value="true" /> </node> </launch> ``` **usb_cam-test.launch 中的重要參數設定** - 解析度與幀率 預設為傳統的VGA解析度640x480 30fps,建議更改為1080p或720p 甚至更高,標籤位姿穩定範圍可提高一倍,但經過測試,USB 3.1最高支援到720p 30fps 。 ```htmlembedded <param name="image_width" value="1280" /> <!--default 640--> <param name="image_height" value="720" /> <!--default 480--> <param name="framerate" value="30" /> <!--default 30--> ``` **存取相機設備節點** 查看所有相機設備,並在launch檔中選擇 。 >$ ls -l /dev/video* ```htmlembedded <param name="video_device" value="/dev/video2" /> ``` **相機校正檔URL路徑** ```htmlembedded <param name="camera_info_url" value="file:///home/ec404/catkin_ws/src/usb_cam/calibrationdata_logitech_black/ost.yaml"/> ``` --- ## Camera Calibration **下載相機校正包** > $ sudo apt-get install ros-melodic-camera-cibration **啟動相機** > $ roslaunch usb_cam usb_cam-test.launch **啟動校正程式並設定參數** ![](https://i.imgur.com/60r6sF9.png =210x270) 準備一個用於標定的棋盤方格,並設定長寬與邊長 size 寬x長 ,如圖所示為6x8 square 正方形邊長 image 相機所發Topic camera 相機tf > $ rosrun camera_calibration cameracalibrator.py --size 6x8 --square 0.0278 image:=/usb_cam/image_raw camera:=/usb_cam ![](https://i.imgur.com/Ve6oWJ9.png =300x210) 開始校正後,標定板需要在視野範圍內做不同角度與不同距離的移動,尤其是視野邊緣的四個邊角 ![](https://i.imgur.com/qVu0KD6.png =300x210) 等到X, Y, Size, Skew進度條為綠色,代表校正完成後,按下CALIBRATE按鍵 ![](https://i.imgur.com/ILgBsSF.png =300x210) 視窗無回應是正常現象,大約3分鐘後可以計算完畢 ![](https://i.imgur.com/bN744GY.png =300x210) 計算完畢後按下Save儲存,預設路徑為Other Locations/Computer/tmp/calibrationdata.tar.gz > ('Wrote calibration data to', '/tmp/calibrationdata.tar.gz') > ![](https://i.imgur.com/qbFvq7y.png =500x210) 解壓縮後將 ost.yaml 校正檔移動到自訂義資料夾中,複製其URL路徑到 usb_cam-test.launch 中 ![](https://i.imgur.com/w2RmADi.png =400x250) ```htmlembedded <param name="camera_info_url" value="file:///home/ec404/catkin_ws/src/usb_cam/calibrationdata_logitech_black/ost.yaml"/> ``` :::warning :zap:**每次啟動,/tmp中的每個文件和目錄都會被刪除。** ::: 如果直接按COMMIT,校正檔會存到預設資料夾 /.ros 中讓 usb_cam 直接使用,可以註解 usb_cam-test.launch 中的 camera_info_url 參數 > [ INFO] [1659336169.302468662]: camera calibration URL : file:///home/ec404/.ros/camera_info/head_camera.yaml :::danger :fire: **不同相機與同相機不同解析度都需要獨立的校正檔,就算型號一樣且使用同解析度也是如此,因此建議不要使用預設路徑。** ::: usb_cam 節點發布校正檔中的參數 Topic 為 camera_info,主要內容是相機畸變模型、內參矩陣、旋轉矩陣、投影矩陣等參數。 詳見Reference [sensor_msgs/CameraInfo 理解](https://blog.csdn.net/wangmj_hdu/article/details/115002514) --- ## Step-by-Step Tutorial **1. 下載並編譯** https://github.com/AprilRobotics/apriltag_ros.git //如果有了common_ws就不用再載一次 **2. 下載並列印 apriltag 標籤** https://github.com/AprilRobotics/apriltag-imgs ![](https://i.imgur.com/Eq7JRYI.png)tag36h11 編號0 為本文範例 Github上,像素預設是9x9,所以要用以下指令縮放png圖片印出 > $ convert <small_marker>.png -scale <scale_chosen_in_percent>% <big_marker>.png ex:放大4倍 > $ convert tag36_11_00000.png -scale 400% tag36_11_00000.png **3. 設定 settings.yaml** 進入專案 apriltag_ros/apriltag_ros/config 中 settings.yaml參數檔設定,tag_family是否與下載的標籤種類相同 ![](https://i.imgur.com/iGVYTZA.png =150x300) ![](https://i.imgur.com/1mg3EWq.png =1000x30) **4. 設定tags.yaml** name: 標籤tf名稱 id: 設定編號 size: 設定邊長大小(m) qw, qx, qy, qz 為四元數, 在這裡調整成與ARTag相同,方便移植 ![](https://i.imgur.com/C6o0aOx.png) ### 自製的AprilTag Node 上述安裝方法為官方AprilTag安裝,我有自行修改過程式碼,讓AprilTag節點在PBVS對位時(收到Topic /AprilTag_[Name]_server/goal 為True的訊息)才開始變識,否則會一直佔用電腦效能,經過實測,能夠節省25%的資源消耗(i7-9700K)。 程式碼放在[common_ws/apriltag_ros](https://github.com/Yuntechec404/common_ws)裡面。 ### Input / Output - **Input** /AprilTag_[Name]_server/goal Type : apriltag_ros/apriltag_ros/action /AprilTag.action ```yaml #goal definition bool goal --- #result definition string result --- #feedback string feedback ``` - **Output** /tag_detections_[Name] Type : aptiltag_ros/AprilTagDetectionArray ### 使用方法 **啟動偵測 Apriltag 節點** > $ roslaunch apriltag_ros continuous_detection.launch P.S. 記得要更改此launch檔中的相機Topic與相機參數Topic為現在使用的相機 **發送開始變識AprilTag命令** 發送開始辨識指令(非完整指令) > $ rostopic pub /AprilTagServer/goal... <tab> <tab> //將goal改為true ![](https://hackmd.io/_uploads/HkIlF4i3h.png) ## Reference ### [2005 ARTag](https://drive.google.com/file/d/1dh5FQUGON6bD8_dE66plewwLkt3pdri4/view) ![](https://i.imgur.com/TyMa35m.png =450x300) > 3.1.2 Quad Detection > " The edge based approach gives some performance improvement over the greyscale region thresholding approach of other systems such as ARToolkit. In ARToolkit, groups of connected pixels below a specified threshold are found, and those possessing a quadrilateral boundary are used as potential markers. Having a spatial derivative of greyscale intensity threshold instead of a simple greyscale intensity threshold allows markers to be found under less controlled lighting conditions. Indeed, the ‘white’ level of one marker edge may be darker than the ‘black’ level of the other side and the marker still be detected. " ### [2011 Apriltag 1](https://april.eecs.umich.edu/papers/details.php?name=olson2011tags) ![](https://i.imgur.com/Sikngdk.png =800x300) ### [2016 Apriltag 2](https://april.eecs.umich.edu/papers/details.php?name=wang2016iros) ![](https://i.imgur.com/NjytvQm.png) ### [2019 Apriltag 3](https://april.eecs.umich.edu/papers/details.php?name=krogius2019iros) ![](https://i.imgur.com/epRsTD7.png =600x450) --- [usb_cam相機標定](https://blog.csdn.net/Will_Ye/article/details/125064472?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165901050116780366520112%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165901050116780366520112&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-3-125064472-null-null.142^v35^new_blog_fixed_pos&utm_term=ros%20usb_cam&spm=1018.2226.3001.4187) [ROS Wiki apriltag_rosTutorials](http://wiki.ros.org/apriltag_ros/Tutorials) [sensor_msgs/CameraInfo 理解](https://blog.csdn.net/wangmj_hdu/article/details/115002514) [Apriltag 官網](https://april.eecs.umich.edu) ### 更改Realsense D435相機解析度 **1.進入rs_camera.launch更改color_height跟color_width** **2.切記color_fps初始值為-1務必更改為30,否則無法順利更改相機解析度**