Matlab to Mission Planner SITL (MavLink connection)

15 views (last 30 days)
Hi,
I am opening the SITL Simulation in Mission Planner (Simulation Tab -> Mulitrotor).
My aim is to setup a Bidirectional Mavlink2 communication between Matlab and Mission Planner simulation. I want to be able to send commands from Matlab to MP and se the behaviour of the dron.
I am able to receive forwarded messages from Mission Planer. I set up Ctrl + F, then open Connection to 14552 UDP Port.
However if I want to send something Back, Mission Planner dose not see it. In Mavlink Inspector I can see only
Vechicle 1
- Comp 1 MAV_COMP_ID_AUTOPILOT1
Mechicle 255
- Comp 190 MAV_COMP_ID_MISSIONPLANNER
The code below I use to try to send a HEARTBEAT messages to Mission Planner.
% === CONNECTION CONFIGURATION ===
localPort = 14552; % Local UDP port (match SITL config)
targetSys = 1;
targetComp = 1;
% === Initialize MAVLinkIO ===
dialect = mavlinkdialect("common.xml", 2);
mav = mavlinkio(dialect);
connect(mav, "UDP", "LocalPort", localPort);
% === Create drone target client ===
client = mavlinkclient(mav, targetSys, targetComp);
% === Register MATLAB as GCS client (sysid 255, compid 1) ===
gcs = mavlinkclient(mav, 255, 1);
% === Subscriptions for responses ===
ackSub = mavlinksub(mav, client, "COMMAND_ACK");
statustextSub = mavlinksub(mav, client, "STATUSTEXT");
% === Send HEARTBEATs periodically ===
disp("📡 Sending HEARTBEAT every 1 second...");
heartbeat = createmsg(dialect, "HEARTBEAT");
heartbeat.Payload.autopilot = 8; % MAV_AUTOPILOT_INVALID
heartbeat.Payload.base_mode = 0;
heartbeat.Payload.custom_mode = uint32(0);
heartbeat.Payload.system_status = 0;
heartbeat.Payload.type = 6; % MAV_TYPE_GCS (Ground Station)
duration = 30; % seconds
startTime = tic;
while toc(startTime) < duration
sendmsg(mav, heartbeat);
fprintf("❤️ Sent HEARTBEAT at t=%.1f sec\n", toc(startTime));
pause(1.0);
end
% === Check COMMAND_ACKs ===
ackMsgs = latestmsgs(ackSub, 1);
for i = 1:numel(ackMsgs)
fprintf("✅ COMMAND_ACK: cmd=%d, result=%d\n", ...
ackMsgs(i).Payload.Command, ackMsgs(i).Payload.Result);
end
% === Check STATUS_TEXTs ===
statusMsgs = latestmsgs(statustextSub, 1);
for i = 1:numel(statusMsgs)
txt = native2unicode(char(statusMsgs(i).Payload.Text'), 'UTF-8');
txt = erase(txt, char(0));
fprintf("📥 STATUS: [%d] %s\n", ...
statusMsgs(i).Payload.Severity, strtrim(txt));
end
% === Cleanup ===
disconnect(mav);
delete(mav);
disp("✅ Done");
I do not know how to solve it. Maybe I have to switch something in Mission Planner, but it is not clear to me what. Is it possible to set a connection I would like to have. All help will be appreciated.

Answers (1)

Jianxin Sun
Jianxin Sun on 31 Jul 2025
Hi, if you are already able to receive message at 14552, you can try the following script. I updated the heartbeat message to preserve message field data type. Previously the fields were overwritten to double type.
I also used parameter protocol to test sending parameter request.
% === CONNECTION CONFIGURATION ===
localPort = 14552; % Local UDP port (match SITL config)
targetSys = 1;
targetComp = 1;
% === Initialize MAVLinkIO ===
dialect = mavlinkdialect("common.xml", 2);
mav = mavlinkio(dialect);
connect(mav, "UDP", "LocalPort", localPort);
% === Create drone target client ===
client = mavlinkclient(mav, targetSys, targetComp);
% === Register MATLAB as GCS client (sysid 255, compid 1) ===
gcs = mavlinkclient(mav, 255, 1);
% === Send HEARTBEATs periodically ===
% It is important to preserve the struct field type using (:)= assignment
disp("📡 Sending HEARTBEAT every 1 second...");
heartbeat = createmsg(dialect, "HEARTBEAT");
heartbeat.Payload.autopilot(:) = 8; % MAV_AUTOPILOT_INVALID
heartbeat.Payload.base_mode(:) = 0;
heartbeat.Payload.custom_mode(:) = uint32(0);
heartbeat.Payload.system_status(:) = 0;
heartbeat.Payload.type(:) = 6; % MAV_TYPE_GCS (Ground Station)
duration = 30; % seconds
startTime = tic;
while toc(startTime) < duration
sendmsg(mav, heartbeat);
fprintf("Sent HEARTBEAT at t=%.1f sec\n", toc(startTime));
pause(1.0);
end
% === Use parameter request to get parameter values ===
sub = mavlinksub(mav, client, "PARAM_VALUE", NewMessageFcn=@(~,msg)disp(msg.Payload), BufferSize=30);
msg = createmsg(dialect,"PARAM_REQUEST_LIST");
msg.Payload.target_system(:) = targetSys;
msg.Payload.target_component(:) = targetComp;
sendmsg(mav,msg,client)
pause(1)
% === Cleanup ===
disconnect(mav);
delete(mav);
disp("✅ Done");

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!