Main Content

nav.StateValidator class

Package: nav

Create state validator for path planning


nav.StateValidator is an interface for all state validators used for path planning. Derive from this class if you want to define your own state validator. This representation allows for state and motion validation.

To create a sample template for generating your own state space class, call createPlanningTemplate("StateValidator"). For specific implementations of the state validator class for general application, see State Validation in Motion Planning.

The nav.StateValidator class is a handle class.

Class Attributes


For information on class attributes, see Class Attributes.




ssObj = nav.StateValidator(stateSpace) creates a state validator object that validates states in the given state space. This constructor can only be called from a derived class. Create your own class definition using createPlanningTemplate.


expand all

State space definition, specified as an object of a subclass from nav.StateSpace. Specify this property using the stateSpace input on construction. You can also specify any of our predefined objects in the State Validation section from Motion Planning.

Example: stateSpaceSE2




expand all


collapse all

This example shows how to use the createPlanningTemplate function to generate a template for customizing your own state validation class. State validation is used with path planning algorithms to ensure valid paths. The template function provides a basic implementation for example purposes.

Call the create template function. This function generates a class definition file for you to modify for your own implementation. Save this file.


Class and Property Definition

The first part of the template specifies the class definition and any properties for the class. Derive from the nav.StateValidator class. You can specify any additional user-defined properties here.

classdef MyCustomStateValidator < nav.StateValidator & ...
       % User-defined properties

Save your custom state validator class and ensure your file name matches the class name.

Class Constructor

Use the constructor to set the name of the state space validator and specify the state space object. Set a default value for the state space if one is not provided. Call the constructor of the base class. Initialize any other user-defined properties. This example uses a default of MyCustomStateSpace, which was illustrated in the previous example.

        function obj = MyCustomStateValidator(space)
            if nargin == 0
                space = MyCustomStateSpace;

           % Initialize user-defined properties

Copy Semantics

Specify the copy method definition. Copy all the values of your user-defined variables into a new object, so copyObj is a deep copy. The default behavior given in this example creates a new copy of the object with the same type.

        function copyObj = copy(obj)
            copyObj = feval(class(obj), obj.StateSpace);

Check State Validity

Define how a given state is validated. The state input can either be a single row vector, or a matrix of row vectors for multiple states. Customize this function for any special validation behavior for your state space like collision checking against obstacles.

        function isValid = isStateValid(obj, state) 
            nav.internal.validation.validateStateMatrix(state, nan, obj.StateSpace.NumStateVariables, ...
                "isStateValid", "state");
            bounds = obj.StateSpace.StateBounds';
            inBounds = state >= bounds(1,:) & state <= bounds(2,:);
            isValid = all(inBounds, 2);

Check Motion Validity

Define how to generate the motion between states and determine if it is valid. For this example, use linspace to evenly interpolate between states and check if these states are valid using isStateValid. Customize this function to sample between states or consider other analytical methods for determining if a vehicle can move between given states.

        function [isValid, lastValid] = isMotionValid(obj, state1, state2)
            state1 = nav.internal.validation.validateStateVector(state1, ...
                obj.StateSpace.NumStateVariables, "isMotionValid", "state1");
            state2 = nav.internal.validation.validateStateVector(state2, ...
                obj.StateSpace.NumStateVariables, "isMotionValid", "state2");
            if (~obj.isStateValid(state1))
                error("statevalidator:StartStateInvalid", "The start state of the motion is invalid.");
            % Interpolate at a fixed interval between states and check state validity
            numInterpPoints = 100;
            interpStates = obj.StateSpace.interpolate(state1, state2, linspace(0,1,numInterpPoints));
            interpValid = obj.isStateValid(interpStates);
            % Look for invalid states. Set lastValid state to index-1.
            firstInvalidIdx = find(~interpValid, 1);
            if isempty(firstInvalidIdx)
                isValid = true;
                lastValid = state2;
                isValid = false;
                lastValid = interpStates(firstInvalidIdx-1,:);

Terminate the methods and class sections.


Save your state space validator class definition. You can now use the class constructor to create an object for validation of states for a given state space.

Extended Capabilities

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

Introduced in R2019b