Editor: Controlling Simulations with MQTT
Description
Via the use of MQTT, this guide will walk Nominal Users through the steps required to create a Simulation that has its tick controlled via a remote source. The MQTT Tick Control
Nominal Editor demo is referenced in this guide and is a provided example of remote control ticking with MQTT. This guide also assumes a basic knowledge of the Message Queuing Telemetry Transport (MQTT) protocol.
Configuring the Simulation
During the Event Begin Play
of the simulation level, an MQTT Client needs to be created and connected. For the MQTT Tick Control
demo, a public broker is used to highlight the remote aspect. However, local hosts or private brokers can be used instead.
Note
The Connect
function of the MQTT client will need to be updated for the selected external broker if used. The following broker is a free and public broker that can be used for testing purposes and is used in many of the provided Nominal MQTT demo levels.
Once the Client is connected, it must Subscribe
to the Tick Topic. As the Demo is using a public broker, users are advised to add a Topic Prefix
to prevent others from controlling the tick of your simulations.
Ticking the Simulation
The simulation level will only start processing Tick commands once the simulation has been initialized. When a new Tick command has been received, the MQTT Tick Control
Demo extracts the command type for the JSON-formatted MQTT message payload.
An example of the MQTT message payload is shown below, where the command is to start ticking the simulation continuously with a step size of 0.1 and 10 iterations/steps per Unreal frame.
data = {
"command": "start",
"continuous": True,
"step": 0.1,
"iterations": 10
}
Note
In this example, an external Python script is used to send the JSON data over MQTT to be processed by the level. This is not covered in the guide.
Based on the received command, the MQTT Tick Control
demo then extracts the rest of the information in the MQTT JSON Payload and actions the command. The referenced demo is capable of starting, stopping, ticking and resetting the simulation.
Example Result
A Python script is also provided with the MQTT Tick Control
demo that is preconfigured to cycle through several commands. The MQTT Tick Control
demo itself is a simulation of a spacecraft in orbit around Jupiter in a sun-pointing mode.