Changeset 299
- Timestamp:
- 09/25/08 15:56:39 (5 years ago)
- Location:
- branches/atorf/RWTHMindstormsNXT
- Files:
-
- 11 modified
-
CalibrateCompass.m (modified) (4 diffs)
-
CloseSensor.m (modified) (4 diffs)
-
GetAccelerator.m (modified) (3 diffs)
-
GetCompass.m (modified) (5 diffs)
-
GetInfrared.m (modified) (5 diffs)
-
GetLight.m (modified) (3 diffs)
-
GetMemoryCount.m (modified) (4 diffs)
-
GetMotorSettings.m (modified) (4 diffs)
-
GetSound.m (modified) (3 diffs)
-
GetSwitch.m (modified) (3 diffs)
-
GetUltrasonic.m (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/atorf/RWTHMindstormsNXT/CalibrateCompass.m
r298 r299 1 function [ok] = CalibrateCompass(port, f_start )2 % Calibratecompass sensor1 function [ok] = CalibrateCompass(port, f_start, varargin) 2 % Enables or calibration mode of the HiTechnic compass sensor 3 3 % 4 4 % Syntax 5 5 % |CalibrateCompass(port, f_start)| 6 6 % 7 % |CalibrateCompass(port, f_start, handle)| 8 % 7 9 % Description 8 % Calibrate compass to reduce influence of motor and brick on compass 9 % values. You have to calibrate a robotor only once until new building 10 % conditions. While calibration the motor very slowly should make two full rotations. 11 % Compass sensor have to be opened before execution. 12 % |f_start| = true starts calibration, |f_start| = false stopps it. 10 % Calibrate the compass to reduce influence of metallic objects, 11 % especially of the NXT motor and brick on compass values. 12 % You have to calibrate a roboter only once until the design changes. 13 % During calibration the compass should make two full rotations very slowly. 14 % The compass sensor has to be opened (using |OpenCompass|) before execution. 15 % 16 % Set |f_start = true| to start calibration mode, and |f_start = false| to stop it. 17 % In between those commands, the calibration (compass rotation) should occur. 18 % 13 19 % The given |port| number specifies the connection port. The value |port| can be 14 20 % addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| 15 21 % analog to the labeling on the NXT Brick. 16 % 22 % 23 % The last optional argument can be a valid NXT handle. If none is 24 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 25 % set one). 26 % 17 27 % Example 28 % 29 %+ % enable calibration mode 30 %+ CalibrateCompass(SENSOR_2, true); 31 %+ 32 %+ % compass is attached to motor A, rotate 2 full turns 18 33 %+ SetMotor(MOTOR_A); 19 %+ SetPower(5);20 %+ SetAngleLimit(720);34 %+ SetPower(5); 35 %+ SetAngleLimit(720); 21 36 %+ SendMotorSettings; 22 %+ CalibrateCompass(SENSOR_2, true);37 %+ 23 38 %+ WaitForMotor(MOTOR_A); 39 %+ 40 %+ % calibration should now be complete! 24 41 %+ CalibrateCompass(SENSOR_2, false); 25 42 % … … 27 44 % 28 45 % Signature 29 % Author: Rainer Schnitzler (see AUTHORS)46 % Author: Rainer Schnitzler, Linus Atorf (see AUTHORS) 30 47 % Date: 2008/08/01 31 48 % Copyright: 2007-2008, RWTH Aachen University … … 50 67 %% Check Parameter 51 68 if nargin < 2 52 error('MATLAB:RWTHMindstormsNXT: Sensor:invalidParameter', 'Two function parameter are required, port and start/stop mode');69 error('MATLAB:RWTHMindstormsNXT:notEnoughInputArguments', 'Two function parameter are required, port and start/stop mode'); 53 70 end 71 72 73 % check if handle is given; if not use default one 74 if nargin > 2 75 handle = varargin{1}; 76 else 77 handle = COM_GetDefaultNXT; 78 end%if 54 79 55 80 %% create this I2C command to start callibration … … 57 82 if f_start 58 83 I2Cdata = hex2dec(['02'; '41'; '43']); % Command (41): Start callibration (43) 59 NXT_LSWrite(port, 0, I2Cdata, 'dontreply' );84 NXT_LSWrite(port, 0, I2Cdata, 'dontreply', handle); 60 85 ok = true; 61 86 else 62 87 % set back to 0 63 88 I2Cdata = hex2dec(['02'; '41'; '00']); % Command (41): Stop callibration (00) 64 NXT_LSWrite(port, 0, I2Cdata, 'dontreply' );89 NXT_LSWrite(port, 0, I2Cdata, 'dontreply', handle); 65 90 66 % wait for compass sensor to get ready and write a 2 in case calibration failed 67 pause(0.2); % 200ms, just a guess, less should be ok? 91 % wait for compass sensor to get ready and decide wether calibration 92 % was successful (it writes a 2 in case calibration failed) 93 pause(0.2); % 200ms, just a guess... enough or too long? 68 94 69 95 % retrieve 1 byte from device 0x02, register 0x41 70 data = COM_ReadI2C(port, 1, uint8(2), uint8(65) ); % calibration ok ?96 data = COM_ReadI2C(port, 1, uint8(2), uint8(65), handle); % calibration ok ? 71 97 if isempty(data) || (data == 2) 72 98 ok = false; -
branches/atorf/RWTHMindstormsNXT/CloseSensor.m
r123 r299 1 function [] = CloseSensor(f_sensorport)1 function CloseSensor(port, varargin) 2 2 % Closes a specified sensor port (e.g. turns off the active light mode of the NXT light sensor) 3 3 % 4 4 % Syntax 5 5 % |CloseSensor(port)| 6 % 7 % |CloseSensor(port, handle)| 6 8 % 7 9 % Description … … 11 13 % mode (the red light is turned off), closing the Ultrasonic sensor stops sending out ultrasound. 12 14 % 15 % The last optional argument can be a valid NXT handle. If none is 16 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 17 % set one). 13 18 % 14 19 % Examples … … 43 48 44 49 %% check parameters 45 % also accept strings as input 46 if ischar(f_sensorport) 47 f_sensorport = str2double(f_sensorport); 50 51 % check if handle is given; if not use default one 52 if nargin > 1 53 handle = varargin{1}; 54 else 55 handle = COM_GetDefaultNXT; 48 56 end%if 49 57 58 59 % also accept strings as input 60 if ischar(port) 61 port = str2double(port); 62 end%if 63 64 50 65 51 66 %% Set correct variables … … 56 71 57 72 %% Call NXT_SetInputMode function 58 NXT_SetInputMode( f_sensorport, sensortype, sensormode, 'dontreply');73 NXT_SetInputMode(port, sensortype, sensormode, 'dontreply', handle); 59 74 60 75 end%function -
branches/atorf/RWTHMindstormsNXT/GetAccelerator.m
r291 r299 1 function [acc_vector] = GetAccelerator(port )2 % Reads the current value of the NXT acceleratorsensor1 function [acc_vector] = GetAccelerator(port, varargin) 2 % Reads the current value of the HiTechnic acceleration sensor 3 3 % 4 4 % Syntax 5 5 % |acc_vector = GetAccelerator(port)| 6 6 % 7 % |acc_vector = GetAccelerator(port, handle)| 8 % 7 9 % Description 8 % |acc_vector = GetAccelerator(port)| returns the current 1x3 accelerator vector |acc_vector| of the NXT 9 % accelerator sensor. |distance| represents the measured distance in cm. 10 % |acc_vector = GetAccelerator(port)| returns the current 1x3 accelerator vector |acc_vector| of 11 % the HiTechnic acceleration sensor. The column vector contains the 12 % readings of the x, y, and z-axis, respectively. A reading of 200 is 13 % equal to the acceleration of 1g. Maximum range is -2g to +2g. 10 14 % The given |port| number specifies the connection port. The value |port| can be 11 15 % addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 12 16 % the labeling on the NXT Brick. 13 17 % 18 % The last optional argument can be a valid NXT handle. If none is 19 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 20 % set one). 14 21 % 15 22 % Example … … 61 68 % 62 69 63 70 % check if handle is given; if not use default one 71 if nargin > 1 72 handle = varargin{1}; 73 else 74 handle = COM_GetDefaultNXT; 75 end%if 76 77 64 78 % initialize 65 79 acc_vector = [NaN; NaN; NaN]; … … 68 82 69 83 % retrieve 6 bytes from device 0x02, register 0x42 70 data = COM_ReadI2C(port, 6, uint8(2), uint8(66) );84 data = COM_ReadI2C(port, 6, uint8(2), uint8(66), handle); 71 85 72 86 if ~isempty(data) -
branches/atorf/RWTHMindstormsNXT/GetCompass.m
r298 r299 1 function [degree] = GetCompass(port )2 % Reads the current value of the NXTcompass sensor1 function [degree] = GetCompass(port, varargin) 2 % Reads the current value of the HiTechnic compass sensor 3 3 % 4 4 % Syntax 5 5 % |degree = GetCompass(port)| 6 6 % 7 % |degree = GetCompass(port, handle)| 8 % 7 9 % Description 8 % |degree = GetCompass(port)| returns the current heading value NXT9 % compass sensor ranging from 0 to 360 where 0 is north and10 % |degree = GetCompass(port)| returns the current heading value of the 11 % HiTechnic magnetic compass sensor ranging from 0 to 360 where 0 is north and 10 12 % counterclockwise (90 = west etc.). 11 13 % The given |port| number specifies the connection port. The value |port| can be addressed by the … … 15 17 % For more complex settings the functions |NXT_LSRead| and |NXT_LSWrite| can be used. 16 18 % 19 % The last optional argument can be a valid NXT handle. If none is 20 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 21 % set one). 22 % 17 23 % Example 18 24 %+ OpenCompass(SENSOR_4); … … 20 26 %+ CloseSensor(SENSOR_4); 21 27 % 22 % See also: OpenCompass, C loseSensor, COM_ReadI2C28 % See also: OpenCompass, CalibrateCompass CloseSensor, COM_ReadI2C 23 29 % 24 30 % Signature … … 43 49 % * RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>. * 44 50 % *********************************************************************************************** 45 51 52 % check if handle is given; if not use default one 53 if nargin > 1 54 handle = varargin{1}; 55 else 56 handle = COM_GetDefaultNXT; 57 end%if 58 46 59 % initialize 47 60 degree = NaN; … … 51 64 % see http://www.hitechnic.com for more register information 52 65 % retrieve 2 bytes from device 0x02, register 0x44 53 data = COM_ReadI2C(port, 2, uint8(2), uint8(68) );66 data = COM_ReadI2C(port, 2, uint8(2), uint8(68), handle); 54 67 55 68 if ~isempty(data) -
branches/atorf/RWTHMindstormsNXT/GetInfrared.m
r291 r299 1 function [direction rawData] = GetInfrared(port )2 % Reads the current value of the NXTinfrared sensor (infrared seeker)1 function [direction rawData] = GetInfrared(port, varargin) 2 % Reads the current value of the Hitechnic infrared sensor (infrared seeker) 3 3 % 4 4 % Syntax 5 5 % |[direction rawData] = GetInfrared(port)| 6 % 7 % |[direction rawData] = GetInfrared(port, handle)| 6 8 % 7 9 % Description … … 9 11 % detected infrared signal. |direction| represents the main direction (1-9) calculated based on 10 12 % the measured raw data given in the |rawData| vector (1x5). Five sensors are provided by the 11 % infrared seeker. 13 % infrared seeker. For more information see http://www.hitechnic.com 14 % 12 15 % The given |port| number specifies the connection port. The value |port| can be 13 16 % addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 14 17 % the labeling on the NXT Brick. 15 18 % 19 % The last optional argument can be a valid NXT handle. If none is 20 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 21 % set one). 16 22 % 17 23 % Example … … 44 50 % *********************************************************************************************** 45 51 52 % check if handle is given; if not use default one 53 if nargin > 1 54 handle = varargin{1}; 55 else 56 handle = COM_GetDefaultNXT; 57 end%if 58 46 59 % initialize 47 60 direction = NaN; … … 49 62 50 63 51 52 64 % retrieve 6 bytes from device 0x02, register 0x42 53 data = COM_ReadI2C(port, 6, uint8(2), uint8(66) );65 data = COM_ReadI2C(port, 6, uint8(2), uint8(66), handle); 54 66 55 67 if ~isempty(data) … … 58 70 59 71 direction = data(1); 60 rawData = data(2:6); 61 72 rawData = data(2:6); 62 73 63 74 end%if -
branches/atorf/RWTHMindstormsNXT/GetLight.m
r123 r299 1 function light = GetLight( f_sensorport)1 function light = GetLight(port, varargin) 2 2 % Reads the current value of the NXT light sensor 3 3 % 4 4 % Syntax 5 5 % |light = GetLight(port)| 6 % 7 % |light = GetLight(port, handle)| 6 8 % 7 9 % Description … … 12 14 % addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 13 15 % the labeling on the NXT Brick. 16 % 17 % The last optional argument can be a valid NXT handle. If none is 18 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 19 % set one). 14 20 % 15 21 % For more complex settings the function |NXT_GetInputValues| can be used. … … 44 50 % *********************************************************************************************** 45 51 52 %% check if handle is given; if not use default one 53 if nargin > 1 54 handle = varargin{1}; 55 else 56 handle = COM_GetDefaultNXT; 57 end%if 58 46 59 %% Call NXT_GetInputValues function 47 in = NXT_GetInputValues( f_sensorport);60 in = NXT_GetInputValues(port, handle); 48 61 49 62 %% Return normalized sound value (0...1023 / 10 Bit) -
branches/atorf/RWTHMindstormsNXT/GetMemoryCount.m
r123 r299 1 function memory = GetMemoryCount(number )1 function memory = GetMemoryCount(number, varargin) 2 2 % Gets the internal NXT memory counter (manual mapping replica) 3 3 % 4 4 % Syntax 5 5 % |memory = GetMemoryCount(port)| 6 % 7 % |memory = GetMemoryCount(port, handle)| 6 8 % 7 9 % Description … … 11 13 % |memory| contains the value of the NXT memory counter (maunal mapping replica). 12 14 % 15 % The last optional argument can be a valid NXT handle. If none is 16 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 17 % set one). 18 % 19 % 13 20 % Note: 14 21 % This function is *recommened for the advanced user*. … … 17 24 %+ memory = GetMemoryCount(MOTOR_A); 18 25 % 19 % See also: SetMemoryCount er, MOTOR_A, MOTOR_B, MOTOR_C26 % See also: SetMemoryCount, MOTOR_A, MOTOR_B, MOTOR_C 20 27 % 21 28 % Signature … … 41 48 % *********************************************************************************************** 42 49 43 % get default handle & motorstate 44 h = COM_GetDefaultNXT(); 50 % check if handle is given; if not use default one 51 if nargin > 1 52 h = varargin{1}; 53 else 54 h = COM_GetDefaultNXT; 55 end%if 56 57 % get motorstate 45 58 NXTMOTOR_State = h.NXTMOTOR_getState(); 46 59 -
branches/atorf/RWTHMindstormsNXT/GetMotorSettings.m
r289 r299 1 function out = GetMotorSettings(whatmotor )1 function out = GetMotorSettings(whatmotor, varargin) 2 2 % Returns the current motor data / settings (e.g. position, speed, etc.) from the specified motor 3 3 % 4 4 % Syntax 5 5 % |data = GetMotorSettings(port)| 6 % 7 % |data = GetMotorSettings(port, handle)| 6 8 % 7 9 % Description … … 10 12 % |MOTOR_C| analog to the labeling on the NXT Brick. The return value |data| is a struct variable. 11 13 % It contains several motor settings and information. 14 % 15 % The last optional argument can be a valid NXT handle. If none is 16 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 17 % set one). 12 18 % 13 19 % Output: … … 66 72 67 73 74 % check if handle is given; if not use default one 75 if nargin > 1 76 h = varargin{1}; 77 else 78 h = COM_GetDefaultNXT; 79 end%if 80 68 81 if whatmotor < 0 || whatmotor > 2 69 82 error('MATLAB:RWTHMindstormsNXT:Motor:invalidPort', 'Input argument for motor must be 0, 1, or 2') 70 83 end%if 71 84 72 % get default handle & motorstate 73 h = COM_GetDefaultNXT(); 85 % get motorstate 74 86 NXTMOTOR_State = h.NXTMOTOR_getState(); 75 87 76 88 77 data = NXT_GetOutputState(whatmotor );89 data = NXT_GetOutputState(whatmotor, h); 78 90 79 91 … … 129 141 NXTMOTOR_State(whatmotor + 1).TachoCount = data.TachoCount; 130 142 143 % TODO the whole concept of storing this freshly retrieved information back 144 % to the handle might not be that useful anymore... as soon as the data 145 % arrives, it's basically out of date. So why would we want to keep it? 146 131 147 % save motor state back to handle 132 148 h.NXTMOTOR_setState(NXTMOTOR_State); -
branches/atorf/RWTHMindstormsNXT/GetSound.m
r289 r299 1 function out = GetSound(f_sensorport )1 function out = GetSound(f_sensorport, varargin) 2 2 % Reads the current value of the NXT sound sensor 3 3 % 4 4 % Syntax 5 5 % |sound = GetSound(port)| 6 % 7 % |sound = GetSound(port, handle)| 6 8 % 7 9 % Description … … 12 14 % addressed by the symbolic constants |SENSOR_1|, |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 13 15 % the labeling on the NXT Brick. 16 % 17 % The last optional argument can be a valid NXT handle. If none is 18 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 19 % set one). 14 20 % 15 21 % For more complex settings the function |NXT_GetInputMode| can be used. … … 44 50 % *********************************************************************************************** 45 51 52 %% check if handle is given; if not use default one 53 if nargin > 1 54 handle = varargin{1}; 55 else 56 handle = COM_GetDefaultNXT; 57 end%if 58 46 59 %% Call NXT_GetInputValues function 47 in = NXT_GetInputValues(f_sensorport );60 in = NXT_GetInputValues(f_sensorport, handle); 48 61 49 62 %% Return normalized sound value (0...1023 / 10 Bit) -
branches/atorf/RWTHMindstormsNXT/GetSwitch.m
r290 r299 1 function out = GetSwitch(f_sensorport )1 function out = GetSwitch(f_sensorport, varargin) 2 2 % Reads the current value of the NXT switch / touch sensor 3 3 % 4 4 % Syntax 5 5 % |switch = GetSwitch(port)| 6 % 7 % |switch = GetSwitch(port, handle)| 6 8 % 7 9 % Description … … 13 15 % the labeling on the NXT Brick. 14 16 % 17 % The last optional argument can be a valid NXT handle. If none is 18 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 19 % set one). 20 % 15 21 % For more complex settings the function |NXT_GetInputValues| can be used. 22 % 16 23 % 17 24 % Example … … 58 65 % 59 66 67 %% check if handle is given; if not use default one 68 if nargin > 1 69 handle = varargin{1}; 70 else 71 handle = COM_GetDefaultNXT; 72 end%if 73 60 74 61 75 %% Call NXT_GetInputValues function 62 in = NXT_GetInputValues(f_sensorport );76 in = NXT_GetInputValues(f_sensorport, handle); 63 77 64 78 %% Do threshold decision -
branches/atorf/RWTHMindstormsNXT/GetUltrasonic.m
r296 r299 1 function [DistanceCM] = GetUltrasonic(port )1 function [DistanceCM] = GetUltrasonic(port, varargin) 2 2 % Reads the current value of the NXT ultrasonic sensor 3 3 % 4 4 % Syntax 5 5 % |distance = GetUltrasonic(port)| 6 % 7 % |distance = GetUltrasonic(port, handle)| 6 8 % 7 9 % Description … … 12 14 % the labeling on the NXT Brick. 13 15 % 14 % For more complex settings the functions |NXT_LSRead| and |NXT_LSWrite| can be used. 16 % The last optional argument can be a valid NXT handle. If none is 17 % specified, the default handle will be used (call |COM_SetDefaultNXT| to 18 % set one). 19 % 20 % Note 21 % This function only works when the sensor was correctly opened with 22 % |OpenUltrasonic(port)|. If the sensor is being used in snapshot mode, 23 % |GetUltrasonic| will not work correctly! 24 % 25 % For different uses, see also |OpenUltrasonic(port, 'snapshot')| and the 26 % functions |USMakeSnapshot| and |USGetSnapshotResults|. 27 % 15 28 % 16 29 % Example … … 43 56 % *********************************************************************************************** 44 57 58 % check if handle is given; if not use default one 59 if nargin > 1 60 handle = varargin{1}; 61 else 62 handle = COM_GetDefaultNXT; 63 end%if 45 64 46 65 % also accept strings as input … … 56 75 57 76 % retrieve 1 byte from device 0x02, register 0x42 58 data = COM_ReadI2C(port, 1, uint8(2), uint8(66) );77 data = COM_ReadI2C(port, 1, uint8(2), uint8(66), handle); 59 78 60 79 if isempty(data)
