root/branches/atorf/RWTHMindstormsNXT-Lejos-Combined/@NXTmotor/set.m @ 272

Revision 272, 10.6 KB (checked in by atorf, 5 years ago)

Added changes from /branches/telle/RWTHMindstormsNXT-Lejos to current testing-branch

Line 
1function obj = set( obj, varargin )
2% SET Configure or display NXTmotor object properties.
3%
4%     SET(OBJ) displays property names and their possible values for all
5%     configurable properties of NXTmotor object OBJ. OBJ must be a single
6%     NXTmotor object.
7%
8%     SET(OBJ, 'PropertyName', PropertyValue, ...) configures the
9%     porperty, PropertyName, to the specified value, PropertyValue, for
10%     NXTmotor object OBJ. You can specify multiple property name/property
11%     value pairs in a singel statement. OBJ must be a single NXTmotor
12%     object.
13%   
14%     You can set properties also by using dot notation.
15%
16%     Examples:
17%         mb = NXTmotor('B');
18%         set(mb) % Display all configurable properties and their possible
19%                 % values
20%         set(mb, 'Power', 80);
21%         mb.RegulationMode = 'speed';
22%
23%     See also NXTmotor/get.
24%
25
26% Signature
27%   Author: Aulis Telle (see AUTHORS)
28%   Date: 2008/08/15
29%   Copyright: 2007-2008, RWTH Aachen University
30%
31%
32% ***********************************************************************************************
33% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
34% *                                                                                             *
35% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
36% *  it under the terms of the GNU General Public License as published by the Free Software     *
37% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
38% *                                                                                             *
39% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
40% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
41% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
42% *                                                                                             *
43% *  You should have received a copy of the GNU General Public License along with the           *
44% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
45% ***********************************************************************************************
46
47
48if ~isa(obj,'NXTmotor')
49    error('MATLAB:RWTHMindstormsNXT:InvalidObject',...
50        'No NXTmotor object.');
51end
52
53if nargout == 0
54    error('MATLAB:RWTHMindstormsNXT:InvalidUsage',...
55        'No output argument supplied to set.');
56end
57
58if numel(obj) > 1
59    error('MATLAB:RWTHMindstormsNXT:InputNotScalar',...
60        'Set method supports only scalar objects.');
61end
62
63if (nargin == 1) % e.g., 'set(OBJ)'
64    fprintf('              Port: [ ''A'' | ''B'' | ''C''] -or- integer in the range [0, 2]\n');
65    fprintf('             Power: integer in the range [-100, 100]\n');
66    fprintf('           MotorOn: [ ''off'' | {''on''} ] \n');
67    fprintf('             Brake: [ {''off''} | ''on'' ] \n');
68    fprintf('    RegulationMode: [ ''idle'' | {''speed''} ]\n');
69    fprintf('         TurnRatio: integer in the rage [-100, 100].\n');
70    fprintf('          RunState: [ ''idle'' | {''running''} | ''rampup'' | ''rampdown'']\n');
71    fprintf('        TachoLimit: positive integer, 0 means no limit.\n');
72else 
73    propertyArgIn = varargin;
74    while length(propertyArgIn) >= 2
75        prop = propertyArgIn{1};
76        val  = propertyArgIn{2};
77        if length(propertyArgIn) >= 3
78            propertyArgIn = propertyArgIn(3:end);
79        else
80            propertyArgIn = {};
81        end
82        switch prop
83            case 'Port'
84                if numel(val) <= 2
85                    % Convert to numeric representation (65 == 'A')
86                    if ischar(val)
87                        val = double(upper(val)) - 65;
88                    end
89                   
90                    % Check, if any port is out of the alt range
91                    if any(val < 0) || any (val > 2)
92                        error('MATLAB:RWTHMindstormsNXT:InvalidPort',...
93                            'One or more of the given ports are invalid.');
94                    end
95                   
96                    % Check, if ports are given in ascending order by
97                    % taking the difference of successive values. If any
98                    % of them is lower zero they are not in ascending order
99                    if any(diff(val)) < 0
100                        error('MATLAB:RWTHMindstormsNXT:InvalidPort',...
101                            'Multiple ports must be given in ascending order.');
102                    end
103                   
104                    % check for non-integer values
105                    if any(rem(val,1) > 0)
106                        warning('MATLAB:RWTHMindstormsNXT:InvalidPort',...
107                            'Port is not an integer or port label.');
108                    end
109                   
110                    obj.Port = fix(val);
111                else
112                    error('MATLAB:RWTHMindstormsNXT:InvalidPort',...
113                        'No more than two ports may be specified.');
114                end
115       
116            case 'Power'
117                % Power must be a scalar, be numerir and be in the range
118                % [-100 100]
119                if isscalar(val) && isnumeric(val) && val >= -100 && val <= 100
120                    if any(rem(val,1) > 0)
121                        warning('MATLAB:RWTHMindstormsNXT:InvalidPort',...
122                            'Power is not an integer.');
123                    end
124                    obj.Power = fix(val);
125                else
126                    error('MATLAB:RWTHMindstormsNXT:InvalidPower',...
127                        'Power must be a numeric scalar in the range [-100, 100].');
128                end
129     
130            case 'MotorOn'
131                if isscalar(val) && isnumeric(val) && (val == 0 || val == 1)
132                    obj.MotorOn = val;
133                elseif islogical(val)
134                    if val
135                        obj.MotorOn = 1;
136                    else
137                        obj.MotorOn = 0;
138                    end
139                elseif ischar(val)
140                    if strcmpi(val,'on')
141                        obj.MotorOn = 1;
142                    elseif strcmpi(val,'off')
143                        obj.MotorOn = 0;
144                    else
145                        error('MATLAB:RWTHMindstormsNXT:InvalidMotorOn',...
146                            'MotorOn may be ''on'' or ''off''.');
147                    end
148                else
149                    error('MATLAB:RWTHMindstormsNXT:InvalidMotorOn',...
150                        'MotorOn may be ''on'' or ''off''.');
151                end
152
153            case 'Brake'
154                if isscalar(val) && isnumeric(val) && (val == 0 || val == 1)
155                    obj.Brake = val;
156                elseif islogical(val)
157                    if val
158                        obj.Brake = 1;
159                    else
160                        obj.Brake = 0;
161                    end
162                elseif ischar(val)
163                    if strcmpi(val,'on')
164                        obj.Brake = 1;
165                    elseif strcmpi(val,'off')
166                        obj.Brake = 0;
167                    else
168                        error('MATLAB:RWTHMindstormsNXT:InvalidBrake',...
169                            'Break may be ''on'' or ''off''.');
170                    end
171                else
172                    error('MATLAB:RWTHMindstormsNXT:InvalidBrake',...
173                        'Break may be ''on'' or ''off''.');
174                end
175
176            case 'RegulationMode'
177                if ischar(val)
178                    switch lower(val)
179                        case 'idle'
180                            obj.RegulationMode = 'IDLE';
181                        case 'speed'
182                            obj.RegulationMode = 'SPEED';
183                        otherwise
184                            error('MATLAB:RWTHMindstormsNXT:InvalidRegulationMode',...
185                                'RegulationMode may be ''idle'' or ''speed''.');
186                    end
187                else
188                    error('MATLAB:RWTHMindstormsNXT:InvalidRegulationMode',...
189                          'RegulationMode may be ''idle'' or ''speed''.');
190                end
191
192            case 'TurnRatio'
193                if isscalar(val) && isnumeric(val) && val >= -100 && val <= 100
194                    if any(rem(val,1) > 0)
195                        warning('MATLAB:RWTHMindstormsNXT:InvalidTurnRatio',...
196                            'TurnRatio is not an integer.');
197                    end
198                    obj.TurnRatio = fix(val);
199                else
200                    error('MATLAB:RWTHMindstormsNXT:InvalidTurnRatio',...
201                        'TurnRatio must be a numeric scalar in the range [-100, 100].');
202                end
203
204            case 'RunState'
205                if ischar(val)
206                    switch lower(val)
207                        case 'idle'
208                            obj.RunState = 'IDLE';
209% Not supported at the moment                           
210%                         case 'rampup'
211%                             obj.runState = 'RAMPUP';
212                        case 'running'
213                            obj.RunState = 'RUNNING';
214% Not supported at the moment                           
215%                         case 'rampdown'
216%                             obj.runState = 'RAMPDOWN';
217                        otherwise
218                            error('MATLAB:RWTHMindstormsNXT:InvalidRunState',...
219                                'RunState may be ''idle'' or ''running''.');
220%                                'RunState may be ''idle'', ''rampup'', ''running'', or ''rampdown''.');
221                    end
222                else
223                    error('MATLAB:RWTHMindstormsNXT:InvalidRunState',...
224                        'RunState may be ''idle'' or ''running''.');
225                end
226
227            case 'TachoLimit'
228                if isscalar(val) && isnumeric(val) && val >= 0
229                    if any(rem(val,1) > 0)
230                        warning('MATLAB:RWTHMindstormsNXT:InvalidTachoLimit',...
231                            'TachoLimit is not an integer.');
232                    end
233                    obj.TachoLimit = fix(val);
234                else
235                    error('MATLAB:RWTHMindstormsNXT:InvalidTachoLimit',...
236                        'TachoLimit must be a non-negative numeric scalar. Zero means run forever.');
237                end
238            otherwise
239                error('MATLAB:RWTHMindstormsNXT:UnsupportedParameter',...
240                    'Unsupported parameter %s',varargin{argIterator});
241        end
242    end
243end
244
245if (nargout == 0)
246    clear obj;
247end
Note: See TracBrowser for help on using the browser.