% Uses Nxtmotor to Move the Robot Forward, Stop, and Move Backward to Its

Total Page:16

File Type:pdf, Size:1020Kb

% Uses Nxtmotor to Move the Robot Forward, Stop, and Move Backward to Its

Lab 2 Part A (I) %% Lab 2 Part A (1) % Uses NXTMotor to move the robot forward, stop, and move backward to its % original position

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Move forward m = NXTMotor( [MOTOR_B; MOTOR_C] ); m.Power = 30; m.TachoLimit = 1080; m.SendToNXT() m.WaitFor()

%% Move backward m.Power = -30; m.SendToNXT() m.WaitFor() Lab 2 Part A (II)

%% Lab 2 Part A (2) % Uses NXTMotor to rotate the robot 180 degrees in place.

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Rotate l_wheel = NXTMotor( MOTOR_B ); r_wheel = NXTMotor( MOTOR_C ); l_wheel.Power = -30; r_wheel.Power = 30; l_wheel.TachoLimit = 550; r_wheel.TachoLimit = 550; l_wheel.SendToNXT() r_wheel.SendToNXT() l_wheel.WaitFor() r_wheel.WaitFor() Lab 2 Part B

%% Lab 2 Part B % Uses NXT_GetOutput state to monitor the rotation of a motor.

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Read motor state s = NXT_GetOutputState( MOTOR_B ); disp( s.TachoCount ) Lab 3 Part A

%% Lab 3 Part A % Gets reading from touch sensor

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Get sensor reading OpenSwitch( SENSOR_1 ) GetSwitch( SENSOR_1 ) CloseSensor( SENSOR_1 ) Lab 3 Part B

%% Lab 3 Part B % Moves robot until button is pressed

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% NXTMotor m = NXTMotor( [MOTOR_B; MOTOR_C] ); m.SmoothStart = true; m.Power = 20;

%% Open sensor OpenSwitch( SENSOR_1 )

%% Start motor m.SendToNXT()

%% Read sensor done = false; while ~done if GetSwitch( SENSOR_1 ) m.Stop('off') done = true; end pause( 0.1 ) end OpenNXT2Color function OpenNXT2Color(port, type, varargin) %OPENCOLOR Summary of this function goes here % Detailed explanation goes here

%% check parameters

% check if handle is given; if not use default one if nargin > 2 handle = varargin{1}; else handle = COM_GetDefaultNXT; end%if

% also accept strings as input if ischar(port) port = str2double(port); end%if

%% check and set mode

sensormode = 'RAWMODE'; if strcmp(type, 'LIGHTRED') || strcmp(type, 'LIGHT') sensortype = 'COLORRED'; elseif strcmp(type, 'LIGHTGREEN') sensortype = 'COLORGREEN'; elseif strcmp(type, 'LIGHTBLUE') sensortype = 'COLORBLUE'; elseif strcmp(type, 'COLOR') sensortype = 'COLORFULL'; else error('Invalid mode. Must be LIGHT, LIGHTRED, LIGHTGREEN, LIGHTBLUE or COLOR.') end%if

%% call NXT_SetInputMode function

NXT_SetInputMode( port, sensortype, sensormode, 'dontreply', handle ); end%function GetNXT2Color function out = GetNXT2Color( f_sensorport, varargin ) %GETNXT2COLOR Summary of this function goes here % Detailed explanation goes here

%% check parameters

if nargin > 1 handle = varargin{1}; else handle = COM_GetDefaultNXT; end%if

%% call NXT_GetInputValues

in = NXT_GetInputValues( f_sensorport, handle );

%% check for valid date, re-request if necessary

startTime = clock(); timeOut = 0.1; % in seconds % loop until valid while (~in.Valid) && (etime(clock, startTime) < timeOut) in = NXT_GetInputValues(f_sensorport, handle); end%while

if strcmp(in.TypeName,'NO_SENSOR') error('No sensor set up for that port') end%if

%% format output

if strcmp(in.TypeName, 'COLORFULL') && strcmp(in.ModeName,'RAWMODE') colors = {'BLACK';'BLUE';'GREEN';'YELLOW';'RED';'WHITE'}; try out = colors{ in.ScaledVal }; catch out = 'UNKNOWN'; end%try else if strcmp(in.ModeName,'RAWMODE') && ( strcmp(in.TypeName,'COLORRED') || strcmp(in.TypeName,'COLORGREEN') || strcmp(in.TypeName,'COLORBLUE') ) out = floor( in.ScaledVal / 1024 * 100 ); else error('Invalid type/mode combination. Try using NXT_GetInputValues.') end%if end end sensortype2byte function byte = sensortype2byte(name) % Determines sensor type byte from given sensor type name % % Syntax % byte = sensortype2byte(name) % % Description % The constant names and enumerations are directly taken from the LEGO % Mindstorms Bluetooth SDK and Direct Commands Appendix % % Signature % Author: Linus Atorf (see AUTHORS) % Date: 2007/10/14 % Copyright: 2007-2009, RWTH Aachen University % % % *********************************************************************************************** % * This file is part of the RWTH - Mindstorms NXT Toolbox. * % * * % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * % * it under the terms of the GNU General Public License as published by the Free Software * % * Foundation, either version 3 of the License, or (at your option) any later version. * % * * % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * % * * % * You should have received a copy of the GNU General Public License along with the * % * RWTH - Mindstorms NXT Toolbox. If not, see . * % ***********************************************************************************************

%% Create look up table NXT__NO_SENSOR = 0; %#ok %hex2dec('00'); %#ok NXT__SWITCH = 1; %#ok %hex2dec('01'); %#ok NXT__TEMPERATURE = 2; %#ok %hex2dec('02'); %#ok NXT__REFLECTION = 3; %#ok %hex2dec('03'); %#ok NXT__ANGLE = 4; %#ok %hex2dec('04'); %#ok NXT__LIGHT_ACTIVE = 5; %#ok %hex2dec('05'); %#ok NXT__LIGHT_INACTIVE = 6; %#ok %hex2dec('06'); %#ok NXT__SOUND_DB = 7; %#ok %hex2dec('07'); %#ok NXT__SOUND_DBA = 8; %#ok %hex2dec('08'); %#ok NXT__CUSTOM = 9; %#ok %hex2dec('09'); %#ok NXT__LOWSPEED = 10; %#ok %hex2dec('0A'); %#ok NXT__LOWSPEED_9V = 11; %#ok %hex2dec('0B'); %#ok NXT__HIGHSPEED = 12;%#ok %hex2dec('0C'); %#ok NXT__COLORFULL = 13;%#ok %hex2dec('0D'); %#ok NXT__COLORRED = 14;%#ok %hex2dec('0E'); %#ok NXT__COLORGREEN = 15;%#ok %hex2dec('0F'); %#ok NXT__COLORBLUE = 16;%#ok %hex2dec('10'); %#ok NXT__COLORNONE = 17;%#ok %hex2dec('11'); %#ok NXT__NO_OF_SENSOR_TYPES = 18; %#ok %hex2dec('12'); %#ok

%% Check parameter if ~exist(['NXT__' name], 'var') || ~isreal(['NXT__' name]) error('MATLAB:RWTHMindstormsNXT:Sensor:invalidTypeName', 'Unknown NXT sensor type "%s". See inside this method (private/sensortype2byte.m) for a list.', name) end%if

%% Interpret sensor type byte byte = eval(['NXT__' name]); end%function byte2sensortype function name = byte2sensortype(byte) % Determines sensor type parameter from given byte % % Syntax % name = byte2sensortype(byte) % % Description % The constant names and enumerations are directly taken from the LEGO % Mindstorms Bluetooth SDK and Direct Commands Appendix % % Signature % Author: Linus Atorf (see AUTHORS) % Date: 2007/10/14 % Copyright: 2007-2009, RWTH Aachen University % % % *********************************************************************************************** % * This file is part of the RWTH - Mindstorms NXT Toolbox. * % * * % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * % * it under the terms of the GNU General Public License as published by the Free Software * % * Foundation, either version 3 of the License, or (at your option) any later version. * % * * % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * % * * % * You should have received a copy of the GNU General Public License along with the * % * RWTH - Mindstorms NXT Toolbox. If not, see . * % ***********************************************************************************************

%% Check parameter if byte < 0 || byte > 255 error('MATLAB:RWTHMindstormsNXT:invalidByteValue', 'Bytes must be between 0 and 255!'); end%if

%% Create look up table sensorname = {'NO_SENSOR';'SWITCH';'TEMPERATURE';'REFLECTION';'ANGLE';'LIGHT_ACTIVE';'LIGHT_INACTIVE';'SOUND_DB';'SOUND_ DBA';'CUSTOM';'LOWSPEED';'LOWSPEED_9V';'HIGHSPEED';'COLORFULL';'COLORRED';'COLORGREEN';'COLORBLUE';'COLORNO NE';'NO_OF_SENSOR_TYPES';};

%% Determine sensor type try name = sensorname{byte + 1}; catch name = 'unknown'; %TODO some kind of warning here to notice the user of this unknown type/mode? end%try end%function Lab 4 Part A (I)

%% Lab 4 Part A (1) % Gets a reading from the color sensor in color mode

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Get color sensor reading OpenNXT2Color( SENSOR_1, 'COLOR' ) pause( 0.1 ) c = GetNXT2Color( SENSOR_1 ); disp( c ) CloseSensor( SENSOR_1 ) Lab 4 Part A (II)

%% Lab 4 Part A (2) % Gets a reading from the color sensor in light mode

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Get light sensor reading OpenNXT2Color( SENSOR_1, 'LIGHT' ) pause( 0.1 ) c = GetNXT2Color( SENSOR_1 ); pause( 0.1 ) disp( c ) CloseSensor( SENSOR_1 ) Lab 4 Part B

%% Lab 4 Part B % Uses the color sensor in color mode to follow a line

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% NXTMotor l_wheel = NXTMotor( MOTOR_B ); r_wheel = NXTMotor( MOTOR_C ); l_wheel.SmoothStart = true; r_wheel.SmoothStart = true;

%% Open color sensor OpenNXT2Color( SENSOR_1, 'COLOR' ) pause( 0.1 )

%% Start right wheel r_wheel.Power = 20; r_wheel.SendToNXT()

%% Follow line while true

%% Right turn l_wheel.Power = 10; l_wheel.SendToNXT() % Wait until robot is over white area while ~strcmp( GetNXT2Color( SENSOR_1 ), 'WHITE' ) pause( 0.01 ) end

%% Left turn l_wheel.Power = 40; l_wheel.SendToNXT() % Wait until robot is over black line while ~strcmp( GetNXT2Color( SENSOR_1 ), 'BLACK' ) pause( 0.01 ) end Lab 4 Part C

%% Lab 4 Part C % Uses the color sensor in light mode to follow a line

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% NXTMotor l_wheel = NXTMotor( MOTOR_B ); r_wheel = NXTMotor( MOTOR_C ); l_wheel.SmoothStart = true; r_wheel.SmoothStart = true;

%% Start color sensor OpenNXT2Color( SENSOR_1, 'LIGHT' ) pause( 0.1 )

%% Start right wheel r_wheel.Power = 20; r_wheel.SendToNXT()

%% Follow line while true

%% Set target reading target = 45;

%% Adjust left wheel's speed based on sensor reading diff = target - GetNXT2Color( SENSOR_1 ); % Floor is necessary because Power must be an integer l_wheel.Power = 20 - floor(diff); l_wheel.SendToNXT() pause( 0.01 ) Lab 5 Part A (I)

%% Lab 5 Part A (1) % Collect readings from the ultrasonic sensor in continous mode

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Get ultrasonic sensor readings OpenUltrasonic( SENSOR_1 ) pause( 0.1 ) for i=1:10 disp( GetUltrasonic( SENSOR_1 ) ) pause( 0.5 ) end Lab 5 Part A (II)

%% Lab 5 Part A (2) % Gets readings from the ultrasonic sensor in snapshot mode

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% Get sensor readings OpenUltrasonic( SENSOR_1, 'SNAPSHOT' ) pause( 0.1 ) for i=1:10 USMakeSnapshot( SENSOR_1 ) pause( 0.5 ) echoes = USGetSnapshotResults( SENSOR_1 ); disp( echoes ) end Lab 5 Part B

%% Lab 5 Part B % Uses the ultrasonic sensor to maintain a given distance from a surface target = 20;

%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end

%% NXTMotor m = NXTMotor( [MOTOR_B; MOTOR_C] ); m.SmoothStart = true;

%% Open sensor OpenUltrasonic( SENSOR_1 ) pause( 0.1 )

%% Get sensor readings & adjust motors while true

d = GetUltrasonic( SENSOR_1 ); power = floor( ( d - target ) * 10 ); %m.Power = power; m.Power = max( -100, min( 100, power ) ); disp( m.Power ) m.SendToNXT() pause( 0.1 ) end

Recommended publications