Matlab internal error before "mavlinkio" callback - how to catch?

13 次查看(过去 30 天)
I am using the UAV Toolbox for full-duplex communication multiple robots (indexed by {i} below) using a Matlab's mavlink support. Here is the setup code called from an appdesigner application:
% Open mavlink connections
app.dialect = mavlinkdialect(".\ares.xml");
% Connect to a socket for receiving mavlink messages
app.sendSock{i} = mavlinkio(app.dialect,"SystemID",254,"ComponentID",190); % 'send' in the sense of the com2udp bridge
connect(app.sendSock{i},"UDP",'LocalPort',app.NodeTable.Data.UDPport(i));
% Subscribe to whatever messages are sent from the nodes to the UDP port
app.subscriber{i} = mavlinksub(app.sendSock{i}, BufferSize=50, NewMessageFcn=@avsCallback);
% Set a different socket for sending mavlink commands
app.recvSock{i} = mavlinkio(app.dialect,"SystemID",254,"ComponentID",190); % 'recv' in the sense of the com2udp bridge
recvPort = app.NodeTable.Data.UDPport(i)+20;
connect(app.recvSock{i},"UDP",'LocalPort',recvPort);
The callback function executes normally, but occasionally I get mavlink receive errors reported as unknown message IDs:
The Message ID is garbage. It was not sent by the robot, nor is it in any dialect. The reported ID appears to be random, and the error occurs repeatedly throughout a session. I suppose it is a communication error, and I would like to disregard it. The problem is that in order to toss it, I must first catch it. Since this is an internal error not in the direct call stack of any user Matlab code, I have been unable to do so. The exception occurs before the "avsCallback" function is executed and appears to be outside the scope of the main application. How can I catch this error?
This report seems similar to my goal:
but unfortunately was not successful, particularly for a compiled application. Any help will be greatly appreciated.

采纳的回答

Jianxin Sun
Jianxin Sun 2025-2-20
编辑:Jianxin Sun 2025-2-20
Hi Jim,
Since this error was thrown from the callback itself, there is no easy way for you to catch it.
One possible cause to this error is that your ares.xml was including some other message definition xmls that are newer than the one shipped with MATLAB. You can open your xml file and look for the files it included and put all the necessary file in the same folder where you run your code. For example the ardupilotmega.xml includes several other xmls.
<mavlink>
<include>common.xml</include>
<!-- Vendors -->
<include>uAvionix.xml</include>
<include>icarous.xml</include>
<include>loweheiser.xml</include>
<include>cubepilot.xml</include>
<include>csAirLink.xml</include>
If this didn't fix your issue, an alternative method could be to use MATLAB udpport object to read binary buffer directly and use mavlinkdialect.deserializemsg method to convert binary data to MATLAB data structure. And you can use try-catch to handle unrecognized packets. An example code might look like:
% Setup a receiver on localport 14550
dialect = mavlinkdialect(".\ares.xml");
udpReceive = udpport("datagram", "LocalPort", 14550);
% readdata callback is called when new data comes in,
configureCallback(udpReceive, "datagram", 1, @(src, ~)readdata(src, dialect))
% Setup a sender
udpsend = udpport("datagram", "LocalPort", 14580);
io = mavlinkio("common.xml", 2, "SystemID", 255, "ComponentID", 1);
msg = io.Dialect.createmsg("HEARTBEAT");
data = io.serializemsg(msg);
% Send heartbeat to 14550, should observe heartbeat msg print out in MATLAB command line
write(udpsend, data, "127.0.0.1", 14550);
function readdata(src, dialect)
data = read(src,1,"uint8");
% Use mavlinkdialect deserializemsg to convert binary data to MATLAB struct
try
msg = dialect.deserializemsg(uint8(data.Data));
disp(msg.Payload);
catch ME
% Catch issue when the binary data cannot be correctly parsed
% Print out the binary data packet that cannot be parsed
disp(uint8(data.Data));
disp(ME.message)
end
end
Also given the way the error was thrown from your code, it seems that the message with random message ID was recognized by MAVLink as a valid packet, I would be curious to see what corrupted binary are received on the receiver side to cause this.
Jianxin
  1 个评论
Jim
Jim 2025-2-25
This answers my question that there isn't a standard way to catch these internal errors. I did check that all the necessary included .xml files are in the home folder.
I have not yet implemented the suggested workaround to read the binary data directly, but will try to find some time to do so. I too am curious how the packet seems to have been accepted, except for the spurious message ID. I'll update this post once I add that instrumentation.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by