Clear Filters
Clear Filters

Send MAVLink command with MATLAB

12 views (last 30 days)
Paolo on 6 Oct 2022
Commented: vahe on 11 Jan 2023
Hello, I'm searching if there is a way for interact and send commands to my quadcopter with MATLAB, instead of a Ground Control Station. I tried with MATMAV but seems not working. I also tried with MAVLink Support and also this seems not working for send command to my quadcopter. This is my code:
dialect = mavlinkdialect('common.xml');
mavlink = mavlinkio(dialect);
connect(mavlink, 'UDP', 'LocalPort', 14550);
client = mavlinkclient(mavlink, 1, 1) % specify client (1 client = 1 drone)
connectionList = listConnections(mavlink)
clientsList = listClients(mavlink)
topicsList = listTopics(mavlink)
message_num = 400;
cmdMsg = createcmd(dialect, 'int', message_num)
num2enum(dialect,"MAV_CMD", message_num)
sendudpmsg(mavlink, cmdMsg, '', 14550)
There are other approaches to send commands to quadcopter through MATLAB?
Thanks in advance!

Accepted Answer

Jianxin Sun
Jianxin Sun on 7 Oct 2022
Hi Paolo,
First the mavlink message needs to be refined:
For arm UAV:
cmd = dialect.createcmd("LONG", "MAV_CMD_COMPONENT_ARM_DISARM");
cmd.Payload.target_component(:) = 1;
cmd.Payload.target_system(:) = 1;
cmd.Payload.param1(:) = 1;
For disarm UAV
cmd = dialect.createcmd("LONG", "MAV_CMD_COMPONENT_ARM_DISARM");
cmd.Payload.target_component(:) = 1;
cmd.Payload.target_system(:) = 1;
cmd.Payload.param1(:) = 0;
Also since you are emulating a ground control station in MATLAB, you will need to keep sending out heartbeat from MATLAB to help the UAV find it:
heartbeat = createmsg(dialect,"HEARTBEAT");
heartbeat.Payload.type(:) = enum2num(obj.Dialect,'MAV_TYPE', 'MAV_TYPE_GCS');
heartbeat.Payload.autopilot(:) = enum2num(obj.Dialect,'MAV_AUTOPILOT', 'MAV_AUTOPILOT_INVALID');
heartbeat.Payload.system_status(:) = enum2num(obj.Dialect,'MAV_STATE',"MAV_STATE_STANDBY");
heartbeatTimer = timer;
heartbeatTimer.ExecutionMode = 'fixedRate';
heartbeatTimer.TimerFcn = @(~,~)sendmsg(obj.IO,heartbeat);
If UAV is also broadcasting its heartbeat, your listClient call should show UAV client as well, then you can send message to the uav client:
sendmsg(mavlink, client);
Paolo on 10 Oct 2022
Hi @Jianxin Sun, thanks for the reply. I figured out how the general mechanism. Thanks for the help!
vahe on 11 Jan 2023
Hi @Jianxin Sun, can you help me?
I have written the following codes to monitor gimbal variables for UAV through Mavlink codes in MATLAB:
dialect = mavlinkdialect('common.xml');
sender = mavlinkio(dialect,'SystemID',1,'ComponentID',1,...
destinationPort = 14551;
destinationHost = '';
receiver = mavlinkio(dialect);
info = msginfo(dialect,"GIMBAL_DEVICE_ATTITUDE_STATUS");
msg = createmsg(dialect,info.MessageName);
subscriber = mavlinksub(receiver,'GIMBAL_DEVICE_ATTITUDE_STATUS',...
for msgIdx = 1:10
--------- in the command window ------
time_boot_ms: 0
q: [0 0 0 0]
angular_velocity_x: 0
angular_velocity_y: 0
angular_velocity_z: 0
failure_flags: 0
flags: 0
target_system: 0
target_component: 0
--------- But how can I show only 'q' in the command window?
Because I want to convert it to Euler angles = quat2eul(q)

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!