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, orUSB4。
NOTE: a warning message is printed if Space Mouse is not connected.
ID ='USB1'; MOUSE = [];try% try to create the space mouse objectMOUSE = vrspacemouse(ID);catchME 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 objectw = vrworld('vrtkoff_hud.x3d','new');open(w);% create the vrfigure showing the virtual scene% use a viewpoint suitable for user navigation无花果= vrfigure(w,'Viewpoint','Ride on the Plane');% get the manipulated airplane nodeairpln = vrnode(w,'Plane');% read plane initial translation and rotationoriginalTranslation = airpln.translation; originalRotation = airpln.rotation;% set the HUD display textoffset = vrnode(w,'HUDOffset');偏移量。翻译=抵消。翻译+ [-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 PROTOspathtomarkers = which('vr_markers.x3d');% use the tetrahedron shapeMarkerName ='Marker_Tetrahedron';% create an EXTERNPROTO with specified markertryaddexternproto(w, pathtomarkers, MarkerName);catchME% if required PROTO is already contained don't throw an exceptionif~strcmpi(ME.identifier,'sl3d:interface:protoexists') throwAsCaller(ME);endend
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 markersiterforname = 0;% set the mouse sensitivity for translations% higher values correspond to higher sensitivityMOUSE.PositionSensitivity = 1e-2;% set the mouse sensitivity for rotations% higher values correspond to higher sensitivityMOUSE.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 closedwhileany(button(MOUSE, [1 2]) == 0) && isvalid(fig) pause(0.01);% use the method vrspacemouse/viewpoint to get the current translation and rotationV = viewpoint(MOUSE);% set the new translation to the aircraft nodeairpln.translation = originalTranslation + [-1 1 -1].*V(1:3);% set the new rotation to the aircraft nodeairpln.rotation = [-1 1 -1 1].*V(4:7);ifbutton(MOUSE, 1) == 1% if mouse button '1' pressed create a new markernewMarker = vrnode(w, sprintf('%s_%d','Marker', iterforname), MarkerName);% set marker translationnewMarker.markerTranslation = originalTranslation + [-1 1 -1].*V(1:3);% increment the iteratoriterforname = iterforname + 1;endifbutton(MOUSE, 2) == 1% if mouse button '2' pressed reset the plane position and rotationairpln.translation = originalTranslation; airpln.rotation = originalRotation; MOUSE.InitialPosition = [0 0 0]; MOUSE.InitialRotation = [0 0 0];end% redraw the virtual scenevrdrawnow;endend
Cleanup
% close the vrfigureclose(fig);% close the vrworldclose(w);% clear all used variablesclearIDMOUSEw无花果airplnoriginalTranslationoriginalRotationoffsethudtexthudstr...pathtomarkersMarkerNameiterfornameVnewMarkerimg_captureimg;% display the end of example message