MATLAB‐ROS2 Integration - JOCIIIII/PX4-PILS-Runner GitHub Wiki
- 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.
This document explains the setup procedure for Ubuntu Linux variant.
- 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.
- First, create structure of ROS2 workspace.
mkdir -p ros_ws/src- Then, clone
PX4/px4_msgsrepository to the source directory. - Be aware that the branch of
px4_msgsmust 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.
- Especially for
- In this case, we use
release/1.14branch since we usev1.14.0of 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.
- Run Simulink and create
ROS2 Subscribeblock. - 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 selectorblock. - Refer to the PX4 msgs definition and set appropriate variable types on Simulink blocks.