Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Plane Manipulation Using Space Mouse MATLAB® Object

This example shows how to use the Space Mouse via MATLAB® interface.

After starting this example, a virtual scene with an aircraft is displayed in the Simulink® 3D Animation™ Viewer. You can navigate the plane in the scene using the Space Mouse. By pressing the device button 1 you can place a marker at the current plane position.

This example requires a Space Mouse or other compatible device.

Create and Initialize the Space Mouse Object

The device ID is set to USB1 in the example. If your device uses a different connection, set the ID accordingly. Valid values for the space mouse ID are:

COM1, COM2, COM3, COM4, USB1, USB2, USB3, or USB4.

NOTE: a warning message is printed if Space Mouse is not connected.

ID = 'USB1';
MOUSE = [];
try
  % try to create the space mouse object
  MOUSE = vrspacemouse(ID);
catch ME
  fprintf('Unable to initialize the Space Mouse on port %s.\n', ID);
end
Unable to initialize the Space Mouse on port USB1.

Load and View the Virtual World

% create and open the vrworld object
w = vrworld('vrtkoff_hud.wrl', 'new');
open(w);

% create the vrfigure showing the virtual scene
% use a viewpoint suitable for user navigation
fig = vrfigure(w, 'Viewpoint', 'Ride on the Plane');

% get the manipulated airplane node
airpln = vrnode(w, 'Plane');
% read plane initial translation and rotation
originalTranslation = airpln.translation;
originalRotation = airpln.rotation;

% set the HUD display text
offset = vrnode(w, 'HUDOffset');
offset.translation = offset.translation + [-0.15 1.9 0];
hudtext = vrnode(w, 'HUDText1');
hudstr = { 'Press button ''1'' to drop a marker', ...
           'Press button ''2'' to reset plane position', ...
           'Press buttons ''1'' and ''2'' to exit' };
hudtext.string = hudstr;
vrdrawnow;

Add an EXTERNPROTO for Trajectory Markers

Load a tetrahedron shape PROTO from VRML file containing various marker shapes.

% get the path to the wrl file with marker PROTOs
pathtomarkers = which('vr_markers.wrl');
% use the tetrahedron shape
MarkerName = 'Marker_Tetrahedron';
% create an EXTERNPROTO with specified marker
try
  addexternproto(w, pathtomarkers, MarkerName);
catch ME
  % if required PROTO is already contained don't throw an exception
  if ~strcmpi(ME.identifier, 'sl3d:interface:protoexists')
    throwAsCaller(ME);
  end
end

Navigation in the Scene

The interactive navigation is finished either by pressing Space Mouse buttons 1 and 2 simultaneously or by closing the Simulink 3D Animation Viewer figure.

if ~isempty(MOUSE)

  % iterator that ensures unique DEF names for created markers
  iterforname = 0;

  % set the mouse sensitivity for translations
  % higher values correspond to higher sensitivity
  MOUSE.PositionSensitivity = 1e-2;
  % set the mouse sensitivity for rotations
  % higher values correspond to higher sensitivity
  MOUSE.RotationSensitivity = 1e-5;

  % read the space mouse values and update the scene objects in a cycle
  % repeat unless buttons '1' and '2' simultaneously pressed or figure closed
  while any(button(MOUSE, [1 2]) == 0) && isvalid(fig)
     pause(0.01);
     % use the method vrspacemouse/viewpoint to get the current translation and rotation
     V = viewpoint(MOUSE);
     % set the new translation to the aircraft node
     airpln.translation = originalTranslation + [-1 1 -1].*V(1:3);
     % set the new rotation to the aircraft node
     airpln.rotation = [-1 1 -1 1].*V(4:7);
     if button(MOUSE, 1) == 1
       % if mouse button '1' pressed create a new marker
       newMarker = vrnode(w, sprintf('%s_%d', 'Marker', iterforname), MarkerName);
       % set marker translation
       newMarker.markerTranslation = originalTranslation + [-1 1 -1].*V(1:3);
       % increment the iterator
       iterforname = iterforname + 1;
     end
     if button(MOUSE, 2) == 1
       % if mouse button '2' pressed reset the plane position and rotation
       airpln.translation = originalTranslation;
       airpln.rotation = originalRotation;
       MOUSE.InitialPosition = [0 0 0];
       MOUSE.InitialRotation = [0 0 0];
     end
     % redraw the virtual scene
     vrdrawnow;
  end
end

Cleanup

% close the vrfigure
close(fig);
% close the vrworld
close(w);
% clear all used variables
clear ID MOUSE w fig airpln originalTranslation originalRotation offset hudtext hudstr ...
      pathtomarkers MarkerName iterforname V newMarker img_capture img;

% display the end of example message
Was this topic helpful?