| 1 | %% Example 3: Drive Around Table (Motor Class) |
|---|
| 2 | % Let a robot (e.g. Tribot or CATTbot) drive a square on the floor |
|---|
| 3 | % |
|---|
| 4 | % Signature |
|---|
| 5 | % |
|---|
| 6 | % * Author: Linus Atorf, Alexander Behrens |
|---|
| 7 | % * Date: 2009/07/17 |
|---|
| 8 | % * License: BSD |
|---|
| 9 | % * RWTH - Mindstorms NXT Toolbox: http: |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | %% Check toolbox installation |
|---|
| 13 | % verify that the RWTH - Mindstorms NXT toolbox is installed. |
|---|
| 14 | if verLessThan('RWTHMindstormsNXT', '3.00'); |
|---|
| 15 | error(strcat('This program requires the RWTH - Mindstorms NXT Toolbox ' ... |
|---|
| 16 | ,'version 3.00 or greater. Go to http: |
|---|
| 17 | ,'and follow the installation instructions!')); |
|---|
| 18 | end%if |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | %% Clear and close |
|---|
| 22 | COM_CloseNXT all |
|---|
| 23 | clear all |
|---|
| 24 | close all |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | %% Constants and so on |
|---|
| 28 | TableLength = 1000; % in degrees of motor rotations :-) |
|---|
| 29 | QuarterTurnTicks = 219; % in motor degrees, how much is a 90° turn of the bot? |
|---|
| 30 | Ports = [MOTOR_B; MOTOR_C]; % motorports for left and right wheel |
|---|
| 31 | DrivingSpeed = 60; |
|---|
| 32 | TurningSpeed = 40; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | %% Open Bluetooth connetion |
|---|
| 36 | h = COM_OpenNXT('bluetooth.ini'); |
|---|
| 37 | COM_SetDefaultNXT(h); |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | %% Initialize motor-objects: |
|---|
| 41 | |
|---|
| 42 | mStraight = NXTMotor(Ports); |
|---|
| 43 | % next command since we are driving in SYNC-mode. This should not be |
|---|
| 44 | % necessary with correct default values, but at the moment, I have to set |
|---|
| 45 | % it manually, |
|---|
| 46 | mStraight.SpeedRegulation = false; % not for sync mode |
|---|
| 47 | mStraight.Power = DrivingSpeed; |
|---|
| 48 | mStraight.TachoLimit = TableLength; |
|---|
| 49 | mStraight.ActionAtTachoLimit = 'Brake'; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | mTurn1 = NXTMotor(Ports(2)); % ports swapped because it's nicer |
|---|
| 53 | mTurn1.SpeedRegulation = false; % we could use it if we wanted |
|---|
| 54 | mTurn1.Power = TurningSpeed; |
|---|
| 55 | mTurn1.TachoLimit = QuarterTurnTicks; |
|---|
| 56 | mStraight.ActionAtTachoLimit = 'Brake'; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | mTurn2 = mTurn1; |
|---|
| 60 | mTurn2.Port = Ports(1); % ports swapped again... |
|---|
| 61 | mTurn2.Power = - mTurn1.Power; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | %% Initialize Motors... |
|---|
| 66 | % we send this in case they should still be spinning or something... |
|---|
| 67 | mStraight.Stop('off'); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | %% Start the engines, main loop begins (repeated 4 times) |
|---|
| 71 | |
|---|
| 72 | % 4 times because we got 4 equal sides of the table :-) |
|---|
| 73 | for j = 1 : 4 |
|---|
| 74 | |
|---|
| 75 | %% Drive |
|---|
| 76 | |
|---|
| 77 | mStraight.SendToNXT(); |
|---|
| 78 | |
|---|
| 79 | %% Wait for the end end of table |
|---|
| 80 | mStraight.WaitFor(); |
|---|
| 81 | |
|---|
| 82 | %% Now please turn |
|---|
| 83 | |
|---|
| 84 | mTurn1.SendToNXT(); |
|---|
| 85 | mTurn1.WaitFor(); |
|---|
| 86 | |
|---|
| 87 | mTurn2.SendToNXT(); |
|---|
| 88 | mTurn2.WaitFor(); |
|---|
| 89 | |
|---|
| 90 | %% Thats it. Repeat 4 times.... |
|---|
| 91 | end%for |
|---|
| 92 | |
|---|
| 93 | %% Done. |
|---|
| 94 | % Hey! End of a hard day's work |
|---|
| 95 | % Just to show good style, we close down our motors again: |
|---|
| 96 | mStraight.Stop('off'); |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | %% Close Bluetooth connection |
|---|
| 100 | COM_CloseNXT(h); |
|---|