Main Content

sim3d.sensors.UltrasonicSensor

Implement ultrasonic sensor model

Since R2024b

    Description

    Use the sim3d.sensors.UltrasonicSensor object to implement an ultrasonic sensor in a 3D environment. After you create a sim3d.sensors.UltrasonicSensor object, you can modify aspects of the actor object by setting property values.

    Creation

    Description

    ultrasonicSensor = sim3d.sensors.UltrasonicSensor() creates a default sim3d.sensors.UltrasonicSensor object in the 3D environment.

    ultrasonicSensor = sim3d.sensors.UltrasonicSensor(Name=Value) specifies options using one or more optional name-value arguments. For example, to create a lidar sensor with a range of 4 meters, set Range to 4.

    example

    Name-Value Arguments

    expand all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: ultrasonicSensor = sim3d.sensors.UltrasonicSensor(ActorName='UltrasonicSensor',Range=4)

    Name of actor, specified as a character array or string. If you do not specify an actor name, then the software assigns the actor an autogenerated name. Use this argument to set the name of the sim3d.sensors.UltrasonicSensor object.

    Note

    If you specify the same name as an actor that already exists, then the software appends actor name you specify with a unique identifier.

    Since R2025a

    Coordinate system that the actor uses for translation and rotation in the 3D environment, specified as one of these listed values:

    • 'Default' – World coordinate system

    • 'MATLAB' – MATLAB® coordinate system

    • 'ISO8855' – ISO 8855 standard coordinate system

    • 'AERO' – SAE coordinate system

    • 'VRML' – X3D ISO standard coordinate system

    • 'SAE' – SAE coordinate system

    For more details on the different coordinate systems, see Coordinate Systems in Simulink 3D Animation.

    Data Types: string

    Relative translation (x,y,z) of the actor object to its parent actor, specified as a real 1-by-3 vector. Use Translation to change the position of the sim3d.sensors.UltrasonicSensor object in the 3D environment along the X, Y, and Z axes of the coordinate system. When you add an actor to the 3D environment, the default parent actor is the Scene Origin at (0,0,0).

    Example: Translation=[1 2 1]

    Relative rotation (roll, pitch, yaw) of the actor object to its parent actor, specified as a real 1-by-3 vector. Use Rotation to rotate the sim3d.sensors.UltrasonicSensor object in the 3D environment.

    Example: Rotation=[pi/4 pi/8 pi/2]

    Detection range of the ultrasonic sensor, specified as a scalar. Units are in meters. This value determines the detections and distance values returned by the ultrasonic sensor. When the detected object is at a distance within the Range, the sensor returns a positive distance value.

    Example: Range=8

    Field of view of ultrasonic sensor, specified as a positive real scalar of the form [horizontalFOV, verticalFOV], in the range (0,180). Units are in degrees. horizontalFOV and verticalFOV define the total angular extent spanned by the sensor in the horizontal direction and the vertical direction, respectively.

    Example: FieldOfView=[40 30]

    Output Arguments

    expand all

    Actor object, returned as a sim3d.sensors.UltrasonicSensor object.

    Properties

    expand all

    Parent of actor, specified as a handle to the parent actor object. After you add an actor to the sim3d.World object, the default parent actor is the Scene Origin at (0,0,0). Use Parent to set any actor in the 3D environment as the parent actor of a sim3d.sensors.UltrasonicSensor object.

    This property is read-only.

    Children of actor, specified as a structure. Each field of the structure contains a handle to the child of a sim3d.sensors.UltrasonicSensor object.

    Parent world, specified as a handle to the parent sim3d.World object. You can use this property only if the sim3d.sensors.UltrasonicSensor object is added to the parent sim3d.World object.

    This property is read-only.

    Unique ID of the sensor, specified as a real positive scalar.

    Data Types: uint32

    Since R2025a

    Coordinate system that the actor uses for translation and rotation in the 3D environment, specified as one of these listed values:

    • 'Default' – World coordinate system

    • 'MATLAB' – MATLAB coordinate system

    • 'ISO8855' – ISO 8855 standard coordinate system

    • 'AERO' – SAE coordinate system

    • 'VRML' – X3D ISO standard coordinate system

    • 'SAE' – SAE coordinate system

    For more details on the different coordinate systems, see Coordinate Systems in Simulink 3D Animation.

    Data Types: string

    Relative translation (x,y,z) of the actor object to its parent actor, specified as a real 1-by-3 vector. Use Translation to change the position of the sim3d.sensors.UltrasonicSensor object in the 3D environment along the X, Y, and Z axes of the coordinate system. When you add an actor to the 3D environment, the default parent actor is the Scene Origin at (0,0,0).

    Example: ultrasonicSensor.Translation = [1 2 1]

    Relative rotation (roll, pitch, yaw) of the actor object to its parent actor, specified as a real 1-by-3 vector. Use Rotation to rotate the sim3d.sensors.UltrasonicSensor object in the 3D environment.

    Example: ultrasonicSensor.Rotation = [pi/4 pi/8 pi/2]

    Type of actor mobility to respond to physics, move the actor during simulation, or both, specified as 'sim3d.utils.MobilityTypes.Movable' or 'sim3d.utils.MobilityTypes.Static'. When Mobility is 'sim3d.utils.MobilityTypes.Static', only the Material Attributes are run-time configurable. When you set Mobility to 'sim3d.utils.MobilityTypes.Movable', all the actor properties become run-time configurable, allowing for programmatic interaction. For more details on programmatic interaction, see Programmatic Interaction.

    Example: ultrasonicSensor.Mobility = sim3d.utils.MobilityTypes.Movable

    Data Types: sim3d.utils.MobilityTypes

    Detection range of the ultrasonic sensor, specified as a scalar. Units are in meters. This value determines the detections and distance values returned by the ultrasonic sensor. When the detected object is at a distance within the Range, the sensor returns a positive distance value.

    Example: ultrasonicSensor.Range = 4

    Field of view of ultrasonic sensor, specified as a positive real scalar of the form [horizontalFOV, verticalFOV], in the range (0,180). Units are in degrees. horizontalFOV and verticalFOV define the total angular extent spanned by the sensor in the horizontal direction and the vertical direction, respectively.

    Example: ultrasonicSensor.FieldOfView = [40 30]

    Object Functions

    readReturn target detections using ultrasonic sensor

    Examples

    collapse all

    Since R2025a

    Create a ultrasonic sensor in the 3D environment using the sim3d.sensors.UltrasonicSensor object. You can adjust the detection range and field of view of the sensor for your scene and test scenario. Use the read function to extract the target detection from the 3D environment.

    Create a 3D environment and set up communication with the Unreal Engine simulation environment using the output function OutputImpl and the update function UpdateImpl. The sim3d.World object can send and receive data about the 3D environment to and from the Unreal Engine at each simulation step using output and update functions, respectively. Before the Unreal Engine simulates, MATLAB calls the output function and sends data to the Unreal Engine. Then, the Unreal Engine executes at each time step and sends data to MATLAB in the update function. You can use the update function to read this data or change values after each simulation step.

    world = sim3d.World('Output',@outputImpl,'Update',@updateImpl);

    Create a box actor in the 3D environment using the sim3d.Actor object and add the box to the world.

    cube = sim3d.Actor( ...
            ActorName="Cube", ...
            Mobility=sim3d.utils.MobilityTypes.Movable);
    createShape(cube,"box");
    add(world,cube);

    Create an ultrasonic sensor object using the sim3d.sensors.UltrasonicSensor object and set the location, range, and field of view of the sensor. Add the UltrasonicSensor to the world.

    UltrasonicSensor = sim3d.sensors.UltrasonicSensor( ...
            ActorName = "UltrasonicSensor", ...
            Translation = [-3, 0, 0], ...
            Range = 10.0, ...
            FieldOfView = [20 20]);
    add(world,UltrasonicSensor);

    Set the Simulation 3D Viewer window point of view and run the co-simulation.

    viewport = createViewport(world,Translation=[-3 0 0]);
    sampletime = 0.5;
    stoptime = 10;
    run(world,sampletime,stoptime);
    2.499793e+00 1.819821e-02 0
    2.499172e+00 3.639460e-02 0
    2.498137e+00 5.458734e-02 0
    2.496687e+00 7.277462e-02 0
    2.491871e+00 1.499477e-01 0
    2.489009e+00 1.679558e-01 0
    2.485736e+00 1.859328e-01 0
    2.482051e+00 2.038770e-01 0
    2.477953e+00 2.217864e-01 0
    2.473444e+00 2.396593e-01 0
    2.468521e+00 2.574937e-01 0
    2.463184e+00 2.752877e-01 0
    2.457434e+00 2.930396e-01 0
    2.451269e+00 3.107473e-01 0
    2.444688e+00 3.284091e-01 0
    2.437690e+00 3.460229e-01 0
    2.430275e+00 3.635869e-01 0
    2.422441e+00 3.810993e-01 0
    2.414569e+00 3.965727e-01 0
    2.410634e+00 3.906916e-01 0
    2.406754e+00 3.847855e-01 0
    

    Output Function

    The output function sends data about the actor to the Unreal Engine environment at each simulation step. For this example, the function rotates the Cube about its Z-axis by updating the Rotation property of the Cube at each simulation step.

    function outputImpl(world)
        world.Actors.Cube.Rotation(3) = world.Actors.Cube.Rotation(3) ...
        + 0.01; 
    end

    Update Function

    The update function reads data from the Unreal Engine environment at each simulation step. For this example, the update function uses the read function of the sim3d.sensors.UltrasonicSensor object to get the distance to the closest target from the UltrasonicSensor in the Unreal Engine environment.

    function updateImpl(world)
        [~, point] = world.Actors.UltrasonicSensor.read();
        fprintf("%d %d %d\n", point(1), point(2), point(3));
    end

    Version History

    Introduced in R2024b

    expand all