Lego Mindstorms NXT
(Checkers Playing Robot)
The first thing I decided was that it must be able to play using a normal chessboard and normal pieces. Furthermore I only have one NXT set and therfeore only three motors. I needed two motors to position the ’hand’ over the correct square so this left just one motor to pickup and move the pieces.
The second major challenge would be the logic strategy. I really didn’t want to spend hours and hours trying to write a program for this: I’d rather spend the time on the actual construction.
Luckily I was able to find a reasonably simple program on the Internet called ’simplech.c’ which was written by Martin Fierz (thanks Martin) . Then came the problem of converting it to RobotC and getting it to run on the NXT.
RobotC does not support recursive functions and ’simplech.c’ uses them extensively. It also allows for a maximum ’depth’ of 99 (ie it can look 99 moves ahead). As the object of the exercise was to build a robot that could play a reasonable game of checkers, not a world beating grandmaster, and because of the small amount of memory available on the NXT I ended up with a depth of three which seems to work fine.
The ’hand’ to pickup and move the pieces proved to be the biggest challenge by far. Not only did I only have one motor but the wooden board I was planning to use was also badly warped so that two corners were several millimetres higher than the other two.
The other major problem I ran into was detecting what pieces were where. I needed to be able to distinguish between a White Man, a White King, a Black Man, a Black King and an empty square. First I decided that a king could be represented by flipping a piece over. I then painted the the black men red on the top and blue on the bottom, and the white men gold on the bottom. The black squares on my board are a very dark brown.
I tried using the light sensor but that didn’t work so I settled on the HiTechnic Colour sensor. The problem is that the colour sensor only works at a small distance (about 1cm) so that meant I needed to be able to raise and lower the sensor. This further complicated the design of the ’hand’ and the stiffness of the NXT cable almost scuttled the whole idea.
The design of the two rails that allow the hand to be positioned over the required square was much less critical. I would have liked to have had a rail down each side of the board with a gear and rack drive on both sides so the movement was smooth and precise but I didn’t have enough racks so had to settle on the design as per the picture. As you can see from the video this meant the hand tends to wobble about quite a lot which further complicated the design of the ’hand’ as I could not guarantee the exact position it would be it relative to the piece I wanted to pickup.
The last major challenge was how to flip a piece over when it needed to become a king. To do this I added a small tray with walls on each side. Then by dropping the piece on the edge of one wall it would flip over and land in the tray. I then pick it up again and move it to the required position. You can see this small tray at the end of the larger tray where the ’jumped’ pieces are deposited at the rear of the machine.
The NXT brick is situated at the front right. This allows for easy replacement of the batteries and, more importantly, tells the player what is happening and what to do. A small representation of the board is also display on the NXT screen.
Apart from the strategy program that I mentioned earlier I obviously needed code to actually make the moves and play the game.
The sequence of events is as follows:
Initialization: The hand is moved as far back and as far to the right as possible using a ’stalled motor’ technique. This determines the ’home’ position.
You are then given the option to calibrate the board. This is necessary the first time you play, or if the ambient lighting changes a lot. You must then set up the board as directed, with the pieces on the back rows turned over as if they are kings. The board is then scanned, the ranges for the five different states measured and stored in a file. This saves having to recalibrate every time you turn the NXT on.
You set up the board are desired, either for a new game or you can set it up for a partially played game. Then press play.
The board is scanned and the robot makes its move. Then it is your turn again. The robot will fall aslepp and start snoring if you take too long. This process continues until the game is finished (or you get bored, or the batteries die). The scanning process is quite slow, about 80 seconds. I found that to get accurate and consistent results I needed to move to each square, and then take three different readings at three different heights, taking the highest reading. I then compare it to the values obtained during calibration to determine what if anything occupies that square.
Quite a few things can go wrong when the robot tries to make a move. It can fail to pick the piece up, it can drop the piece while moving it, it can bump another piece when picking it up, or it can disturb other pieces when dropping the piece. So I added a ’reset and try again’ option. If you press the ’Play’ button while the robot is making a move, it will stop, go to the home position and make sure the hand is open, and then if you press play again after you have fixed up the board, it will repeat the move. The usual problem is that it fails to pick then piece up.
Flipping a piece over to make a king tends to be a bit hit or miss so you may need to help it out sometimes.
It wasn’t practical to film an entire game so the video shows the various functions and is not in in sequence or even all from the same game.
Home The NXT Step Nxtasy RobotC Bricklink HiTechnic Mindstorms Home Contact Me
LEGO, MINDSTORMS and NXT are registered trademarks of The LEGO Group.