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

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

<p>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</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% Move forward m = NXTMotor( [MOTOR_B; MOTOR_C] ); m.Power = 30; m.TachoLimit = 1080; m.SendToNXT() m.WaitFor()</p><p>%% Move backward m.Power = -30; m.SendToNXT() m.WaitFor() Lab 2 Part A (II)</p><p>%% Lab 2 Part A (2) % Uses NXTMotor to rotate the robot 180 degrees in place.</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% 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</p><p>%% Lab 2 Part B % Uses NXT_GetOutput state to monitor the rotation of a motor.</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% Read motor state s = NXT_GetOutputState( MOTOR_B ); disp( s.TachoCount ) Lab 3 Part A </p><p>%% Lab 3 Part A % Gets reading from touch sensor</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% Get sensor reading OpenSwitch( SENSOR_1 ) GetSwitch( SENSOR_1 ) CloseSensor( SENSOR_1 ) Lab 3 Part B</p><p>%% Lab 3 Part B % Moves robot until button is pressed</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% NXTMotor m = NXTMotor( [MOTOR_B; MOTOR_C] ); m.SmoothStart = true; m.Power = 20;</p><p>%% Open sensor OpenSwitch( SENSOR_1 )</p><p>%% Start motor m.SendToNXT()</p><p>%% 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</p><p>%% check parameters</p><p>% check if handle is given; if not use default one if nargin > 2 handle = varargin{1}; else handle = COM_GetDefaultNXT; end%if</p><p>% also accept strings as input if ischar(port) port = str2double(port); end%if</p><p>%% check and set mode</p><p> 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</p><p>%% call NXT_SetInputMode function</p><p>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</p><p>%% check parameters</p><p> if nargin > 1 handle = varargin{1}; else handle = COM_GetDefaultNXT; end%if</p><p>%% call NXT_GetInputValues</p><p> in = NXT_GetInputValues( f_sensorport, handle );</p><p>%% check for valid date, re-request if necessary</p><p> 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</p><p> if strcmp(in.TypeName,'NO_SENSOR') error('No sensor set up for that port') end%if</p><p>%% format output</p><p> 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 <http://www.gnu.org/licenses/>. * % ***********************************************************************************************</p><p>%% Create look up table NXT__NO_SENSOR = 0; %#ok<NASGU> %hex2dec('00'); %#ok<NASGU> NXT__SWITCH = 1; %#ok<NASGU> %hex2dec('01'); %#ok<NASGU> NXT__TEMPERATURE = 2; %#ok<NASGU> %hex2dec('02'); %#ok<NASGU> NXT__REFLECTION = 3; %#ok<NASGU> %hex2dec('03'); %#ok<NASGU> NXT__ANGLE = 4; %#ok<NASGU> %hex2dec('04'); %#ok<NASGU> NXT__LIGHT_ACTIVE = 5; %#ok<NASGU> %hex2dec('05'); %#ok<NASGU> NXT__LIGHT_INACTIVE = 6; %#ok<NASGU> %hex2dec('06'); %#ok<NASGU> NXT__SOUND_DB = 7; %#ok<NASGU> %hex2dec('07'); %#ok<NASGU> NXT__SOUND_DBA = 8; %#ok<NASGU> %hex2dec('08'); %#ok<NASGU> NXT__CUSTOM = 9; %#ok<NASGU> %hex2dec('09'); %#ok<NASGU> NXT__LOWSPEED = 10; %#ok<NASGU> %hex2dec('0A'); %#ok<NASGU> NXT__LOWSPEED_9V = 11; %#ok<NASGU> %hex2dec('0B'); %#ok<NASGU> NXT__HIGHSPEED = 12;%#ok<NASGU> %hex2dec('0C'); %#ok<NASGU> NXT__COLORFULL = 13;%#ok<NASGU> %hex2dec('0D'); %#ok<NASGU> NXT__COLORRED = 14;%#ok<NASGU> %hex2dec('0E'); %#ok<NASGU> NXT__COLORGREEN = 15;%#ok<NASGU> %hex2dec('0F'); %#ok<NASGU> NXT__COLORBLUE = 16;%#ok<NASGU> %hex2dec('10'); %#ok<NASGU> NXT__COLORNONE = 17;%#ok<NASGU> %hex2dec('11'); %#ok<NASGU> NXT__NO_OF_SENSOR_TYPES = 18; %#ok<NASGU> %hex2dec('12'); %#ok<NASGU></p><p>%% 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</p><p>%% 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 <http://www.gnu.org/licenses/>. * % ***********************************************************************************************</p><p>%% Check parameter if byte < 0 || byte > 255 error('MATLAB:RWTHMindstormsNXT:invalidByteValue', 'Bytes must be between 0 and 255!'); end%if</p><p>%% 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';};</p><p>%% 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)</p><p>%% Lab 4 Part A (1) % Gets a reading from the color sensor in color mode</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% 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)</p><p>%% Lab 4 Part A (2) % Gets a reading from the color sensor in light mode</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% 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</p><p>%% Lab 4 Part B % Uses the color sensor in color mode to follow a line</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% NXTMotor l_wheel = NXTMotor( MOTOR_B ); r_wheel = NXTMotor( MOTOR_C ); l_wheel.SmoothStart = true; r_wheel.SmoothStart = true;</p><p>%% Open color sensor OpenNXT2Color( SENSOR_1, 'COLOR' ) pause( 0.1 )</p><p>%% Start right wheel r_wheel.Power = 20; r_wheel.SendToNXT()</p><p>%% Follow line while true</p><p>%% 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</p><p>%% 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</p><p>%% Lab 4 Part C % Uses the color sensor in light mode to follow a line</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% NXTMotor l_wheel = NXTMotor( MOTOR_B ); r_wheel = NXTMotor( MOTOR_C ); l_wheel.SmoothStart = true; r_wheel.SmoothStart = true;</p><p>%% Start color sensor OpenNXT2Color( SENSOR_1, 'LIGHT' ) pause( 0.1 )</p><p>%% Start right wheel r_wheel.Power = 20; r_wheel.SendToNXT()</p><p>%% Follow line while true</p><p>%% Set target reading target = 45;</p><p>%% 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)</p><p>%% Lab 5 Part A (1) % Collect readings from the ultrasonic sensor in continous mode</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% 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)</p><p>%% Lab 5 Part A (2) % Gets readings from the ultrasonic sensor in snapshot mode</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% 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</p><p>%% Lab 5 Part B % Uses the ultrasonic sensor to maintain a given distance from a surface target = 20;</p><p>%% Get handle try COM_GetDefaultNXT; catch e if strcmp(e.identifier,'MATLAB:RWTHMindstormsNXT:Bluetooth:defaultHandleNotSet') error('Default handle not set') end end</p><p>%% NXTMotor m = NXTMotor( [MOTOR_B; MOTOR_C] ); m.SmoothStart = true;</p><p>%% Open sensor OpenUltrasonic( SENSOR_1 ) pause( 0.1 )</p><p>%% Get sensor readings & adjust motors while true</p><p> 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</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us