1. 카메라 확인
lsusb                                   # Arducam 항목이 보이는지
sudo apt install v4l-utils
v4l2-ctl --list-devices                 # /dev/videoN 번호 확인
v4l2-ctl -d /dev/video0 --list-formats-ext #0이 아닐 수도 있음 카메 global sutter 번호 따라감

lsusb # Arducam 항목이 보이는지 sudo apt install v4l-utils v4l2-ctl --list-devices # /dev/videoN 번호 확인 v4l2-ctl -d /dev/video0 --list-formats-ext

  1. ROS2 드라이버 및 추가 파일 설치

저희가 목표로 하는 카메라 유틸 구조는 다음과 같습니다.

(You would change workspace’s name, this is just for example)

~/seminar_ws/
├── src/
│   └── seminar_camera/
│       ├── package.xml
│       ├── setup.py
│       ├── seminar_camera/
│       │   ├── __init__.py
│       │   └── cam_sub.py
│       ├── config/
│       │   ├── ar0234.yaml          # 드라이버 설정
│       │   └── ar0234_calib.yaml    # (캘리 후 생성)
│       └── launch/
│           └── camera.launch.py, cam_sub.py

└── data/                            # bag, 캘리 이미지 (빌드 대상 아님)

Before you make this structure, the followings are needed to be installed.

dpkg -l | grep -E "usb-cam|v4l-utils|camera-calibration|cv-bridge|image-transport-plugins"
sudo apt install ros-humble-usb-cam ros-humble-camera-calibration ros-humble-cv-bridge \
  ros-humble-image-transport-plugins ros-humble-rqt-image-view v4l-utils python3-opencv

Now think a moment, and figure how to make this structure.

mkdir -p ~/seminar_ws/src ~/seminar_ws/data
cd ~/seminar_ws/src
ros2 pkg create seminar_camera --build-type ament_python \
  --dependencies rclpy sensor_msgs cv_bridge
mkdir -p seminar_camera/config seminar_camera/launch
mv ~/seminar5/ar0234.yaml seminar_camera/config/     # 아까 만든 파일 옮기기

Now you need to make yaml file (this is for calibration, and this information is found by me in July It would be incorrect. So you may be change this one.)

Edit ar0234.yaml or make ar0234.yaml. Then paste them.

cd ~/seminar_ws/src/seminar_camera/config
nano ar0234.yaml
/**:
  ros__parameters:
    video_device: "/dev/video0" # 0이 아닐수 있음
    image_width: 1280
    image_height: 720
    framerate: 30.0
    pixel_format: "mjpeg2rgb"
    io_method: "mmap"
    frame_id: "ar0234"
    camera_name: "ar0234"
    camera_info_url: ""        # 캘리 후 file:///.../ar0234.yaml 로 채움

Then make two code files, and replace one code pile.

First, seminar_camera/seminar_camera/cam_sub.py

import rclpy, cv2
from rclpy.node import Node
from rclpy.qos import qos_profile_sensor_data
from sensor_msgs.msg import Image
from cv_bridge import CvBridge

class CamSub(Node):
    def __init__(self):
        super().__init__('cam_sub')
        self.declare_parameter('topic', '/image_raw')
        topic = self.get_parameter('topic').value
        self.br = CvBridge()
        self.create_subscription(Image, topic, self.cb, qos_profile_sensor_data)
        self.get_logger().info(f'subscribing {topic}')

    def cb(self, msg):
        img = self.br.imgmsg_to_cv2(msg, 'bgr8')
        cv2.imshow('cam', img)
        cv2.waitKey(1)

def main():
    rclpy.init()
    rclpy.spin(CamSub())

if __name__ == '__main__':
    main()

Second, replace setup.py

from glob import glob
from setuptools import setup

pkg = 'seminar_camera'
setup(
    name=pkg,
    version='0.0.1',
    packages=[pkg],
    data_files=[
        ('share/ament_index/resource_index/packages', ['resource/' + pkg]),
        ('share/' + pkg, ['package.xml']),
        ('share/' + pkg + '/config', glob('config/*.yaml')),
        ('share/' + pkg + '/launch', glob('launch/*.py')),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    entry_points={
        'console_scripts': ['cam_sub = seminar_camera.cam_sub:main'],
    },
)