| 1 | %% Example 5: Next Generation Ultrasound |
|---|
| 2 | % This script demonstrates the results of the ultrasound "snapshot mode"! |
|---|
| 3 | % Interpretation of the results however is difficult. |
|---|
| 4 | % |
|---|
| 5 | % Just connect an NXT to the USB port, adjust the US port (or connect it to |
|---|
| 6 | % SENSOR_2), and see what's happening. The script will exit after 200 |
|---|
| 7 | % measurements... |
|---|
| 8 | % |
|---|
| 9 | % Signature |
|---|
| 10 | % Author: Linus Atorf, Alexander Behrens |
|---|
| 11 | % Date: 2009/07/17 |
|---|
| 12 | % RWTH - Mindstorms NXT Toolbox: http: |
|---|
| 13 | |
|---|
| 14 | %% Clean up |
|---|
| 15 | % Close previous handles (if existing) |
|---|
| 16 | COM_CloseNXT all |
|---|
| 17 | |
|---|
| 18 | %% Set up Matlab |
|---|
| 19 | clear all |
|---|
| 20 | close all |
|---|
| 21 | format compact |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | %% Set up ports |
|---|
| 25 | portUS = SENSOR_2; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | %% Get USB handle |
|---|
| 29 | COM_CloseNXT all |
|---|
| 30 | h = COM_OpenNXT(); |
|---|
| 31 | COM_SetDefaultNXT(h); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | %% Lets go then! |
|---|
| 35 | figure('name', 'Next Generation Ultrasound') |
|---|
| 36 | set(gca, 'Color', 'black'); |
|---|
| 37 | hold on |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | OpenUltrasonic(portUS, 'snapshot') |
|---|
| 41 | |
|---|
| 42 | n = 8; % bytes the US sensor received |
|---|
| 43 | count = 200; % how many readings until end? |
|---|
| 44 | plotcols = 8; % how many out of n echos to plot? |
|---|
| 45 | outOfRange = 160; % setting for out of range readings |
|---|
| 46 | |
|---|
| 47 | colors = flipud(hot(8)); |
|---|
| 48 | |
|---|
| 49 | data = zeros(1, n); |
|---|
| 50 | allX = (1:count+1)'; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | for i = 1 : count |
|---|
| 54 | USMakeSnapshot(portUS) |
|---|
| 55 | pause(0.05); % wait for the sound to travel |
|---|
| 56 | echos = USGetSnapshotResults(portUS); |
|---|
| 57 | |
|---|
| 58 | echos(echos == 255) = outOfRange; |
|---|
| 59 | |
|---|
| 60 | echos = [echos(1); diff(echos)]; |
|---|
| 61 | |
|---|
| 62 | data = vertcat(data, echos'); |
|---|
| 63 | x = allX(1:i+1); |
|---|
| 64 | |
|---|
| 65 | clf |
|---|
| 66 | hold on |
|---|
| 67 | set(gca, 'Color', 'black'); |
|---|
| 68 | |
|---|
| 69 | axis([0 count 0 outOfRange]) |
|---|
| 70 | |
|---|
| 71 | for j = plotcols : -1 : 1 |
|---|
| 72 | area(x, data(:, j) , 'FaceColor', colors(j, :)) |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | end%for |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | %% Clean up |
|---|
| 79 | CloseSensor(portUS) |
|---|
| 80 | COM_CloseNXT(h); |
|---|