root/branches/telle/v2.00-at/mfiles/@NXTmotor/NXTmotor.m @ 403

Revision 403, 8.9 KB (checked in by telle, 5 years ago)

* Port to new MATLAB R2008 class syntax
* output at least one set of settings in ReadFromNXT, also if no output arg is given

Line 
1% NXTmotor Construct NXTmotor object
2%
3%     M = NXTmotor(PORT) constructs an NXTmotor object with motor
4%     port PORT and default attributes. PORT may be either the port
5%     number (0, 1, 2 or MOTOR_A, MOTOR_B, MOTOR_C) or a  string
6%     specifying the port ('A', 'B', 'C'). To have two motors
7%     synchronized PORT may be a vector of two ports in ascending
8%     order.
9%
10%     M = NXTmotor(PORT, 'PropName1', PropValue1, 'PropName2',
11%     PropValue2, ...) constructs an NXTmotor object with motor
12%     port(s) PORT in which the given Property name/value pairs are
13%     set on the object. All properties can also be set after
14%     creation by dot-notation (see example).
15%   
16%     Example:
17%       % Construct a NXTmotor object on port 'B' with a power of
18%       % 60, disabled speed regulation, a TachoLimit of 360 and
19%       % send the motor settings to the NXT brick.
20%       mb = NXTmotor('B', 'Power', 60)
21%       mb.SpeedRegulation = 1;
22%       mb.TachoLimit = 360;
23%       SendToNXT(mb);
24%
25%     See also NXTmotor/SendToNXT, NXTmotor/ReadFromNXT,
26%              NXTmotor/WaitFor, NXTmotor/Stop,
27%              NXTmotor/ResetPosition.   
28%
29%
30
31% Signature
32%   Author: Aulis Telle (see AUTHORS)
33%   Date: 2008/10/20
34%   Copyright: 2007-2008, RWTH Aachen University
35%
36%
37% ***********************************************************************************************
38% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
39% *                                                                                             *
40% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
41% *  it under the terms of the GNU General Public License as published by the Free Software     *
42% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
43% *                                                                                             *
44% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
45% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
46% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
47% *                                                                                             *
48% *  You should have received a copy of the GNU General Public License along with the           *
49% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
50% ***********************************************************************************************
51
52classdef NXTmotor
53    properties
54        Port              = 0;
55        Power             = 0;
56        SpeedRegulation   = 1;
57        TachoLimit        = 0;
58        BrakeAtTachoLimit = 1;
59        TurnRatio         = 0;
60    end % END OF PROPERTIES SECTION
61   
62    methods
63        % constructor
64        function obj = NXTmotor(varargin)
65            %
66           
67           
68            propertyArgIn = varargin;
69           
70            % check, whether first argument is a port specification
71            if nargin > 0
72                obj.Port = propertyArgIn{1};
73
74                if nargin >= 2
75                    propertyArgIn = propertyArgIn(2:end);
76                else
77                    propertyArgIn = {};
78                end
79               
80                while length(propertyArgIn) >= 2
81                    prop = propertyArgIn{1};
82                    val  = propertyArgIn{2};
83                    if length(propertyArgIn) >= 3
84                        propertyArgIn = propertyArgIn(3:end);
85                    else
86                        propertyArgIn = {};
87                    end
88                    switch prop
89                        case 'Power'
90                            obj.Power = val;
91                        case 'SpeedRegulation'
92                            obj.SpeedRegulation = val;
93                        case 'TachoLimit'
94                            obj.TachoLimit = val;
95                        case 'BrakeAtTachoLimit'
96                            obj.Power = val;
97                        case 'BrakeAtTachoLimit'
98                            obj.Power = val;
99                        otherwise
100                            error(['MATLAB:RWTHMindstormsNXT:'...
101                                'UnsupportedParameter'],...
102                                'Unsupported parameter %s',...
103                                varargin{argIterator});
104                    end
105                end
106            end
107        end
108        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110        % Setter methods for properties
111        function obj = set.Port(obj, val)
112            if numel(val) <= 2
113                % Convert to numeric representation (65 == 'A')
114                if ischar(val)
115                    val = double(upper(val)) - 65;
116                end
117
118                % Check, if any port is out of the alt range
119                if any(val < 0) || any (val > 2)
120                    error('MATLAB:RWTHMindstormsNXT:InvalidPort',...
121                        'One or more of the given ports are invalid.');
122                end
123
124                % Check, if ports are given in ascending order by
125                % taking the difference of successive values. If any
126                % of them is lower zero they are not in ascending order
127                if (numel(val) == 2) && (val(1) >= val(2))
128                    error('MATLAB:RWTHMindstormsNXT:InvalidPort',...
129                        'Two ports must be given in ascending order.');
130                end
131
132                % check for non-integer values
133                if any(rem(val,1) > 0)
134                    errror('MATLAB:RWTHMindstormsNXT:InvalidPort',...
135                        'Port is not an integer or port label.');
136                end
137
138                obj.Port = val;
139            else
140                error('MATLAB:RWTHMindstormsNXT:InvalidPort',...
141                    'No more than two ports may be specified.');
142            end
143        end
144        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145       
146        function obj = set.Power(obj, val)
147            % Power must be a scalar, be numerir and be in the range
148            % [-100 100]
149            if isscalar(val) && isnumeric(val) && val >= -100 && val <= 100
150                if any(rem(val,1) > 0)
151                    warning('MATLAB:RWTHMindstormsNXT:InvalidPort',...
152                        'Power is not an integer. The value is rounded.');
153                end
154                obj.Power = fix(val);
155            else
156                error('MATLAB:RWTHMindstormsNXT:InvalidPower',...
157                    'Power must be a numeric scalar in the range [-100, 100].');
158            end
159        end
160        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162        function obj = set.SpeedRegulation(obj, val)
163            if isscalar(val) && (isnumeric(val) || islogical(val))
164                obj.SpeedRegulation = logical(val);
165            else
166                error('MATLAB:RWTHMindstormsNXT:InvalidSpeedRegulation',...
167                    'SpeedRegulation must be a boolean.');
168            end
169        end
170        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172        function obj = set.TachoLimit(obj,val)
173            if isscalar(val) && isnumeric(val) && val >= 0
174                if any(rem(val,1) > 0)
175                    warning('MATLAB:RWTHMindstormsNXT:InvalidTachoLimit',...
176                        'TachoLimit is not an integer.');
177                end
178                obj.TachoLimit = fix(val);
179            else
180                error('MATLAB:RWTHMindstormsNXT:InvalidTachoLimit',...
181                    'TachoLimit must be a non-negative numeric scalar. Zero means run forever.');
182            end
183        end
184        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186        function obj = set.BrakeAtTachoLimit(obj, val)
187            if isscalar(val) && (isnumeric(val) || islogical(val))
188                obj.BrakeAtTachoLimit = logical(val);
189            else
190                error('MATLAB:RWTHMindstormsNXT:InvalidBrakeAtTachoLimit',...
191                    'BrakeAtTachoLimit must be a boolean.');
192            end
193        end
194        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195
196        function obj = set.TurnRatio(obj, val)
197            if isscalar(val) && isnumeric(val) && val >= -100 && val <= 100
198                if any(rem(val,1) > 0)
199                    warning('MATLAB:RWTHMindstormsNXT:InvalidTurnRatio',...
200                        'TurnRatio is not an integer.');
201                end
202                obj.TurnRatio = fix(val);
203            else
204                error('MATLAB:RWTHMindstormsNXT:InvalidTurnRatio',...
205                    'TurnRatio must be a numeric scalar in the range [-100, 100].');
206            end
207        end
208        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209       
210    end % END OF METHODS SECTION
211end % END OF CLASS DEFINITION
Note: See TracBrowser for help on using the browser.