Saturday 28 May 2016

Integrating with the Python Chess Module

My next step was to integrate my rudimentary Novag Citrine interface with python-chess:

https://python-chess.readthedocs.io/en/v0.14.0/

The main 'glue' that was needed was code to convert moves from the Universal Chess Interface (UCI) format to the Novag format and vice versa. This module makes it easy to keep track of the position from within Python, and connect to external chess engines.

As noted previously, I found it necessary to send engine moves to the Citrine twice. A 0.1 second delay after each command worked well for non-captures. In this case, the piece to be moved was clearly indicated, as was the destination square when the piece was lifted. I found that this did not work well when the engine's move was a capture. In this case, the destination square flashed very briefly, and the piece to be moved was again indicated clearly, but the destination square was not indicated at all when the piece was lifted. I added another 3 second delay after sending the first capture, which gave more time to see which piece was being captured.

I can now play against the Stockfish engine on my Novag Citrine board.

Tuesday 24 May 2016

Rudimentary Novag Citrine Interface

My next experiment was to write a rudimentary interface for the Novag Citrine, using Python 3. With this simple interface, the user has to type the moves of an external engine onto the command line. The Citrine's responses are also written to the command line. I found that it was necessary to impose a 0.1 second delay between commands, to prevent some of the commands being lost. As noted previously, moves have to be sent to the Citrine twice. It is necessary to wait for the user to reply.  If the user takes back moves while the engine is thinking, the engine's move is discarded. If the user takes back the engine's move, and plays another move, the engine changes sides. Setting up the start position terminates the program, when "New Game" is received from the Citrine. Here is the program:

import serial, time
port = '/dev/ttyUSB0'
ser = serial.Serial(port, 57600, timeout=0.05)
print(ser.name, 'opened')

def send_command(command):
    ''' Send a command from the PC to the Citrine.
    '''
    ser.write((command + '\r\n').encode(encoding='UTF-8'))
    time.sleep(0.1)

def make_move(move):
    ''' Tell the Citrine to make a move, and return True if a valid reply is
    received. 'j' tells the Citrine to use its own engine to make the move.
    '''
    if move == 'j':
        send_command('j')
    else:
        send_command('m' + move)
        send_command('m' + move)
    line = ser.readline().decode(encoding='UTF-8')[:-1]
    print(line)
    return line != '' and line[0] == 'M'

def get_reply():
    ''' Get the reply, which should be a move, optionally preceded by one or
    more take backs. Return True if a valid reply is received.
    '''
    while True:
        line = ser.readline().decode(encoding='UTF-8')[:-1]
        if line != '':
            print(line)
            if line[0] != 'T':
                break
        time.sleep(0.1)
    return line[0] == 'M'

send_command('u on') # Turn Referee Mode on.
send_command('x on') # Turn Xmit on.
send_command('l tr8') # Set the Level.
ser.flushInput() # Flush the input buffer.

while True:

    move = input('Engine move? ')
    if ser.inWaiting() == 0: # Check that the input buffer is empty
        if not make_move(move):
            break
    if not get_reply():
        break

ser.close()


Everything appears to work satisfactorily.

Friday 20 May 2016

Connecting to the Novag Citrine using Python

I have been learning Python 3, and wrote a simple program to connect to the Novag Citrine:

import serial
port = '/dev/ttyUSB0'
ser = serial.Serial(port, 57600, timeout=0.05)
print(ser.name, 'opened')
print("When prompted, enter a Novag command, '' to read, or q to quit")

while True:
    while True:
        byteline = ser.readline()
        if byteline == b'':
            break
        print(byteline.decode(encoding='UTF-8')[:-1])

    command = input('Command? ')
    if command == 'q':
        break
    elif command != '':
        command += '\r\n'
        ser.write(command.encode(encoding='UTF-8'))


ser.close()

I tested this program on Ubuntu Linux. It should also work on Windows, a Mac and any other platform that supports Python 3. The variable "port" has to be set to the serial port that you are using to connect to the Citrine. Here is a sample session, in which the moves for white are entered on the Citrine, and the moves for black are sent from the PC:

/dev/ttyUSB0 opened
When prompted, enter a Novag command, '' to read, or q to quit
Command?
New Game
Command? u on
.Referee on
Command? x on
Xmit on
Command? l tr8
Level tr 8
Command?
M   1   e2-e4
Command? me7e5
Command? me7e5
M   1,  e7-e5
Command?
M   2   g1-f3
Command? mb8c6
Command? mb8c6
M   2,  b8-c6
Command?
M   3   d2-d4
Command? me5d4
Command? me5d4
M   3,  e5xd4
Command?
M   4   d1xd4
Command?
T   4   d1xd4
Command?
M   4   f3xd4
Command? q


Everything appears to work. After entering a move for white on the Citrine, enter an empty line as a command to read the reply from the PC. As noted previously, it is necessary to send the moves for black twice from the PC. Typing 'q' ends the session. Responses from the Citrine are buffered, and can be read after more commands have been sent.

Monday 16 May 2016

Communicating with the Novag Citrine

I have done some experiments in communicating with the Novag Citrine via CuteCom. I found that the commands are not case sensitive, and that invalid commands are simply ignored. The Citrine sends "New Game" at the start of a new game. Setting up the pieces or sending the N command from the PC starts a new game. If the Citrine has been disconnected from the mains for long enough for it to lose its internal memory, it beeps and sends New Game when it is switched on. The Citrine forgets any user settings for the new game. The following enquiries successfully send information back to the PC:

C shows the Clocks.
G shows the Game.
P shows the Position.
I shows the ID of the Citrine.
L shows the Level (time limit).
U shows whether Referee Mode is on.
X shows whether Xmit is on or off.

In Referee Mode, the user plays both sides, and Citrine just checks for the legality of moves. When Xmit is on, moves made on the board are echoed back to the PC. Here is an example game score:

Game   6
.      white       black
.   1  e2-e4       e7-e5
.   2  f2-f4       f8-c5
.   3  f4xe5       d8-h4
.   4  e1-e2       h4-f2
.   5  e2-d3       f2-d4
.   6  d3-e2       d4xe4

The default Level is TR1 (TouRnament Level 1), i.e. 40 moves in 5 minutes. (When I exceed the time limit, the Citrine shows "Flag" but lets me play on.) I found that the undocumented L AT1 (Average Time Level 1) works, setting the Level to an average of 2 seconds per move, and the other Levels appear to work too.

Xmit is off by default, but can be turned on with X ON, so that moves made by the user are echoed back to the PC, e.g.:

M   7   g1-f3
M   7,  c5xd4
M   5   d7-d8/N
M   7   e5xd6ep
M   4   O-O
M   5,  O-O-O

The comma indicates a move for black. Checks are not shown. Move when a move is taken back, this is indicated by a T in place of the M, e.g.:

M   1,  e7-e5
T   1,  e7-e5

The following codes are used to indicate the end of a game:

M#1 Draw by repetition.
M#2 Draw by the 50 move rule.
M#3 Draw by insufficient material.
M#4 Stalemate.
M#5 Checkmate.
M#6 Citrine resigns.

(There is no way for the user to resign, but he can just start a new game.)

Putting the Citrine into Referee Mode with U ON works, but sending a move from the PC to the Citrine once does not work at all well. The LEDs flash so quickly as to be almost invisible, and when the piece has been moved, the LEDs on the destination square are illuminated until either the piece is lifted again or an information request is sent from the PC. The move is echoed back to the PC. Miraculously, sending the same move twice works as it should, except that the LCD shows an error, which can be ignored. The move is not echoed a second time. Taking a move back by sending T (once) works as it should. The Citrine's behaviour is not ideal, but it is worth noting that the more expensive DGT boards do not even have LEDs to indicate the moves on the board.

MG1F3 and MG1F3 both work. MO-O and MO-O-O both work. When taking back castling, it is necessary to move the king before the rook. ME7E8/N works for promotion to a knight, and I expect that the other under-promotions work too.

The J (Go) command causes the Citrine to make a single move, which is echoed back to the PC, as does, lifting the king of the side without the move. Neither action turns Referee Mode off. F (Flip) works at the start of a new game. it is also possible to set up a custom start position. If a custom start position has been set, O (Origin starting position) successfully reverts to that custom start position.

Some of the other functions listed in the Novag documentation do not work. The key functions do not work. There does not appear to be any way of setting Easy mode remotely. V (video mode) and R (Replay) are both ignored. It does not appear to be possible to set separate Levels for white and black. Autoclock does not work. Send Info does not work. I expect that the problem here is that the documentation covers several Novag machines, and not just the Citrine.

Novag Citrine - Quick Start Guide

The Citrine starts a new game whenever the pieces are set up in the initial position, and it is your move unless you change that.

Whenever it is your move, you can lift the Citrine's king off the board and replace it to make the Citrine change sides. When it is the Citrine's move, lifting your king off the board and replacing makes the Citrine move immediately. (The Go function).

To make a move, lift the piece to be moved, and put it down on its destination square. Your move will be shown on the LCD. Note the colour and piece indicators. Check is indicated by “+” on LCD box. The Citrine's reply is shown both by LEDs and the LCD box. You can verify a piece by lifting it. The LCD shows symbol, square and colour.

To capture a piece, lift the capturing piece, lift the captured piece, and put the capturing piece on its destination square. When a piece is captured, “x” shows on the LCD, along with “ep” for an en passant capture. When making an en passant capture, remove the captured pawn last.

When taking back moves, replace the captured piece after moving back the capturing piece. Exit take back mode by making a different move.

When the Citrine promotes, the LCD displays the promotion. When you promote, A1 to D1 flash. Raise and lower a piece on whichever of these squares held the promotion piece in the initial position.

Menu functions are accessed by pushing Menu button. The LCD displays functions in rotation, with the corresponding squares lighting up. To enter a function, lift the piece on the corresponding square and put it back, or place the function access piece on the square and lift it off. Some important functions are A8 = Level setting, C8 = Easy mode and E8 = Referee mode. Tap the piece on the square until desired setting is displayed on the LCD. Pushing the Menu button again saves and exits menu mode.

Level Menu A8. Average Time, B1...B8 (AT1...AT8). Beginner G1...G8 (BE1...BE8). Set the level by lifting and replacing the piece (or function access piece) on the corresponding square, and pressing the menu button to exit menu mode.

Easy Menu C8. Raising and lowering a piece on C8 cycles through the options (which are shown on the LCD). Easy indicates no thinking on the user's time. Easy ≡ indicates thinking on the user's time.

Referee Menu E8. Raising and lowering a piece on E8 cycles through the options. rEF indicates play against Citrine. rEF ≡ indicates play both sides. Referee mode can be exited by using the Go function (see above). Referee mode can be used to enter opening moves before starting a game.

Wednesday 11 May 2016

Connecting to the Novag Citrine using CuteCom

As I said in my previous post, Hyperterminal was discontinued by Microsoft from Vista onwards. I decided to try to connect to the Novag Citrine using CuteCom on Ubuntu Linux. I downloaded Cutecom from the Ubuntu Software Centre. (I did not tick "Tools for zmodem/xmodem/ymodem file transfer".) I booted Ubuntu with the Citrine connected to my PC, as described in my previous post, again with the Citrine switched off. I opened a terminal with Ctrl-T, and typed:

dmesg | grep tty

The list included "pl2303 converter now attached to ttyUSB0", which is the entry for my USB-to-Serial converter. I then typed:

sudo cutecom

This brought up the CuteCom window. I typed /dev/ttyUSB0 into the Device field, selected 57600 Baud, keeping 8 Data bits and 1 Stop bit. I left the Handshake boxes unticked. I found that I also needed to select "CR.LF line end" from the pull-down at the bottom of the window. I then clicked the "Open Device" button, after which I turned the Citrine on.

I typed P into the Input field, and hit the return key. The Citrine responded with the position.


In the above, I had to use sudo to gain superuser privileges. This was because my user account did not belong to the dialout group, which I verified by using the terminal command:

groups ${USER}

I fixed problem this with:

sudo gpasswd --add ${USER} dialout

I was then able to run CuteCom from the launcher, or by typing just cutecom in a terminal, without using sudo and giving my password. On returning to CuteCom, I found that the window was populated with my settings. PuTTY is a popular alternative to CuteCom that will run on Windows. It can be downloaded from here:

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Tuesday 10 May 2016

Connecting the Novag Citrine to Arena 3.5

I had previously failed to connect my Novag Citrine to Arena, and thought that my USB-to-Serial adapter was at fault. I recently decided to gather all the information that I could and have another try.


The Novag Cirine instruction manual can be found here:

http://www.chesshouse.com/v/vspfiles/pdf/novag-citrine-chess-computer-english.pdf

Here are instructions for interacting with the Citrine via Hyperterminal:

http://www.chesshouse.com/v/vspfiles/pdf/novag-chess-to-pc-connection.pdf

Unfortunately, Hyperterminal was discontinued by Microsoft from Vista onwards.  Even when the Citrine's internal engine is being used, interaction with a PC can be be very useful. The Citrine lacks the additional buttons that some dedicated chess computers have, and its user interaction is not the best. It is easy to get a situation where the position on the board differs from that in the Citrine's processor, and it can be difficult to sort the problem out. It is also useful to be able to get a score of the game.

I found that a precise procedure had to be followed to connect the Novag Citrine to Arena 3.5.

Make sure that the Citrine is turned off.

Connect the “LCD box” to the Citrine via the “LCD box connection cable”. Connect the USB-to-Serial adapter to the “LCD box” via the “PC connection cable”. Plug the USB-to-Serial adapter's USB plug into a USB socket on the PC.

Start Windows 10. Type “Devices” into the search box in the lower left hand corner of the screen. Select the “Devices desktop app”. My PC shows “Prolific USB-to-Serial Comm Port (COM3)”.

Start Arena. Select Extras > “Configure Novag-Chessboard”.

Select the Options tab. Select the COM3 radio button (or whatever COM port showed in Devices).

Select the Start tab. Click the “Start Novag Support” button.

Switch the Citrine on.

Select the Information tab. Click the “Get Information” button. My PC shows “ID UCB11 V1.09”.

Select the Communications tab. Type P into the text box. Click the “Send to Novag!” button. My PC responds with the start position.

Subsequently, to play against Arena, making moves on the Citrine board, start Windows 10, and start Arena. Click the new game icon. Select Extras > “Novag – Chessboard”. Turn the Citrine on. Make your move on the Citrine board. Arena should echo your move on the Arena board. It should then make its reply on that board, and flash that reply on the Citrine board.