| 1 | function SendMotorSettings(f_port, f_power, f_angle, f_speed, f_sync, f_ratio, f_ramp) |
|---|
| 2 | % Sends previously specified settings to the currently active motor. |
|---|
| 3 | % |
|---|
| 4 | % Syntax |
|---|
| 5 | % |SendMotorSettings()| |
|---|
| 6 | % |
|---|
| 7 | % |SendMotorSettings(port, power, angle, speedRegulation, syncedToMotor, turnRatio, rampMode)| |
|---|
| 8 | % |
|---|
| 9 | % Description |
|---|
| 10 | % |SendMotorSettings()| sends the previously specified settings of the current motor. The motor |
|---|
| 11 | % settings are set by the functions |SetMotor|, |SetPower|, |SetAngleLimit|, |SpeedRegulation|, |
|---|
| 12 | % |SyncToMotor|, |SetTurnRatio| and |SetRampMode|. |
|---|
| 13 | % |
|---|
| 14 | % |SendMotorSettings(port, power, angle, speedRegulation, syncedToMotor, turnRatio, rampMode)| |
|---|
| 15 | % sends the given settings like motor |port| (|MOTOR_A|, |MOTOR_B| or |MOTOR_C|), the |power| |
|---|
| 16 | % (|-100...100|, the |angle| limit, |speedRegulation| (|'on'|, |'off'|), |syncedToMotor| |
|---|
| 17 | % (|MOTOR_A|, |MOTOR_B|, |MOTOR_C|), |turnRatio| (|-100...100|) and |rampMode| (|'off'|, |'up'|, |
|---|
| 18 | % |'down'|). |
|---|
| 19 | % |
|---|
| 20 | % Note: |
|---|
| 21 | % All settings like power, angle limit, and so on, will only take effect once you send them to the |
|---|
| 22 | % motor using THIS function. Note that if you have synced two motors together this function |
|---|
| 23 | % internally sends two packets to both the motors. This is required, but you can work as if this |
|---|
| 24 | % was just one command. |
|---|
| 25 | % |
|---|
| 26 | % Example |
|---|
| 27 | %+ SetMotor(MOTOR_B); |
|---|
| 28 | %+ SyncToMotor(MOTOR_C); |
|---|
| 29 | %+ SetPower(76); |
|---|
| 30 | %+ SetAngleLimit(4*360); |
|---|
| 31 | %+ SetTurnRatio(20); |
|---|
| 32 | %+ SendMotorSettings(); |
|---|
| 33 | % |
|---|
| 34 | % See also: GetMotorSettings, SetMotor, SetPower, SpeedRegulation, SyncToMotor, SetTurnRatio, SetRampMode, NXT_SetOutputState, MOTOR_A, MOTOR_B, MOTOR_C |
|---|
| 35 | % |
|---|
| 36 | % Signature |
|---|
| 37 | % Author: Linus Atorf, Alexander Behrens (see AUTHORS) |
|---|
| 38 | % Date: 2007/10/15 |
|---|
| 39 | % Copyright: 2007, RWTH Aachen University |
|---|
| 40 | % |
|---|
| 41 | ; |
|---|
| 42 | % |
|---|
| 43 | % *********************************************************************************************** |
|---|
| 44 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 45 | % * * |
|---|
| 46 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 47 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 48 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 49 | % * * |
|---|
| 50 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 51 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 52 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 53 | % * * |
|---|
| 54 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 55 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 56 | % *********************************************************************************************** |
|---|
| 57 | |
|---|
| 58 | %% check given arguments |
|---|
| 59 | if (nargin ~= 0) && (nargin ~= 7) |
|---|
| 60 | help SendMotorSettings; |
|---|
| 61 | error('MATLAB:RWTHMindstormsNXT:invalidParameterCount', ... |
|---|
| 62 | ['Either none or 7 function arguments are needed. ' ... |
|---|
| 63 | 'Type "help SendMotorSettings" or see documentation!']); |
|---|
| 64 | end |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | global NXTMOTOR_State; |
|---|
| 68 | |
|---|
| 69 | if isempty(NXTMOTOR_State) |
|---|
| 70 | initializeGlobalMotorStateVar; |
|---|
| 71 | end%if |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | %% set parameter for function call with arguments |
|---|
| 75 | if nargin > 0 |
|---|
| 76 | |
|---|
| 77 | if ~strcmpi(f_sync, 'off') && ~strcmpi(f_speed, 'off') |
|---|
| 78 | error('MATLAB:RWTHMindstormsNXT:Motor:simultaneousSyncAndSpeedRegulationError', 'You can only use motor synchronization OR speed regulation at a time, but not both settings together.') |
|---|
| 79 | end%if |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | % don't influence with MotorSet, if different was set |
|---|
| 83 | try |
|---|
| 84 | oldmotor = GetMotor; |
|---|
| 85 | catch |
|---|
| 86 | oldmotor = NaN; |
|---|
| 87 | end%try |
|---|
| 88 | |
|---|
| 89 | %TODO In this short version of SendMotorSettings, we use SyncToMotor |
|---|
| 90 | % and SpeedRegulation consecutively. Even if the parameters are correct |
|---|
| 91 | % (meaning that motor sync and speed reg are mutually exclusive), we |
|---|
| 92 | % produce a warning at this point, if e.g. speedreg was activated |
|---|
| 93 | % before and should now be deactivated while using sync. In this |
|---|
| 94 | % combination of long and short versions of SendMotorSettings, this |
|---|
| 95 | % warning produced by SpeedRegulation() is wrong. We have to avoid it |
|---|
| 96 | % at this point by adding a SpeedRegulation('off'); before |
|---|
| 97 | % SyncToMotor in the following command sequence, or by adding a little |
|---|
| 98 | % if statement, depending on performance and complications... |
|---|
| 99 | |
|---|
| 100 | SetMotor(f_port); |
|---|
| 101 | SyncToMotor(f_sync); |
|---|
| 102 | SetPower(f_power); |
|---|
| 103 | SetAngleLimit(f_angle); |
|---|
| 104 | SpeedRegulation(f_speed); |
|---|
| 105 | SetTurnRatio(f_ratio); |
|---|
| 106 | SetRampMode(f_ramp); |
|---|
| 107 | |
|---|
| 108 | % for restore see later down |
|---|
| 109 | end |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | %% regular command sequence |
|---|
| 113 | whatmotor = GetMotor; |
|---|
| 114 | |
|---|
| 115 | %function status = NXT_SetOutputState(OutputPort, Power, IsMotorOn, IsBrake, RegModeName, TurnRatio, RunStateName, TachoLimit, ReplyMode, varargin) |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | %% only use turnratio if motor is synced, ignore if all motors set... |
|---|
| 119 | if whatmotor ~= 255 |
|---|
| 120 | TurnRatio = 0; % only has affect if regmode is SYNC |
|---|
| 121 | syncedmotor = -1; |
|---|
| 122 | if NXTMOTOR_State(whatmotor + 1).SyncedToSpeed |
|---|
| 123 | RegModeName = 'SPEED'; |
|---|
| 124 | elseif NXTMOTOR_State(whatmotor + 1).SyncedToMotor ~= -1 |
|---|
| 125 | RegModeName = 'SYNC'; |
|---|
| 126 | TurnRatio = NXTMOTOR_State(whatmotor + 1).TurnRatio; |
|---|
| 127 | syncedmotor = NXTMOTOR_State(whatmotor + 1).SyncedToMotor; |
|---|
| 128 | else |
|---|
| 129 | RegModeName = 'IDLE'; |
|---|
| 130 | end%if |
|---|
| 131 | end%if |
|---|
| 132 | |
|---|
| 133 | %% if synced.... |
|---|
| 134 | if whatmotor ~= 255 |
|---|
| 135 | if syncedmotor ~= -1 |
|---|
| 136 | % send other packet as well |
|---|
| 137 | NXT_SetOutputState(syncedmotor, ... % port |
|---|
| 138 | NXTMOTOR_State(syncedmotor + 1).Power, ... % power |
|---|
| 139 | true, ... % motoron |
|---|
| 140 | ~NXTMOTOR_State(syncedmotor + 1).BrakeDisabled, ... % brake |
|---|
| 141 | RegModeName, ... % reg mode |
|---|
| 142 | NXTMOTOR_State(syncedmotor + 1).TurnRatio, ... % turn ratio |
|---|
| 143 | NXTMOTOR_State(syncedmotor + 1).RunStateName, ... % runstate |
|---|
| 144 | NXTMOTOR_State(syncedmotor + 1).AngleLimit, ... |
|---|
| 145 | 'dontreply'); |
|---|
| 146 | |
|---|
| 147 | if (NXTMOTOR_State(syncedmotor+1).Power ~= 0) |
|---|
| 148 | % set memory counter |
|---|
| 149 | SetMemoryCount(syncedmotor, GetMemoryCount(syncedmotor) + ... |
|---|
| 150 | sign(NXTMOTOR_State(syncedmotor+1).Power) * NXTMOTOR_State(syncedmotor+1).AngleLimit); |
|---|
| 151 | end |
|---|
| 152 | if (NXTMOTOR_State(syncedmotor + 1).AngleLimit == 0) |
|---|
| 153 | SetMemoryCount(syncedmotor, 0); |
|---|
| 154 | end |
|---|
| 155 | end%if |
|---|
| 156 | end%if |
|---|
| 157 | |
|---|
| 158 | % if all motor ports are set, we remember this now |
|---|
| 159 | realport = whatmotor; |
|---|
| 160 | % but whatmotor cannot be > 2, or else the global var will be out of |
|---|
| 161 | % index... |
|---|
| 162 | if whatmotor == 255 |
|---|
| 163 | % we set it to the first motor in this case, if all motors are set, |
|---|
| 164 | % their settings should be the same anyway... |
|---|
| 165 | whatmotor = 0; % looks like bad style to override it here, i know... |
|---|
| 166 | |
|---|
| 167 | % need to set these now as they weren't set above |
|---|
| 168 | if NXTMOTOR_State(whatmotor + 1).SyncedToSpeed |
|---|
| 169 | RegModeName = 'SPEED'; |
|---|
| 170 | else |
|---|
| 171 | % SYNC doesn't make sense for all motors! |
|---|
| 172 | RegModeName = 'IDLE'; |
|---|
| 173 | end%if |
|---|
| 174 | % also no sense makes a TurnRatio: |
|---|
| 175 | TurnRatio = 0; |
|---|
| 176 | |
|---|
| 177 | end%if |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | %% send "regular" packet... |
|---|
| 181 | NXT_SetOutputState(realport, ... % port |
|---|
| 182 | NXTMOTOR_State(whatmotor + 1).Power, ... % power |
|---|
| 183 | true, ... % motoron |
|---|
| 184 | ~NXTMOTOR_State(whatmotor + 1).BrakeDisabled, ... % brake |
|---|
| 185 | RegModeName, ... % reg mode |
|---|
| 186 | TurnRatio, ... % turn ratio |
|---|
| 187 | NXTMOTOR_State(whatmotor + 1).RunStateName, ... % runstate |
|---|
| 188 | NXTMOTOR_State(whatmotor + 1).AngleLimit, ... |
|---|
| 189 | 'dontreply'); |
|---|
| 190 | |
|---|
| 191 | if (NXTMOTOR_State(whatmotor+1).Power ~= 0) |
|---|
| 192 | % set memory counter |
|---|
| 193 | SetMemoryCount(whatmotor, GetMemoryCount(whatmotor) + ... |
|---|
| 194 | sign(NXTMOTOR_State(whatmotor+1).Power) * NXTMOTOR_State(whatmotor+1).AngleLimit); |
|---|
| 195 | end |
|---|
| 196 | if (NXTMOTOR_State(whatmotor + 1).AngleLimit == 0) |
|---|
| 197 | SetMemoryCount(whatmotor, 0); |
|---|
| 198 | end |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | %% Restore previous motor number setting |
|---|
| 203 | try % try because oldmotor might not've been set |
|---|
| 204 | % applies only to short-notation with nargin == 7... |
|---|
| 205 | if ~isnan(oldmotor) |
|---|
| 206 | SetMotor(oldmotor); |
|---|
| 207 | end |
|---|
| 208 | catch |
|---|
| 209 | % nothing, its nothing to restore here... |
|---|
| 210 | end% |
|---|
| 211 | |
|---|
| 212 | end%function |
|---|