| 1 | |
|---|
| 2 | <!DOCTYPE html |
|---|
| 3 | PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|---|
| 4 | <html> |
|---|
| 5 | <head> |
|---|
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
|---|
| 7 | |
|---|
| 8 | <!-- |
|---|
| 9 | This HTML is auto-generated from an M-file. |
|---|
| 10 | To make changes, update the M-file and republish this document. |
|---|
| 11 | --> |
|---|
| 12 | <title>Example 4: Drive Until Wall</title> |
|---|
| 13 | <meta name="generator" content="MATLAB 7.7"> |
|---|
| 14 | <meta name="date" content="2008-11-05"> |
|---|
| 15 | <meta name="m-file" content="Example_4_DriveUntilWall"> |
|---|
| 16 | <link type="text/css" rel="stylesheet" href="../style.css"> |
|---|
| 17 | </head> |
|---|
| 18 | <body> |
|---|
| 19 | <p class="header">RWTH - Mindstorms NXT Toolbox</p> |
|---|
| 20 | <div class="content"> |
|---|
| 21 | <h1>Example 4: Drive Until Wall</h1> |
|---|
| 22 | <!--introduction--> |
|---|
| 23 | <p>In this little demo our robot drives forward until it detects a wall.</p> |
|---|
| 24 | <!--/introduction--> |
|---|
| 25 | <h2>Contents</h2> |
|---|
| 26 | <div> |
|---|
| 27 | <ul> |
|---|
| 28 | <li><a href="#1">Matlab</a></li> |
|---|
| 29 | <li><a href="#2">Constants</a></li> |
|---|
| 30 | <li><a href="#3">Initialize NXT connection</a></li> |
|---|
| 31 | <li><a href="#4">Reset Motor / remember start position</a></li> |
|---|
| 32 | <li><a href="#5">Prepare sensor</a></li> |
|---|
| 33 | <li><a href="#6">And GO!</a></li> |
|---|
| 34 | <li><a href="#7">Detect wall (sensor loop)</a></li> |
|---|
| 35 | <li><a href="#8">Immediately stop all motors!</a></li> |
|---|
| 36 | <li><a href="#9">Signal</a></li> |
|---|
| 37 | <li><a href="#10">Drive back home!</a></li> |
|---|
| 38 | <li><a href="#11">Wait until there</a></li> |
|---|
| 39 | <li><a href="#12">clean up</a></li> |
|---|
| 40 | <li><a href="#13">Close NXT connection</a></li> |
|---|
| 41 | </ul> |
|---|
| 42 | </div> |
|---|
| 43 | <h2>Matlab<a name="1"></a></h2><pre class="codeinput">clear <span class="string">all</span> |
|---|
| 44 | close <span class="string">all</span> |
|---|
| 45 | </pre><h2>Constants<a name="2"></a></h2><pre class="codeinput">MaxDriveTime = 30; <span class="comment">% in seconds, stop after this time...</span> |
|---|
| 46 | USPort = SENSOR_4; |
|---|
| 47 | </pre><h2>Initialize NXT connection<a name="3"></a></h2><pre class="codeinput">handle = COM_OpenNXT(<span class="string">'bluetooth.ini'</span>, <span class="string">'check'</span>); |
|---|
| 48 | COM_SetDefaultNXT(handle); |
|---|
| 49 | </pre><h2>Reset Motor / remember start position<a name="4"></a></h2><pre class="codeinput">ResetMotorAngle(MOTOR_B); |
|---|
| 50 | ResetMotorAngle(MOTOR_C); |
|---|
| 51 | StartPos = GetMotorSettings(MOTOR_B); |
|---|
| 52 | </pre><h2>Prepare sensor<a name="5"></a></h2><pre class="codeinput">OpenUltrasonic(USPort) |
|---|
| 53 | </pre><h2>And GO!<a name="6"></a></h2><pre class="codeinput">SetMotor(MOTOR_B); |
|---|
| 54 | SyncToMotor(MOTOR_C); |
|---|
| 55 | SetPower(60); |
|---|
| 56 | SetAngleLimit(0); |
|---|
| 57 | SetTurnRatio(0); |
|---|
| 58 | SetRampMode (<span class="string">'off'</span>); |
|---|
| 59 | SendMotorSettings(); |
|---|
| 60 | </pre><h2>Detect wall (sensor loop)<a name="7"></a></h2><pre class="codeinput">tictic(1); |
|---|
| 61 | <span class="keyword">while</span>(toctoc(1) < MaxDriveTime) |
|---|
| 62 | <span class="keyword">if</span> GetUltrasonic(USPort) < 30 |
|---|
| 63 | <span class="keyword">break</span> |
|---|
| 64 | <span class="keyword">end</span><span class="comment">%if</span> |
|---|
| 65 | <span class="keyword">end</span><span class="comment">%while</span> |
|---|
| 66 | </pre><h2>Immediately stop all motors!<a name="8"></a></h2><pre class="codeinput">StopMotor(<span class="string">'all'</span>, <span class="string">'off'</span>); |
|---|
| 67 | <span class="comment">% but where are we now?</span> |
|---|
| 68 | CurPos = GetMotorSettings(MOTOR_B); |
|---|
| 69 | </pre><h2>Signal<a name="9"></a></h2><pre class="codeinput">NXT_PlayTone(440, 1000); |
|---|
| 70 | </pre><h2>Drive back home!<a name="10"></a></h2> |
|---|
| 71 | <p>To try to understand what exactly the different rotation count values mean, we compare them just for fun</p><pre class="codeinput">TotalAbsDist = StartPos.TachoCount - CurPos.TachoCount; |
|---|
| 72 | Dist = CurPos.Angle; |
|---|
| 73 | |
|---|
| 74 | SetMotor(MOTOR_B); |
|---|
| 75 | SyncToMotor(MOTOR_C); |
|---|
| 76 | SetPower(-60); |
|---|
| 77 | SetAngleLimit(abs(Dist)); |
|---|
| 78 | SendMotorSettings(); |
|---|
| 79 | </pre><h2>Wait until there<a name="11"></a></h2><pre class="codeinput">WaitForMotor(1, MaxDriveTime); |
|---|
| 80 | |
|---|
| 81 | <span class="comment">% brake</span> |
|---|
| 82 | StopMotor(<span class="string">'all'</span>, <span class="string">'brake'</span>); |
|---|
| 83 | |
|---|
| 84 | pause(1); |
|---|
| 85 | </pre><h2>clean up<a name="12"></a></h2><pre class="codeinput">CloseSensor(SENSOR_4) |
|---|
| 86 | StopMotor (<span class="string">'all'</span>, <span class="string">'off'</span>); |
|---|
| 87 | </pre><h2>Close NXT connection<a name="13"></a></h2><pre class="codeinput">COM_CloseNXT(handle); |
|---|
| 88 | </pre><p class="footer"><br> |
|---|
| 89 | Published with MATLAB® 7.7<br></p> |
|---|
| 90 | </div> |
|---|
| 91 | <!-- |
|---|
| 92 | ##### SOURCE BEGIN ##### |
|---|
| 93 | %% Example 4: Drive Until Wall |
|---|
| 94 | % In this little demo our robot drives forward until it detects a wall. |
|---|
| 95 | |
|---|
| 96 | %% Matlab |
|---|
| 97 | clear all |
|---|
| 98 | close all |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | %% Constants |
|---|
| 102 | MaxDriveTime = 30; % in seconds, stop after this time... |
|---|
| 103 | USPort = SENSOR_4; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | %% Initialize NXT connection |
|---|
| 107 | handle = COM_OpenNXT('bluetooth.ini', 'check'); |
|---|
| 108 | COM_SetDefaultNXT(handle); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | %% Reset Motor / remember start position |
|---|
| 112 | ResetMotorAngle(MOTOR_B); |
|---|
| 113 | ResetMotorAngle(MOTOR_C); |
|---|
| 114 | StartPos = GetMotorSettings(MOTOR_B); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | %% Prepare sensor |
|---|
| 118 | OpenUltrasonic(USPort) |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | %% And GO! |
|---|
| 122 | |
|---|
| 123 | SetMotor(MOTOR_B); |
|---|
| 124 | SyncToMotor(MOTOR_C); |
|---|
| 125 | SetPower(60); |
|---|
| 126 | SetAngleLimit(0); |
|---|
| 127 | SetTurnRatio(0); |
|---|
| 128 | SetRampMode ('off'); |
|---|
| 129 | SendMotorSettings(); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | %% Detect wall (sensor loop) |
|---|
| 133 | tictic(1); |
|---|
| 134 | while(toctoc(1) < MaxDriveTime) |
|---|
| 135 | if GetUltrasonic(USPort) < 30 |
|---|
| 136 | break |
|---|
| 137 | end%if |
|---|
| 138 | end%while |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | %% Immediately stop all motors! |
|---|
| 142 | StopMotor('all', 'off'); |
|---|
| 143 | % but where are we now? |
|---|
| 144 | CurPos = GetMotorSettings(MOTOR_B); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | %% Signal |
|---|
| 148 | NXT_PlayTone(440, 1000); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | %% Drive back home! |
|---|
| 152 | % To try to understand what exactly the different rotation count values |
|---|
| 153 | % mean, we compare them just for fun |
|---|
| 154 | TotalAbsDist = StartPos.TachoCount - CurPos.TachoCount; |
|---|
| 155 | Dist = CurPos.Angle; |
|---|
| 156 | |
|---|
| 157 | SetMotor(MOTOR_B); |
|---|
| 158 | SyncToMotor(MOTOR_C); |
|---|
| 159 | SetPower(-60); |
|---|
| 160 | SetAngleLimit(abs(Dist)); |
|---|
| 161 | SendMotorSettings(); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | %% Wait until there |
|---|
| 165 | WaitForMotor(1, MaxDriveTime); |
|---|
| 166 | |
|---|
| 167 | % brake |
|---|
| 168 | StopMotor('all', 'brake'); |
|---|
| 169 | |
|---|
| 170 | pause(1); |
|---|
| 171 | |
|---|
| 172 | %% clean up |
|---|
| 173 | CloseSensor(SENSOR_4) |
|---|
| 174 | StopMotor ('all', 'off'); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | %% Close NXT connection |
|---|
| 178 | COM_CloseNXT(handle); |
|---|
| 179 | |
|---|
| 180 | ##### SOURCE END ##### |
|---|
| 181 | --> |
|---|
| 182 | </body> |
|---|
| 183 | </html> |
|---|