%% Getting Started Guide
% This file walks you through the Finch interface and gives you some
% example programs to learn how to program your Finch.
%% Setup Hardware Connectivity (only need to run this once)
% Call the setup function to download the Finch Java communication code
%
% Finch.setup
%% Creation of the Object
x=Finch('test');
%%
% See the object code in pdf <./mFinch.pdf here>
% or in html <./mFinch.html here>
%% Looking for help
% Help on the main object
help Finch
%%
% Help on a method particular method
help Finch.isTemperature
%%
% List out all the available methods using standard MATLAB
methods(x)
%%
% List out all the available methods using class call
% Finch.getMethods
%
%
% Methods for Finch Robot
%
% Finch()
% delete()
% quit()
%
% *Utilities*
%
% buzz(frequency,timeDuration)
% playClip(wavFile)
% playTone(frequency,timeDuration)
% sleep(timeDuration)
% saySomething(text)
%
% *Motor Control*
%
% stopWheels()
% setWheelVelocities(LeftVelocity,RightVelocity,timeDuration)
%
% *Set LED*
%
% setLED(obj, [red,green,blue],duration)
%
% *Get Sensor Data*
%
% varArray(R,L)=obstacleSensors()
% temp = temperature()
% varArray(x,y,z) = accelerations()
% varArray = lightSensors()
%
% *Boolean Tests*
%
% boolean = isFinchUpsideDown()
% boolean = isLeftWingDown()
% boolean = isRightWingDown()
% boolean = isFinchLevel()
% boolean = isTemperature(testVal)
% boolean = isLeftLightSensor(testVal)
% boolean = isRightLightSensor(testVal)
% boolean = isObstacle()
% boolean = isObstacleLeftSide()
% boolean = isObstacleRightSide()
% boolean = isBeakDown()
% boolean = isBeakUp()
% boolean = isShaken()
% boolean = isTapped()
%
% *Graphics*
%
% showLightSensorGraph()
% updateLightSensorGraph()
% closeLightSensorGraph()
% showTemperatureGraph()
% updateTemperatureGraph()
% closeTemperatureGraph()
% showAccelerometerGraph()
% updateAccelerometerGraph()
% closeAccelerometerGraph()
%%
% Looking for a method at command prompt -> type variable name and dot then
% hit the <TAB> key. This will bring up a list of available methods and
% give you syntax for the call
%% Calling Methods
% Get the Temperature Sensor data
f = x.temperature();
% Moving your Finch forward until you call stop
x.setWheelVelocities(100,100);
% Call Stop on the Finch
x.stopWheels();
% Moving your Finch forward for one second
x.setWheelVelocities(100,100,1000);
% Change the LED Color.
% This takes three arguments: Red,Green,Blue values. 0 is off and 255 is
% as bright as it can go.
red = 100;
green = 0;
blue = 0;
x.setLED([red,green,blue]);
%% Graphics
% Show a plot of the temperature
x.showTemperatureGraph();
for idx=1:20;
% Get current data
val = x.temperature();
% Put value on the graph
x.updateTemperatureGraph(val);
x.sleep(100);
end
%%
% close graph
x.closeTemperatureGraph();
% Show the Acceleration
x.showAccelerometerGraph();
for idx=1:10;
% Get current data
val = x.accelerations();
% Put value on the graph
x.updateAccelerometerGraph(val);
% Pause for one second
x.sleep(100);
% Alternatively you can get each one and pass them.
val = x.accelerations();
x.updateAccelerometerGraph(val(1),val(2),val(3));
% Pause for one second
x.sleep(100);
end
%%
% close graph
x.closeAccelerometerGraph();
%% Close connection and delete object
x.quit();
%% Creating Autonomous Robot
% Create connection to hardware
x=Finch();
% Tell the Finch to move forward
x.setWheelVelocities(100,100);
% Drive forward for 2 seconds before looking for obstacle
x.sleep(2000);
% Wait until it sees an obstacle
while(~any(x.isObstacle));end;
% Stop moving forward
x.stopWheels
% Quit connection
x.quit();
%% Creating a light following robot
% Use 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();
clear x