23  Self study - Session 2

In this session, we will revisit Chapter 21 and try to optimize the placement accuracy of the pick-and-place machine.

Before we are touching the live machine, we will first try to find a suitable model based on the provided dataset.

Note

Since we are working with a live feedback system, we will need multiple terminals/consoles at the same time:

  1. One terminal to run the simulated pick and place machine (the OPC UA server).
  2. Optionally, one terminal to run the OPC UA client that connects to the machine and provides real-time.
  3. Optionally, one terminal to run the opcua-client GUI to visualize the live data.

Exercise 23.1 (Train a model to predict placement error)  

We are told that the placement can be adjusted until the end of step 2 by providing a correction vector that modifies the target placement location. This means that we need to predict the placement error (Error x [mm] and Error y [mm]) and provide the correction vector based on the available variables until the end of step 2.

Furthermore, engineers tell us that the rows where Process step is 1 should contain the necessary information to predict the placement error.

Your task:

  1. Split the provided dataset into a training and test set (depending on the chosen model, you will also need a validation set).

  2. Train a model that predicts Error x [mm] and Error y [mm].

  3. Take a look at the model’s performance on the test set by looking at certain metrics, e.g., by calculating the Mean Absolute Error (MAE).

  4. Visualize the predictive capabilities by e.g. plotting a histogram showing the errors and residuals (predicted - true values).

  5. Persist the model to disk so that we can later load it in the live feedback system. In case you use a scaler, you will need to persist it as well.

Once you have a model that performs well on the test set, we can work on integrating it in the live feedback system.

But first, let us understand how to interact with the simulator. The accompanying readme.md file shipped with the industrial_datascience_live_machine repository contains all necessary information.

Exercise 23.2 (First encounter with the live machine simulator)  

Running the opc-ua-server.py or the provided docker-container will start the simulated pick and place machine that exposes the sensor data via OPC UA. The machine runs in real-time, meaning that one cycle takes 3-6 seconds to complete. You will see the process steps advancing in the console output. Simultaneously, the sensor data is streamed via the OPC UA protocol.

Your task:

  1. Start the machine and copy/paste the console output for the first 5 cycles.

  2. Use opcua-client or any other OPC UA client of your choice to connect to the machine:

    • Usually it should be enough to run uv add opcua-client, followed by uv run opcua-client (Windows might need an extra step to install the GUI dependencies; use Python 3.13 to avoid issues in the following steps).
    • The server URL should be opc.tcp://localhost:4840, which is prefilled in the client. Simply click on Connect (the machine must be actively running in another terminal before connecting).
    • Once connected, you should see a tree structure on the left side. Navigate to 0:Root,0:Objects,2:plcSimulator.
    • Provide a screenshot of the variable tree below 2:plcSimulator.
  3. Look at the attributes of a variable:

    • Click through the variables and take a close look at the Attributes tab on the right side.
    • The server is constantly streaming new values for each variable, but you need to hit the Refresh button to see the updated value.
    • Provide a screenshot of the attributes of position_x.
  4. Subscribe to data changes:

    • Right-click on one of the variables (e.g. position_x) and select Subscribe to data changes.
    • Subscribed variables will appear in the Subscriptions tab at the bottom.
    • Observe that the values are changing in real-time as the machine is running.
    • Also observe that they are not updated at the very same time, but rather whenever there is a new measurement available.
    • Provide a screenshot of the Subscriptions tab showing the data changes of position_x, position_y, and cycle.
  5. Look at the graph of subscribed variables:

    • This requires two more packages to be installed. If you are using uv, run uv add numpy pyqtgraph to add the necessary dependencies.
    • Switch to the Graphs tab at the bottom.
    • Change update interval to 1s (unfortunately it appears that it is not possible to go below 1 second in this client).
    • Contrary to subscribing to data changes, this graph will poll the server for new values at the specified interval (meaning that we might miss some updates if the frequency is higher).
    • Add the variables you want to visualize by right-clicking on them and selecting Add to Graph.
    • Provide a screenshot of the graph showing error_x and error_y over time.
  6. Interact with the machine by writing values:

    • While looking at the graph, click on correction_x in the variable tree on the left side.
    • In the Attributes tab, double click on the Value field to edit it.
    • Provide a correction value (e.g. 10.0) and hit ENTER to write the new value to the server.
    • Observe how the machine reacts to the provided correction in the graph (note that the server always resets the correction values to 0.0 after applying them).
    • Provide a screenshot of the graph showing the effect of your correction on error_x.
    • Have a look at the console output of the running machine simulator to see how the correction was applied.
    • Provide a screenshot of the console output showing the application of your correction.

Now that we know how to interact with the live machine simulator, in a first step we will set up a Python script opc-ua-client.py that connects to the machine and subscribes to the data changes of the necessary variables.

Exercise 23.3 (Set up an OPC UA client in Python)  

Have a look at the provided opc-ua-client.py script, understand how it works and run it while the machine simulator is active.

Your task:

  1. For understanding, it is likely best to start by studying the main() function at the bottom of the script and then look at the SubHandler class that handles the data changes.

  2. Explain in your own words what the script is doing (a brief summary is sufficient).

  3. Run the script while the machine simulator is active and observe the printed output.

  4. Provide a screenshot of the console output showing the data changes received from the machine.

Great! It is now time to integrate our trained model into the live feedback system and hope that the chosen model generalizes well to the live data and is fast enough to provide corrections in real-time.

Exercise 23.4 (Integrate the trained model into the live feedback system)  

The opc-ua-client.py script contains various placeholders (marked with # >>> Exercise) where you need to integrate your trained model to provide real-time corrections to the pick and place machine.

Your task:

Load model:

  1. Part 1: Load your model (and optionally scaler) from the first exercise.
  2. Part 2: Select and order the features that your model needs to make predictions (they must be in the very same order as during training).

Use the model to compute correction values:

  1. Part 3 - Optional: Scale your data, if you used a scaler during training.
  2. Part 4: Predict the placement error using your model.
  3. Part 5: Based on your prediction, compute the necessary correction values.

Integrate the model into the live feedback loop:

  1. Part 6: Print the collected variables (this is for debugging purposes in case you need it).
  2. Part 7: Use the previously implemented function predict_error to get correction values for x and y.
  3. Part 8: Write the computed correction values back to the OPC UA server to adjust the placement in real-time.
  4. Part 9: Now remove the pass statement to enable your implementation.

Test your implementation:

  1. Run the modified opc-ua-client.py script while the machine simulator is active.
  2. In case it is working fine: Well done. Otherwise, debug your implementation until it is working as expected.
  3. Part 10: Remove datachange notification to reduce console output clutter.

Proof of work:

  1. Provide a screenshot of the client’s console output showing the computed correction values being sent to the server.
  2. Provide a screenshot of the machine simulator’s console output showing the effect of your corrections on the placement error.

This is it! Almost.

Visualizations are almost always nicer to look at than raw console output.

Exercise 23.5 (Visualize the live placement error and corrections)  

Your task:

  1. Ensure that the machine simulator is running.

  2. Ensure that your live feedback client is not running.

  3. Start the GUI client opcua-client once more and connect it to your machine.

  4. Add error_x, error_y to the graph and set the update interval to 1s.

  5. Wait for a couple of cycles to see how the placement error evolves over time.

  6. Now, start your live feedback client.

  7. Observe how the placement error changes as your model provides corrections in real-time.

  8. Provide a screenshot of the graph showing the live placement error and the effect of your corrections: It should show both, the error before the feedback loop is started as well as the reduced error once the feedback loop is active.