root/branches/telle/RWTHMindstormsNXT/@NXTmotor/set.m @ 218

Revision 218, 7.9 KB (checked in by telle, 5 years ago)

First attempt of new class-based motor control.

Line 
1function 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% Author: Aulis Telle, IND, RWTH Aachen
27
28if ~isa(obj,'NXTmotor')
29    error('RWTHMINDSTORMS:NXTmotor:InvalidObject', 'No NXTmotor object.');
30end
31
32if numel(obj) > 1
33    error('RWTHMINDSTORMS:NXTmotor:inputNotScalar',...
34        'Set method supports only scalar objects.');
35end
36
37if (nargin == 1) % e.g., 'set(OBJ)'
38    fprintf('              Port: [ ''A'' | ''B'' | ''C''] -or- integer in the range [0, 2]\n');
39    fprintf('             Power: integer in the range [-100, 100]\n');
40    fprintf('           MotorOn: [ ''off'' | {''on''} ] \n');
41    fprintf('             Brake: [ {''off''} | ''on'' ] \n');
42    fprintf('    RegulationMode: [ {''idle''} | ''speed'' | ''sync'' ]\n');
43    fprintf('         TurnRatio: integer in the rage [-100, 100].\n');
44    fprintf('          RunState: [ ''idle'' | {''running''} | ''rampup'' | ''rampdown'']\n');
45    fprintf('        TachoLimit: positive integer, 0 means no limit.\n');
46else 
47    propertyArgIn = varargin;
48    while length(propertyArgIn) >= 2
49        prop = propertyArgIn{1};
50        val  = propertyArgIn{2};
51        if length(propertyArgIn) >= 3
52            propertyArgIn = propertyArgIn(3:end);
53        else
54            propertyArgIn = {};
55        end
56        switch prop
57            case 'Port'
58                if isscalar(val) && isnumeric(val) && val >= 0 && val <= 2
59                    obj.port = val;
60                elseif ischar(val)
61                    val = lower(val);
62                    switch val
63                        case 'a'
64                            obj.data('port', 0);
65                        case 'b'
66                            obj.data('port', 1);
67                        case 'c'
68                            obj.closure('port', 2);
69                        otherwise
70                            error('MotorSettingError:InvalidPort',...
71                                'Port may be one of ''A'', ''B'', or ''C''.');
72                    end
73                else
74                    error('MotorSettingError:InvalidPort',...
75                        'Port number must be MOTOR_A, MOTOR_B, or MOTOR_C.');
76                end
77     
78            case 'Power'
79                if isscalar(val) && isnumeric(val) && val >= -100 && val <= 100
80                    obj.data('power',val);
81                else
82                    error('MotorSettingError:InvalidPower',...
83                        'Power must be a numeric scalar in the range [-100, 100].');
84                end
85     
86            case 'MotorOn'
87                if isscalar(val) && isnumeric(val) && (val == 0 || val == 1)
88                    obj.data('motorOn', val);
89                elseif islogical(val)
90                    if val
91                        obj.data('motorOn', 1);
92                    else
93                        obj.data('motorOn', 0);
94                    end
95                elseif ischar(val)
96                    if strcmpi(val,'on')
97                        obj.data('motorOn', 1);
98                    elseif strcmpi(val,'off')
99                        obj.data('motorOn', 0);
100                    else
101                        error('MototSettingError:InvalidMotorOn',...
102                            'MotorOn may be ''on'' or ''off''.');
103                    end
104                else
105                    error('MototSettingError:InvalidMotorOn',...
106                        'MotorOn may be ''on'' or ''off''.');
107                end
108
109            case 'Brake'
110                if isscalar(val) && isnumeric(val) && (val == 0 || val == 1)
111                    obj.data('brake', val);
112                elseif islogical(val)
113                    if val
114                        obj.data('brake', 1);
115                    else
116                        obj.data('brake', 0);
117                    end
118                elseif ischar(val)
119                    if strcmpi(val,'on')
120                        obj.data('brake', 1);
121                    elseif strcmpi(val,'off')
122                        obj.data('brake', 0);
123                    else
124                        error('MototSettingError:InvalidBreak',...
125                            'Break may be ''on'' or ''off''.');
126                    end
127                else
128                    error('MototSettingError:InvalidBreak',...
129                        'Break may be ''on'' or ''off''.');
130                end
131                %         case 'Regulated'
132                %             if ischar(val)
133                %                 if strcmpi(val,'on')
134                %                     obj.mode.regulated = 1;
135                %                 elseif strcmpi(val,'off')
136                %                     obj.mode.regulated = 0;
137                %                 else
138                %                     error('MototSettingError:InvalidRegulated', ...
139                %                         'MotorOn may be ''on'' or ''off''.');
140                %                 end
141                %             end
142            case 'RegulationMode'
143                if ischar(val)
144                    switch lower(val)
145                        case 'idle'
146                            obj.data('regulationMode', 'IDLE');
147                        case 'speed'
148                            obj.data('regulationMode', 'SPEED');
149                        case 'sync'
150                            obj.data('regulationMode', 'SYNC');
151                        case 'speedsync'
152                            obj.data('regulationMode', 'SPEEDSYNC');
153                        otherwise
154                            error('MototSettingError:InvalidRegulationMode', ...
155                                'RegulationMode may be ''idle'', ''speed'', or ''sync''.');
156                    end
157                end
158
159            case 'TurnRatio'
160                if isscalar(val) && isnumeric(val) && val >= -100 && val <= 100
161                    obj.data('turnRatio', val);
162                else
163                    error('MotorSettingError:InvalidTurnRatio',...
164                        'TurnRatio must be a numeric scalar in the range [-100, 100].');
165                end
166
167            case 'RunState'
168                if ischar(val)
169                    switch lower(val)
170                        case 'idle'
171                            obj.data('runState', 'IDLE');
172                        case 'rampup'
173                            obj.data('runState', 'RAMPUP');
174                        case 'running'
175                            obj.data('runState', 'RUNNING');
176                        case 'rampdown'
177                            obj.data('runState', 'RAMPDOWN');
178                        otherwise
179                            error('MototSettingError:InvalidRunState', ...
180                                'RunState may be ''idle'', ''rampup'', ''running'', or ''rampdown''.');
181                    end
182                end
183
184            case 'TachoLimit'
185                if isscalar(val) && isnumeric(val)
186                    obj.data('tachoLimit', val);
187                else
188                    error('MotorSettingError:InvalidTachoLimit',...
189                        'TachoLimit must be a numeric scalar. Zero means run forever.');
190                end
191            otherwise
192                disp(sprintf('Unsupported parameter %s',varargin{argIterator}));
193        end
194    end
195end
Note: See TracBrowser for help on using the browser.