createCustomSensorTemplate
Description
createCustomSensorTemplate
creates a sample implementation for UAV
custom sensor that inherits from the uav.SensorAdaptor
class. This function opens a new file in the MATLAB® Editor.
Examples
Simulate IMU Sensor Mounted on UAV
Create a sensor adaptor for an imuSensor
from Navigation Toolbox™ and gather readings for a simulated UAV flight scenario.
Create Sensor Adaptor
Use the createCustomSensorTemplate
function to generate a template sensor and update it to adapt an imuSensor
object for usage in UAV scenario.
createCustomSensorTemplate
This example provides the adaptor class uavIMU
, which can be viewed using the following command.
edit uavIMU.m
Use Sensor Adaptor in UAV Scenario Simulation
Use the IMU sensor adaptor in a UAV Scenario simulation. First, create the scenario.
scenario = uavScenario("StopTime", 8, "UpdateRate", 100);
Create a UAV platform and specify the trajectory. Add a fixed-wing mesh for visualization.
plat = uavPlatform("UAV", scenario, "Trajectory", ... waypointTrajectory([0 0 0; 100 0 0; 100 100 0], "TimeOfArrival", [0 5 8], "AutoBank", true)); updateMesh(plat,"fixedwing", {10}, [1 0 0], eul2tform([0 0 pi]));
Attach the IMU sensor using the uavSensor
object and specify the uavIMU
as an input. Load parameters for the sensor model.
imu = uavSensor("IMU", plat, uavIMU(imuSensor)); fn = fullfile(matlabroot,'toolbox','shared',... 'positioning','positioningdata','generic.json'); loadparams(imu.SensorModel,fn,"GenericLowCost9Axis");
Visualize the scenario.
figure ax = show3D(scenario); xlim([-20 200]); ylim([-20 200]);
Preallocate the simData
structure and fields to store simulation data. The IMU sensor will output acceleration and angular rates.
simData = struct; simData.Time = duration.empty; simData.AccelerationX = zeros(0,1); simData.AccelerationY = zeros(0,1); simData.AccelerationZ = zeros(0,1); simData.AngularRatesX = zeros(0,1); simData.AngularRatesY = zeros(0,1); simData.AngularRatesZ = zeros(0,1);
Setup the scenario.
setup(scenario);
Run the simulation using the advance
function. Update the sensors and record the data.
updateCounter = 0; while true % Advance scenario. isRunning = advance(scenario); updateCounter = updateCounter + 1; % Update sensors and read IMU data. updateSensors(scenario); [isUpdated, t, acc, gyro] = read(imu); % Store data in structure. simData.Time = [simData.Time; seconds(t)]; simData.AccelerationX = [simData.AccelerationX; acc(1)]; simData.AccelerationY = [simData.AccelerationY; acc(2)]; simData.AccelerationZ = [simData.AccelerationZ; acc(3)]; simData.AngularRatesX = [simData.AngularRatesX; gyro(1)]; simData.AngularRatesY = [simData.AngularRatesY; gyro(2)]; simData.AngularRatesZ = [simData.AngularRatesZ; gyro(3)]; % Update visualization every 10 updates. if updateCounter > 10 show3D(scenario, "FastUpdate", true, "Parent", ax); updateCounter = 0; drawnow limitrate end % Exit loop when scenario is finished. if ~isRunning break; end end
Visualize the simulated IMU readings.
simTable = table2timetable(struct2table(simData)); figure stackedplot(simTable, ["AccelerationX", "AccelerationY", "AccelerationZ", ... "AngularRatesX", "AngularRatesY", "AngularRatesZ"], ... "DisplayLabels", ["AccX (m/s^2)", "AccY (m/s^2)", "AccZ (m/s^2)", ... "AngularRateX (rad/s)", "AngularRateY (rad/s)", "AngularRateZ (rad/s)"]);
Version History
Introduced in R2021a
See Also
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 (한국어)