The 3Robot - An Update with Arduino Giga and Stockfish
Introduction
After using the 3Robot for a while it got to the point where I was waiting 30 to 60 seconds for the chess engine to decide its move. At this skill level it gave me a tough game but winnable. In addition the arm takes another 30 seconds to move. This added up to the best part of an hour per game waiting on the 3Robot and so I was keen to find a way to speed up the chess engine and the robot arm.
If you've not read my previous blogs on how I converted a broken Novag 2Robot chess computer into The 3Robot here are the links to Part One and Part Two.
How to make the chess engine faster
There are a few options available to make the 3Robot to play faster.
∙ Add a Raspberry Pi (or Arduino Uno Q) Both these computers have a Linux environment so Stockfish and other engines can run on them. The Pi/Q can talk with the existing Arduino Mega via a serial connection. This means having to accommodate a second computer inside the 3Robot and another USB cable for power. But it's a neat all-in-one solution without relying on external chess engines via wi-fi or Bluetooth. However the internal Arduino chess engine does not speed up.
∙ Add a wireless module to the Mega and get Stockfish moves remotely, e.g. from the internet or a local chess web server on my laptop computer. The main downside of this is that the extra wi-fi libraries will take up valuable RAM on the Mega needed for the internal chess engine, and that RAM cannot be freed regardless of whether the wireless feature is used or not. Again the internal Arduino chess engine does not speed up.
∙ Replace the Mega with an Arduino Giga They have the same form factor and the Giga is faster, comes with more RAM and has wireless features. The internal chess engine will get a speed boost. The 3Robot innards are kept simple with only one board to worry about. However it's the most expensive option.
I finally opted to replace the Mega with a Giga.
The Arduino Giga
The Arduino Giga is one the latest additions to the Arduino range and is, in my opinion, one of the most exciting (but also see the Arduino Uno Q). It has the same form factor as the Mega but comes with an ARM Cortex chipset featuring two CPU cores and much more RAM, program and flash storage. There's wireless and Bluetooth, extra I/O pins, a real-time clock, USB slot for an external flash drive and a 3.5mm audio jack for stereo output and microphone. It's expensive at £65 but there's a lot of tech there and existing Mega projects should convert over to it fine.

Swapping the Mega for the Giga
The first challenge was to get the code compiled for Giga. The Arduino IDE uses different compilers for the Mega and Giga and threw up what seemed like a hundred errors and warnings. Many of the warnings highlighted issues in the chess engine (particularly around types) which, if undetected, may have caused the engine to play incorrectly... and I may have been at a lost to correct them. After a long day fixing the errors it finally compiled. First indications were the chess engine ran fine on the Giga... which is a gross understatement because it ran blisteringly fast, around 75 times faster! Moves which took minutes to compute can now be played in seconds. Beforehand I thought a 3x speed increase would be good... so 75x is just incredible.
The Giga has two processor cores, a primary M7 core and a slower M4 co-processor. At the moment all the code runs on the M7 core.
Satisified that the code was working OK it was time to replace the old Mega board with the Giga. The swap was reasonably straightforward, the motor board and all the wires plugged into the same sockets on the Giga with only a few wires (e.g. SPI for the SD Card reader) having to move elsewhere.

The Giga runs at 3.3V rather than 5V (as on the Mega) but this didn't cause any problems. A quick test showed the arm moved fine. The LCD display worked OK but the keypads (buttons and chess board) were sometimes returning the wrong key, or giving phantom key presses. I assumed it was a hardware problem as I'd read there were issues with the Giga and pull-up pins. But it was actually a software problem and after hours of trying to fix it I decided to dump the Keypad library and write my own keypad code.
The SD card reader code also needed attention and with the extra RAM I was able to use the standard SD card library rather than the limited but lightweight PetitFS which was necessary for the Mega due to RAM considerations. Memory was tight on the Mega with only 8K of RAM but now I can forget about those worries as the Giga has about 500K of useful RAM (technically 1MB but not contiguous). The EEPROM API is not available to Giga so I'm now saving and loading games to the SD card instead.
Adding external chess engines
The wi-fi feature of the Giga provides an opportunity to play against other chess engines like Stockfish on the 3Robot. There are a couple of Stockfish servers online which can be accessed via HTTP... you simply send the FEN and the required depth and it returns a move. However I decided to set up my own chess web server on my laptop so 3Robot can play against multiple engines like Stockfish, Komodo, Leela Chess Zero and Maia. Moves using the chess server only takes a few seconds, depending on depth.
For more about integrating Stockfish and other chess engines into your projects see my blog on "How to integrate Stockfish into your projects using Python".
As Stockfish tends to make the same opening move for any given depth I've made use of my existing opening book code to give Stockfish and these other engines some variation. (The opening book can be turned off.)

You may ask why bother to keep the Arduino chess engine now I have access to other powerful chess engines. The Arduino chess engine on the Giga is still needed as it does more than just play chess. Each Stockfish move is fed into it (just like a human move) and the Arduino chess engine updates its internal chess board and history. It also provides chess information and services (e.g. move details, move legality, if in check, en passant, game history, FEN, piece verification, takeback, etc.) to the user interface and arm. So it's still an important component of the system even if it doesn't calculate any moves itself. And besides it's nice to keep that engine so the 3Robot can play offline.
Speeding up the robot arm
Another way to save time on the 3Robot move is to speed up the arm. Other than making the arm move at full speed all the time, which isn't desirable, there are a couple of tricks.
One trick is to keep the arm in the START position (arm swung all the way back) after moving, rather than move it to the REST position (arm parallel with back of computer). This saves the arm moving to and fro from the REST position. The REST position is really an aesthetic nicety, and not vital to the function of the arm. At the end of the game, it's possible to tell 3Robot to move the arm to REST so it can be stored more conveniently.
Another trick concerns the arm always going to the START position before moving to a square. This improves accuracy as both arm motors move in the forward direction. Reversing the motors to get to a square needs to be avoided, as the "play" between gears will make it wildly inaccurate . However, if the arm is moving a piece to a square that requires both arm motors to continue moving forward, as in a bishop moving from a8 to h1, then this "quick-move" method should work well enough without the arm needing to return to START after picking up the piece.
After some coding, the quick-move worked really well, though I had to reduce the minimum motor speeds, but this helped with arm accuracy for all other moves. Around half of 3Robot's moves were completed with quick-move. Some pieces were also parked on the side using quick-move.
These two tricks not only save time, but reduces wear and tear on the arm, so definitely worth having.
Stockfish challenge
Here's a chess position I faced as White against Stockfish at 5 ply depth. Black just played Qa5... and things look bad for my knight but Black's knight is hanging. Two challenges for you... which side would you rather play as and how should White continue?
Arduino vs Stockfish
Finally just for fun I made the two engines face off against each other. I was curious to see at what level the two engines were equivalent. So turning off the opening book and setting the Arduino to 1 sec/move it was able to defeat Stockfish up to and including 4 ply depth. At 10 secs/move Arduino drew and won twice in three games against Stockfish at 5 ply depth. Here's an example game... Stockfish had the better of it but blundered its King at the end.
Since the Stockfish evaluation engine is much better this suggests that Arduino has to look a few ply deeper to compensate. Arduino at 10 secs/move on the Giga is equivalent to about 12 minutes/move on the old Mega board.
Conclusion
The 3Robot is now fitted with an Arduino Giga which greatly speeds up the internal chess engine and now moves in seconds rather than minutes. I also set up my own chess web server on my laptop computer and, using wi-fi, I can now use the 3Robot to play against Stockfish, Komodo, Leela Chess Zero and Maia.
The robot arm also has a couple of enhancements which speed up its moves, and help to reduce wear and tear on the motors and gears.
One future plan is to add speech and voice recognition. With Giga's fast processors and extra memory and storage this shouldn't prove a problem. The Giga can connect via Bluetooth to a combined speaker/microphone unit. The 3Robot can then read out moves and chat during the game. Maybe add voice commands so you can say "e2e4" and the arm will make the move for you... how's that for being lazy!