| [267] | 1 | function [ 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 | |
|---|
| 32 | % Signature |
|---|
| 33 | % Author: Aulis Telle (see AUTHORS) |
|---|
| 34 | % Date: 2008/08/15 |
|---|
| 35 | % Copyright: 2007-2008, RWTH Aachen University |
|---|
| 36 | % |
|---|
| 37 | % |
|---|
| 38 | % *********************************************************************************************** |
|---|
| 39 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 40 | % * * |
|---|
| 41 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 42 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 43 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 44 | % * * |
|---|
| 45 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 46 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 47 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 48 | % * * |
|---|
| 49 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 50 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 51 | % *********************************************************************************************** |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | if nargin > 0 && isa(varargin{1}, 'NXTmotor') |
|---|
| 55 | obj = varargin{1}; |
|---|
| 56 | else |
|---|
| 57 | % port = 0; |
|---|
| 58 | % if nargin > 0 |
|---|
| 59 | % val = varargin{1}; |
|---|
| 60 | % if numel(val) <= 2 |
|---|
| 61 | % if ischar(val) |
|---|
| 62 | % val = double(val) - 65; |
|---|
| 63 | % end |
|---|
| 64 | % if any(val < 0) || any (val > 2) |
|---|
| 65 | % error('MotorSettingError:InvalidPort',... |
|---|
| 66 | % 'A port may be one of ''A'', ''B'', ''C'' of 0, 1, 2 respectively.'); |
|---|
| 67 | % end |
|---|
| 68 | % if any(diff(val)) < 0 |
|---|
| 69 | % error('MotorSettingError:InvalidPort',... |
|---|
| 70 | % 'Multiple ports must be given in ascending order.'); |
|---|
| 71 | % end |
|---|
| 72 | % port = val; |
|---|
| 73 | % else |
|---|
| 74 | % error('MotorSettingError:InvalidPort',... |
|---|
| 75 | % 'No more than two ports may be specified.'); |
|---|
| 76 | % end |
|---|
| 77 | % end |
|---|
| 78 | % |
|---|
| [272] | 79 | obj.Port = 0; |
|---|
| 80 | obj.Power = 0; |
|---|
| 81 | obj.MotorOn = 1; |
|---|
| 82 | obj.Brake = 1; |
|---|
| 83 | obj.RegulationMode = 'SPEED'; |
|---|
| 84 | obj.TurnRatio = 0; |
|---|
| 85 | obj.RunState = 'RUNNING'; |
|---|
| 86 | obj.TachoLimit = 0; |
|---|
| [267] | 87 | end |
|---|
| 88 | |
|---|
| 89 | if ~isa(obj, 'NXTmotor') |
|---|
| 90 | obj = class(obj, 'NXTmotor'); |
|---|
| 91 | |
|---|
| 92 | % Set port (this makes checking port argument unnecessary) |
|---|
| 93 | obj = set(obj, 'Port', varargin{1}); |
|---|
| 94 | end |
|---|
| 95 | |
|---|
| 96 | if nargin > 1 |
|---|
| 97 | obj = set(obj, varargin{2:end}); |
|---|
| 98 | end |
|---|
| 99 | |
|---|
| 100 | end |
|---|
| 101 | % END OF MAIN FUNCTION |
|---|