root/trunk/publishing/MATLABCentral/Example_7_ShinyRadar.m @ 701

Revision 701, 3.7 KB (checked in by behrens, 4 years ago)
  • update MATLAB Central documents
Line 
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://www.mindstorms.rwth-aachen.de
10
11function Example_7_ShinyRadar
12
13%% Clean up previous handles
14COM_CloseNXT all
15
16%% Set up Matlab
17clear all % if you use clear all, call COM_CloseNXT all before, as we did!
18close all
19format compact
20
21
22%% Set up ports & vars
23portMotor   = MOTOR_A;
24portUS      = SENSOR_4;
25GearFactor  = 4;
26MotorRange  = 170 * GearFactor;
27MotorSpeed  = 25;
28
29
30%% Initialize GFX
31figure('Name', 'Shiny Radar'); %, 'Position', [50 60 1200 500]);
32axis equal
33axis([-170 170 0 170]);
34set(gca, 'Color', 'black');
35hold on
36
37
38hScanLine = ResetFigure(false);
39
40
41
42%% Open connection
43h = COM_OpenNXT('bluetooth.ini');
44COM_SetDefaultNXT(h);
45
46
47%% Reset Motor, open sensor
48StopMotor all off
49NXT_ResetMotorPosition(portMotor, false);
50NXT_ResetMotorPosition(portMotor, true);
51
52OpenUltrasonic(portUS);
53
54%% Turn right so start position
55SetMotor(portMotor)
56    SetPower(20)
57    SetAngleLimit(MotorRange/2 - 20)
58SendMotorSettings
59WaitForMotor(portMotor)
60pause(0.5)
61StopMotor(portMotor, 'off');
62
63
64%% First motor go
65tmp = GetMotorSettings(portMotor);
66StartPos = tmp.TachoCount;
67
68SetMotor(portMotor);
69    SetPower(-MotorSpeed);
70    SpeedRegulation on
71    SetAngleLimit(MotorRange);
72SendMotorSettings
73
74
75
76%% Main loop
77while(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
132end%while
133
134
135
136%% Clean up
137StopMotor all off
138CloseSensor(portUS);
139
140COM_CloseNXT(h)
141
142
143end%function
144
145
146function 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
176end%function
Note: See TracBrowser for help on using the browser.