isMotionValid
Class: nav.StateValidator
Namespace: nav
Check if path between states is valid
Description
[
determines if the motion between two states is valid by interpolating between states. The
function also returns the last valid state along the path.isValid
,lastValid
] = isMotionValid(validatorObj
,state1
,state2
)
A default implementation for this method is provided when you call createPlanningTemplate
.
Input Arguments
validatorObj
— State validator object
object from a subclass of nav.StateValidator
State validator object, specified as an object from a subclass of
nav.StateValidator
. For provided state validator objects, see
validatorOccupancyMap
or validatorVehicleCostmap
.
state1
— Initial state position
n-element vector | m-by-n matrix of row vectors
Initial state position, specified as a n-element vector or
m-by-n matrix of row vectors.
n is the dimension of the state space specified in the state space
property in validatorObj
.
state2
— Final state position
n-element vector | m-by-n matrix of row vectors
Final state position, specified as a n-element vector or
m-by-n matrix of row vectors.
n is the dimension of the state space specified in the state space
property in validatorObj
.
Output Arguments
isValid
— Valid states
m-element vector of 1
s and
0
s
Valid states, specified as a m-element vector of
1
s and 0
s.
Data Types: logical
lastValid
— Final valid state along path
n-element vector
Final valid state along path, specified as a n-element vector.
n is the dimension of the state space specified in the state space
property in validatorObj
.
Examples
Create Custom State Space Validator for Path Planning
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.
createPlanningTemplate("StateValidator")
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 & ... matlabshared.planning.internal.EnforceScalarHandle properties % User-defined properties end
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.
methods function obj = MyCustomStateValidator(space) narginchk(0,1) if nargin == 0 space = MyCustomStateSpace; end obj@nav.StateValidator(space); % Initialize user-defined properties end
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); end
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) narginchk(2,2); 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); end
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) narginchk(3,3); 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."); end % 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; else isValid = false; lastValid = interpStates(firstInvalidIdx-1,:); end end
Terminate the methods and class sections.
end end
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.
Version History
Introduced in R2019b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)