| 1 | %% Example 6: ShinyRadar |
|---|
| 2 | % This function provides a live moving ultrasonic radar. |
|---|
| 3 | % |
|---|
| 4 | % Note: This demo uses an robot with a rotating ultrasonic sensor. |
|---|
| 5 | % |
|---|
| 6 | % Signature |
|---|
| 7 | % Author: Linus Atorf, Alexander Behrens |
|---|
| 8 | % Date: 2009/07/17 |
|---|
| 9 | % RWTH - Mindstorms NXT Toolbox: http: |
|---|
| 10 | |
|---|
| 11 | function Example_7_ShinyRadar |
|---|
| 12 | |
|---|
| 13 | %% Clean up previous handles |
|---|
| 14 | COM_CloseNXT all |
|---|
| 15 | |
|---|
| 16 | %% Set up Matlab |
|---|
| 17 | clear all % if you use clear all, call COM_CloseNXT all before, as we did! |
|---|
| 18 | close all |
|---|
| 19 | format compact |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | %% Set up ports & vars |
|---|
| 23 | portMotor = MOTOR_A; |
|---|
| 24 | portUS = SENSOR_4; |
|---|
| 25 | GearFactor = 4; |
|---|
| 26 | MotorRange = 170 * GearFactor; |
|---|
| 27 | MotorSpeed = 25; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | %% Initialize GFX |
|---|
| 31 | figure('Name', 'Shiny Radar'); %, 'Position', [50 60 1200 500]); |
|---|
| 32 | axis equal |
|---|
| 33 | axis([-170 170 0 170]); |
|---|
| 34 | set(gca, 'Color', 'black'); |
|---|
| 35 | hold on |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | hScanLine = ResetFigure(false); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | %% Open connection |
|---|
| 43 | h = COM_OpenNXT('bluetooth.ini'); |
|---|
| 44 | COM_SetDefaultNXT(h); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | %% Reset Motor, open sensor |
|---|
| 48 | StopMotor all off |
|---|
| 49 | NXT_ResetMotorPosition(portMotor, false); |
|---|
| 50 | NXT_ResetMotorPosition(portMotor, true); |
|---|
| 51 | |
|---|
| 52 | OpenUltrasonic(portUS); |
|---|
| 53 | |
|---|
| 54 | %% Turn right so start position |
|---|
| 55 | SetMotor(portMotor) |
|---|
| 56 | SetPower(20) |
|---|
| 57 | SetAngleLimit(MotorRange/2 - 20) |
|---|
| 58 | SendMotorSettings |
|---|
| 59 | WaitForMotor(portMotor) |
|---|
| 60 | pause(0.5) |
|---|
| 61 | StopMotor(portMotor, 'off'); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | %% First motor go |
|---|
| 65 | tmp = GetMotorSettings(portMotor); |
|---|
| 66 | StartPos = tmp.TachoCount; |
|---|
| 67 | |
|---|
| 68 | SetMotor(portMotor); |
|---|
| 69 | SetPower(-MotorSpeed); |
|---|
| 70 | SpeedRegulation on |
|---|
| 71 | SetAngleLimit(MotorRange); |
|---|
| 72 | SendMotorSettings |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | %% Main loop |
|---|
| 77 | while(true) |
|---|
| 78 | |
|---|
| 79 | % get current pos |
|---|
| 80 | tmp = GetMotorSettings(portMotor); |
|---|
| 81 | CurPos = tmp.TachoCount; |
|---|
| 82 | phi = pi - ((CurPos - (StartPos - MotorRange)) / MotorRange) * pi; |
|---|
| 83 | %alpha = phi * 180 / pi |
|---|
| 84 | %x = cos(phi) |
|---|
| 85 | %y = sin(phi) |
|---|
| 86 | |
|---|
| 87 | % get ultrasonic |
|---|
| 88 | distUS = GetUltrasonic(portUS); |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | % plot where we are |
|---|
| 92 | set(hScanLine, 'XData', [0; cos(phi) * 150]) |
|---|
| 93 | set(hScanLine, 'YData', [0; sin(phi) * 150]); |
|---|
| 94 | |
|---|
| 95 | % plot radar dot |
|---|
| 96 | if (distUS > 1) && (distUS < 200) |
|---|
| 97 | plot(cos(phi) * distUS, sin(phi) * distUS, 'g.') |
|---|
| 98 | end%if |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | drawnow |
|---|
| 102 | |
|---|
| 103 | % reverse direction if necessary |
|---|
| 104 | if CurPos < (StartPos - MotorRange) |
|---|
| 105 | StopMotor(portMotor, 'off') |
|---|
| 106 | NXT_ResetMotorPosition(portMotor, false); |
|---|
| 107 | NXT_ResetMotorPosition(portMotor, true); |
|---|
| 108 | |
|---|
| 109 | SetMotor(portMotor) |
|---|
| 110 | SetPower(MotorSpeed) |
|---|
| 111 | SetAngleLimit(MotorRange) |
|---|
| 112 | SendMotorSettings |
|---|
| 113 | |
|---|
| 114 | hScanLine = ResetFigure(true); |
|---|
| 115 | end%if |
|---|
| 116 | |
|---|
| 117 | % reverse direction if necessary |
|---|
| 118 | if CurPos > StartPos |
|---|
| 119 | StopMotor(portMotor, 'off') |
|---|
| 120 | NXT_ResetMotorPosition(portMotor, false); |
|---|
| 121 | NXT_ResetMotorPosition(portMotor, true); |
|---|
| 122 | SetMotor(portMotor) |
|---|
| 123 | SetPower(-MotorSpeed) |
|---|
| 124 | SetAngleLimit(MotorRange) |
|---|
| 125 | SendMotorSettings |
|---|
| 126 | |
|---|
| 127 | hScanLine = ResetFigure(false); |
|---|
| 128 | end%if |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | end%while |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | %% Clean up |
|---|
| 137 | StopMotor all off |
|---|
| 138 | CloseSensor(portUS); |
|---|
| 139 | |
|---|
| 140 | COM_CloseNXT(h) |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | end%function |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | function hScanNew = ResetFigure(leftTrue) |
|---|
| 147 | |
|---|
| 148 | cla |
|---|
| 149 | |
|---|
| 150 | %% Plot Radar outlines |
|---|
| 151 | |
|---|
| 152 | % circles |
|---|
| 153 | col = [0.3 0.3 0.3]; |
|---|
| 154 | for j = 1 : 3 |
|---|
| 155 | phi = linspace(0, pi, 60) ;% - pi; |
|---|
| 156 | x = cos(phi) * 50 * j; |
|---|
| 157 | y = sin(phi) * 50 * j; |
|---|
| 158 | plot(x, y, '-', 'Color', col) |
|---|
| 159 | end%for |
|---|
| 160 | |
|---|
| 161 | % lines |
|---|
| 162 | len = 160; |
|---|
| 163 | plot([0; cos(pi/4)*len], [0; sin(pi/4)*len], '-', 'Color', col) |
|---|
| 164 | plot([0; 0], [0; len], '-', 'Color', col) |
|---|
| 165 | plot([0; -cos(pi/4)*len], [0; sin(pi/4)*len], '-', 'Color', col) |
|---|
| 166 | |
|---|
| 167 | % draw scanline again: |
|---|
| 168 | if leftTrue |
|---|
| 169 | hScanNew = plot([0; -150], [0; 0], '-r'); |
|---|
| 170 | else |
|---|
| 171 | hScanNew = plot([0; 150], [0; 0], '-r'); |
|---|
| 172 | end%if |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | drawnow |
|---|
| 176 | end%function |
|---|