Queen’s Hand Chess Robot

Queen's Hand Chess Robot Demo

Queen’s Hand Chess Robot Demo

The renesas_demo_queens_hand package is the top-level launch, node, and configuration package for the Queen’s Hand chess-playing robot: an Agilex Piper arm fitted with a dexterous hand physically plays chess against a human, driven by a BehaviorTree.CPP mission with Stockfish as the game brain.

Perception runs rcar_chess_pieces_detection on the R-Car V4H SH: a 12-class YOLOv8 detector turns the camera image into a Forsyth-Edwards Notation (FEN) board state. See rcar_chess_pieces_detection for the detector itself.

Chess piece detection and the reconstructed board state

Chess Piece Detection and the Reconstructed Board State

Note

The demo has a hardware-free mode. behavior_bringup_mock.launch.py runs the whole stack with a mock arm and a mock perception node, so you can exercise the game logic and the behavior tree without an arm, a hand, or a camera.

Nodes

The following table lists the executables the package provides:

Executable

Role

chess_engine_node.py

Authoritative game state, built on python-chess and Stockfish. Serves the /chess/ services for board setup, move validation, move update, best move, human-move detection, and Portable Game Notation (PGN) handling. Publishes the latched /chess/board_state FEN and /chess/game_state.

chess_board_geometry_node

Board geometry derived from the probe-teach calibration. Serves /chess/get_square_pose, /chess/get_capture_bin_pose, /chess/get_promotion_pose, and /chess/get_piece_height.

chess_game_manager_node

Game control; drives the behavior-tree engine lifecycle. Serves /chess/game_control and publishes the latched /chess/game_paused and /chess/game_control_result.

chess_opponent_node.py

Software Stockfish opponent for the software-only demo. Serves /chess/opponent/enable.

mock_perception_node.py

Camera and detector replacement for hardware-free runs. Serves /chessboard/detect_board_state, /get_detected_piece, and /mock/play_human_move.

board_teach_calibration_node.py

Interactive probe-teach board calibration. Writes calibrated_board.yaml.

chess_move_client.py

Command-line helper with the move, human, best, state, reset, pause, and resume subcommands.

Run the Python nodes with the .py suffix, for example ros2 run renesas_demo_queens_hand chess_move_client.py state.

Launch Files

The following table lists the launch files and what each one brings up:

Launch file

Purpose

behavior_bringup.launch.py

Real hardware: the execute-layer servers, chess game logic, board geometry, game manager, and behavior-tree engine. Run the robot bringup and the perception launch separately.

behavior_bringup_mock.launch.py

Hardware-free: the same stack with a ros2_control mock arm and mock_perception_node instead of the camera and detector.

chess_vs_stockfish.launch.py

Software only, with no arm at all: the engine, the Stockfish opponent, the board renderer, and Foxglove.

chess_perception_realsense_camera_rcar.launch.py

RealSense camera, rcar_chess_pieces_detection, Foxglove bridge and overlays. Run this alongside the real bringup.

board_teach_calibration.launch.py

Probe-teach calibration of the physical board.

Foxglove layouts live in config/foxglove/.

Hardware Setup

The demo uses the following hardware:

Item

Purpose

Agilex Piper 6-DOF arm

Moves the pieces. Connects through a USB-to-CAN adapter.

Dexterous hand

Grips the pieces. The Ruiyan RH2 is recommended for this demo; the Inspire RH56E2 also works. Connects through a second USB-to-CAN adapter.

Intel RealSense depth camera

Sees the board. Configured by config/realsense/realsense_config.yaml.

Chessboard and pieces

A standard 8x8 board, placed within the arm’s reach and fully inside the camera’s view.

Probe tool

Mounted on the arm for the board teach calibration described below.

Note

This demo requires several 3D-printed parts. Download the STL files from the robot_printables GitHub repository and print them before starting.

Complete the Prerequisites for Running Sample Applications first. Mount the camera so that all 64 squares are visible and unobstructed, and place the board so that the arm can reach every square. Both calibrations below assume that neither the board nor the camera moves afterwards.

Important

Run the steps in this order. Each one depends on the previous:

  1. Calibrate the board to the robot once, and again whenever the board moves relative to the arm.

  2. Calibrate the camera to the board once, and again whenever the board or the camera moves.

  3. Bring up the robot.

  4. Start perception.

  5. Start the demo stack.

Quick Software Setup Instructions

Note

Run every command below inside the cross-compilation Docker container set up in Prerequisites for Running Sample Applications.

  1. Get the ros2_demo_workspace repository, which carries the manifest for each demo:

    cd ~/ros2_ws
    git clone https://github.com/renesas-rdk/ros2_demo_workspace.git
    
  2. Import the repositories this demo needs with the vcs tool:

    vcs import < ./ros2_demo_workspace/vcs_manifests/rcar-v4h/queens_hand.target.lock.repos
    

    Every repository the demo needs is cloned into the src/ folder of the workspace, each pinned to the revision the manifest locks.

  3. Install the demo’s build dependencies into the target sysroot:

    arm64-chroot apt update
    sysroot-rosdep-install
    
  4. Cross-compile the workspace:

    cross-colcon-build --packages-up-to renesas_demo_queens_hand
    
  5. Deploy the result to the board and install the runtime dependencies there, as described in Deploying and Installing Dependencies.

  6. Install the non-ROS runtime dependencies on the R-Car V4H SH board. The game logic needs the Stockfish engine and the python-chess library, which rosdep does not provide:

    bash ./install/renesas_demo_queens_hand/share/renesas_demo_queens_hand/setup/install_dependencies.sh
    

    Run this once per board.

Calibrating the Board to the Robot

The arm needs to know where each square is in its own base_link frame. That mapping comes from a probe-teach calibration whose output is a YAML file holding the 64 square centroids. The geometry node reads it through its calibration_file parameter, which points at config/calibrated_board.yaml by default.

If the board has not moved since the shipped calibration was taken, skip to the camera calibration.

  1. Mount the probe tool on the Piper arm.

  2. Start the calibration node:

    ros2 launch renesas_demo_queens_hand board_teach_calibration.launch.py
    
  3. Switch the arm to teaching mode. Enable the hand’s teaching mode first, then the arm’s.

  4. Move the tool center point (TCP) to the center of each square in turn. The node records each centroid from the base_link to TCP transform.

  5. Read the result from /tmp/calibrated_board.yaml, where the node writes it. Override the path with the output_file argument to write it elsewhere.

  6. Copy the file into the package’s config/ directory and rebuild to make it the default. The demo stack also picks up on-disk changes to the file automatically.

Caution

Teaching mode leaves the arm in a state that has to be cleared. Power the Piper arm off and back on before running the verification below or starting the demo. The Piper arm may need to be recalibrated after a power cycle.

Verifying the Board Calibration

The verification drives the arm to every square in turn, so you can see whether the probe lands on the square centers.

  1. Copy the calibration file into your workspace, inside the cross-build container:

    cd ~/ros2_ws/src/apps/renesas_demo_queens_hand/config
    scp ubuntu@<board_ip>:/tmp/calibrated_board.yaml .
    
  2. Generate one probe pose per square. Adjust the paths to match your checkout:

    python3 ~/ros2_ws/src/apps/renesas_demo_queens_hand/test/calculate.py \
      ~/ros2_ws/src/apps/renesas_demo_queens_hand/config/calibrated_board.yaml > cmd_probe.txt
    
  3. Launch the arm with the probe end effector at a low speed:

    ros2 launch agilex_piper_arm_bringup agilex_piper_native_cartesian_control.launch.py \
      end_effector:=probe speed:=10
    
  4. In a second terminal, send each pose from cmd_probe.txt, replacing the pose: block with that square’s entry:

    ros2 topic pub --once /agilex_piper_gpio_controller/target_pose geometry_msgs/msg/PoseStamped "
    {
      header: {frame_id: 'base_link'},
      pose: {
        position: {x: 0.2500, y: 0.0261, z: -0.0200},
        orientation: {x: -0.0520, y: 0.9986, z: 0.0000, w: 0.0000}
      }
    }"
    

The arm should stop at each square center. If it does not, repeat the calibration.

Calibrating the Camera to the Board

The probe-teach file above holds the board geometry in the robot’s frame. The detector needs a second calibration: the center of every square in the camera image, which is what it fits the image-to-board homography to with RANSAC before it can turn detections into a FEN.

chessboard_analyzer produces that mapping. It finds the four outer corners of the board, rectifies it to a top-down view, and writes the center of every square to /tmp/board_square_points.yaml. The detector reads that path through its calibrated_board_file parameter.

Run the analyzer on the R-Car V4H SH board itself, with the chessboard empty so the corner detector sees the board pattern rather than the pieces, and with the camera already in its final position.

  1. Start the analyzer:

    ros2 launch chessboard_analyzer camera_chessboard_perspective_transformation.launch.py
    

    Note

    This is a launch file of the chessboard_analyzer package, not of renesas_demo_queens_hand. The demo’s own perception launch does not start the analyzer.

  2. Once the camera is streaming, trigger the calculation from a second terminal:

    ros2 service call /chessboard_analyzer/trigger_calculate_squares std_srvs/srv/Trigger
    

    A successful trigger writes the 64 square centers, keyed a1 to h8 with image-pixel and deprojected 3D positions, to /tmp/board_square_points.yaml. Check the analyzer’s log for the success message before continuing.

  3. Check the debug images under /tmp/chessboard_analyzer_debug to confirm the corners were found correctly. Set save_debug_images:=true to have them written.

  4. Stop the calibration launch.

    Important

    The analyzer owns the RealSense camera, and the perception launch cannot open the camera while the analyzer still holds it. Pass shutdown_after_calibration:=true to have the launch shut itself down one second after a successful calibration.

The launch accepts the following arguments:

Argument

Default

Description

robot_plays_as

white

Which side the robot plays.

shutdown_after_calibration

false

Shut the launch down one second after a successful calibration.

save_debug_images

false

Save the corner-detection debug images.

debug_output_dir

/tmp/chessboard_analyzer_debug

Where those debug images are written.

Caution

/tmp does not survive a reboot. Keep a copy of the file and put it back before starting the perception launch, otherwise redo the calibration:

# Right after calibrating
cp /tmp/board_square_points.yaml ~/board_square_points.yaml

# After a reboot
cp ~/board_square_points.yaml /tmp/board_square_points.yaml

The detector picks the file up as soon as it appears and re-reads it whenever it changes on disk, so FEN publishing starts without restarting anything, and restoring the copy also works while the perception stack is already running. Until a valid calibration exists the node warns and skips FEN output; the piece detections themselves are unaffected.

Note

The camera_position setting in chessboard_analyzer’s config/chess_board.yaml must match where the camera actually sits, because it alone decides which image corner becomes a1:

Value

Camera placement

a1 lands at

white

At white’s seat, 0 degrees

Image bottom-left

side_left

Beside the board, 90 degrees

Image bottom-right

black

At black’s seat, 180 degrees

Image top-right

side_right

Beside the board, 270 degrees

Image top-left

Pick the value whose a1 corner matches your live image. The detector’s own square lookup depends on this, so a wrong camera_position produces a board state that looks plausible but is rotated.

Running the Demo

Hardware-Free Run

To exercise the game logic and the behavior tree without any hardware:

ros2 launch renesas_demo_queens_hand behavior_bringup_mock.launch.py

For a software-only game with no arm in the graph at all:

ros2 launch renesas_demo_queens_hand chess_vs_stockfish.launch.py

Full Demo

Complete both calibrations, board to robot and camera to board, before starting the demo. Then bring the stack up in this order, each in its own terminal.

  1. Bring up the robot. Initialize the hand’s USB-to-CAN adapter first, then launch the arm and the hand together, naming the interface of each adapter:

    # Ruiyan RH2 hand: initialize its CAN interface once per power cycle
    cd ~/ros2_ws
    ./install/ruiyan_rh2_hand_bringup/share/ruiyan_rh2_hand_bringup/setup/ruiyan_rh2_init.sh
    
    ros2 launch piper_arm_ruiyan_hand_bringup piper_arm_ruiyan_hand_joint_position.launch.py \
      arm_can_interface:=can2 hand_can_interface:=can3
    

    can2 and can3 are the interfaces the two USB-to-CAN adapters enumerate as, not the onboard CAN-FD header. With both adapters plugged in, run ip link show | grep can to confirm which name belongs to the arm and which to the hand, and pass them accordingly.

  2. Start perception.

    ros2 launch renesas_demo_queens_hand chess_perception_realsense_camera_rcar.launch.py
    

    This starts the RealSense camera, the chess-piece detector with convert_to_fen: true and a confidence threshold of 0.7, chessboard_analyzer, the board renderer, the Foxglove bridge, and the overlay nodes. It accepts the following arguments:

    Argument

    Default

    Description

    robot_plays_as

    white

    Which side the robot plays. Sets the board renderer’s orientation.

    model_type

    yolov8_chess

    Model key in the config/models/models_config.yaml of rcar_chess_pieces_detection.

    calibrated_board_file

    /tmp/board_square_points.yaml

    Square-center calibration written by chessboard_analyzer. Picked up automatically when it appears or changes.

  3. Start the demo stack.

    ros2 launch renesas_demo_queens_hand behavior_bringup.launch.py
    

    This starts the execute-layer servers, chess_engine_node, chess_board_geometry_node, chess_game_manager_node, and the behavior-tree engine. It accepts initial_fen to start or resume from a given position, and pgn_file with ply to replay a PGN up to a given number of half-moves. Sample games are in config/pgn/.

  4. Start the game. Nothing moves until the game manager activates the engine. Wait for the behavior-tree engine to report that it configured the stack:

    [behavior_tree_engine_node-7] [INFO] [1783596452.058739786] [bt_engine]: Configured stack: Chess Playing
    

    Then start the game:

    ros2 service call /chess/game_control chess_interfaces/srv/GameControl "{command: start}"
    
  5. Play.

    The demo is ready to play chess against a human. Open Foxglove to see the board state, the detected pieces, and the 3D pose of each square. Load the renesas_demo_queens_hand/config/foxglove/chess_demo.json layout and control the game from its buttons.

Note

The behavior-tree engine starts 10 seconds after the launch, 12 seconds in the mock bringup, so the servers advertise their actions and services first. The launch then emits only the CONFIGURE lifecycle transition about 2 seconds later, which builds the tree but does not tick it. ACTIVATE comes from the game manager’s start command.

Game Control

The game manager serves /chess/game_control (chess_interfaces/srv/GameControl) with the commands start, pause, resume, restart, and stop, and drives the bt_engine lifecycle accordingly. restart optionally takes a fen: field. The manager also publishes a latched /chess/game_paused, consumed by the tree’s GamePauseGate, and /chess/game_control_result.

ros2 service call /chess/game_control chess_interfaces/srv/GameControl "{command: pause}"
ros2 service call /chess/game_control chess_interfaces/srv/GameControl "{command: resume}"
ros2 service call /chess/game_control chess_interfaces/srv/GameControl "{command: stop}"

Pause takes effect at the next turn boundary. The Foxglove layouts provide service-call buttons for every command.