Introduction: Chess Robot Raspberry Pi Lynxmotion AL5D Arm
Build this chess robot and see it beat everybody!
It's pretty easy to build if you can follow the instructions on how to build the arm, and if you have at least an elementary knowledge of computer programming and Linux.
The human, playing white, makes a move. This is detected by the visual recognition system. The robot then ponders and then makes its move. And so on ...
Perhaps the most novel thing in this robot is the code for move recognition. This vision code is also usable for chess robots built in many other ways (such as my chess robot with LEGO build).
Because the human's move is recognised by a vision system, no special chess board hardware (such as reed switches, or whatever) is needed.
My code is available for personal use.
Step 1: Requirements
All the code is written in Python, which will run on, amongst other things, a Raspberry Pi.
The Raspberry Pi is a small, inexpensive (around $40) single-board computer developed by the Raspberry Pi Foundation. The original model became far more popular than anticipated, selling for uses such as robotics
My robot uses a Raspberry Pi, and the robot arm is built from a kit: Lynxmotion AL5D. The kit comes with a servo controller board. (The link I've just given is to RobotShop's US site; click on one of the flags at the top right of their site pages for your country, e.g. UK).
You will also need a table, a camera, lighting, a keyboard, screen and pointing device (e.g. mouse). And of course, chess pieces and a board. I describe all these things in more detail in the subsequent steps.
Step 2: The Hardware Build
As I previously indicated, the heart of the vision code will work with a variety of builds.
This build uses a robotic arm kit from Lynxmotion, the AL5D. Included with the kit is an SSC-32U servo controller board, which is used to control the motors in the arm.
I chose the AL5D because the arm has to be able to make repeated accurate movements and not drift off. The grabber has to be able to get between pieces and the arm has to be able to reach to the far side of the board. I still needed to make some modifications as detailed below.
EDIT: The Raspberry Pi 4 is now available.
You will need:
- A 15W USB-C power supply – we recommend the official Raspberry Pi USB-C Power Supply
- A microSD card loaded with NOOBS, the software that installs the operating system (buy a pre-loaded SD card along with your Raspberry Pi, or download NOOBS to load a card yourself)
- A keyboard and mouse (see later)
- A cable to connect to a display via a Raspberry Pi 4's micro HDMI port
I needed further reach on the robot arm, so I made some minor modifications to it, using additional Lynxmotion parts which can be bought from RobotShop:
1. Replaced the 4.5 inch tube by a 6 inch one - Lynxmotion part AT-04, product code RB-Lyn-115.
2. Tried using an additional set of springs, but went back to one pair when I implemented item 3 below
3. Extended the height using a 1 inch spacer - Lynxmotion part HUB-16, product code RB-Lyn-336.
4. Extended the gripper reach using spare gripper pads attached by some spare LEGO pieces I had and elastic bands(!) This works very well, as it introduces flexibility when lifting pieces.
These modifications can be seen in the image above on the right.
There is a camera mounted above the chess board. This is used to determine the human's move.
Step 3: The Software Which Moves the Robot
All the code is written in Python 2. Inverse kinematics code is needed in order to move the various motors correctly such that chess pieces can be moved. I use library code from Lynxmotion which supports moving the motors in two dimensions and have added to that with my own code for 3 dimensiions, gripper angle and gripper jaw movement.
So, we then have code which will move pieces, take pieces, castle, support en passant, and so on.
The chess engine is Stockfish - which can beat any human! "Stockfish is one of the strongest chess engines in the world. It is also much stronger than the best human chess grandmasters."
The code to drive the chess engine, validate that a move is valid, and so forth is ChessBoard.py
I use some code from http://chess.fortherapy.co.uk to interface with that. My code (above) then interfaces with that!
Step 4: The Software Which Recognises the Human's Move
I have described this in detail in the Instructable for my Chess Robot Lego build - so I don't need to repeat it here!
My "black" pieces were originally brown, but I painted them matt black (with "blackboard paint"), which makes the algorithm work better under more variable lighting conditions.
Step 5: Camera, Lights, Keyboard, Table, Display
These are the same as in my Chess Robot Lego build, so I don't need to repeat them here.
Except that this time I used a different and significantly better speaker, a Lenrui Bluetooth speaker, which I connect to the RPi by USB.
Also I am now using a different camera - an HP Webcam HD 2300, as I couldn't get the previous camera to behave reliably.
The algorithms work best if the chessboard has a colour that is a long way from the colour of the pieces! In my robot, the pieces are off-white and brown, and the chess board is hand-made in card, and is a light green with little difference between the "black" and "white" squares.
The algorithms need a particular orientation of camera to board. Please comment below if you are having an issue. The arm has limited reach, and so the square size should be 3.5 cm.
Step 6: Obtaining the Software
If you run Raspbian on your RPi you can use the Stockfish 7 engine - it's free. Just run:
sudo apt-get install stockfish
2. ChessBoard.py Get this from here.
3. Code based on http://chess.fortherapy.co.uk/home/a-wooden-chess... Comes with my code.
4. Python 2D Inverse Kinematics library - https://github.com/Lynxmotion/Arms/tree/master/Code%20examples/Python%20(RPi)
5. My code which invokes all the code above and which gets the robot to make the moves, and my vision code. Get this from me by first subscribing to my YouTube channel, then clicking on the "Favorite" button near the top of this Instructable and then posting a comment to this Instructable, and I will respond.