root/trunk/RWTHMindstormsNXT/SetTurnRatio.m @ 3

Revision 3, 3.9 KB (checked in by behrens, 5 years ago)

add tag and trunk toolbox

Line 
1function SetTurnRatio(NewRatio)
2% Sets the turn ratio of the current active motor
3
4% Syntax
5%   |SetTurnRatio(ratio)|
6%
7% Description
8%   |SetTurnRatio(ratio)| sets the turn |ratio| of the current active motor which can be controlled
9%   by |SendMotorSettings|. The value |ratio| has to be wihtin the Integer interval |-100...100|.
10%   The value specifies the ratio of power distribution between two motors (e.g. left and right
11%   wheel of the robot).
12%   The |ratio| setting takes only affect with the next |SendMotorSettings| command and if two
13%   motors are synchronied with |SyncToMotor|.
14%
15%   According to the LEGO Mindstorms communication protocol documentation, a ratio of 0 means equal
16%   power to both motors. Set 50 if you want one wheel spinning and the other stopped. Turn ratio of
17%   100 will turn one wheel in the opposite direction (i.e. reverse) and the other forward, hence
18%   giving the maximum "turn around on the spot" effect. Set values between 1 and 49 to get nice
19%   curves or circles driven by your robot.
20%
21% Note:
22%   When using several motor commands with |SyncToMotor| statements,
23%   unexpected behaviour can occur, due to the NXTs internal error correction counters.
24%   Sometimes it can help to issue the commands |NXT_ResetMotorPosition(port, true)|,
25%   |NXT_ResetMotorPosition(port, false)| and |StopMotor(port, 'off')| for each of both
26%   motors. Although this seems like a waste of packets, this can do the trick, especially
27%   when working with certain turn ratios.
28%
29% Example
30%+   SetMotor(MOTOR_B);
31%+      SyncToMotor(MOTOR_A);
32%+      SetPower(76);
33%+      SetTurnRatio(50);
34%+   SendMotorSettings();
35%
36% See also: SendMotorSettings, SyncToMotor, SetMotor, SetPower
37%
38% Signature
39%   Author: Linus Atorf, Alexander Behrens (see AUTHORS)
40%   Date: 2007/10/15
41%   Copyright: 2007, RWTH Aachen University
42%
43;
44%
45% ***********************************************************************************************
46% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
47% *                                                                                             *
48% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
49% *  it under the terms of the GNU General Public License as published by the Free Software     *
50% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
51% *                                                                                             *
52% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
53% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
54% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
55% *                                                                                             *
56% *  You should have received a copy of the GNU General Public License along with the           *
57% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
58% ***********************************************************************************************
59
60
61%% Check parameter
62global NXTMOTOR_State;
63
64if isempty(NXTMOTOR_State)
65    initializeGlobalMotorStateVar;
66end%if
67
68whatmotor = GetMotor;
69
70if ischar(NewRatio)
71    NewRatio = str2double(NewRatio);
72end%if
73
74if abs(NewRatio) > 100
75    error('MATLAB:RWTHMindstormsNXT:Motor:invalidTurnRatio', 'TurnRatio must be between -100 and 100')
76end%if
77
78
79%% Set turn ratio
80if whatmotor == 255
81    % feel the power of vectors :-)
82    NXTMOTOR_State(1:3).TurnRatio = NewRatio .* [1; 1; 1];
83else
84    NXTMOTOR_State(whatmotor + 1).TurnRatio = NewRatio;
85    if NXTMOTOR_State(whatmotor + 1).SyncedToMotor ~= -1
86        NXTMOTOR_State(NXTMOTOR_State(whatmotor + 1).SyncedToMotor + 1).TurnRatio = NewRatio;
87    end%if
88end%if
89
90end%function
Note: See TracBrowser for help on using the browser.