function exampleHelperGazeboAvoidanceTimer(~, ~, handles)
    %exampleHelperGazeboAvoidanceTimer - Timer update function called in GazeboRobotAutonomyExample
    %   avoidanceTimer(~,~,handles) updates the velocity message to the
    %   TurtleBot based on information collected from the laser scanner. If the
    %   TurtleBot is too close to an obstacle, it will turn to avoid the
    %   obstacle.
    %
    %   See also GazeboRobotAutonomyExample
    
    %   Copyright 2014-2017 The MathWorks, Inc.
    
    % Declare persistent variables
    persistent mindist
    
    % Use global publisher and message
    global robot
    global velmsg
    
    % Assign callback for laser scan 
    if isempty(handles.sub.NewMessageFcn)
        handles.sub.NewMessageFcn = @laserCallbackInternal;
    end
    
    % Define constants for TurtleBot movement
    spinVelocity = 1.0;    % Angular velocity (rad/s)
    forwardVelocity = 0.75; % Linear velocity (m/s)
    backwardVelocity = -0.05;  % Linear velocity (reverse) (m/s)
    distanceThreshold = 1.5;  % Distance threshold (m) for turning
    
    % Command robot action    
    if mindist < distanceThreshold
        % If close to obstacle, back up slightly and spin
        velmsg.Angular.Z = spinVelocity;
        velmsg.Linear.X = backwardVelocity;
    else                                                                                                                                                                                                             
        % Continue on forward path
        velmsg.Linear.X = forwardVelocity;
        velmsg.Angular.Z = 0;
    end
    
    % Publish command to the motor
    send(robot,velmsg);
    
    function laserCallbackInternal(~, message)
        %laserCallbackInternal collects laser scan data
        scan = message.Ranges;
        scan = scan(100:540);
        mindist = min(scan(~isnan(scan)));
    end
    
end