Initial Version
This commit is contained in:
40
MATLAB/+XPlaneConnect/clearUDPBuffer.m
Normal file
40
MATLAB/+XPlaneConnect/clearUDPBuffer.m
Normal file
@@ -0,0 +1,40 @@
|
||||
function socket = clearUDPBuffer(socket,varargin)
|
||||
% clearUDPBuffer Script that clears an UDP Socket Buffer by closing and
|
||||
% reopening the socket
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% Socket: UDP Socket to be cleared
|
||||
%
|
||||
% Outputs
|
||||
% Socket: UDP Socket
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. Socket = openUDP(49005);
|
||||
% 3. Socket = clearUDPBuffer(Socket);
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] Added Versioning
|
||||
% 09/12/13: [CT] Add optional arguments
|
||||
% 09/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Close and reopen socket
|
||||
if ~socket.isClosed()
|
||||
port = socket.getLocalPort(); %get port of socket
|
||||
socket.close; %Close Socket
|
||||
socket = openUDP(port,varargin); %open new socket
|
||||
end
|
||||
end
|
||||
32
MATLAB/+XPlaneConnect/closeUDP.m
Normal file
32
MATLAB/+XPlaneConnect/closeUDP.m
Normal file
@@ -0,0 +1,32 @@
|
||||
function [socket] = closeUDP(socket)
|
||||
% closeUDP Script that closes a UDP Socket
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% Socket: UDP Socket to be closed
|
||||
%
|
||||
% Outputs
|
||||
% Socket: Closed Socket
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. Socket = openUDP(49005);
|
||||
% 3. Status = closeUDP(Socket);
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: Added Versioning
|
||||
% 09/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
socket.close;
|
||||
|
||||
end
|
||||
46
MATLAB/+XPlaneConnect/openUDP.m
Normal file
46
MATLAB/+XPlaneConnect/openUDP.m
Normal file
@@ -0,0 +1,46 @@
|
||||
function [socket] = openUDP(port, varargin)
|
||||
%openUDP Script that opens an UDP Socket
|
||||
% Version 0.25
|
||||
%
|
||||
%Inputs
|
||||
% port: UDP Port for socket
|
||||
% timeout (optional): Optional parameter for time to UDP timeout (in ms)
|
||||
%Outputs
|
||||
% Socket: UDP Socket
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. Socket = openUDP(49005); %Open socket at port 49005 with timeout=0.1 sec
|
||||
% or
|
||||
% 2. Socket = openUDP(49005); %Open socket at port 49005 with timeout=0.2 sec
|
||||
%
|
||||
%Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] Added Versioning
|
||||
% 09/12/13: [CT] Added optional timeout input argument
|
||||
% 09/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import java.net.DatagramSocket
|
||||
|
||||
%% create socket
|
||||
socket = DatagramSocket(port);
|
||||
socket.setSoTimeout(100);
|
||||
socket.setReceiveBufferSize(2000);
|
||||
|
||||
%% interpret input
|
||||
if ~isempty(varargin)
|
||||
if isnumeric(varargin(1))
|
||||
socket.setSoTimeout(varargin(1));
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
50
MATLAB/+XPlaneConnect/pauseSim.m
Normal file
50
MATLAB/+XPlaneConnect/pauseSim.m
Normal file
@@ -0,0 +1,50 @@
|
||||
function status = pauseSim( pause, varargin )
|
||||
%pauseSim pause Simulation
|
||||
% Version 0.25
|
||||
%
|
||||
%Inputs
|
||||
% Pause: binary value 0=run, 1=pause
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin
|
||||
%
|
||||
%Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
%Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. status = pauseSim(1);
|
||||
%
|
||||
%Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/19/14: [CT] V0.2: First Created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
%BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Handle Input
|
||||
% Optional parameters
|
||||
p = inputParser;
|
||||
addRequired(p,'pause',@isnumeric);
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,pause,varargin{:});
|
||||
|
||||
% Check format of input-TODO
|
||||
|
||||
%% BODY
|
||||
message = zeros(1,7);
|
||||
|
||||
message(1:4) = 'SIMU'-0;
|
||||
message(6) = uint8(p.Results.pause);
|
||||
message(7) = 0;
|
||||
|
||||
status = sendUDP(message, p.Results.IP, p.Results.port);
|
||||
end
|
||||
98
MATLAB/+XPlaneConnect/readDATA.m
Normal file
98
MATLAB/+XPlaneConnect/readDATA.m
Normal file
@@ -0,0 +1,98 @@
|
||||
function [ sensor ] = readDATA( socket )
|
||||
% readDATA Reads UDP Socket and interprets data
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% location: Either an opened UDP Socket or integer port number
|
||||
%
|
||||
% Outputs
|
||||
% If data is X-Plane data format:
|
||||
% data: Matlab X-Plane DATA Structure
|
||||
% .h: Header array containing header numbers corresponding to values in the X-Plane UDP Data screen.
|
||||
% .d: 2-D Data array. Each row contains eight values corresponding to the header value (.h(1) corresponds to .d(1,:))
|
||||
% .raw: raw UDP data array received by readUDP
|
||||
%
|
||||
% If data is matlab structure:
|
||||
% data: Matlab Structure. raw UDP data saved to data.raw
|
||||
%
|
||||
% If data is any other format:
|
||||
% data: Matlab Structure containing one field
|
||||
% .raw & .d: raw UDP data array received by readUDP
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. socket = openUDP(49005);
|
||||
% 3. data = readDATA(socket);
|
||||
% 4. status = closeUDP(socket);
|
||||
% or
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. data = readDATA(49005);
|
||||
%
|
||||
% NOTE: sending in a port number instead of an opened socket clears the UDP buffer. This only works if the data is being sent at a fast rate. Sending in a UDP Socket reads the first packet in the buffer.
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: Updated to work with new Plugin
|
||||
% 09/10/13: [CT] Updated to receive UDP socket or port number
|
||||
% 06/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Read UDP Socket
|
||||
[ sensor.raw] = readUDP(socket);
|
||||
|
||||
%% Interpret Input
|
||||
bits = size(sensor.raw);
|
||||
if sensor.raw ~= -998 %If the signal exists
|
||||
header = char(sensor.raw(1:4)');
|
||||
if strcmp(header,'DATA') %DATA signal type
|
||||
Values = floor((bits-5)/36);
|
||||
sensor.d = [];
|
||||
sensor.h = zeros(Values(1),1);
|
||||
for i=1:Values(1)
|
||||
sensor.h(i) = sensor.raw(6+(i-1)*36);
|
||||
sensor.d = [sensor.d; typecast(uint8(sensor.raw(10+(i-1)*36:5+i*36))','single')];
|
||||
end
|
||||
elseif strcmp(header,'STRU') %STRU signal type
|
||||
a = 6;
|
||||
while a<length(sensor.raw)
|
||||
strdim = sensor.raw(a);
|
||||
if strdim == 0
|
||||
break
|
||||
end
|
||||
fieldName = char(sensor.raw(a+1:a+strdim)');
|
||||
a = a+strdim+1;
|
||||
dim1 = sensor.raw(a);
|
||||
dim2 = sensor.raw(a+1);
|
||||
if dim1 == 0 %String
|
||||
value = char(sensor.raw(a+2:a+1+dim2));
|
||||
a = a + dim2 + 2;
|
||||
else
|
||||
value = [];
|
||||
for i=1:dim1
|
||||
value(i,:) = typecast(uint8(sensor.raw(a+2+(i-1)*dim2*4:a+1+i*dim2*4))','single');
|
||||
end
|
||||
a = a + dim1*dim2*4+2;
|
||||
end
|
||||
sensor.(fieldName) = value;
|
||||
end
|
||||
elseif strcmp(header,'OTHR')
|
||||
sensor.d = sensor.raw(6:end);
|
||||
else %Other signal type
|
||||
sensor.d = sensor.raw;
|
||||
end
|
||||
else %No Signal
|
||||
sensor.d = -998;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
79
MATLAB/+XPlaneConnect/readUDP.m
Normal file
79
MATLAB/+XPlaneConnect/readUDP.m
Normal file
@@ -0,0 +1,79 @@
|
||||
function [ data ] = readUDP( input )
|
||||
%readUDP Read Array from UDP Socket
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% input: Either an opened UDP Socket or integer port number
|
||||
%
|
||||
% Outputs
|
||||
% data: UDP uint8 Array. Equal to -998 in the case of an error
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. socket = openUDP(49005);
|
||||
% 3. data = readUDP(socket);
|
||||
% 4. status = closeUDP(socket);
|
||||
%
|
||||
% or
|
||||
%
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. data = readUDP(49005);
|
||||
%
|
||||
% NOTE: sending in a port number instead of an opened socket clears the
|
||||
%UDP buffer. This only works if the data is being sent at a fast rate.
|
||||
%Sending in a UDP Socket reads the first packet in the buffer.
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: Added Versioning
|
||||
% 09/08/13: [CT] Added option for either UDP Socket or port number input
|
||||
% 06/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
import java.net.DatagramPacket
|
||||
bits = 2000;
|
||||
|
||||
%% Interpret Input
|
||||
socket = input;
|
||||
if isnumeric(input)
|
||||
socket=openUDP(input);
|
||||
end
|
||||
|
||||
%% Try reading packet
|
||||
try
|
||||
packet = DatagramPacket(zeros(1,bits,'int8'),bits);
|
||||
socket.receive(packet)
|
||||
data = packet.getData;
|
||||
|
||||
data = int16(data);
|
||||
data(data(:)<0) = uint8(data(data(:)<0) + 256); %fix signed issue
|
||||
size = int16(data(5)); %size of data stream
|
||||
|
||||
%% trim trailing data
|
||||
for i=1:floor(length(data)/256)+1
|
||||
if data(size+1:end)==0
|
||||
break
|
||||
end
|
||||
size = size + 256;
|
||||
end
|
||||
data = data(1:size);
|
||||
|
||||
catch err %Read Unsuccessful
|
||||
data = -998;
|
||||
end
|
||||
|
||||
%% Close Port (if opened in code)
|
||||
if isnumeric(input)
|
||||
socket.close()
|
||||
end
|
||||
end
|
||||
|
||||
59
MATLAB/+XPlaneConnect/requestDREF.m
Normal file
59
MATLAB/+XPlaneConnect/requestDREF.m
Normal file
@@ -0,0 +1,59 @@
|
||||
function status = requestDREF( DREFArray, varargin )
|
||||
%requestDREF request the value of a specific DataRef from X-Plane over UDP
|
||||
% Version 0.25
|
||||
%
|
||||
%Inputs
|
||||
% DREFArray: Cell Array of DataRefs to be requested
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin
|
||||
%
|
||||
%Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
%Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. DREFArray = {'sim/cockpit2/controls/yoke_heading_ratio','sim/cockpit2/controls/yoke_roll_ratio'};
|
||||
% 3. status = requestDREF( DREFArray, '172.0.100.54' );
|
||||
%
|
||||
%Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/19/14: [CT] V0.2: First Created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Complete- Receive response
|
||||
%
|
||||
%BEGIN CODE
|
||||
|
||||
disp('This Function is not functional yet-use the C version. Sorry for any inconvenience')
|
||||
import XPlaneConnect.*
|
||||
|
||||
message = zeros(1,6);
|
||||
len = 7;
|
||||
|
||||
%% Handle Input
|
||||
p = inputParser;
|
||||
addRequired(p,'DREFArray');
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,DREFArray,varargin{:});
|
||||
|
||||
%% BODY
|
||||
% Header
|
||||
message(1:4) = 'GETD'-0;
|
||||
message(6) = length(p.Results.DREFArray);
|
||||
|
||||
% DREFS
|
||||
for i=1:length(p.Results.DREFArray)
|
||||
message(len) = length(p.Results.DREFArray{i});
|
||||
message(len+1:len+message(len)) = p.Results.DREFArray{i};
|
||||
len = len+1+message(len);
|
||||
end
|
||||
|
||||
% Send UDP
|
||||
status = sendUDP(message, p.Results.IP, p.Results.port);
|
||||
|
||||
end
|
||||
49
MATLAB/+XPlaneConnect/selectDATA.m
Normal file
49
MATLAB/+XPlaneConnect/selectDATA.m
Normal file
@@ -0,0 +1,49 @@
|
||||
function [ status ] = selectDATA( index, varargin )
|
||||
% selectDATA Choose specific X-Plane parameters to be send over UDP
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% index: An array of the values that to be sent. Corresponds to the numbers on the XPlane Output screen
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin and 49005 to send to the x-plane udp
|
||||
%
|
||||
% Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. values = [1, 2, 3, 27, 40];
|
||||
% 3. status = selectDATA( values, '127.0.0.1', 49005 ); % send to localhose port 49005
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: Added Versioning
|
||||
% 06/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Handle Input
|
||||
p = inputParser;
|
||||
addRequired(p,'index');
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,index,varargin{:});
|
||||
|
||||
%% BODY
|
||||
dataString = ['DSEL'-0,0];
|
||||
for i=1:length(index)
|
||||
dataString = [dataString, p.Results.index(i), 0, 0, 0];
|
||||
end
|
||||
status = sendUDP(dataStream, p.Results.IP, p.Results.port);
|
||||
|
||||
end
|
||||
|
||||
66
MATLAB/+XPlaneConnect/sendCTRL.m
Normal file
66
MATLAB/+XPlaneConnect/sendCTRL.m
Normal file
@@ -0,0 +1,66 @@
|
||||
function [ status ] = sendCTRL( ctrl, acft, IP, port )
|
||||
% sendCTRL Send X-Plane Aircraft Control Commands over UDP
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% ctrl: control array where the elements are as follows:
|
||||
% 1. Latitudinal Stick [-1,1]
|
||||
% 2. Longitudinal Stick [-1,1]
|
||||
% 3. Pedal [-1, 1]
|
||||
% 4. Throttle [-1, 1]
|
||||
% 5. Gear (0=up, 1=down)
|
||||
% 6. Flaps [0, 1]
|
||||
% acft (optional): Aircraft # (default is 0; 0 = own aircraft)
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin and 49005 to send to the x-plane udp
|
||||
%
|
||||
% Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. #Send the data array to port 49009 on the computer at IP address 172.0.100.54.
|
||||
% 3. status = sendCTRL([0, 0, 0, 0.8, 0, 1], 1, '172.0.100.54',49009);
|
||||
%
|
||||
% Note: send the value -998 to not overwrite that parameter. That is, if -998 is sent, the parameter will stay at the current X-Plane value
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 09/26/14: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Handle Input
|
||||
% Optional parameters
|
||||
if ~exist('IP','var'), IP = "127.0.0.1"; end
|
||||
if ~exist('port','var'), port = 49009; end
|
||||
if ~exist('acft','var'), acft = 0; end
|
||||
|
||||
% Check format of input-TODO
|
||||
|
||||
%%BODY
|
||||
header = ['CTRL'-0,0];
|
||||
dataStream = [header, acft];
|
||||
|
||||
% Deal with position update
|
||||
control = [0, 0, 0, 0.8, 0, 1];
|
||||
|
||||
for i=1:min(length(ctrl),length(control))
|
||||
control(i) = ctrl(i);
|
||||
end
|
||||
dataStream = [dataStream, typecast(single(control),'uint8')];
|
||||
|
||||
% Send DATA
|
||||
status = sendUDP(IP, port, dataStream);
|
||||
|
||||
end
|
||||
|
||||
63
MATLAB/+XPlaneConnect/sendDATA.m
Normal file
63
MATLAB/+XPlaneConnect/sendDATA.m
Normal file
@@ -0,0 +1,63 @@
|
||||
function [ status ] = sendDATA(data, varargin)
|
||||
% sendDATA Send X-Plane formatted DATA over UDP
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% data: X-Plane formatted data. Is a matlab structure with the following fields:
|
||||
% .h: Header array containing header numbers corresponding to values in the X-Plane UDP Data screen.
|
||||
% .d: 2-D Data array. Each row contains eight values corresponding to the header value (.h(1) corresponds to .d(1,:))
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent . Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin and 49005 to send to the x-plane udp interface.
|
||||
%
|
||||
% Outputs
|
||||
% status: 0: no error, <0: error
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. data = struct('h',27,'d',[1,-998,-998,-998,-998,-998,-998,-998]);
|
||||
% 3. %Send the data array to port 49005 on the computer at IP address 172.0.100.54.
|
||||
% 4. status = sendDATA(data, '172.0.100.54', 49005);
|
||||
%
|
||||
% Note: send the value -998 to not overwrite that parameter. That is, if -998 is sent, the parameter will stay at the current X-Plane value
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: Updated to work with new Plugin
|
||||
% 06/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Handle Input
|
||||
p = inputParser;
|
||||
addRequired(p,'data');
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,data,varargin{:});
|
||||
|
||||
%% BODY
|
||||
|
||||
header = ['DATA'-0,0];
|
||||
dataStream = header;
|
||||
|
||||
for i=1:length(p.Results.data.h)
|
||||
dataStream = [dataStream, p.Results.data.h(i), 0, 0, 0, typecast(single(p.Results.data.d(i,1:8)),'uint8')]; %#ok<AGROW>
|
||||
end
|
||||
|
||||
% Send DATA
|
||||
if length(dataStream) > 5
|
||||
status = sendUDP(dataStream, p.Results.IP, p.Results.port);
|
||||
else
|
||||
disp('Warning in sendDATA: Sending empty dataStream')
|
||||
status = -2;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
63
MATLAB/+XPlaneConnect/sendDREF.m
Normal file
63
MATLAB/+XPlaneConnect/sendDREF.m
Normal file
@@ -0,0 +1,63 @@
|
||||
function status = sendDREF( dataRef, Value, varargin )
|
||||
% sendDREF Send a command to change any DataRef in X-Plane over UDP. This requires the X-Plane Connect Plugin to be running
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% dataRef: The X-Plane Dataref that will be chaged
|
||||
% Value: The value that the above dataref is set to
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin and 49005 to send to the x-plane udp
|
||||
%
|
||||
% Outputs
|
||||
% status: If there was an error. status<0 means there was an error.
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*
|
||||
% 2. dataRef = 'sim/aircraft/parts/acf_gear_deploy'; // Landing Gear
|
||||
% 3. Value = 0;
|
||||
% 4. status = sendDREF(dataRef, Value);
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: First Version
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
status = 0;
|
||||
|
||||
%% Handle Input
|
||||
p = inputParser;
|
||||
addRequired(p,'dataRef');
|
||||
addRequired(p,'Value');
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,dataRef, Value ,varargin{:});
|
||||
|
||||
%% BODY
|
||||
dataStream = zeros(1,7+length(p.Results.dataRef)+length(p.Results.Value)*4);
|
||||
% Build Header
|
||||
dataStream(1:4) = 'DREF'-0;
|
||||
|
||||
% Add DREF
|
||||
dataStream(6) = uint8(length(p.Results.dataRef));
|
||||
len = 6+length(p.Results.dataRef);
|
||||
dataStream(7:len)=p.Results.dataRef-0;
|
||||
|
||||
% Add Value
|
||||
dataStream(len+1) =uint8(length(p.Results.Value));
|
||||
dataStream(len+2:end)=typecast(single(p.Results.Value),'uint8');
|
||||
|
||||
% Send DREF
|
||||
if length(dataStream) > 5
|
||||
status = sendUDP(dataStream, p.Results.IP, p.Results.port);
|
||||
end
|
||||
end
|
||||
66
MATLAB/+XPlaneConnect/sendPOSI.m
Normal file
66
MATLAB/+XPlaneConnect/sendPOSI.m
Normal file
@@ -0,0 +1,66 @@
|
||||
function [ status ] = sendPOSI( posi, varargin )
|
||||
% sendPOSI Send X-Plane Aircraft Position over UDP
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% posi: Position array where the elements are as follows:
|
||||
% 1. Latitiude (deg)
|
||||
% 2. Longitude (deg)
|
||||
% 3. Altitude (m above MSL)
|
||||
% 4. Roll (deg)
|
||||
% 5. Pitch (deg)
|
||||
% 6. True Heading (deg)
|
||||
% 7. Gear (0=up, 1=down)
|
||||
% acft (optional): Aircraft # (default is 0; 0 = own aircraft)
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin and 49005 to send to the x-plane udp
|
||||
%
|
||||
% Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. #Send the data array to port 49009 on the computer at IP address 172.0.100.54.
|
||||
% 3. status = sendPOSI([37.5242422, -122.06899, 2500, 0, 0, 0, 1], 1, '172.0.100.54');
|
||||
%
|
||||
% Note: send the value -998 to not overwrite that parameter. That is, if -998 is sent, the parameter will stay at the current X-Plane value
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 09/25/14: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
%
|
||||
% BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Handle Input
|
||||
p = inputParser;
|
||||
addRequired(p,'posi');
|
||||
addOptional(p,'acft',0,@isnumeric);
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,posi,varargin{:});
|
||||
|
||||
%% BODY
|
||||
header = ['POSI'-0,0];
|
||||
dataStream = [header, p.Results.acft];
|
||||
|
||||
% Deal with position update
|
||||
position = [37.4185718,-121.935565,500,0,0,0, 0];
|
||||
|
||||
for i=1:min(length(position),length(p.Results.posi))
|
||||
position(i) = p.Results.posi(i);
|
||||
end
|
||||
dataStream = [dataStream, typecast(single(position),'uint8')];
|
||||
|
||||
% Send POSI
|
||||
status = sendUDP(dataStream, p.Results.IP, p.Results.port);
|
||||
|
||||
end
|
||||
|
||||
63
MATLAB/+XPlaneConnect/sendSTRU.m
Normal file
63
MATLAB/+XPlaneConnect/sendSTRU.m
Normal file
@@ -0,0 +1,63 @@
|
||||
function [ status ] = sendSTRU( STRU, varargin )
|
||||
%sendSTRU Send a MATLAB structure over UDP
|
||||
% Version 0.25
|
||||
%
|
||||
%Inputs
|
||||
% stru: A MATLAB structure
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin
|
||||
%
|
||||
%Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
%Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. data = struct('a',[1:20],'b',1.853,'name','Example Structure');
|
||||
% 3. #Send the data structure to port 49005 on the computer at IP address 172.0.100.54
|
||||
% 4. status = sendUDP( data, '172.0.100.54', 49005 );
|
||||
%
|
||||
%Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/18/14: [CT] V0.2: Added Versioning
|
||||
% 08/01/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
%
|
||||
%BEGIN CODE
|
||||
|
||||
import XPlaneConnect.*
|
||||
|
||||
%% Handle Input
|
||||
p = inputParser;
|
||||
addRequired(p,'STRU');
|
||||
addOptional(p,'IP','127.0.0.1',@ischar);
|
||||
addOptional(p,'port',49009,@isnumeric);
|
||||
parse(p,STRU,varargin{:});
|
||||
|
||||
%% Form Data Array representing the structure
|
||||
DATA = ['STRU'-0,0]; %array header
|
||||
fieldName = fieldnames(p.Results.STRU); %all Struct fields
|
||||
for i=1:length(fieldName) %for each field
|
||||
field = getfield(p.Results.STRU,fieldName{i}); %get field
|
||||
if ischar(field) %String
|
||||
dim1 = 0; %Indicates string
|
||||
dim2 = length(field); %length
|
||||
data = field-0; %data
|
||||
else %Numeric
|
||||
dim1 = size(field,1); %Array Dim1
|
||||
dim2 = size(field,2); %Array Dim2
|
||||
data = typecast(single(reshape(field',1,dim1*dim2)),'uint8'); %Data
|
||||
end
|
||||
|
||||
DATA = [DATA,length(fieldName{i}),fieldName{i}-0,dim1,dim2,data]; %add to array
|
||||
end
|
||||
|
||||
%% Send Array
|
||||
status = sendUDP(DATA, p.Results.IP, p.Results.port);
|
||||
|
||||
end
|
||||
|
||||
64
MATLAB/+XPlaneConnect/sendUDP.m
Normal file
64
MATLAB/+XPlaneConnect/sendUDP.m
Normal file
@@ -0,0 +1,64 @@
|
||||
function [ status ] = sendUDP( data, IP, port )
|
||||
%sendUDP Send an one dimensional array of type uint8 data over an UDP connection using a java DatagramSocket
|
||||
% Version 0.25
|
||||
%
|
||||
%Inputs
|
||||
% Data: 1-D array of type uint8 data to be sent
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin
|
||||
%
|
||||
%Outputs
|
||||
% status: If there was an error. Status<0 means there was an error.
|
||||
%
|
||||
%Use
|
||||
% 1. import XPlaneConnect.*;
|
||||
% 2. data = uint8([1:20]);
|
||||
% 3. #Send the data array to port 49005 on the computer at IP address 172.0.100.54.
|
||||
% 4. status = sendUDP( data, '172.0.100.54', 49005 );
|
||||
%
|
||||
%Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 09/25/14: [CT] V0.24: Added persistant socket
|
||||
% 04/21/14: [CT] V0.2:Added Versioning, support for DREF/SIMU/GETD/CONN
|
||||
% 06/10/13: [CT] Code created
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
%BEGIN CODE
|
||||
|
||||
import java.net.DatagramSocket
|
||||
import java.net.DatagramPacket
|
||||
import java.net.InetAddress
|
||||
|
||||
data(5) = length(data);
|
||||
status = 0;
|
||||
|
||||
%% Send array
|
||||
persistent socket
|
||||
if isempty(socket)
|
||||
try
|
||||
socket = DatagramSocket;
|
||||
catch err
|
||||
status = 1;
|
||||
disp(err)
|
||||
end
|
||||
end
|
||||
|
||||
try
|
||||
IP = InetAddress.getByName(IP);
|
||||
packet = DatagramPacket(data, length(data), IP, port); %create packet
|
||||
socket.send(packet);
|
||||
catch err
|
||||
status = 1;
|
||||
disp(err)
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
52
MATLAB/+XPlaneConnect/setConn.m
Normal file
52
MATLAB/+XPlaneConnect/setConn.m
Normal file
@@ -0,0 +1,52 @@
|
||||
function status = setConn( recvPort, IP, port )
|
||||
% setConn Send a command to set up the port where you will receive data on
|
||||
% this computer.
|
||||
% Version 0.25
|
||||
%
|
||||
% Inputs
|
||||
% Receiving Port: Port that data will be sent to in the future for this connection
|
||||
% IP Address (optional): IP Address of the machine that will receive the data as a character array. Default is '127.0.0.1' (local machine)
|
||||
% port (optional): Port on the receiving machine where the data will be sent. Default is 49009 (XPlaneConnect). In general use 49009 to send to the plugin and 49005 to send to the x-plane udp
|
||||
%
|
||||
% Outputs
|
||||
% status: If there was an error. status<0 means there was an error.
|
||||
%
|
||||
% Use
|
||||
% 1. import XPlaneConnect.*
|
||||
% 2. status = setConn(49011);
|
||||
%
|
||||
% Change Log
|
||||
% 10/02/14: [CT] V0.25: Updated to work with updated xpcPlugin
|
||||
% 04/21/14: [CT] V0.2: First Version
|
||||
%
|
||||
% Contributors
|
||||
% [CT] Christopher Teubert (SGT, Inc.)
|
||||
% christopher.a.teubert@nasa.gov
|
||||
%
|
||||
% To Do
|
||||
% 1. Verify Input
|
||||
%
|
||||
% BEGIN CODE
|
||||
import XPlaneConnect.*
|
||||
|
||||
status = 0;
|
||||
message = zeros(1,9);
|
||||
|
||||
%% Handle Input
|
||||
% Optional parameters
|
||||
if ~exist('IP','var'), IP = "127.0.0.1"; end
|
||||
if ~exist('port','var'), port = 49009; end
|
||||
|
||||
% Check format of input-TODO
|
||||
|
||||
%% BODY
|
||||
% Header
|
||||
message(1:4) = 'CONN'-0;
|
||||
|
||||
% RecvPort
|
||||
message(6:9) = typecast(uint32(recvPort),'uint8');
|
||||
|
||||
% Send
|
||||
sendUDP(IP,port,message);
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user