Code covered by the BSD License  

Highlights from
MATLAB Support Package for Finch Robot

image thumbnail

MATLAB Support Package for Finch Robot

by

 

20 Jan 2012 (Updated )

MATLAB connectivity code to a Finch Robot

lightFollower.m
%% Creating a light following robot
% User this in a dark room and then use a flashlight to have the robot
% follow you around.

% Create connection to hardware
x=Finch();

% Get initial read of light sensors
val = x.lightSensors();

% Show values in graph to understand the readings
x.showLightSensorGraph

% Wait until it sees the light 
while (sum(val)<100)
    val = x.lightSensors();
    % Update the graph with latest values
    x.updateLightSensorGraph(val(1),val(2));
end

%%
%
pathForwardOK = ~x.isObstacle;

% Continue to follow the light until you bump into an object
while (pathForwardOK)
    
    % if the light goes away wait for light again
    if sum(val)>100
        % If the light is to one side move towards it
        if val(2)<val(1)
            % turn right
            x.setWheelVelocities(100,-100);
        else
            % turn left
            x.setWheelVelocities(-100,100);
        end
        % Move forward toward the light
        x.setWheelVelocities(100,100);
    else
        % Can see the light so wait
        x.stopWheels();
    end
    % Get lates value of light
    val = x.lightSensors();
    try
        % Update the graph.  If you close the graph it will error so use
        % this to stop following the light
        x.updateLightSensorGraph(val(1),val(2));
    catch ME
        break
    end
    
    % see if anything is in front of the robot
    pathForwardOK = ~x.isObstacle;
    
end

disp('Obstacle detected');

% stop moving forward
x.stopWheels();

x.quit();

Contact us