Read and Plot Real-Time Data from BNO055 Sensor in NDOF Mode
This example shows you how to read and plot calibrated data from a Bosch Sensortec BNO055 a 9-axis absolute orientation sensor in real time. To read the orientation values directly from the BNO055 sensor, configure the sensor in the NDOF operating mode.
BNO055 is a 9-axis sensor with an integrated accelerometer, gyroscope, and magnetometer. The accelerometer measures acceleration, the gyroscope measures angular velocity, and the magnetometer measures the magnetic field along x-, y-, and z-axis. The axis of the sensor depends on the make of the sensor.
Required Products
MATLAB®
MATLAB Support Package for Arduino® Hardware
Required Hardware
Arduino Uno
Bosch BNO055 Sensor
Hardware Connection
Connect the SDA, SCL, GND, and the VCC pins of the BNO055 sensor to the corresponding pins on the Arduino hardware. This example uses an Arduino Uno board with the following connections:
SDA — A4
SCL — A5
VCC — +5V
GND — GND
Ensure that the connections to the sensor is intact. We recommended that you use a prototype shield and solder the sensor to it to avoid loose connections when moving the sensor. For information on debugging sensor related issues, see Troubleshooting Sensors.
Create Connection to BNO055 Sensor in NDOF Mode
Create a connection to the BNO055 sensor in the NDOF mode. Ensure that you create the arduino object with the library set to 'I2C'
.
arduinoObj = arduino('COM7', 'Uno', 'Libraries', 'I2C');bno = bno055(arduinoObj, 'OperatingMode', 'ndof');
Calibrate BNO055 Sensor
Calibrate the sensor using the steps described in Calibrate BNO055 Sensors.
% Accelerometer calibration flagaccCalib = 0; % Gyroscope calibration flaggyrCalib = 0; % Magnetometer calibration flagmagCalib = 0; fprintf('Calibrating the BNO055 sensor . . . \n');while(prod([accCalib, gyrCalib, magCalib]) ~= 1) if strcmpi(bno.readCalibrationStatus.Accelerometer, "full") && isequal(accCalib, 0) accCalib = 1; fprintf('Accelerometer is calibrated! . . .\n'); end if strcmpi(bno.readCalibrationStatus.Gyroscope, "full") && isequal(gyrCalib, 0) gyrCalib = 1; fprintf('Gyroscope is calibrated! . . .\n'); end if(strcmpi(bno.readCalibrationStatus.Magnetometer, "full"))&& isequal(magCalib, 0) magCalib = 1; fprintf('Magnetometer is calibrated! . . .\n'); endendfprintf('BNO055 sensor is fully calibrated!\n');
Read Sensor Data
Read the orientation, acceleration, angular velocity, and magnetic field strength from the BNO055 sensor.
Estimate the time between consecutive read cycles.
% Provide time frame in secondssenseFrame = 60; % Measure approximate execution time of a single read cycletic;readOrientation(bno);readAcceleration(bno);readAngularVelocity(bno);readMagneticField(bno);tDelta = toc;% Number of samples to be collected in the senseFrame time framenumSamples = floor(senseFrame/tDelta); % Time vectortVector = linspace(0, senseFrame, numSamples); tCorrection = 0;recordedData = zeros(numSamples, 3, 4);
Set up the figure handles to plot the change in orientation, acceleration, and magnetic field strength as the device moves.
subplot(4, 1, 1)hold on% Create handle to Azimuth animatedline objecthAzimuth = animatedline('color', 'r', 'linewidth', 1.25); % Create handle to Pitch animatedline object hPitch = animatedline('color', 'k', 'linewidth', 1.25); % Create handle to Roll animatedline object hRoll = animatedline('color', 'b', 'linewidth', 1.25); legend('Azimuth (rad)','Pitch (rad)','Roll (rad)');ylabel('Euler Angles (rad)');xlabel('Time (s)');title('Reading Orientation of the BNO055 sensor', 'fontsize', 12);axis([0 senseFrame -6.5 6.5])grid minorhold offsubplot(4, 1, 2)hold on% Create handle to X-axis acceleration animatedline objecthAx = animatedline('color', 'r', 'linewidth', 1.25); % Create handle to Y-axis acceleration animatedline objecthAy = animatedline('color', 'k', 'linewidth', 1.25); % Create handle to Z-axis acceleration animatedline objecthAz = animatedline('color', 'b', 'linewidth', 1.25); legend('A_x (m/s^2)','A_y (m/s^2)','A_z (m/s^2)');ylabel('Acceleration (m/s^2)');xlabel('Time (s)');title('Reading Accelerometer values from BNO055 sensor', 'fontsize', 12);axis([0 senseFrame -30 30]);hold offgrid minorsubplot(4, 1, 3)% Create handle to X-axis angular velocity animatedline objecthVx = animatedline('color', 'r', 'linewidth', 1.25); % Create handle to Y-axis angular velocity animatedline objecthVy = animatedline('color', 'k', 'linewidth', 1.25); % Create handle to Z-axis angular velocity animatedline objecthVz = animatedline('color', 'b', 'linewidth', 1.25); legend('\omega_x (rad/s)','\omega_y (rad/s)','\omega_z (rad/s)');ylabel('Angular Velocity (rad/s)');xlabel('Time (s)');title('Reading Angular velocity values from BNO055 sensor', 'fontsize', 12);axis([0 senseFrame -10 10]);hold offgrid minorsubplot(4, 1, 4)% Create handle to X-axis magnetic field animatedline objecthMagx = animatedline('color', 'r', 'linewidth', 1.25); % Create handle to Y-axis magnetic field animatedline objecthMagy = animatedline('color', 'k', 'linewidth', 1.25); % Create handle to Z-axis magnetic field animatedline objecthMagz = animatedline('color', 'b', 'linewidth', 1.5); legend('\mu_x (\muT)','\mu_y (\muT)','\mu_z (\muT)');ylabel('Magnetic Field (\muT)');xlabel('Time (s)');title('Reading Magnetometer values from BNO055 sensor', 'fontsize', 12);axis([0 senseFrame -50 50]);hold offgrid minor
Read and plot the calibrated data from the sensor.
for i = 1:numSamples % Read Orientation values (Euler angles) from the sensor [sensorVal, ~] = readOrientation(bno); % Read Acceleration values from the sensor [AccelVal, ~] = readAcceleration(bno); % Read Angular velocity values from the sensor [AngVelVal, ~] = readAngularVelocity(bno); % Read Magnetic field strength values from the sensor [MagVal, ~] = readMagneticField(bno); tic; addpoints(hAzimuth, tVector(i) + tCorrection,sensorVal(1)); addpoints(hPitch, tVector(i) + tCorrection,sensorVal(2)); addpoints(hRoll, tVector(i) + tCorrection,sensorVal(3)); addpoints(hAx, tVector(i) + tCorrection, AccelVal(1)); addpoints(hAy, tVector(i) + tCorrection, AccelVal(2)); addpoints(hAz, tVector(i) + tCorrection, AccelVal(3)); addpoints(hVx, tVector(i) + tCorrection, AngVelVal(1)); addpoints(hVy, tVector(i) + tCorrection, AngVelVal(2)); addpoints(hVz, tVector(i) + tCorrection, AngVelVal(3)); addpoints(hMagx, tVector(i) + tCorrection, MagVal(1)); addpoints(hMagy, tVector(i) + tCorrection, MagVal(2)); addpoints(hMagz, tVector(i) + tCorrection, MagVal(3)); recordedData(i, :, 1) = sensorVal; recordedData(i, :, 2) = AccelVal; recordedData(i, :, 3) = AngVelVal; recordedData(i, :, 4) = MagVal; tCorrection = toc; drawnow;end
Clean Up
When the connection is no longer needed, release and clear the objects.
release(bno);clear arduinoObj;clear bno;
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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:
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)
- 中国
- 简体中文
- English
- 日本 (日本語)
- 한국 (한국어)
Contact your local office