Changeset 248

Show
Ignore:
Timestamp:
09/08/08 14:35:13 (5 years ago)
Author:
telle
Message:

Mostly bugfixes.

Location:
branches/telle/RWTHMindstormsNXT
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • branches/telle/RWTHMindstormsNXT

  • branches/telle/RWTHMindstormsNXT/@NXTmotor/read.m

    r246 r248  
    1414end 
    1515 
    16 data = NXT_GetOutputState(obj.port); 
    1716 
     17 
     18settings = NXT_GetOutputState(obj.port); 
     19 
     20data.Port = char(double('A') + settings.Port); 
     21data.Power = settings.Power; 
     22if settings.ModeIsMOTORON 
     23    data.MotorOn = 'on'; 
     24else 
     25    data.MotorOn = 'off'; 
     26end 
     27if settings.ModeIsBRAKE 
     28    data.Brake = 'on'; 
     29else 
     30    data.Brake = 'off'; 
     31end 
     32data.RegulationMode = lower(settings.RegModeName); 
     33data.TurnRatio = settings.TurnRatio; 
     34data.RunState = lower(settings.RunStateName); 
     35data.TachoLimit = settings.TachoLimit; 
     36data.TachoCount = settings.TachoCount; 
     37data.BlockTachoCount = settings.BlockTachoCount; 
  • branches/telle/RWTHMindstormsNXT/@NXTmotor/set.m

    r246 r248  
    194194 
    195195            case 'TachoLimit' 
    196                 if isscalar(val) && isnumeric(val) 
     196                if isscalar(val) && isnumeric(val) && val >= 0 
    197197                    obj.tachoLimit = val; 
    198198                else 
    199199                    error('MotorSettingError:InvalidTachoLimit',... 
    200                         'TachoLimit must be a numeric scalar. Zero means run forever.'); 
     200                        'TachoLimit must be a non-negative numeric scalar. Zero means run forever.'); 
    201201                end 
    202202            otherwise 
  • branches/telle/RWTHMindstormsNXT/@NXTmotor/stop.m

    r246 r248  
    2121end 
    2222 
    23 if (ischar(withBrake) && strcmpi(withBrake, 'brake')) ... 
    24         || (withBrake ~= 0) 
    25     withBrake = 1; 
     23if ischar(withBrake) 
     24    if strcmpi(withBrake, 'brake') || strcmpi(withBrake, 'on') 
     25        withBrake = 1; 
     26    elseif strcmpi(withBrake, 'nobrake') || strcmpi(withBrake, 'off') 
     27        withBrake = 0; 
     28    else 
     29        error('RWTHMINDSTORMS:NXTmotor:InvalidArgument',... 
     30            'Invalid second argument to function NXTmotor/send.'); 
     31    end 
     32elseif isnumeric(withBrake) 
     33    if (withBrake ~= 0) 
     34        withBrake = 1; 
     35    else 
     36        withBrake = 0; 
     37    end 
     38elseif islogical(withBrake) 
     39    if (withBrake == true) 
     40        withBrake = 1; 
     41    else 
     42        withBrake = 0; 
     43    end 
     44else 
     45    error('RWTHMINDSTORMS:NXTmotor:InvalidArgument',... 
     46        'Invalid second argument to function NXTmotor/send.'); 
    2647end 
    2748 
    28 NXT_SetOutputState(... 
    29     obj.port, ... % Port 
    30     0, ...           % Power 
    31     withBrake, ...   % IsMotorOn 
    32     1, ...           % IsBrake 
    33     'SPEED', ...     % RegModeName 
    34     0, ...           % TurnRatio 
    35     'RUNNING', ...   % RunStateName 
    36     0, ...           % TachoLimit 
    37     'dontreply'...   % ReplyMode 
    38     ); 
    39  
    40      
     49StopMotor(obj.port, withBrake); 
  • branches/telle/RWTHMindstormsNXT/@NXTmotorsettings/read.m

    r246 r248  
    1616end 
    1717 
    18 for k = 1:length(data.motors) 
     18for k = 1:length(obj.motors) 
    1919    settings(k) = read(obj.motors(k)); 
    2020end 
  • branches/telle/RWTHMindstormsNXT/@NXTmotorsettings/send.m

    r246 r248  
    1616end 
    1717 
     18if nargout == 1 
     19    out = zeros(size(obj.motors)); 
     20    for k = 1:length(obj.motors) 
     21        out(k) = send(obj.motors(k)); 
     22    end 
    1823 
    19 out = zeros(size(obj.motors)); 
    20 for k = 1:length(obj.motors) 
    21     out(k) = send(obj.motors(k)); 
     24    out = any(out); 
     25else 
     26    for k = 1:length(obj.motors) 
     27        send(obj.motors(k)); 
     28    end 
    2229end 
    23  
    24 out = any(out); 
     30     
  • branches/telle/RWTHMindstormsNXT/@NXTmotorsettings/stop.m

    r246 r248  
    2323end 
    2424 
    25 if ~iscell(withBrake) 
     25if ischar(withBrake) 
    2626    withBrake = {withBrake}; 
    2727end 
     
    3030    withBrake = repmat(withBrake, size(obj.motors)); 
    3131elseif numel(withBrake) ~= numel(obj.motors) 
    32     error('MotorSettingsError:InvalidParameterSize',... 
     32    error('RWTHMINDSTORMS:NXTmotorsettings:InvalidParameterSize',... 
    3333        'Number of elements must be 1 or the number of motors.'); 
    3434end 
    3535 
    36 for k = 1:length(obj.motors) 
    37     stop(obj.motors(k), withBrake{k}); 
     36if length(obj.motors) == 3  
     37    if withBrake(1) == withBrake(2) && withBrake(1) == withBrake(3) 
     38        if iscell(withBrake) 
     39            StopMotor('all', withBrake{1}); 
     40        else 
     41            StopMotor('all', withBrake(1)); 
     42        end 
     43        return 
     44    end 
    3845end 
    3946 
    40  
     47if iscell(withBrake) 
     48    for k = 1:length(obj.motors) 
     49        stop(obj.motors(k), withBrake{k}); 
     50    end 
     51else 
     52    for k = 1:length(obj.motors) 
     53        stop(obj.motors(k), withBrake(k)); 
     54    end 
     55end 
    4156     
  • branches/telle/RWTHMindstormsNXT/GetMotorSettings.m

    r201 r248  
    9797    out.SyncToMotor = -1; 
    9898else % SYNC then... :-) 
     99     
     100    % THIS IS LIKELY NOT TO BE CORRECT AS THE USER MAY HAVE MODIFIED 
     101    % THE MOTOR SETTINGS WITHOUT HAVING SENT THEM TO THE BRICK YET !!!! 
     102     
    99103    % shouldnt be necessary but whatever 
    100104    NXTMOTOR_State(whatmotor + 1).SyncedToSpeed = false; 
  • branches/telle/RWTHMindstormsNXT/SendMotorSettings.m

    r201 r248  
    9595 
    9696 
     97%% One argument of type NXTmotor or NXTmotorsettings 
     98% If there is exactly one argument of type NXTmotor or NXTmotorsettings 
     99% just call the send function for this object 
     100 
     101if nargin == 1 && ... 
     102        (isa(f_port,'NXTmotor') || isa(f_port, 'NXTmotorsettings')) 
     103    send(f_port); 
     104    return 
     105end 
     106 
     107 
    97108%% check given arguments 
    98109if (nargin ~= 0) && (nargin ~= 7) 
     
    102113          'Type "help SendMotorSettings" or see documentation!']); 
    103114end 
     115 
     116 
    104117 
    105118%% get handle & motorstate 
  • branches/telle/RWTHMindstormsNXT/StopMotor.m

    r201 r248  
    66% 
    77% Description 
    8 %   StopMotor(port, mode) stops the motor connected to the given port. The value port can be 
    9 %   addressed by the symbolic constants MOTOR_A, MOTOR_B, MOTOR_C and 'all' (all motor at at 
    10 %   the same time) analog to the labeling on the NXT Brick. The argument mode can be equal to 
    11 %   'off' which cuts off the electrical power to the specific motor, so called "COAST" mode. The 
    12 %   option 'brake' will actively halt the motor at the current position (until the next command). 
     8%   StopMotor(port, mode) stops the motor connected to the given port. The 
     9%   value port can be addressed by a string representing the label of the 
     10%   port on the brick ('A', 'B', or 'C') or the index of the port starting 
     11%   with 0, i.e., 0, 1, or 2 corresponding to A, B, and C respectively. The 
     12%   index may also be given as a string, e.g., '1'. If all motors shall be 
     13%   stopped the string 'all' can be used for the first argument.  
     14%    
     15%   The argument mode can take the following values: 
     16%      'nobrake', 'off', 0: The electrical power to the specified motor is 
     17%                           simply disconnected, the so-called "COAST" 
     18%                           mode. 
     19%      'brake',   'on',  1: This will actively halt the motor at the 
     20%                           current position (until the next movement 
     21%                           command).  
    1322%  
    1423% Note: 
    15 %   The value port equal to 'all' can be used to stopp all motors at the same time using only 
    16 %   one single Bluetooth packet. After a StopMotor command the motor snychronization will be lost. 
     24%   The value port equal to 'all' can be used to stopp all motors at the 
     25%   same time using only one single Bluetooth packet. After a StopMotor 
     26%   command the motor snychronization will be lost.  
    1727% 
    18 %   With mode equal to 'off', the motor will slowly stop spinning, but using 'brake' applies 
    19 %   real force to the motor to stand still at the current position, just like a real brake.  
     28%   With mode equal to 'off', the motor will slowly stop spinning, but 
     29%   using 'brake' applies real force to the motor to stand still at the 
     30%   current position, just like a real brake.   
    2031%    
    21 %   When calling StopMotor(..., 'off'), you also internally issue the commands 
    22 %   SpeedRegulation('off') and SyncToMotor('off'), while using  'brake' will call 
    23 %   SpeedRegulation('on') and SyncToMotor('off').  
     32%   When calling StopMotor(..., 'off'), internally the speed regulation  
     33%   and motor synchronization are turned off, while using 'brake' will onle 
     34%   turn off synchronization but enable speed regulation.  
    2435% 
    2536% Example 
    26 %   SetMotor(MOTOR_B); 
    27 %       SetPower(-55); 
    28 %       SetAngleLimit(1200); 
    29 %   SendMotorSettings(); 
     37%   m = NXTmotor('A'); 
     38%   m.Power = -55; 
     39%   m.TachoLimit = 1200; 
    3040% 
    31 %   WaitForMotor(MOTOR_A); 
     41%   m2 = NXTmotor('B'); 
     42%   m2.Power = 60; 
    3243% 
    33 %   StopMotor(MOTOR_B, 'brake'); 
     44%   send(m); 
     45%   send(m2); 
    3446% 
    35 % See also: WaitForMotor, SendMotorSettings, MOTOR_A, MOTOR_B, MOTOR_C 
     47%   wait(m); 
     48 
     49%   StopMotor('all', 'brake'); 
     50% 
     51% See also: NXTmotor, NXTmotorsettings 
    3652% 
    3753% Signature 
    38 %   Author: Linus Atorf, Alexander Behrens (see AUTHORS) 
    39 %   Date: 2007/10/15 
     54%   Author: Aulis Telle, Linus Atorf, Alexander Behrens (see AUTHORS) 
     55%   Date: 2008/09/02 
    4056%   Copyright: 2007-2008, RWTH Aachen University 
    4157% 
     
    6278        whatmotor = 255; 
    6379    else 
    64         whatmotor = str2double(whatmotor); 
     80        switch lower(whatmotor) 
     81            case {'a', '0'} 
     82                whatmotor = 0; 
     83            case {'b', '1'} 
     84                whatmotor = 1; 
     85            case {'c', '2'} 
     86                whatmotor = 2; 
     87            otherwise 
     88                whatmotor = -1; 
     89        end%switch 
    6590    end%if 
    6691end%if 
     92 
     93switch lower(brakemode) 
     94    case {'nobrake', 'off', 0, false} 
     95        brakemode = 'off'; 
     96    case {'brake', 'on', 1, true} 
     97        brakemode = 'on'; 
     98    otherwise 
     99        brakemode = 'off'; 
     100        warning('MATLAB:RWTHMindstormsNXT:StopMotor:invalidBrakeMode', 'Invalid brakemode. Brakemode is switched to ''off''.'); 
     101end%switch 
    67102 
    68103 
    69104%% Check Parameter 
    70105if (whatmotor < 0 || whatmotor > 2) && (whatmotor ~= 255) 
    71     error('MATLAB:RWTHMindstormsNXT:Motor:invalidPort', 'Input argument for motor port must be 0, 1 or 2 or ''all''.'); 
     106    error('MATLAB:RWTHMindstormsNXT:Motor:invalidPort', 'Input argument for motor port must be ''A'', ''B'', ''C'', ''0'', ''1'', ''2'', 0, 1, 2 or ''all''.'); 
    72107end%if 
    73108 
    74109 
    75 %% get default handle & motorstate 
    76 h = COM_GetDefaultNXT(); 
    77 NXTMOTOR_State = h.NXTMOTOR_getState(); 
     110%% Brake Mode 
     111if strcmpi(brakemode, 'on') 
    78112 
    79 %% reset regulation state 
    80 h.NXTMOTOR_resetRegulationState(whatmotor); 
     113    % set to power 0 with speed regulation 
     114    NXT_SetOutputState(whatmotor, ... 
     115        0, ...      % power 
     116        true, ...   % motoron 
     117        true, ...   % brake 
     118        'SPEED', ...% regulation 
     119        0, ...      % turnratio 
     120        'RUNNING', ... % runstate 
     121        0, ...      % tacho limit 
     122        'dontreply'); % replymode     
    81123 
    82124%% Turn Off Mode (Coast mode) 
    83 if strcmpi(brakemode, 'off') 
    84      
    85      
     125else 
     126 
    86127    % basically turn electric power to motor off, COAST mode 
    87128    NXT_SetOutputState(whatmotor, ... 
     
    95136        'dontreply'); % replymode 
    96137   
     138end%if 
    97139         
    98 %% Brake Mode 
    99 elseif strcmpi(brakemode, 'brake') 
    100      
    101      
    102     % set speed regulation mode 
    103     if whatmotor == 255 
    104         NXTMOTOR_State(1).SyncedToSpeed = true; 
    105         NXTMOTOR_State(2).SyncedToSpeed = true; 
    106         NXTMOTOR_State(3).SyncedToSpeed = true; 
    107     else 
    108         NXTMOTOR_State(whatmotor+1).SyncedToSpeed = true; 
    109     end%if 
    110      
    111     % set to power 0 with speed regulation 
    112     NXT_SetOutputState(whatmotor, ... 
    113         0, ...      % power 
    114         true, ...   % motoron 
    115         true, ...   % brake 
    116         'SPEED', ...% regulation 
    117         0, ...      % turnratio 
    118         'RUNNING', ... % runstate 
    119         0, ...      % tacho limit 
    120         'dontreply'); % replymode     
    121  
    122  
    123 %% Error     
    124 else 
    125     error('MATLAB:RWTHMindstormsNXT:Motor:invalidBrakeMode', ... 
    126          ['Input argument for brake mode must either be ''off'' (motor will be turned off) ' ... 
    127           'or ''brake'' (motor will be actively halted at speed 0)']);  
    128 end%if 
    129  
    130  
    131 %% save motor state back to handle 
    132 h.NXTMOTOR_setState(NXTMOTOR_State); 
    133  
    134  
    135  
    136140end%function 
    137141