MATLAB‐ROS2 Integration - JOCIIIII/PX4-PILS-Runner GitHub Wiki

Overview

  • MATLAB supports direct communication with ROS2 network starting from R2019b.
  • MATLAB and Simulink is a very useful tool since it can be used for data visualization and intuitive control systems design.
  • By adding PX4 msg. definitions to MATLAB, it can control PX4 flight stack by offboard control mode.

MATLAB Setup

This document explains the setup procedure for Ubuntu Linux variant.

Prequisites

  • MATLAB must have ROS toolbox installed.
  • MATLAB PC must be located in the same network as the ROS2 container running uXRCE DDS agent.
    • For the most case, running MATLAB on system that runs SITL is recommended.
    • Running MATLAB on Windows while running SITL on WSL2 is feasible, but needs testing.

Importing PX4-Autopilot Message Definition to MATLAB

  • First, create structure of ROS2 workspace.
mkdir -p ros_ws/src
  • Then, clone PX4/px4_msgs repository to the source directory.
  • Be aware that the branch of px4_msgs must be compatible to the PX4-Autopilot you use.
    • Especially for 1.13.x $\to$ 1.14.0, this is critical since DDS implementation has changed.
    • Check the migration guide for more details about this.
  • In this case, we use release/1.14 branch since we use v1.14.0 of PX4-Autopilot.
git clone https://github.com/PX4/px4_msgs.git \
  -b release/1.14 \
  ros_ws/src
  • Run MATLAB and run following command:
ros2genmsg("/<THE>/<ABSOLUTE>/<PATH>/<TO>/ros_ws/src")
  • MATLAB will build the message definition and import it.

Subscribe/Publish ROS2 topics on Simulink

  • Run Simulink and create ROS2 Subscribe block.
  • Double click and select Configure ROS2 Domain ID and ROS Middleware (RMW)
  • Set appropriate ROS2 domain ID. The default value is 0.
  • Then, run the SITL with ROS2 container that have uXRCE DDS Agent running.

  • If everything went right, you can select topic from the ROS2 network.
  • Select a topic ans set desired sample time for it.
  • Then, you can select a msg. you desire by bus selector block.
  • Refer to the PX4 msgs definition and set appropriate variable types on Simulink blocks.
⚠️ **GitHub.com Fallback** ⚠️