The 3Robot Chess Computer

The 3Robot Chess Computer

Avatar of chrislamuk
| 0

The 3Robot Chess Computer - Successor to the Novag 2Robot

The 3Robot is my project to restore a faulty Novag 2Robot to full functionality with a new chess engine, user interface and fully working arm. Read on to see how I finished the project.

If you haven't already, read my previous blogs first:

Inside the Novag 2Robot - A deep dive into the original Novag 2Robot

Introducing The 3Robot - The opening chapters of my 3Robot project

Introduction

Recently I bought a faulty Novag 2Robot off eBay for about £100. After some initial attempts to fix it I decided to remove everything electrical from the 2Robot and install an Arduino Mega microcontroller along with new wiring and photo sensors. By the end of the Introducing The 3Robot blog the 3Robot was moving its arm and making moves to The Opera Game.

The Arduino Mega Chess II engine

The original plan was to buy a Raspberry Pi 4 and use Stockfish, with the Pi and Arduino talking to each other. But then I saw that someone had written a chess program for the Arduino Mega with a small touchscreen. So here was an opportunity to avoid adding a Pi and keep all the software on one board.

All the touchscreen code had to be removed and a simple text interface added so I could talk to the chess engine via the serial port with my PC. Initially I emulated the touchscreen, outputting any text to the serial port. My typed commands were translated into touchscreen gestures. Over time all the old touchscreen code was slowly unpicked and I was left with just the engine and my serial user interface. I could now play chess using text commands.

The chess engine is an impressive bit of coding, densely and economically written with few comments. I find it amazing that someone can code something so complex in this manner... when I code I like to use long meaningful variable names and spread the code out so it's more understandable and maintainable. The innards of the chess code (search, pruning and evaluation) I haven't looked at too closely but I've always been interested in how computers play chess and this may be a good place to learn as a future project. It lacks an opening book, so has a habit of playing knights as a first move, but I added one later. It's also missing endgame hash tables and its endgame tends to be weak.

As the project continued I noticed the chess engine starting to do some odd things, like making illegal moves or pieces disappearing off the board. Eventually I realised that lack of memory was the problem. As more program code (in particular, strings) was added, less memory became available for the engine and this caused problems (heap and stack colliding) during its recursive routines, resulting in corrupted data. Once I freed up more RAM it behaved fine.

Extra code was added so chess engine and user interface could exchange the information needed to drive a physical chess computer. Also some bug fixes and enhancements.

The author claimed the engine has an Elo of around 1900 (at 5 seconds per move) and it does seem quite strong. Auto-games (games where it played itself) suggested accuracies of around 80-90% when imported into Lichess. It's stronger than some of my handhelds in the 1400 region so I would guess it's around 1700 at 2 seconds a move.

The 3Robot Program

The program code can be divided into three parts: arm control, chess engine and user interface. The user interface acts as a manager between hardware and chess engine, processing user input and generating output to the display and speaker.

The Arm, the Board and the Engine

With the chess engine ready I got 3Robot to move the arm on the engine's move. But how to park the captured pieces on the side? I had already calibrated the car park squares (as seen in The Opera Game demo) but there I was telling it where to park the pieces whereas now I had to automate it. I noticed the old 2Robot parked the pieces from top-to-bottom so I decided to set up variables to point to the topmost square for each type of piece and adjust the variable to point to the square below after a piece was parked. If a piece came off the car park due to take-back or promotion then the variable will be adjusted to point to the square above.

So far all my moves were typed in. It was time to get the chess board working. I made a pair of cables to connect the two 8-line flat cables to the Arduino. Adding code to integrate the board was straightforward... it's a 8x8 keypad and Arduino has a library for that.

It was a great moment when the arm, the board and the chess engine started to work together! A proper chess computer at last! Here's my first game with The 3Robot with real pieces over the board.

The 3Robot triumphs... and with an accuracy of 97%... so I can't be too disappointed.

Buttons, Display and Speaker

It was starting to feel like a proper chess computer now but it was still missing its buttons and LCD screen. The buttons were fitted back into the base and wired up, and now I could add some functionality for the New and Go buttons. A piezo-loudspeaker was added to make beeps to acknowledge board and button presses... amazing how a few beeps can really add to the overall experience.

For the display I chose a LCD display (1602) with 2 lines of 16 characters though only 13 characters are visible through the plastic slot. The RGB backlight colour and brightness are adjustable and there's the option to use custom characters. This device uses the I2C protocol and needs only four wires in total compared to twenty for the original LCD. The text layout is ideal for chess and will also provide a much better experience than the old display.

In order to fit the new display all the plastic in the base that held the old one was cut away and sanded down.

Once done the new display sat nice and flush against the underside of the display slot and was glued in place with silicon sealant. There was only one choice of colour for the LCD display... a deep red to match the robot hand.

I added a MicroSD card adapter so I can conveniently transfer games to my PC for analysis. The device uses the SPI interface and needs 4 data wires. It fits nicely at the back of the base where the old power switch used to be; a slot was cut and the device screwed into place (it was lucky there was some excess plastic there to fit screws, else I would've had to leave it dangling out the back... the eject mechanism for the SD card needs a lot of force). Also the 3Robot can serve as a simple game recorder... you play on another chess machine and mirror the moves into 3Robot (with computer play switched off) and analyse later.

More plastic was removed from the base... the plastic internal pillar which goes into the arm pillar was sawed off as it would have only got in the way.

A plastic mount for the Arduino Mega was glued to the underside of the base and the computer fitted. Wiring was re-arranged and taped down. So at long last the lid to the base can be screwed back on.

With a gentle press the lid screwed back on fine, a huge relief. Otherwise I would've had to separate the Arduino and the motor board, and sit them side by side and wire them together. The Arduino will be powered by the USB cable (needed for updates) with mains adapter, and a separate 6V mains adapter for the motor power.

With the 3Robot fully assembled it's was a case of finishing off the program and adding any last minute features and fixes. The last code added was the openings book. The program eventually weighed in at 6000 lines of code; about 2000 was the user interface, 1000 the arm and 3000 the chess engine.

Presenting The 3Robot

A quick tour of The 3Robot's features: there are the usual things you'd expect from any chess computer like clocks, take-backs, change level, save/load game (2 slots in EEPROM), verify pieces, sound on/off. Press Go and the computer makes the next move and you can swap sides any time. The computer engine can be turned off so you can play the first few moves manually (say you wish to practise a particular opening) and then turn the engine back on. 

Extra features include LCD brightness and colour options, a screensaver and a PGN viewer to view all the moves. The current game's moves can be exported to a text file on the SD card. The arm can, of course, be turned off, and you will be prompted to make its moves manually. And there is an auto-play option (press and hold Go) and the computer and arm will take over, making all the moves.

The engine generates an evaluation of the position but it's an internal number, not centipawns; but just for fun I added an option to display it as a little bar chart of plus and minus symbols.

Prompts are provided during castling (to move rook), en passant (remove pawn), promotion (choice of promoted piece) and take-backs. The computer can detect draws due to threefold repetition, 50-move rule and insufficient material, as well as stalemate.

Features omitted include setting up a position, solving mate-in-N, blitz and tournament-type games... all things I wouldn't use. I also didn't bother with the arm performing take-backs.

There's a calibrate option but it's not as sophisticated as the original 2Robot procedure; The 3Robot moves the arm to a chosen square and lowers and closes the fingers to point to the square... this I found a good way to see how accurately it has reached the square; but any adjustment has to be made manually in the code and uploaded to the computer. The arm has been pretty accurate of late and I've found little need to tweak the calibration data.

Error handling is improved; rather than displaying a vague "ErrArn" message, 3Robot will report which part of the process failed. It will ask if you want to retry or cancel rather than keep retrying endlessly!

Here's a video demonstrating all the buttons and features.

My First Game vs The 3Robot

Time for my first game with The 3Robot since completion... this was an epic 3 hour struggle but I managed to edge it in the end after a bad start.

Conclusion

So there we have it... The 3Robot done and dusted. I'm very pleased with it, it's turned out much better than I could've ever imagined. The arm has behaved itself and I've not had to open it since assembling it. Total spend on parts was about £120. Plus a lot of time, of course.

Being able to analyse games easily means this is now my chess computer of choice... one of the downsides of old chess computers is the inability to analyse games unless you write down the moves or mirror the moves into Lichess as you play.

As for future plans... tinkering aside perhaps add some speech synthesis and speech recognition so you tell The 3Robot your move and the arm makes it for you. I could give The 3Robot a "personality" and it would say things during the game, helpful or otherwise. Depending on how I get on with the chess engine I could eventually add a Raspberry Pi with Stockfish or Leela engines. 

Hope you've enjoyed my 3Robot blogs, thanks for reading and bye for now