Main Content

setParticles

Set particles from localization algorithm

Since R2024a

    Description

    setParticles(mcl,particles) sets the particle poses and sets the weights of each particle to 1/n, where n is the number of particles in the mcl object at the current timestep.

    setParticles(mcl,particles,weights) specifies the poses and weights to set to each of the specified particles.

    example

    Examples

    collapse all

    Set particles from the particle filter used in the monteCarloLocalization object.

    Create a map and a monteCarloLocalization object.

    sm = likelihoodFieldSensorModel;
    sm.Map = binaryOccupancyMap(10,10,20);
    mcl = monteCarloLocalization(UseLidarScan=true);
    mcl.SensorModel = sm;

    Call the reset method to initialize the mcl. The monteCarloLocalization object must be initialized using the step or reset methods first before you can use the setParticles function.

    reset(mcl);

    Load particle states and weights to use to set the particles in the monteCarloLocalization object.

    load("particleInfo.mat")
    setParticles(mcl,particleStates,weights);

    Create lidar scan data. Create 300 range with measured values of 10 meters. Set the middle values to 2 meters to represent an obstacle. Create corresponding angles for the range readings and set the angle range to be -pi/2 to pi/2 radians.

    ranges = 10*ones(1,300);
    ranges(1,130:170) = 2.0;
    angles = linspace(-pi/2,pi/2,300);
    scan = lidarScan(ranges,angles);

    Call mcl on a pose at the origin with the created lidar scan data.

    [isUpdated,particlePoses,covariance] = mcl([0 0 0],scan)
    isUpdated = logical
       1
    
    
    particlePoses = 1×3
    
        0.0075    0.0518   -0.0010
    
    
    covariance = 3×3
    
        0.8914    0.0100         0
        0.0100    0.9305         0
             0         0    0.9823
    
    

    Input Arguments

    collapse all

    monteCarloLocalization object, specified as an object handle.

    Estimation particles, specified as an n-by-3 matrix, where each row corresponds to the position and orientation of a single particle [x y theta]. n is the total number of particles to set and must be equal to the number of particles in mcl. Use the output particle poses and weights from getParticles to determine the number of particles at the current timestep and set particle poses and weights.

    Weights of particles, returned as a n-element column vector. Each row corresponds to the weight of the particle in the matching row of particles. These weights are used in the final estimate of the pose of the vehicle. n is the total number of weights to set and must be equal to the number of particles in mcl. Use the output particle poses and weights from getParticles to determine the number of particles at the current timestep and set particle poses and weights.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    Version History

    Introduced in R2024a