function [VectorToMATLAB] = SimulationConnection(Mode,ConnectionID,VectorToAVDS)
%SimulationConnection - passes data between MATLAB and AVDS through shared memory.
% 
%  SYNTAX:
%  1. [] = SimulationConnection('OpenConnection',ConnectionID,'AC') or 
%     [] = SimulationConnection('OpenConnection',ConnectionID,'FCS')
%  2. [] = SimulationConnection('SendData',ConnectionID,VectorToAVDS)
%  3. [VectorFromAVDS] = SimulationConnection('ReceiveData',ConnectionID)
%  4. [] = SimulationConnection('CloseConnections')
%
%  To communicate between MATLAB and AVDS first open the comunications connection 
%  with a call to SimulationConnection using the syntax of #1 above. Next use #2 and #3
%  to acomplish the communications. Finally use #4 to close all of the connections.
%
%  NOTE: The sizes and ID numbers of the vectors must be the same as those in the 
%  AVDS shared simulation model. The definitions for this model are in the sample file
%  ".\SharedMemoryLibrary\AVDS2MATLAB.h", for this sample they are:
%
%  For Type 'FCS':
%  ID of ToAVDS (FCS) -> 1
%  size of VectorToAVDS (FCS) -> 14
%    (EleCmd, AilCmd, RudCmd, ThrottleCmd, ToAVDS01,ToAVDS02,ToAVDS03,ToAVDS04,ToAVDS05,
%    ToAVDS06,ToAVDS07,ToAVDS08,ToAVDS09,ToAVDS10)
%  ID of ToMATLAB (FCS) -> 2
%  size of VectorToMATLAB (FCS) -> 19
%    (Alive, StickPitch(rad), StickRoll(rad), StickYaw(rad), StickThottle(rad), P(rad/sec), 
%     Q(rad/sec), R(rad/sec),ElapsedTime(sec),ToMATLAB01,ToMATLAB02,ToMATLAB03,ToMATLAB04,ToMATLAB05,
%    ToMATLAB06,ToMATLAB07,ToMATLAB08,ToMATLAB09,ToMATLAB10 )
%
%  For Type 'AC':
%  ID of ToAVDS (AC) -> 3
%  size of VectorToAVDS (AC) -> 25
%    (Posiiton North(feet), Posiiton East(feet), Altitude(feet), Phi(rad), Theta(rad),
%     Psi(rad), Alpha(rad), Beta(rad), G, Velocity (feet/sec), M, DeltaRudder(rad), 
%     DeltaElevator(rad), DeltaAileron(rad),EngineLevel(0-1),ToAVDS01,ToAVDS02,ToAVDS03,
%     ToAVDS04,ToAVDS05,ToAVDS06,ToAVDS07,ToAVDS08,ToAVDS09,ToAVDS10)
%     
%  ID of ToMATLAB (AC) -> 4
%  size of VectorToMATLAB (AC) -> 16
%    (Alive, ElevatorCommand, AileronCommand, RudderCommand,ThrottleCommand,ElapsedTime(sec),
%     ToMATLAB01,ToMATLAB02,ToMATLAB03,ToMATLAB04,ToMATLAB05,ToMATLAB06,ToMATLAB07,
%     ToMATLAB08,ToMATLAB09,ToMATLAB10)
%
%  NOTE: the "ToAVDS??" and "ToMATLAB??" are generic elements of the vectors for passing data not
%      defined by the other elements.


%   Steve Rasmussen, October 2000
%   Copyright (c) 2000 by RasSimTech Ltd
%   $Revision: 1.0 $  $Date: 2000/10/31 00:00:00 $

VectorToMATLAB = [];

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

switch Mode
case 'OpenConnection'
	if (nargin <2) 
   	VectorToAVDS = 'Error';
	end
		%% The following numbers must be coodinated with the AVDS shared simulation model
      %% (see the file ".\SharedMemoryLibrary\AVDS2MATLAB.h")
   switch VectorToAVDS
   case 'FCS'
      iToAVDS_ID = 1;
      iSizeToAVDS = 14;
      iToMATLAB_ID = 2;
      iSizeToMATLAB = 19;
   case 'AC'
		iToAVDS_ID = 3;
		iSizeToAVDS = 25;
      iToMATLAB_ID = 4;
		iSizeToMATLAB = 16;
   otherwise
   	ErrorString = sprintf('** SimulationConnection:: When using ''OpenConnection'' mode must also specify connection type.\n**     Valid Types are: ''AC'' and ''FCS''.\n**     Please type "help SimulationConnection" for more information.\n');
   	error(ErrorString);
      return;
   end;	%switch VectorToAVDS
   AVDSSimulation(1,ConnectionID,iToAVDS_ID,iSizeToAVDS,iToMATLAB_ID,iSizeToMATLAB);
case 'SendData'
   AVDSSimulation(2,ConnectionID,VectorToAVDS);
case 'ReceiveData'
   VectorToMATLAB = AVDSSimulation(3,ConnectionID);
case 'CloseConnection'
   AVDSSimulation(4);
otherwise
   ErrorString = sprintf('** SimulationConnection:: function called with incorrect syntax or incorrect Mode.\n**     Valid Modes are: ''OpenConnection'', ''SendData'', ''ReceiveData'' and ''CloseConnection''\n**     Please type "help AVDSSimulation" for more information.\n\n');
   error(ErrorString);
end; %switch Mode