Professional Documents
Culture Documents
Data Connection Between MATLAB and XPlane
Data Connection Between MATLAB and XPlane
The UDP occurs over a network (i.e. 2 computers connected through Ethernet cable), but the
following can also be applied to a single computer. It is recommended though to use 2 separate
computers, to divide the calculation load.
I will explain the bare minimum in simple words to set the connection up, assuming you are an
average computer user. A basic understanding of MATLAB, Simulink and X-Plane is required. This is
written for an airplane in X-Plane, but it is extendable for any aircraft (helicopter, multicopter, …).
If you want to dig deeper, I greatly recommend Jeff’s website. (See resources)
http://www.nuclearprojects.com/xplane/info.shtml
Whatever the source is, make sure the inputs scale between -1 and 1 (roll, pitch, yaw), or 0 and 1
(thrust). To do this you may need to add a calibration block, like I did.
Next thing you need to do is prepare the input to be sent to X-Plane. This consists of the following
steps:
3) Construct a datagram that X-Plane understands. There are several ways to do this, if you
know something about C++ programming and building S-blocks, you can do that, but I chose
for a simple yet effective method; writing a MATLAB function. This is possible with the
“MATLAB Function” block. The only thing that needs to be done here is make a string of
numbers using the uint8 data from earlier. I suggest reading Nuclear Project’s website to
understand the construction of the datagram.
end
The marked numbers refer to the “data number”, I call it the channel, in X-Plane. You can
find them in X-Plane in the Data Set tab, found under the Data Input & Output menu option.
In my case, I only sent channel #11, which are the flight controls, and #25, the throttle signal.
If you want to control for example a quad rotor (which has 4 throttles), the code would look
like this:
end
Cyriel Notteboom Menno Hochstenbach
Each channel consists of maximum 8 entries. For example channel 11 has 3 entries, aileron,
elevator and rudder. Be sure to check these channel numbers, as they change with different
versions of X-Plane.
Channel 11
Channel 18,
entry 2
4) Finally send the datagram to X-Plane through UDP, using the DSP System toolbox block “UDP
Send”. This block needs to know where it needs to send the datagram, so you need to fill in
the right address, IP-address and port number. X-Plane receives on port 49000 on default.
You can check this in X-Plane in Settings -> Net Connections, on the last tab, UDP Port.
Cyriel Notteboom Menno Hochstenbach
If you are running both X-Plane and Simulink on the same computer, you use the localhost IP-
adress, ‘127.0.0.1’. If not, you can find the “X-Plane computer’s” IP address with the ipconfig
command in command window. Look for the IPv4 address in the Ethernet section.
Alternatively, you can see the IP-address in X-Plane under Settings -> Net Connections ->Data.
Now is the time to test, run both the Simulink simulation and X-Plane and you should see a
message like the one on the screen-shot. Yay, it’s working! Now for the receiving part…
It is also very important to note which data X-Plane is sending, as shown in Settings -> Data input &
output. In the same window you can change the rate at which X-Plane throws out data. I just put it to
maximum, 99.9 UDP/sec. Lower values may be more efficient, I encourage the motivated reader to
experiment.
%--------------------------------------------------------------------------
% flight_con = [elev ailrn ruddr]; % flight controls
% attitude = [pitch roll hding_true hding_mag]; % pitch, roll, yaw
% location = [lat lon alt_msl alt_agl]; % latitude,
longitude, altitude [deg],[ftmsl],[ftagl] (mean sea level, actual ground
level)
%--------------------------------------------------------------------------
function [elev, ailrn, ruddr, pitch, roll, hding_true, hding_mag, lat, lon,
alt_msl, alt_agl] = UDP_decode(UDP)
% We don't need the DATA header, which are the first 5 bytes
UDP = UDP(6:end);
%-------------------------
% At last we extract each entry from their channel and output them.
elev = Channel_to_single(CH11,1);
ailrn = Channel_to_single(CH11,2);
ruddr = Channel_to_single(CH11,3);
pitch = Channel_to_single(CH17,1);
roll = Channel_to_single(CH17,2);
hding_true = Channel_to_single(CH17,3);
hding_mag = Channel_to_single(CH17,4);
lat = Channel_to_single(CH20,1);
lon = Channel_to_single(CH20,2);
alt_msl = Channel_to_single(CH20,3);
alt_agl = Channel_to_single(CH20,4);
%---------------------------------
end
start_byte = 1 + 4*data_pos;
stop_byte = start_byte + 3;
single = CH(start_byte:stop_byte);
end
4) Now is another good time to test. For exmple the elevator output from X-Plane. We see a
sampled output, but we only need the envelope of it. So I wrote a block that remembers the
last nonzero sample until a new one is received. And there you have it!
Raw samples
5) The only thing that’s left to do, is to group the entries again in channels for clarity. Now you
can start adding your own blocks. Good luck!
Cyriel Notteboom Menno Hochstenbach
Resources
Ernst, D. (2007). Development of research platform for unmanned vehicle controller design ,
evaluation , and implementation system : From MATLAB to hardware based embedded
system.
Ernst, D., Valavanis, K., Garcia, R., & Craighead, J. (2007). Unmanned Vehicle Controller
Design, Evaluation and Implementation: From MATLAB to Printed Circuit Board.
Journal of Intelligent and Robotic Systems, 49(1), 85–108. doi:10.1007/s10846-007-
9130-4
Garcia, R., & Barnes, L. (2009). Multi-UAV Simulator Utilizing X-Plane. Journal of
Intelligent and Robotic Systems, 57(1-4), 393–406. doi:10.1007/s10846-009-9372-4
Ribeiro, L. R., & Oliveira, N. M. F. (2010). UAV autopilot controllers test platform using
Matlab/Simulink and X-Plane. 2010 IEEE Frontiers in Education Conference (FIE),
S2H–1–S2H–6. doi:10.1109/FIE.2010.5673378
Ronaldo, S., Maria, N., & Oliveira, F. De. (2011). EXPERIMENTAL FRAMEWORK FOR
EVALUATION OF GUIDANCE.
http://www.jefflewis.net/XPlaneUDP_9.html
http://www.nuclearprojects.com/xplane/
http://en.wikipedia.org/wiki/User_Datagram_Protocol