% AMLCOM -- Matlab version 2.2.2 "Rechargeable Salamander"
% Will_Ferenc@hmc.edu
% 6/21/2011 UCLA Applied Math Lab
% 2011 Applied Math REU 

%Revision History
%1.0.0 Alpha
%1.5.0 Started tracking Revision history
%2.0.0 Changed to TCPIP from UDP
%2.1.0 Readablility Improvements
%2.1.1 Added User Prompts & Usage Instructions
%2.2.2 Adjusted connection timeout



%=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-USAGE-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
% AMLCOM is a matlab script that allows communication between the UCLA AML
% Micro car testbed and other swarm testbeds over a TCPIP connection. The
% script should be activated simotaneously with a listening script, such as
% AMLlistenOPEN.m at the receving computer. Both of the Scripts must be
% calibrated with the other computer's IP address. In case of an error
% during the script, AMLClean.m is also provided. Summary follows.
%
% 0. Obtain and load AMLCOMTCPIP and a listening script (eg. AMLlistenOPEN)
% 1. Find IP address of both computers (google "find my IP address")
% 2. Input IP address of the remote computer for each script
% 3. Run scripts in tandem. (If not they will timeout and error)
% 4. Wait for prompt, then activate the AML car, then press any key
% 5. Check receiving computer for results. (perhaps use fread(t,#bytessent)
%-------------------------------------------------------------------------

%=-=-=-=-=-=-=-=-=-=-=-=-=-IMPORTANT CONSTANTS-=-=-=-=-=-=-=-=-=-=-=-=-=-=
REMOTEIP = '128.97.12.33'; %IP of other computer
PORT = [4433, 4455]; %[localPort, remotePort]
COM = 'COM5'; %COM port of transmitter.
%NOTE: COM5 on highKerf, COM8 on AMLadmin
BAUD = 115200; %Baudrate for serial connection
%NOTE: usually 115200
%-------------------------------------------------------------------------
'Check IP address' %prompt user
REMOTEIP %display IP address
%enable pausing for debugging
pause on

%configure and open the serial connection
car = serial(COM,'BAUD',BAUD,'Timeout',0.5);
fopen(car);
%configure and open the UDP connection
t = tcpip(REMOTEIP,'RemotePort',PORT(2),'LocalPort',PORT(1));

set(t, 'Timeout', 99); %Allow long timeout for initial connection
fopen(t);
set(t, 'Timeout', 0.2); %Resume short timeout for normal running
'Turn on car...Then press any key to continue'
pause %prompt user to turn on the car

k = 0; % initilize loop counter
while(1) % infinite loop... I'm sorry
pause(0.5)
 %ADD functionality::Interuppt for User Data
    
 %read UDP 
    if (t.BytesAvailable)
        %if stuff in UDP buffer
        tcpipMessage = fscanf(t,'%c',u.BytesAvailable) 
        %read out of udp buffer to local buffer
    else
        %if NO stuff in UDP buffer
        tcpipMessage = ''; %empty local buffer
    end
 %Read Serial
    if(car.BytesAvailable) %if items in serial buffer
        serialMessage = char(fread(car,car.bytesavailable))'
    else
        serialMessage = '';
    end
    
 %ADD FUNCTIONALITY Post commands to Serial
    
    
 %Post commands to UDP
    fprintf(t,'%c',serialMessage,'sync');
 %Timeout
    if(k>10)
        break
    end
    k = k + 1;
end

%close and clean serial connection
fclose(car);
delete(car);
clear car
%close and clean UDP connection
fclose(t);
delete(t);
clear t
%cleanup vars
clear BAUD
clear COM
clear IP
clear PORT
clear k
instrfind