Chess-Competition uses a simplified method to create a chess game experience between a UR5 robot arm and a human player. A perception process was created in order to mitigate the error during the piece recognition process by using OpenCV to recognize different colors of the chess pieces.
Read Install ROS Wrapper 2.0 for Intel RealSense Devices for the full instruction.
Connect with the gripper and the UR5 arm
roslaunch ur_modern_driver ur5_bringup.launch robot_ip:=10.42.0.175
rosrun hiro ur5_arm_node.py _robot_ip:=10.42.0.175
rosrun robotiq_2f_gripper_control Robotiq2FGripperRtuNode.py /dev/ttyUSB0
(optional) rosrun robotiq_2f_gripper_control Robotiq2FGripperSimpleController.py
Run Chess Competition python script
python movement.py
Chess Competition is a program that enables robotic arms to play chess against a human player. Using an RGB picture of the current board layout, it masks all colors except the specific blue and red of the chess pieces to locate the positions of those chess pieces. It then determines its moves based on the chess pieces’ positions on the board. The program stops when either the player or the robot wins.
In Chess-Competition, there are three different forms of information about the chessboard used. The first form is in the form of a virtual chessboard inside the program, written with FEN (Forsyth–Edwards Notation), so that the chess engine can recognize the position of each chess piece later on. The second form is the actual chessboard, in which the program stores information about the real game situation. The last form is a simplified chessboard which is stored as an 8x8 matrix. This form stores color information of each position.
When human player moves a piece, the information on the actual chessboard is converted into a new simplified chessboard by using OpenCV. OpenCV recognizes different colors on each position of the actual chessboard, and saves them into a single 8*8 matrix.
By comparing the new simplified chessboard with the one from the previous turn, the program figures out which movement is made by the human player. From the example above, the human player moves “f7f5”, meaning a piece from the cell f7 is moved to the cell f5.
The movement is then saved to the virtual chessboard (written with FEN), by the program running the command “execute movement f7f5” to the virtual chessboard.
The program uses an open-source chess engine called “Stockfish 10” to calculate the next best movement from the current chessboard information. By entering the virtual chessboard information retrieved at the end of the human turn to the engine, it calculates the best movement that can be made by the robot. From the above chessboard, Stockfish calculated that “g1f3” would be the best movement, and is then executed.
The UR5 robot arm then updates the actual chess board to match the virtual chessboard.
OpenCvRealSenseCameras.py
This is the script for the chess pieces detection. This script will:
color_range()
This function calculates the acceptable range to generate a mask to only show the specified color.
color_detect()
This function masks the image and reveals only the color needed. It then finds the contours of the shown shapes of color and finds the center points of those contours.
thresh_callback()
Calls color_range()
and color_detect()
.
Grid()
This function checks each square on the chessboard and determines if there is a red or blue object in the squares.
movement.py
This is the master script that moves the arm and opens the gripper to pick up and move the chess pieces. This script will:
OpenCvRealSenseCameras.py
script.