root/branches/telle/RWTHMindstormsNXT/@NXTmotor/NXTmotor.m @ 246

Revision 246, 2.4 KB (checked in by telle, 5 years ago)

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

Line 
1function [ obj ] = NXTmotor( varargin )
2% NXTMOTOR Construct NXTmotor object
3%
4%     M = NXTMOTOR(PORT) constructs a NXTmotor object with motor port PORT and
5%     default attributes. PORT may be either the port number (0, 1, 2 or
6%     MOTOR_A, MOTOR_B, MOTOR_C) or a  string specifying the port ('A',
7%     'B', 'C'). If PORT is omitted, the port defaults to 'A'.
8%
9%     M = NXTMOTOR(PORT, 'PropName1', PropValue1, 'PropName2', PropValue2, ...)
10%     constructs a NXTmotor object with motor port PORT in which the given
11%     Property nam/value pairs are set on the object. All properties can
12%     also be set after creation by dot-notation or the set function (see
13%     example).
14%
15%     Note that the property value pairs can be in any format supported by
16%     the SET function.
17%   
18%     Example:
19%       % Construct a NXTmotor object on port 'B' with a power of 60,
20%       % switch on the brake, set a TachoLimit of 360 and send the
21%       % motor settings to the NXT brick.
22%       mb = NXTmotor('B', 'Power', 60)
23%       mb.Brake = 'on';
24%       set(mb, 'TachoLimit', 360);
25%       send(mb);
26%
27%     See also NXTMOTOR/SET, NXTMOTOR/GET, NXTMOTOR/WAIT, NXTMOTOR/STOP,
28%     NXTMOTOR/READ, NXTMOTOR/SEND, NXTMOTOR/RESET.
29%
30
31% Author: Aulis Telle, IND, RWTH Aachen
32
33
34if nargin > 0 && isa(varargin{1}, 'NXTmotor')
35    obj = varargin{1};
36else
37    port = 0;
38    if nargin > 0
39        val = varargin{1};
40        if isscalar(val) && isnumeric(val) && val >= 0 && val <= 2
41            port = val;
42        elseif ischar(val)
43            switch lower(val)
44                case 'a'
45                    port = 0;
46                case 'b'
47                    port = 1;
48                case 'c'
49                    port = 2;
50                otherwise
51                    error('MotorSettingError:InvalidPort',...
52                        'Port may be one of ''A'', ''B'', or ''C''.');
53            end
54        else
55            error('MotorSettingError:InvalidPort',...
56                'Port may be one of ''A'', ''B'', or ''C''.');
57        end
58    end
59
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;
68end
69
70
71obj = class(obj, 'NXTmotor');
72
73if nargin > 1
74    obj = set(obj, varargin{2:end});
75end
76
77end
78% END OF MAIN FUNCTION
Note: See TracBrowser for help on using the browser.