Welcome to ROSMASTER A1 repository
13.Edge Detection
Edge Detection
1. Content Description
This lesson explains how to use depth images for edge detection. Combined with a chassis, this allows the vehicle to stop at edges, preventing the risk of falling. Future development can extend this to include depth image obstacle avoidance. This lesson requires inputting commands in the terminal; the appropriate terminal depends on the motherboard type. This lesson uses a Raspberry Pi 5 as an example.
For Raspberry Pi and Jetson Nano boards, you need to open a terminal on the host computer and enter the command to enter the Docker container. Once inside the Docker container, enter the commands mentioned in this lesson in the terminal. For instructions on entering the Docker container from the host computer, refer to [01. Robot Configuration and Operation Guide] -- [5.Enter Docker (For JETSON Nano and RPi 5)].
For Orin and RDK boards, simply open the terminal and enter the commands mentioned in this lesson.
2. Program Startup
For PI5/JETSON NANO controllers, you need to enter a Docker container first; for RDK X5 and Orin boards, this is not required.
Entering a Docker container (see the Docker course for steps: 4. Docker Startup Script).
All commands below must be executed within the same Docker container (see the Docker course for steps: 3. Docker Commit and Multi-Terminal Entry).
First, in the terminal, enter the following command to start the camera.
ros2 launch ascamera hp60c.launch.pyAfter the camera successfully starts, open another terminal and enter the following command to start the edge detection program.
xxxxxxxxxxros2 run yahboomcar_depth Edge_Detection
As shown in the image above, after the program starts, it will print the current status, indicating that it is stopped. Pressing the spacebar changes the status. After pressing the spacebar, if the robot does not detect an edge, it will move forward and print "Moving..."; if it detects an edge, it will stop and print "Stop!!!".


3. Core Code
Program Code Path:
Raspberry Pi 5 and Jetson Nano Board
The program code is running in Docker. The path in Docker is /root/yahboomcar_ws/src/yahboom_depth/yahboom_M3Pro_DepthCam/Edge_Detection.py
Orin board
The program code path is /home/jetson/yahboomcar_ws/yahboom_M3Pro_DepthCam/yahboom_M3Pro_DepthCam/Edge_Detection.py
RDK X5 board
The program code path is /home/sunrise/yahboomcar_ws/yahboom_M3Pro_DepthCam/yahboom_M3Pro_DepthCam/Edge_Detection.py
Import the necessary library files.
xxxxxxxxxximport rclpyfrom rclpy.node import Nodefrom sensor_msgs.msg import Imagefrom geometry_msgs.msg import Twistfrom arm_msgs.msg import ArmJointsfrom cv_bridge import CvBridgeimport cv2import numpy as npimport threadingDepth image decoding format,
xxxxxxxxxxencoding = ['16UC1', '32FC1']Initialize variables and define publishers and subscribers,
xxxxxxxxxxdef __init__(self, name): super().__init__(name) self.pub_vel = self.create_publisher(Twist,'/cmd_vel',1) self.sub_depth = self.create_subscription(Image,"/ascamera_hp60c/camera_publisher/depth0/image_raw",self.get_DepthImageCallBack,100)