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
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.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)