Create Obstacle Scene Using UAV Scenario
Remove all variables from the workspace and closes all figures.
Create a UAV scenario and set the simulation update rate to 2 Hz.
Add polygonal obstacle meshes to the scene.
Visualize the scenario in 3D.
Create Multirotor Guidance Model and UAV Waypoint Follower
Create the multirotor
guidance model.
Create the waypoint follower. Set the UAV type, transition radius for each waypoint, and the waypoints for the UAV to follow.
Create UAV Platform and Mount Sensor
Specify the initial pose of the UAV.
Create a quadrotor platform using a north-east-down (NED) reference frame. Specify the initial position and orientation.
Add a quadrotor mesh for visualization. Add a rotation to orient the mesh to the UAV body frame.
Create a statistical sensor model to generate point clouds for the lidar sensor.
Create a lidar sensor and mount the sensor on the quadrotor. Specify a mounting location for the sensor that is relative to the UAV body frame.
Set Up Simulation and Visualize Scenario
Visualize the scene. Remove edges from the floor mesh.
Update the plot view for better visibility.
Configure ControllerVFH3D
Object and Integrate with Waypoint Follower
Create a controllerVFH3D
object. Configure the HistogramResolution
and MaxAge
properties of the controllerVFH3D
object to improve the obstacle avoidance. A low HistogramResolution
value captures most of the obstacle points, but increases computation time. You can use the MaxAge
property to store obstacle points from previous time steps for computation of a stable obstacle-free direction, and for avoiding local minima. Set the sensor-related parameters.
The navigation logic uses two lookahead distances: one for the waypoint follower, and the other for the controllerVFH3D
object. The lookahead point for the waypoint follower is the target position for the controllerVFH3D
object. Compute the desired position for the PID controller from the desired direction output of the controllerVFH3D
object. A larger lookahead distance for the waypoint follower ensures that the target position is ahead of the desired position. This improves tracking by the PID position controller.
Tune ControllerVFH3D
Object
The controllerVFH3D
object has a number of properties that you must tune to obtain the desired obstacle avoidance behavior.
Tune Lidar Sensor Range
Modify the obstacle avoidance algorithm behavior can be modified by changing the range sensor distance limits of the controllerVFH3D
object.
UAV Path Weights
Modify the path of the UAV by changing the weights associated with the target direction, previous direction, and desired direction.
UAV Radius
Modify the UAV radius and the safety distance properties to control the proximity of the UAV to the obstacles.
Simulate Obstacle Avoidance in UAV Scenario
Set up the simulation. Then, iterate through the positions and show the scene each time the lidar sensor updates. Advance the scene, move the UAV platform, and update the sensors.
setup(scene)
% Simulate
for idx = 1:numIter
[isupdated,lidarSampleTime,pt] = read(lidar);
xLidar = reshape(pt.Location(:,:,1),[],1);
yLidar = reshape(pt.Location(:,:,2),[],1);
zLidar = reshape(pt.Location(:,:,3),[],1);
sensorPoints = [xLidar yLidar zLidar];
[targetPosition,~,desYaw] = wf(uavPose(1:4),lookaheadWF);
[desiredDirection,desiredYaw,status] = vfh3D(uavPosition,uavOrientation, ...
sensorPoints,targetPosition);
% Visualize the histogram.
figure(2)
show(vfh3D,PlotsToShow="2D Inflated Histogram");
% Select desired position along the obtacle-free direction.
desiredPosition = uavPosition + lookaheadOA*desiredDirection;
[t,y] = ode23(@(t,x)exampleHelperDerivative(t,x,model,desiredPosition,desiredYaw,kp,kd,ki,kyaw), ...
tStart:hStep:tStart + tStep,initialState);
% Store data.
tTotal((idx-1)*(tStep/hStep+1) + 1:idx*(tStep/hStep+1)) = t;
states((idx-1)*(tStep/hStep+1) + 1:idx*(tStep/hStep+1),:) = y;
% Update states.
uavPosition = y(end,1:3)';
uavOrientEul = y(end,7:9)';
uavOrientation = eul2quat(uavOrientEul')';
tStart = t(end);
initialState = y(end,:);
% Plot the path.
desPosition = plot3(desiredPosition(2),desiredPosition(1),-desiredPosition(3),".g",Parent=ax);
tgtPosition = plot3(targetPosition(2),targetPosition(1),-targetPosition(3),".r",Parent=ax);
goalPosition = plot3(wf.Waypoints(end,2),wf.Waypoints(end,1),-wf.Waypoints(end,3),"ob",Parent=ax);
legend(ax,[desPosition tgtPosition goalPosition],["Desired Position","Target Position","Goal"])
if isupdated
% Use fast update to move platform visualization frames.
show3D(scene,Parent=ax,Time=lidarSampleTime,FastUpdate=true);
% % Adjust the plot view
% xlim([2.0 24.5])
% ylim([-7.3 30.3])
% zlim([-0.3 14.7])
% view([-134.3 46.0])
% Move the platform.
move(plat,[uavPosition' y(end,4:6) zeros(1,3) eul2quat(uavOrientEul') zeros(1,3)]);
drawnow limitrate
end
% Advance scene simulation time.
advance(scene);
% Update all sensors in the scene.
updateSensors(scene)
uavPose = [uavPosition; quat2eul(uavOrientation',"ZYX")'];
end
Visualize Path
Visualize the path of the UAV through the environment.