You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

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. 

Multithreading between object detection and stockfish


This was achieved using the multiprocessing and queue library which basically queue's events  

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 recieves 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


  • No labels