Middleware communication via Python API

I’d like to connect a middleware to the simulation. Further, the data transported by the middleware should update my robots joint values. Therefore, I tested the communication with MQTT middleware:

  • Publishing data from the OnSimulationUpdate() hook works quite fine and I can receive the message outside VC
  • Receiving data in VC is the problem at the moment. Either I don’t receive the message or VC is crashing in that moment I’m sending a message from a console outside to VC

However, I ask myself if the handling of threads in stackless python cause the problem. Do you have any suggestion how and in which hook (OnRun, OnSimUpdate, OnSignal …) I should set up such an event based communication.

Hi,

This thread might be a little too old but I’ll show how I got visual components to work with a MQTT broker (mosquitto) to exchange information with a webbrowser over websockets. The mosquitto broker is run as a desktop application in my case (available from https://mosquitto.org/download/).

Architecture

The webbrowser client is implemented as below:

<html>
<head>
    <link href="css/button-style.css" rel="stylesheet">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
    </script>
    <script type = "text/javascript" language = "javascript">

        var host="127.0.0.1";
        var port=8001;

        var client = new Paho.MQTT.Client(host,port,"webbrowser");

        client.onMessageArrived = onMessageArrived;

        var options = {
            timeout: 3,
            onSuccess: onConnect,
        };

        client.connect(options)

        function onMessageArrived(message) {
            console.log("onMessageArrived:"+message.payloadString);
        }

        function onConnect() {
            // Once a connection has been made, make a subscription.
            console.log("Connected ");

         //Subscribing to Topics
            client.subscribe("/factory/MC/status");
        }

        function myFunction(){

                message = new Paho.MQTT.Message(event.target.id );
                message.destinationName = "/factory/robot/command";
                client.send(message);
        }
    </script>

</head>
<body>

Pick From Conveyor A
Place in Machining Center
Place on Conveyor B
Pick from Machining Center

</body>

</html>

The CSS file for the HTML is avoided for the sake of simplicity

The Robot Client in VC is implemented as below:

from vcScript import *
import paho.mqtt.client as mqtt

def executeFunction(fnName):
  global robotExecutor
  comp = getComponent()
  robotExecutor = comp.findBehavioursByType('rRobotExecutor')[0]
  robotProgram = robotExecutor.Program
  routine = robotProgram.findRoutine(fnName)

  if routine is not None:
    robotExecutor.callRoutine(routine)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("/factory/robot/command")

def on_message(client, userdata, msg):
  print "Some message received", msg.payload.decode()
  comp = getComponent()
  executeFunction(msg.payload.decode())
  

def OnStart():
  global client
  
  client = mqtt.Client(client_id = "VC_robot", clean_session = False)
  client.connect("127.0.0.1",1883,60)
  
  client.on_connect = on_connect
  client.on_message = on_message

def OnRun():
  global client
  
  while True:
    client.loop()
    delay(1)

 

1 Like

I’m very interested in this feature.Can you provide the demo source file