Separating sensor orientation as new function call
[platform/core/system/sensord.git] / src / sensor_fusion / design / sf_orientation.m
1 % sf_orientation
2 %
3 % Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 %
5 % Licensed under the Apache License, Version 2.0 (the "License");
6 % you may not use this file except in compliance with the License.
7 % You may obtain a copy of the License at
8 %
9 % http://www.apache.org/licenses/LICENSE-2.0
10 %
11 % Unless required by applicable law or agreed to in writing, software
12 % distributed under the License is distributed on an "AS IS" BASIS,
13 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 % See the License for the specific language governing permissions and
15 % limitations under the License.
16
17 % Sensor Fusion Implementation for Orientation Estimation
18 %
19 % - Input Accelerometer, Gyroscope and Magnetometer sensor data
20 % - Call estimate_orientation function
21 % - Plot results for orientation
22
23 addpath('lib');
24 clear
25 close all
26 clc
27
28 GRAVITY = 9.80665;
29
30 Max_Range_Accel = 39.203407; Min_Range_Accel = -39.204006; Res_Accel = 0.000598;
31 Max_Range_Gyro = 1146.862549; Min_Range_Gyro = -1146.880005; Res_Gyro = 0.017500;
32 Max_Range_Magnetic = 1200; Min_Range_Magnetic = -1200; Res_Magnetic = 1;
33
34 Bias_Ax = 0.098586;
35 Bias_Ay = 0.18385;
36 Bias_Az = 10.084 - GRAVITY;
37
38 Bias_Gx = -5.3539;
39 Bias_Gy = 0.24325;
40 Bias_Gz = 2.3391;
41
42 BUFFER_SIZE = 1095;
43
44 Accel_data = zeros(4,BUFFER_SIZE);
45 Gyro_data = zeros(4,BUFFER_SIZE);
46 Mag_data =  zeros(4,BUFFER_SIZE);
47
48 OR_driv = zeros(3,BUFFER_SIZE);
49 OR_aid = zeros(3,BUFFER_SIZE);
50 OR_err = zeros(3,BUFFER_SIZE);
51
52 % Sensor Data simulating orientation motions
53
54 % get accel x,y,z axis data from stored file
55 Accel_data(1,:) = (((dlmread("data/100ms/roll_pitch_yaw/accel.txt")(:,1))') - Bias_Ax)(1:BUFFER_SIZE);
56 Accel_data(2,:) = (((dlmread("data/100ms/roll_pitch_yaw/accel.txt")(:,2))') - Bias_Ay)(1:BUFFER_SIZE);
57 Accel_data(3,:) = (((dlmread("data/100ms/roll_pitch_yaw/accel.txt")(:,3))') - Bias_Az)(1:BUFFER_SIZE);
58 Accel_data(4,:) = ((dlmread("data/100ms/roll_pitch_yaw/accel.txt")(:,4))')(1:BUFFER_SIZE);
59
60 % get gyro x,y,z axis data from stored file
61 Gyro_data(1,:) = (((dlmread("data/100ms/roll_pitch_yaw/gyro.txt")(:,1))') - Bias_Gx)(1:BUFFER_SIZE);
62 Gyro_data(2,:) = (((dlmread("data/100ms/roll_pitch_yaw/gyro.txt")(:,2))') - Bias_Gy)(1:BUFFER_SIZE);
63 Gyro_data(3,:) = (((dlmread("data/100ms/roll_pitch_yaw/gyro.txt")(:,3))') - Bias_Gz)(1:BUFFER_SIZE);
64 Gyro_data(4,:) = ((dlmread("data/100ms/roll_pitch_yaw/gyro.txt")(:,4))')(1:BUFFER_SIZE);
65
66 scale_Gyro = 575;
67 Gyro_data(1,:) = Gyro_data(1,:)/scale_Gyro;
68 Gyro_data(2,:) = Gyro_data(2,:)/scale_Gyro;
69 Gyro_data(3,:) = Gyro_data(3,:)/scale_Gyro;
70
71 % get magnetometer x,y,z axis data from stored file
72 Mag_data(1,:) = (((dlmread("data/100ms/roll_pitch_yaw/magnetic.txt")(:,1))'))(1:BUFFER_SIZE);
73 Mag_data(2,:) = (((dlmread("data/100ms/roll_pitch_yaw/magnetic.txt")(:,2))'))(1:BUFFER_SIZE);
74 Mag_data(3,:) = (((dlmread("data/100ms/roll_pitch_yaw/magnetic.txt")(:,3))'))(1:BUFFER_SIZE);
75 Mag_data(4,:) = ((dlmread("data/100ms/roll_pitch_yaw/magnetic.txt")(:,4))')(1:BUFFER_SIZE);
76
77 % estimate orientation
78 [OR_driv, OR_aid, OR_err]  = estimate_orientation(Accel_data, Gyro_data, Mag_data);
79
80 % Rotation Plot Results
81 hfig=(figure);
82 scrsz = get(0,'ScreenSize');
83 set(hfig,'position',scrsz);
84 subplot(3,1,1)
85 UA = OR_aid(2,:);
86 p1 = plot(1:length(UA),UA(1,1:length(UA)),'r');
87 hold on;
88 grid on;
89 UA = OR_driv(2,:);
90 p2 = plot(1:length(UA),UA(1,1:length(UA)),'b');
91 hold on;
92 grid on;
93 UA = OR_err(2,:);
94 p3 = plot(1:length(UA),UA(1,1:length(UA)),'g');
95 title(['Pitch']);
96 legend([p1 p2 p3],'Aiding System', 'Driving System', 'Quaternion based error');
97 subplot(3,1,2)
98 UA = OR_aid(1,:);
99 p1 = plot(1:length(UA),UA(1,1:length(UA)),'r');
100 hold on;
101 grid on;
102 UA = OR_driv(1,:);
103 p2 = plot(1:length(UA),UA(1,1:length(UA)),'b');
104 hold on;
105 grid on;
106 UA = OR_err(1,:);
107 p3 = plot(1:length(UA),UA(1,1:length(UA)),'g');
108 title(['Roll']);
109 legend([p1 p2 p3],'Aiding System', 'Driving System', 'Quaternion based error');
110 subplot(3,1,3)
111 UA = OR_aid(3,:);
112 p1 = plot(1:length(UA),UA(1,1:length(UA)),'r');
113 hold on;
114 grid on;
115 UA = OR_driv(3,:);
116 p2 = plot(1:length(UA),UA(1,1:length(UA)),'b');
117 hold on;
118 grid on;
119 UA = OR_err(3,:);
120 p3 = plot(1:length(UA),UA(1,1:length(UA)),'g');
121 title(['Yaw']);
122 legend([p1 p2 p3],'Aiding System', 'Driving System', 'Quaternion based error');