Roban2 Docs
Roban2 (ROS1) Legacy VersionsFeature ExamplesGeneral Examples

YOLO Box-Moving Case

Feature Overview

This case implements the ability for the robot to visually recognize boxes and perform grasping operations. The main features include:

  1. Using a YOLO model to detect boxes in the image in real time
  2. Calculating the offset between the box and the image center
  3. Controlling robot movement (forward/backward/turning) based on the offset
  4. Performing the grasp action when the robot moves to a suitable position
  5. Supporting keyboard control (spacebar starts/stops robot motion)

Directory Structure of the roban2_move_boxes Package

roban2_move_boxes/
├── CMakeLists.txt                  # Build configuration file
├── package.xml                     # ROS package metadata file
├── model                           # Model files (YOLO)
│   └── best.pt                     # Trained box model weights
│   └── yolov8n.pt                  # YOLO pretrained model weights
├── launch                          # Launch file directory
│   └── get_boxes_image.launch      # Launch file for starting the box image capture feature
│   └── roban_move_boxes.launch     # Launch file for starting the move_boxes feature
│   └── yolo_auto_annotation.launch # Launch file for starting the auto-annotation feature
├── src                             # Source code directory
│   └── scripts                     # Executable script directory
│       └── generate_yolo_dataset.py# Converts annotated images into a YOLO dataset
│       └── get_boxes_image.py      # Captures images via the camera and saves them to a specified folder
│       └── move_boxes_demo.py      # Main program: robot autonomous movement and grasping logic
│       └── yolo_auto_annotation.py # Automatically annotates similar boxes using the trained box model
│       └── yolo_train.py           # Trains on images using the YOLO training model
└── README.md                       # Package documentation

1. Starting the Main Program:

cd ~/kuavo_ros_application
catkin build roban2_move_boxes kuavo_msgs
source devel/setup.bash
roslaunch roban_move_boxes roban_move_boxes.launch

Note: When logged in to the robot via SSH, the image window cannot be displayed

  1. Configure the master/slave setup, then subscribe to the image topic in rviz or rqt
  2. Connect to the robot via VNC

2. Environment Setup

pip install labelimg
pip install ultralytics

3. Model Training

1. Data Collection

  • Launch the script: roslaunch roban2_move_boxes get_boxes_image.launch
  • Usage: Press the spacebar to save the image in the image window to the ~/Desktop/yolo/images directory

2. Data Annotation

  • Run labelimg in the terminal img_1.png

  • Using labelimg

    1. Click Open Dir and select the directory where the images were saved in the previous step
    2. Click Change Save Dir and select the directory for storing annotation files
    3. Click YOLO to change the annotation format to YOLO
    4. Click Create RectBox and draw a bounding box around the box in the image, then assign a class label
    5. Click Save after annotating
    6. Click Next Image to switch to the next image
    7. Repeat steps 4-6 until all images have been annotated
  • Dataset generation

    python3 generate_yolo_dataset.py 
      --img_dir ~/Desktop/yolo/images 
      --label_dir ~/Desktop/yolo/labels 
      --output_dir ~/Desktop/yolo/dataset 
      --split_ratio 0.8 
      --shuffle

    Set the directory containing the images image_directory Set the directory containing the annotation files label_directory
    Set the output directory output_directory
    Set the training set ratio split_ratio

3. Model Training

python3 yolo_train.py --model_path yolov8n.pt --dataset_path dataset.yaml --epochs 200

model_path: Path to the pretrained model file dataset_path: Path to the YOLO dataset directory epochs: Sets the number of training epochs

Automatic Data Annotation (Optional)

Run the script

python3 yolo_auto_annotation.py --model_name best.pt --base_dir my_dataset --yolo_conf 0.6
  1. Main features: Uses the new model obtained from training on part of the data in Model Training to automatically annotate new data

    • Captures the video stream from the camera, performs real-time detection, and displays the results
    • Saves detection results as images and annotation files via key press, for dataset construction
    • Generates a classes.txt file containing all class names of the model
  2. Parameter description: Load model: --model_name best.pt specifies the trained YOLO model file Specify dataset: --base_dir my_dataset sets the path to the directory containing the images to be annotated Set confidence: --yolo_conf 0.6 keeps only detection results with confidence ≥ 0.6 (filters out low-quality predictions)

  3. Usage:

    1. After the script starts, check the image window; once the detection results meet expectations, click the save button to save the image and annotation information.
    2. After automatic annotation is complete, open labelimg to review the dataset, check for missed or incorrect annotations, correct them, and save.
    3. Use the generate_yolo_dataset.py script to generate the YOLO dataset.
    4. Train on the generated data using yolo_train.py .

move_boxes_demo.py Documentation

The following are the ROS topics and services involved in the roban2_move_boxes node:


📡 ROS Topics

Topic NameMessage TypePublisher/SubscriberDescription
/joysensor_msgs/JoyPublisherPublishes the Joy controller state, used to control robot gait switching.
/key_pressedstd_msgs/StringPublisher / SubscriberPublishes and subscribes to keyboard key events (e.g., spacebar triggers grasping).
/camera/color/image_rawsensor_msgs/ImageSubscriberSubscribes to raw RGB image data for object detection and visual navigation.
/camera/color/image_processedsensor_msgs/ImagePublisherPublishes processed images (with detection boxes, offset information, etc. drawn).
/robot_head_motion_datakuavo_msgs/robotHeadMotionDataPublisherControls the robot head rotation angle.
/cmd_posegeometry_msgs/TwistPublisherControls the robot's crouching and standing actions (via z-axis linear velocity).
/cmd_velgeometry_msgs/TwistPublisherControls the robot chassis motion (linear and angular velocity).

⚙️ ROS Services

Service NameService TypeCallerDescription
/roban2_arm_actionkuavo_msgs/Roban2ArmActionClient (self.grab_boxes)Calls the robotic arm to perform the grasp action and returns the grasp result status.

✅ Summary

🧠 Functional Logic

  • Manual control:
    • Spacebar toggles the movement mode (start/stop);
    • Ctrl+C exits the program.
  • Image recognition: Subscribes to /camera/color/image_raw, uses YOLO to detect boxes, and calculates their position in the image.
  • Autonomous movement:
    • Adjusts the robot's direction and speed based on the box offset;
    • Slows down and stops as the box gets closer, then prepares to grasp.
  • Grasping process:
    • Stops robot motion;
    • Crouches;
    • Calls /roban2_arm_action to grasp the box;
    • Stands up;
    • Returns

📌 Notes

  1. Dependency checks:

    • Ensure the /roban2_arm_action service is available;
    • Ensure the camera node is started and /camera/color/image_raw is publishing images properly; to start the camera: roslaunch realsense2_camera rs_camera.launch
    • Ensure [model/best.pt] exists and the path is correct.
    • Run roslaunch roban2_move_boxes roban2_move_boxes.launch in the upper-computer repository to start the box-moving program.
  2. Debugging tools:

    • Use rqt_image_view to view /camera/color/image_processed;
    • Use rostopic echo /key_pressed to monitor key events;
    • Use rviz to view the motion trajectory corresponding to /cmd_vel.