Python has a very simple format for publishing and subscribing to data when it comes to MQTT. The first step was to install the needed libraries and the python version was to be updated to python 3 as the libraries only support this version. The library used was paho-mqtt. Since we had not worked with MQTT in python during the course, we watched an introduction to using this library, the link for this is; https://www.youtube.com/watch?v=c_DPKujOmGw. The publisher and subscriber is mostly similar with some differences. This will be outlined and detailed throughout this section. 

The setup of MQTT is not explained in detail here as it is similar to the one done in blender. For more information visit MQTT via Blender with Python Module

Multithreading between object detection and stockfish


This was achieved using the multiprocessing and queue library which basically queue's events. The code for multithreading was as follows;

 

This was done because the camera needed to be controlled from the stockfish file so they needed to be run together. This was the best way to get the two files running bug free. 

To add threading in the object detection, the main loop was coded inside a threaded function and this was then joined with the stockfish process using the main code shown above. The object detection code is quite long so a short snippet is shown below. For full code, check the report for the GITHUB link 

When this function is called in the stockfish file, the camera starts running. A snapshot function was added as well so that it can be called when the program requires the camera to take a snapshot however, this is not fully functioning yet, there is some problem with the multi-threading in object detection. We are trying to debug this for the presentation. The function is as follows:

This function is supposed to generate the movement string that is taken in by the stockfish engine to play against so currently this part is not working. This was called in the on message function as such;

Since we do not have a lot of experience with multi-threading, the debugging is taking longer than expected. However, other aspects of the MQTT connection itself is working well. 

Before implementing the publisher and subscriber in python for our project, we chose to create the basic logic of the stockfish. Let's run through the code


  1. The global variables are initialized and defined
  2.  The needed functions are defined. The first function is a function that finds the type of piece that is moved. This is done using the stockfish inbuilt function(get_what_is_on_square(move)) that names each piece. 

  • After this the main logic for the stockfish starts. The only difference between the normal logic code we created and adding in the mqtt was that the logic was all inserted inside of the on_message function so that it runs when the subscriber receives a message.
  • The topic that stockfish was subscribing to was PLCupdates which was sent in the json formatting, similar to the blender python this was parsed using json.loads and then the variables were accessed using  for example: data.get('playerMove')
  •  once the variable were obtained, the if statements were implemented. If the game state was 4 or 5, it meant that the main program is initialising the board so nothing from stockfish is required so the code was set such that it would print "Setting up Board". Otherwise if the player_move was empty, it meant player hadn't moved which means it would return "White not Started". Lastly if the player turn was false, then a snapshot from the camera is triggered. This screenshot will generate a movement string which means that player move != prev_player_move. This triggered stockfish engine to play against the move. If a piece was captured then the game state was 2 if not then game state is 1. This logic was added for the main code to run the capture sequence if something was captured. The code is shown below

  • Since the on message requires the camera to take a screenshot, we needed to find a way to have the camera and the stockfish running. To do this we integrated multithreading


Results; 


The figures above show that the stockfish connects to the MQTT connection and the camera is running so threading is working. It is waiting to receive the message and when it does, it prints out the message received. 

  • No labels