Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Setting up the python script with paho-mqtt


  • Paho-mqtt is the library we used to implement mqtt messaging in python. Since blender uses the python module, installing this library had to be done a little differently to if it were VSCode. The  steps were as follows;
    • Locate the blender python dictionary (python.exe) this can be done in blender by running a file that has the following code;
      • import sys
      • print(sys.executable)
    • Open command prompt and go into the python dictionary using the path and add -m ensurepip (this ensures that pip is installed)
    • To install the paho-mqtt use -m pip install paho-mqtt. 
    • To double check it works, we tried importing it into the blender python script. 

Python script for initializing and implementing MQTT

The format that MQTT follows is as such:

The comments in the above code specify what each line represents and how its set up. Throughout the section, we will highlight how this format was adjusted for our project. 

Important notes


  1. The client_ID initialisation was done differently before so a lot of sources have the old version which will not work with python 3 so this is the new way of doing it. It was found here; https://eclipse.dev/paho/files/paho.mqtt.python/html/migrations.html
  2. The only things changed during adapting this standard code to our project was our on_message logic, the published string and the topics. 
  3. For our project, the publisher was irrelevant as blender was not publishing anywhere, just recieving new updates. 


Python script for Visualization


  •  The visualisation requires two things RobotMove and PlayerMove in order to mirror the movement done physically, so from the same codesys publisher, the blender subscriber takes out the RobotMove and playerMove. 
    • The topic this script is subscribing to is "PLCupdates" 
    • The board positions are initialized with objects, each square on the board was separated so that it was its own object(For more info on the object setup refer to 24_6 Blender Simulation), these objects were renamed such that they correspond to a board position "A1-H8". Each  piece is set to its corresponding position using the following code; 
  •  
    • Since we publish in JSON format with UTF-8, the json library can be used to parse the published message so that it's possible to use it in the python logic. The code below shows this implementation;

  • Once the data is parsed the data values can be accessed using data2.get('variable_name'). This was done for both robotMove and playerMove. The published message comes in the format for example: "e2e4" and "g1f3" so the code will separate the first two characters for each and a startPos and endPos will be set so the simulation knows where to move to and where to move from. This was all done in the on_message function;

  • If you notice, there is a movement function that is being called. This function is responsible for carrying out the movement in the simulation. In blender, it is possible to extract X, Y, Z positions of the objects, these were used to simulate similar movement to the robot. It also takes captures into account so if there is a piece on the end_square then it will be taken off the board and placed at (x=0). The function is shown below;

  • Since the positions are initialized every time the code is run, the client.loop_start was no longer an option as we could not press run every single time, to make up for this we integrated multi-threading that allows the program to run forever(client.loop_forever) without crashing. The integration was quite simple and is shown below. Remember to import the library (import threading)


Results

The blender code shows that it is connected to mqtt and waiting for a message and the second screenshot shows that it receives the message and prints it out. The visualization movement results can be found in 24_6 Blender Simulation

Click here to see blender code