Dark Matters
  • About Me
  • Publications
  • Educational Resources
  • Blog
  • Other Interests

Towards a Robotic Chess Board: Stockfish and Python

9/6/2013

7 Comments

 
I have had it in my head for a while to build a robotic chess board, a chess board where I play against a computer that controls a robotic arm of some sort that moves real pieces on a real board. The "thinking" will be done on a Raspberry Pi computer, which I bought this week as a graduate gift to myself for having finished my PhD program. The arm will be controlled by an Arduino microcontroller. I have already learned how to get Arduino and Raspberry Pi to communicate with each other by reading Simon Monk's nice book Programming the Raspberry Pi:  Getting Started with Python. The Arduino and Raspberry Pi communicate nicely though the serial port using the PySerial package. I have tested this out and it works wonderfully. I must write the Arduino code on my Mac though because the Arduino software released for the Linux Debian operating system is a very old version.

For the chess engine, I have decided to use Stockfish, which uses the Universal Chess Interface. It is consistently rate one of the strongest chess engines. The creators of Stockfish have not provided any documentation for their engine, and the Universal Chess Interface was nontrivial to decipher. Therefore, it took me some time to figure out how to communicate with Stockfish through the terminal.

Tonight, I was able to write a proof-of-concept Python program which let me play chess with Stockfish through the Python shell. This is the first step in writing the software for a USB chessboard. I have included the Python code below. Of course, the Stockfish engine must be installed first. 
Python Code

#This code talks to the Stockfish Chess Engine. Entries are to be made in algebraic
#chess notation (eg. e2e4). Stockfish is set to think for a maximum of 1 sec. 

import sys

chess = r'/Applications/stockfish-4-mac/Mac/stockfish-4-64'.split()['linux' in sys.platform]

import subprocess as S
getprompt = 'isready'
done= 'readyok'

proc= S.Popen(chess, stdin=S.PIPE, stdout=S.PIPE, bufsize=1, universal_newlines=True)

print(proc.stdout.readline().strip())
proc.stdin.write('uci\n')

while True :
        text = proc.stdout.readline().strip()
        print(text)
        if text == "uciok":
            break
        
print('Choose skill level (0-20):')
skillLevel=input()
proc.stdin.write('setoption name Skill Level value '+skillLevel+'\n')
proc.stdin.write('ucinewgame\n')

moveList='position startpos moves '
checkmate=False
while checkmate==False:
    print('Enter move:')
    move=input()
    moveList=moveList+move+' '
    proc.stdin.write(moveList+'\n')
    proc.stdin.write('go movetime 1000\n')
    print('Computer moves:')
    while True :
        text = proc.stdout.readline().strip()
        if text[0:8] == 'bestmove':
            cpuMove=text[9:13]
            print(cpuMove)
            moveList=moveList+cpuMove+' '
            break

7 Comments
Igor Rogov link
2/9/2014 10:10:35 pm

Hi,

Very impressive work, thank you for sharing it. Looks like I am working on an identical project.
Have you managed to accomplish it?
Since I have some background of embedded programming, I stared with OWI Robotic arm modification and found your blog upon searching for APIs to some web based chess engines.
Have you tested your Python Code?

By the way, you could read 64 inputs of the chess board with only 3 pins of Arduino using a cascade of two SN74HC165N SPI shift registers.

Best regards,
Igor

Reply
Ben link
2/10/2014 10:54:32 am

Hi Igor,

I have taken a bit of a break from the robotic chess board because I am using all my free time looking for a new job. I hope to get back to this project soon.

Thank you for the advice with the shift registers. I will probably end up using it. I haven't used shift registers before so that will be fun.

I have tested my Python code in that I can play chess in the terminal with it. I need to figure out how to effectively iterate through all my Hall sensors effectively before expanding the program.

One piece of advice is that if you are using Hall sensor, make sure they are bipolar. I bought the wrong one and am trying to just make it work instead of buying another $70 in sensors.

Good luck!

Ben

Reply
Igor Rogov
2/10/2014 06:14:00 pm

Hi Ben,

Thanks for your reply. Its pity you postponed your robotic chess board project; together we could finish it much faster.
If you will decide to move forward with it - just drop me your email address; it will be much easier to communicate.
By the way, I purchase all the parts directly from Chinese sellers on Ebay. It is 5 times cheaper than purchasing it from Amazon. 64 hall effect sensor would cost ~10USD, free delivery.
Although, after second thought, I don't think hall effect is a best choice. You need an analog input to use the hall effect sensor ; means there is no option to use the shift register. I would use simple micro-buttons.

Anyway, I wish you good luck in your job search!

Best regards,
Igor

Reply
Igor Rogov
2/10/2014 06:14:06 pm

Hi Ben,

Thanks for your reply. Its pity you postponed your robotic chess board project; together we could finish it much faster.
If you will decide to move forward with it - just drop me your email address; it will be much easier to communicate.
By the way, I purchase all the parts directly from Chinese sellers on Ebay. It is 5 times cheaper than purchasing it from Amazon. 64 hall effect sensor would cost ~10USD, free delivery.
Although, after second thought, I don't think hall effect is a best choice. You need an analog input to use the hall effect sensor ; means there is no option to use the shift register. I would use simple micro-buttons.

Anyway, I wish you good luck in your job search!

Best regards,
Igor

Reply
Blair Davis link
3/29/2016 09:12:00 am

I am also working on this, Have been for some time, I think that I have an option or solution for getting this completed. I would like to know if you are still working on this as my development is going slow. I have to learn a lot of programming that I have not used in a long time. I could use some help developing.

Thanks for the time and the code, I hope to hear from you if this is still a work in progress for you.

Reply
sk shahid ali link
7/31/2016 03:07:01 am

this code is not running in python 2.7 its showing some error......plzz help me.....in which version should i run ths...

Reply
Ben link
7/31/2016 05:15:03 am

You can tell from the print statement that it's Python 3.

I haven't looked at this project in nearly 3 years. I don't think I can be of too much more help.

Reply



Leave a Reply.

    Archives

    July 2015
    February 2014
    November 2013
    October 2013
    September 2013
    July 2013
    June 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013

    Categories

    All
    Arduino
    Chess
    Lightsaber
    Math
    Probability
    Projects
    Python
    Raspberry Pi
    Robotics
    Statistics

    The Great Arduino Project

    RSS Feed

Powered by Create your own unique website with customizable templates.