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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)