| 1 | function SyncToMotor(OtherMotor) |
|---|
| 2 | % Enables synchronization regulation for current active and specified motor |
|---|
| 3 | % |
|---|
| 4 | % Syntax |
|---|
| 5 | % |SyncToMotor(OtherMotor)| |
|---|
| 6 | % |
|---|
| 7 | % Description |
|---|
| 8 | % |SyncToMotor(OtherMotor)| sets the synchronization mode to the curren active motor (set by |
|---|
| 9 | % |SetMotor|) and the given motor port |OtherMotor|. The value |OtherMotor| can be addressed by |
|---|
| 10 | % the symbolic constants |MOTOR_A| , |MOTOR_B| and |MOTOR_C| analog to the labeling on the NXT |
|---|
| 11 | % Brick. The synchronization mode can be set off if the value |OtherMotor| is set equal to |
|---|
| 12 | % |'off'|. The synchronization setting takes only affect with the next |SendMotorSettings| command. |
|---|
| 13 | % |
|---|
| 14 | % This means that both motors will act as if they were connected through an axle. Motors with more |
|---|
| 15 | % load on them (rough underground) will automatically be corrected and regulated for example. This |
|---|
| 16 | % "synchronization regulation" is the setting you want to use when driving with your robot. Also |
|---|
| 17 | % turning (SetTurnRatio) only affects motors that are synced. |
|---|
| 18 | % |
|---|
| 19 | % Note: |
|---|
| 20 | % One motor can not be synchronized to itself. The synchronization mode and the speed regulation |
|---|
| 21 | % mode can be set only together at one time. Once a motor is synced to another motor, all |
|---|
| 22 | % settings set to it will be applied to both motors (until synchronisation is lost of course). |
|---|
| 23 | % Once 2 motors are synced, you effectively control 2 motors with 1 set of commands. This means, |
|---|
| 24 | % when calling |SendMotorSettings|, in fact 2 packets will be send to the 2 synced motors, hence |
|---|
| 25 | % you will experience about twice the lag than usual. Take this into consideration... |
|---|
| 26 | % |
|---|
| 27 | % When using several motor commands with |SyncToMotor| statements, |
|---|
| 28 | % unexpected behaviour can occur, due to the NXTs internal error correction counters. |
|---|
| 29 | % Sometimes it can help to issue the commands |NXT_ResetMotorPosition(port, true)|, |
|---|
| 30 | % |NXT_ResetMotorPosition(port, false)| and |StopMotor(port, 'off')| for each of both |
|---|
| 31 | % motors. Although this seems like a waste of packets, this can do the trick, especially |
|---|
| 32 | % when working with certain turn ratios (see |SetTurnRatio|). |
|---|
| 33 | % |
|---|
| 34 | % Example |
|---|
| 35 | %+ SetMotor(MOTOR_B); |
|---|
| 36 | %+ SyncToMotor(MOTOR_C); |
|---|
| 37 | %+ SetPower(76); |
|---|
| 38 | %+ SendMotorSettings(); |
|---|
| 39 | % |
|---|
| 40 | % See also: SendMotorSettings, SetMotor, SetPower, SetTurnRatio |
|---|
| 41 | % |
|---|
| 42 | % Signature |
|---|
| 43 | % Author: Linus Atorf, Alexander Behrens (see AUTHORS) |
|---|
| 44 | % Date: 2007/10/15 |
|---|
| 45 | % Copyright: 2007-2011, RWTH Aachen University |
|---|
| 46 | % |
|---|
| 47 | ; |
|---|
| 48 | % |
|---|
| 49 | % *********************************************************************************************** |
|---|
| 50 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 51 | % * * |
|---|
| 52 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 53 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 54 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 55 | % * * |
|---|
| 56 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 57 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 58 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 59 | % * * |
|---|
| 60 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 61 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 62 | % *********************************************************************************************** |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | %% Check parameter |
|---|
| 66 | |
|---|
| 67 | % check function argument |
|---|
| 68 | if ischar(OtherMotor) |
|---|
| 69 | if (~strcmpi(OtherMotor, 'off') && isnan(str2double(OtherMotor))) |
|---|
| 70 | error('MATLAB:RWTHMindstormsNXT:Motor:invalidSyncParameter', 'Given parameter has to be a motor number or the string "off"!'); |
|---|
| 71 | end |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | %% get default handle & motorstate |
|---|
| 75 | h = COM_GetDefaultNXT(); |
|---|
| 76 | NXTMOTOR_State = h.NXTMOTOR_getState(); |
|---|
| 77 | |
|---|
| 78 | whatmotor = h.NXTMOTOR_getCurrentMotor(); |
|---|
| 79 | |
|---|
| 80 | %% Set synchronization mode |
|---|
| 81 | |
|---|
| 82 | % check if synchronization has to be turned off |
|---|
| 83 | if strcmpi(OtherMotor, 'off') |
|---|
| 84 | NXTMOTOR_State(1).SyncedToMotor = -1; |
|---|
| 85 | NXTMOTOR_State(2).SyncedToMotor = -1; |
|---|
| 86 | NXTMOTOR_State(3).SyncedToMotor = -1; |
|---|
| 87 | else |
|---|
| 88 | |
|---|
| 89 | if ischar(OtherMotor) |
|---|
| 90 | OtherMotor = str2double(OtherMotor); |
|---|
| 91 | end%if |
|---|
| 92 | |
|---|
| 93 | if OtherMotor < 0 || OtherMotor > 2 |
|---|
| 94 | error('MATLAB:RWTHMindstormsNXT:Motor:invalidPort', 'Other motor to sync to must be 0, 1 or 2.') |
|---|
| 95 | end%if |
|---|
| 96 | |
|---|
| 97 | if OtherMotor == whatmotor |
|---|
| 98 | error('MATLAB:RWTHMindstormsNXT:Motor:syncToSelf', ... |
|---|
| 99 | ['You are trying to sync a motor to itself, which is not possible. ' ... |
|---|
| 100 | 'The current motor cannot be the other motor you want to sync to.']) |
|---|
| 101 | end%if |
|---|
| 102 | |
|---|
| 103 | if NXTMOTOR_State(whatmotor + 1).SyncedToSpeed |
|---|
| 104 | warning('MATLAB:RWTHMindstormsNXT:Motor:simultaneousSyncAndSpeedRegulationWarning', ... |
|---|
| 105 | ['You are activating synchronization for an already speed-regulated motor. ' ... |
|---|
| 106 | 'This will automatically turn off speed regulation. Make sure to call ' ... |
|---|
| 107 | 'SpeedRegulation(''off'') first to disable this warning.']); |
|---|
| 108 | end%if |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | % check if synced to the other other motor (^^) before: |
|---|
| 112 | % what i mean: we've got 3 motors. now 1 is to be synced to another one. |
|---|
| 113 | % if the this new target was synced before to the 3rd motor, we must detect |
|---|
| 114 | % it now and set free the 3rd, as now the other two are synced. ok? |
|---|
| 115 | % whatever, it IS necessary... |
|---|
| 116 | |
|---|
| 117 | % ok we solved the above mentioned problem very easy now: |
|---|
| 118 | NXTMOTOR_State(1).SyncedToMotor = -1; |
|---|
| 119 | NXTMOTOR_State(2).SyncedToMotor = -1; |
|---|
| 120 | NXTMOTOR_State(3).SyncedToMotor = -1; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | % again, careful with the indices now: |
|---|
| 124 | |
|---|
| 125 | % disable/overwrite speed syncing |
|---|
| 126 | NXTMOTOR_State(whatmotor + 1).SyncedToSpeed = false; |
|---|
| 127 | NXTMOTOR_State(OtherMotor + 1).SyncedToSpeed = false; |
|---|
| 128 | |
|---|
| 129 | % now write both states |
|---|
| 130 | NXTMOTOR_State(OtherMotor + 1).SyncedToMotor = whatmotor; |
|---|
| 131 | NXTMOTOR_State(whatmotor + 1).SyncedToMotor = OtherMotor; |
|---|
| 132 | |
|---|
| 133 | % copy property settings from whatmotor to OtherMotor |
|---|
| 134 | NXTMOTOR_State(OtherMotor + 1).Power = NXTMOTOR_State(whatmotor + 1).Power; |
|---|
| 135 | NXTMOTOR_State(OtherMotor + 1).AngleLimit = NXTMOTOR_State(whatmotor + 1).AngleLimit; |
|---|
| 136 | NXTMOTOR_State(OtherMotor + 1).TurnRatio = NXTMOTOR_State(whatmotor + 1).TurnRatio; |
|---|
| 137 | NXTMOTOR_State(OtherMotor + 1).RunStateName = NXTMOTOR_State(whatmotor + 1).RunStateName; |
|---|
| 138 | |
|---|
| 139 | % this works, as we just set what we want, the submit method will figure |
|---|
| 140 | % out the difference between the current state and the wanted state... |
|---|
| 141 | |
|---|
| 142 | % that's the plan at least |
|---|
| 143 | |
|---|
| 144 | end%if |
|---|
| 145 | |
|---|
| 146 | %% save motor state back to handle |
|---|
| 147 | h.NXTMOTOR_setState(NXTMOTOR_State); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | end%function |
|---|