Skip to content

To set the PiCamera on Ubuntu 22.04 ​


  • πŸ”—The refernce is take from the gaseoustortoise.
  • This document is made for ros2 humble.
  • Ensure that system has internet connection.
  • Assuming ROS2 humble is already downloaded.
  • Make a laptop Hotspot name swiftPro and set password to swift123.
  • On the raspberry Pi connect it to screen login using the credentials that we set during setup.
  • Ensure it is connected to hotspot and get the wlan0: inet address eg. 192.168.0.15.
  • Do SSH ssh swiftPro@IP-ADDRESS and enter password swift123 in another system which is connected to the swiftPro hotspot.
  • Run the below commnads.

1. Update and Upgarde the system ​

sudo apt update && sudo apt upgrade

2. Install reqired library for raspberryPi, video for Linux and ros- humble camera ​

sudo apt install libraspberrypi-bin v4l-utils ros-humble-v4l2-camera

3. Install ros humble transport plugin ​

sudo apt install ros-humble-image-transport-plugin

4. If you are some another user or root user check you have camera and video access ​

groups
  • After listing you will see some list on screen ensure that video is present, if not present run the below command.

5. If group does not have video then run thos command (Optional) ​

sudo usermod -aG video swiftPro
  • Ensure the video is present in group. If your username is different then then use that name for swiftPro drone model username is swiftPro.

6. Download raspberry Pi congigurator tool to enable services* ​

sudo apt install raspi-config

7. open raspberry Pi configuratior ​

sudo raspi-config
  • 8. Enable legacy camera option SPI and I2C communication prototcol
  • Do the below process as shown.

raspi-config

interface-option

Legacy-camera option

spi-option

i2c-option

finish-option

reboot

9. Check the camera is properly connected ​

vcgencmd get_camera
  • # supported=1 dected=1 , libcamera interfaces=0 this should be the output.

10. Source the ros2 humble bin path ​

source /opt/ros/humble/setup.bash

11. Run the ROS node for using video for Linux ​

ros2 run v4l2_camera v4l2_camera_node --ros-args -p image_size:="[640,480]"
  • There should be some warning and errors you can safely ignore that as output is alreading publishing the sensor_msg.

13. View Output ​

  • Make a file in a other system which has GUI and connect to swiftPro
nano videoSubscriber.py
  • write or copy paste the code given below in videoSubscriber.py
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image, CameraInfo
from cv_bridge import CvBridge
import cv2

class CameraSubscriber(Node):

    def __init__(self):
        super().__init__('camera_subscriber')
        
        # Create a CvBridge object to convert ROS messages to OpenCV images
        self.bridge = CvBridge()

        # Subscribe to the /image_raw topic to get raw images
        self.image_sub = self.create_subscription(
            Image,
            '/image_raw',
            self.image_callback,
            10
        )

        # Subscribe to the /camera_info topic to get camera calibration data
        self.camera_info_sub = self.create_subscription(
            CameraInfo,
            '/camera_info',
            self.camera_info_callback,
            10
        )
        
        self.camera_info = None

    def image_callback(self, msg):
        # Convert ROS Image message to OpenCV image
        try:
            cv_image = self.bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8')
            
            # Display the image in an OpenCV window
            cv2.imshow("Camera Feed", cv_image)
            cv2.waitKey(1)  # Wait for 1 ms to display the image
        except Exception as e:
            self.get_logger().error(f"Error converting image: {e}")

    def camera_info_callback(self, msg):
        # Store the camera info (this is for debugging or future use)
        self.camera_info = msg
        self.get_logger().info(f"Camera Info received: {msg}")

def main(args=None):
    rclpy.init(args=args)

    camera_subscriber = CameraSubscriber()

    rclpy.spin(camera_subscriber)

    # Destroy the node explicitly (optional but good practice)
    camera_subscriber.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
  • Save it using ctrl + x -> Y -> enter.

14. Install dependancies ​

pip3 install opencv-python cv-bridge rclpy sensor-msgs

15. Run the file ​

python3 videoSubscriber.py
  • You can see the window pop up showing camera feed.