Show
Ignore:
Timestamp:
08/29/08 16:28:25 (5 years ago)
Author:
telle
Message:

Create version without the use of function closures. This is necessary to make
copying motor objects more intuitive. With function closures a motor object was
like a handle, so that 'set' worked like it works with, e.g., matlab figure
handles. As a result, copying a motor object makes no independent copy. If a
property was set on the copy it was also set on the original. I think this is not
very intuitive as it does not act like a struct.

In this version also synchronization of the properties of synced motors is forced

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/telle/RWTHMindstormsNXT/@NXTmotor/NXTmotor.m

    r221 r246  
    3131% Author: Aulis Telle, IND, RWTH Aachen 
    3232 
    33 data = []; 
    3433 
    3534if nargin > 0 && isa(varargin{1}, 'NXTmotor') 
    36     tmp = varargin{1}; 
    37     data = tmp.data(); 
     35    obj = varargin{1}; 
    3836else 
    3937    port = 0; 
     
    6058    end 
    6159 
    62     data.port           = port; 
    63     data.power          = 0; 
    64     data.mode.motorOn   = 1; 
    65     data.mode.brake     = 1; 
    66     data.regulationMode = 'IDLE'; 
    67     data.turnRatio      = 0; 
    68     data.runState       = 'RUNNING'; 
    69     data.tachoLimit     = 0; 
     60    obj.port           = port; 
     61    obj.power          = 0; 
     62    obj.motorOn        = 1; 
     63    obj.brake          = 1; 
     64    obj.regulationMode = 'IDLE'; 
     65    obj.turnRatio      = 0; 
     66    obj.runState       = 'RUNNING'; 
     67    obj.tachoLimit     = 0; 
    7068end 
    7169 
    72  
    73 obj.data = @dataClosure; 
    7470 
    7571obj = class(obj, 'NXTmotor'); 
    7672 
    7773if nargin > 1 
    78     set(obj, varargin{2:end}); 
     74    obj = set(obj, varargin{2:end}); 
    7975end 
    80  
    81 % This is the nested function for the function closure. The variable 
    82 % "data", which is in the scope of the outer function, can be accessed from 
    83 % within the nested function. A handel to this function is returned to the 
    84 % caller of the outer function. This makes the variable "data" persistent 
    85 % and from now on it can be accessed from within the nested function, even 
    86 % when it is called from another scope. 
    87 function val = dataClosure(varargin) 
    88  
    89     % if there are two arguments, this indicates, that a property field 
    90     % shall be set. varargin{1} is the field name and varargin{2} the  
    91     % value, to which it should be set. 
    92     if nargin == 2 
    93         switch varargin{1} 
    94             case {'port'} 
    95                 data.port = varargin{2}; 
    96                  
    97             case {'power'} 
    98                 data.power = varargin{2}; 
    99                  
    100             case {'motorOn'} 
    101                 data.mode.motorOn = varargin{2}; 
    102      
    103             case {'brake'} 
    104                 data.mode.brake = varargin{2}; 
    105      
    106             case {'regulationMode'} 
    107                 data.regulationMode = varargin{2}; 
    108      
    109             case {'turnRatio'} 
    110                 data.turnRatio = varargin{2}; 
    111      
    112             case {'runState'} 
    113                 data.runState = varargin{2}; 
    114      
    115             case {'tachoLimit'} 
    116                 data.tachoLimit = varargin{2}; 
    117         end 
    118     end 
    119     val = data; 
    120 end 
    121 % END OF NESTED FUNCTION dataClosure() 
    12276 
    12377end