Document toolboxDocument toolbox

Integration of Matlab/Simulink with Prespective

Tutorial Goal

The goal of this tutorial is to learn how to set up a live-link between MATLAB/Simulink and Prespective using the MQTT networking protocol.

Step 1: Import the MQTT in MATLAB Plugin

Link to the plugin → MQTT in MATLAB

There are two options to the plugin. A user can download either the zip folder or the Toolbox and integrate it in their MATLAB/Simulink project.

Note: The zip file should be unzipped in the directory that contains the MATLAB/Simulink project.

Step 2: Create Publish/Subscribe Simulink Blocks

Open an empty Simulink Project and create two MATLAB blocks in the empty Simulink project along with inputs as shown below.

The blocks shown below will be used to publish on and subscribe to topics to send and receive values respectively on a centralized broker. A user can make several blocks to publish on and subscribe to several topics on one centralized broker.

Step 3: Set up the Publisher Block

The MQTTWrapperPublish block is used to extrinsically call a MQTTWrapperPublish function that is saved as a separate MATLAB file. This is done so because the toolbox is built specifically for MATLAB and hence it’s functions and operations need to be compiled from within the MATLAB compiler and not Simulink.

The MATLAB file is used to initialize a client connect to an external broker and publish values based on the input given from the Simulink block. In case the input is not a string it should be first converted into a string.

The MQTTWrapperPublish block is given below:

The MQTTWrapperPublish MATLAB file is given below:

Step 4: Set up the Subscriber Block

Similarly the MQTTWrapperSubscribe block is used to extrinsically call a MQTTWrapperSubscribe function that is saved as a separate MATLAB file.

The MATLAB file is used to initialize a client connect to an external broker and subscribe to specific topics based on the input given from the Simulink block. The value will then be received inside MATLAB.

The MQTTWrapperPublish block is given below:

The MQTTWrapperPublish MATLAB file is given below. The callback function is used to return the message obtained from the subscription. Subsequently we write the message obtained from the callback function onto a constant block inside Simulink so that it can be displayed in Simulink.

Note: The name of the block in Simulink should be the same as that in the set_param function (In this case ‘Output’)

Step 5: Connect to an external broker

For out tutorial we are going to use a Mosquitto broker. The inputs for the publish and subscribe blocks inside Simulink can be set as given below. The output block is set by using the set_param function in the callback function of subscribe as described in the previous step.

Step 6: Setup the PreLogic Component inside Prespective

For our tutorial we are going to use the 3D printer example. The PreLogic component inside Prespective is setup for the Y axis motor component as shown below

The PreLogic component setup is given below. The IP address is set to 127.0.0.1(localhost) with a port no of 1883.

The naming convention for the variables generated after exporting the policy to XML is shown below:

Step 7: Final Testing

The MATLAB/Simulink model is fitted with a switch block. The preferred velocity of the driven stepper motor are published and the position of the Y motor are subscribed. When the position of the Y motor is less than '2200 deg' the Y motor stops in it’s position.

The topics to publish to and subscribe from can be obtained by opening the policy xml file that is exported after we set up the PreLogic component.

 

Hence the system is set up and the components of the 3D printer can be controlled using MATLAB/Simulink.

Prespective Documentation