% EF 230 OLR-1: P0
% Noah Marks <nmarks1>; Curtis Schunk <cschunk>
% Date Modified: April 10, 2018 
% Objective: Test commands of roomba to be placed in the main program.
% Tested: Automatic velocity, bumper structure output manipulation
%         and roomba timer commands. Distance coordination also
%         tested for surveillance procedures. Added subfunction for photo
%         taking and object counting. Identified camera of having ~45
%         degree radius of each photo; changes in command account for it.
function [E_tot,T] = redux(r,t,v)

mustBePositive(t); mustBePositive(v); % must be positive values
r.timeStart;
T=r.timeGet;% initiate timer
E_tot=0; % image counter
D=0; % distance variable
v_b=v; % set base velocity

while T<t
 r.setDriveVelocity(v_b) % constant speed
 A=r.getBumpers;
 B=r.getLightBumpers;
 C=r.getCliffSensors;
 D=D+r.getDistance;
 
 if A.right~=0 || A.front~=0 % hits a wall
  r.stop
  r.moveDistance(-0.1,0.1)
  r.turnAngle(70,0.1) % turn left
  v_b=v;
 elseif A.left~=0 
  r.stop
  r.moveDistance(-0.1,0.1)
  r.turnAngle(-70,0.1) % turn right
  v_b=v;
 elseif B.left>100 || B.leftFront>100 || B.leftCenter>100 || B.rightCenter>100 || B.rightFront>100 || B.right>100
  v_b=0.1041*log(v)+0.3121; % caution as it approaches wall
 elseif C.left<1400 || C.leftFront<1400 
  r.stop
  r.moveDistance(-0.2,0.25)
  r.turnAngle(-100,0.1)
  v_b=v;
 elseif C.rightFront<1400 || C.right<1400
  r.stop
  r.moveDistance(-0.2,0.25)
  r.turnAngle(100,0.1)
  v_b=v;  
 elseif D>3 % maximum distance before reconaissance
  for i=1:8
   r.turnAngle(-45,0.1)
   pause(0.5)
  end 
   v_b=v;
  else
  v_b=v; % reset when all is normal
 end
 T=r.timeGet; % change time with each movement
end
r.stop;
end