function [ReceivedMatrix] = AVDSNetwork(Mode,VehicleID,Data2Network)
%AVDSNetwork - sends/receives vehicle packets from/to MATLAB to AVDS using multicast network functions
% 
%  SYNTAX:
%  1. [] = AVDSNetwork('InitializeVehicle',VehicleID,Data2Network) 
%       NOTE: 
%         - VehicleID is an identification number for the vehicle. If packets 
%           are being sent out for more than one vehicle then a unique VehicleID is required
%           for each vehicle.
%         - Data2Network is an optional vector of initial conditions: 
%             [CraftType,InitLongitudeDegDecimal,InitLatitudeDegDecimal,InitAltitudeFeet]
%           Valid entries include:
%             []
%             [CraftType]
%             [CraftType,InitLongitudeDegDecimal]
%             [CraftType,InitLongitudeDegDecimal,InitLatitudeDegDecimal]
%             [CraftType,InitLongitudeDegDecimal,InitLatitudeDegDecimal,InitAltitudeFeet]
%         - Latitude is in degrees decimal and must be in the range [-90.0,90.0]
%         - Longitude is in degrees decimal and must be in the range [-180.0,180.0]
%
%  2. [] = AVDSNetwork('SendData',VehicleID,Data2Network)
%       NOTE: 
%         - VehicleID is the identification number used to associate the data specified in 
%           Data2Network with the vehicle.
%         - Data2Network is a vector containing the vehicle data to send out:
%         [PositionEastFeet,PositionNorthFeet,AltitudeFeet,PhiDeg,ThetaDeg,PsiDeg,
%          DeltaElevatorRad,DeltaAileronRad,DeltaRudderRad,DeltaEngine0_255,
%          uFeetPerSec,vFeetPerSec,wFeetPerSec,pDegPerSec,qDegPerSec,rDegPerSec]. 
%          The elements of this vector are optional, but the use of an element
%          is determined by its location in the vector. Valid entries include:
%            []
%            [PositionEastFeet] 
%            [PositionEastFeet,PositionNorthFeet,AltitudeFeet]
%            [PositionEastFeet,PositionNorthFeet,AltitudeFeet,PhiDeg]
%                                 .
%                                 .
%                                 .
%            [PositionEastFeet,PositionNorthFeet,AltitudeFeet,PhiDeg,ThetaDeg,PsiDeg,
%             DeltaElevatorRad,DeltaAileronRad,DeltaRudderRad,DeltaEngine0_255,
%             uFeetPerSec,vFeetPerSec,wFeetPerSec,pDegPerSec,qDegPerSec,rDegPerSec] 
%          
%  3. [ReceivedMatrix] = AVDSNetwork('ReceiveData')
%    NOTE:
%      - ReceivedMatrix is a matrix that contains all of the vehicle packets received 
%        since the last call to 'ReceiveData'. Received packets are appended as new rows
%        to ReceivedMatrix. All vehicle packets available on the local network are appended
%        to ReceivedMatrix, therefore one must extract the CraftID from the packets to
%        differintiate between the packets from various vehicles. The structure of a vehicle
%        packet (row) vector is:
%        [CraftID,PositionEastFeet,PositionNorthFeet,AltitudeFeet,PhiDeg,ThetaDeg,PsiDeg,
%          DeltaElevatorRad,DeltaAileronRad,DeltaRudderRad,DeltaEngine0_255,
%          uFeetPerSec,vFeetPerSec,wFeetPerSec,pDegPerSec,qDegPerSec,rDegPerSec]
%
%  4. [] = AVDSNetwork('CloseConnections')
%
%  USAGE:
%  To send packets from MATLAB to AVDS:
%    - initialize the vehicle using the syntax of #1, above.
%    - send the vehicle packets using #2.
%    - when the simulation is finished, close the connection using #3.
%    SEND EXAMPLE:
%      VehicleID = 123;
%      Data2Network = [1,-122.063400,37.981523,5000.0];
%      AVDSNetwork('InitializeVehicle',VehicleID,Data2Network)
%      Data2Network = [0,0,5000.0,0.0,0.0,45.0,
%                      0.0873,0.0,0.0,53,
%                      300.0,0.0,0.0,0.0,0.0,0.0];
%      AVDSNetwork('SendData',VehicleID,[1,-122.063400,37.981523,5000.0])
%        .
%        .
%        .
%      Data2Network = [345533,-234747,12367.0,5.8,13.4,135.6,
%                      0.163,0.028,0.00023,203,
%                      523.0,50.0,23.0,18.5,-23.6,5.34];
%      AVDSNetwork('SendData',VehicleID,Data2Network)
%      AVDSNetwork('CloseConnections')
%
%  To receive vehicle packets in MATLAB:
%    - receive the vehicle packets using #3 above.
%    - when the simulation is finished, close the connection using #3.
%    RECEIVE EXAMPLE:
%      [ReceivedMatrix] = AVDSNetwork('ReceiveData')
%      (process ReceivedMatrix vehicle packets)
%        .
%        .
%        .
%
%      [ReceivedMatrix] = AVDSNetwork('ReceiveData')
%      (process ReceivedMatrix vehicle packets)
%      AVDSNetwork('CloseConnections')
%


%   Steve Rasmussen, December 2000
%   Copyright (c) 2000 by RasSimTech Ltd
%   $Revision: 1.0 $  $Date: 2000/12/28 00:00:00 $

ReceivedMatrix = [];

iInitializeVehicle = 1;
iSendData = 2;
iReceiveData = 3;
iCloseConnection = 4;
iOpenConnection = 5;

if nargin <1,
   Mode = 'Error';
end

switch Mode
case 'InitializeVehicle'
	if (nargin < 3), 
   	NetworkAVDS(iInitializeVehicle,VehicleID);
	else,	%if (nargin < 3)
   	NetworkAVDS(iInitializeVehicle,VehicleID,Data2Network);
	end	%if (nargin < 3)
case 'SendData'
	if (nargin < 3), 
   	NetworkAVDS(iSendData,VehicleID);
	else,	%if (nargin < 3)
   	NetworkAVDS(iSendData,VehicleID,Data2Network);
	end	%if (nargin < 3)
case 'ReceiveData'
   ReceivedMatrix = NetworkAVDS(iReceiveData);
case 'OpenConnection'
   NetworkAVDS(iOpenConnection);
case 'CloseConnections'
   NetworkAVDS(iCloseConnection);
otherwise
   ErrorString = sprintf('** AVDSNetwork:: function called with incorrect syntax or incorrect Mode.\n** Valid Modes are: ''InitializeVehicle'', ''SendData'', ''ReceiveData'' and ''CloseConnection''\n** Please type "help AVDSNetwork" for more information.\n\n');
   error(ErrorString);
end; %switch Mode