Updating sf_orientation implementation for quaternion
[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 RAD2DEG = 57.2957795;
30
31 Max_Range_Accel = 39.203407; Min_Range_Accel = -39.204006; Res_Accel = 0.000598;
32 Max_Range_Gyro = 1146.862549; Min_Range_Gyro = -1146.880005; Res_Gyro = 0.017500;
33 Max_Range_Magnetic = 1200; Min_Range_Magnetic = -1200; Res_Magnetic = 1;
34
35 PITCH_PHASE_CORRECTION = -1;
36 ROLL_PHASE_CORRECTION = -1;
37 YAW_PHASE_CORRECTION = -1;
38
39 Bias_Ax = 0.098586;
40 Bias_Ay = 0.18385;
41 Bias_Az = 10.084 - GRAVITY;
42
43 Bias_Gx = -5.3539;
44 Bias_Gy = 0.24325;
45 Bias_Gz = 2.3391;
46
47 Bias_Mx = 0;
48 Bias_My = 0;
49 Bias_Mz = 0;
50
51 Sign_Mx = 1;
52 Sign_My = 1;
53 Sign_Mz = 1;
54
55 BUFFER_SIZE = 1095;
56
57 Accel_data = zeros(4,BUFFER_SIZE);
58 Gyro_data = zeros(4,BUFFER_SIZE);
59 Mag_data =  zeros(4,BUFFER_SIZE);
60
61 OR_driv = zeros(3,BUFFER_SIZE);
62 OR_aid = zeros(3,BUFFER_SIZE);
63 OR_err = zeros(3,BUFFER_SIZE);
64
65 euler = zeros(BUFFER_SIZE,3);
66
67 % Sensor Data simulating orientation motions
68
69 % get accel x,y,z axis data from stored file
70 Accel_data(1,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/accel.txt")(:,1))') - Bias_Ax)(1:BUFFER_SIZE);
71 Accel_data(2,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/accel.txt")(:,2))') - Bias_Ay)(1:BUFFER_SIZE);
72 Accel_data(3,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/accel.txt")(:,3))') - Bias_Az)(1:BUFFER_SIZE);
73 Accel_data(4,:) = ((dlmread("data/100ms/orientation/roll_pitch_yaw/accel.txt")(:,4))')(1:BUFFER_SIZE);
74
75 % get gyro x,y,z axis data from stored file
76 Gyro_data(1,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/gyro.txt")(:,1))') - Bias_Gx)(1:BUFFER_SIZE);
77 Gyro_data(2,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/gyro.txt")(:,2))') - Bias_Gy)(1:BUFFER_SIZE);
78 Gyro_data(3,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/gyro.txt")(:,3))') - Bias_Gz)(1:BUFFER_SIZE);
79 Gyro_data(4,:) = ((dlmread("data/100ms/orientation/roll_pitch_yaw/gyro.txt")(:,4))')(1:BUFFER_SIZE);
80
81 scale_Gyro = 575;
82 Gyro_data(1,:) = Gyro_data(1,:)/scale_Gyro;
83 Gyro_data(2,:) = Gyro_data(2,:)/scale_Gyro;
84 Gyro_data(3,:) = Gyro_data(3,:)/scale_Gyro;
85
86 % get magnetometer x,y,z axis data from stored file
87 Mag_data(1,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/magnetic.txt")(:,1))') + Bias_Mx)(1:BUFFER_SIZE) * Sign_Mx;
88 Mag_data(2,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/magnetic.txt")(:,2))') + Bias_My)(1:BUFFER_SIZE) * Sign_My;
89 Mag_data(3,:) = (((dlmread("data/100ms/orientation/roll_pitch_yaw/magnetic.txt")(:,3))') + Bias_Mz)(1:BUFFER_SIZE) * Sign_Mz;
90 Mag_data(4,:) = ((dlmread("data/100ms/orientation/roll_pitch_yaw/magnetic.txt")(:,4))')(1:BUFFER_SIZE);
91
92 % estimate orientation
93 [Quat_driv, Quat_aid, Quat_err]  = estimate_orientation(Accel_data, Gyro_data, Mag_data);
94
95 euler = quat2euler(Quat_aid);
96 OR_aid(1,:) = euler(:,2)' * RAD2DEG;
97 OR_aid(2,:) = euler(:,1)' * RAD2DEG;
98 OR_aid(3,:) = euler(:,3)' * RAD2DEG;
99
100 euler = quat2euler(Quat_driv);
101 OR_driv(1,:) = ROLL_PHASE_CORRECTION * euler(:,2)' * RAD2DEG;
102 OR_driv(2,:) = PITCH_PHASE_CORRECTION * euler(:,1)' * RAD2DEG;
103 OR_driv(3,:) = YAW_PHASE_CORRECTION * euler(:,3)' * RAD2DEG;
104
105 euler = quat2euler(Quat_err);
106 OR_err(1,:) = euler(:,2)' * RAD2DEG;
107 OR_err(2,:) = euler(:,1)' * RAD2DEG;
108 OR_err(3,:) = euler(:,3)' * RAD2DEG;
109
110 % Rotation Plot Results
111 hfig=(figure);
112 scrsz = get(0,'ScreenSize');
113 set(hfig,'position',scrsz);
114 subplot(3,1,1)
115 UA = OR_aid(2,:);
116 p1 = plot(1:length(UA),UA(1,1:length(UA)),'r');
117 hold on;
118 grid on;
119 UA = OR_driv(2,:);
120 p2 = plot(1:length(UA),UA(1,1:length(UA)),'b');
121 hold on;
122 grid on;
123 UA = OR_err(2,:);
124 p3 = plot(1:length(UA),UA(1,1:length(UA)),'g');
125 title(['Pitch']);
126 legend([p1 p2 p3],'Aiding System', 'Driving System', 'Quaternion based error');
127 subplot(3,1,2)
128 UA = OR_aid(1,:);
129 p1 = plot(1:length(UA),UA(1,1:length(UA)),'r');
130 hold on;
131 grid on;
132 UA = OR_driv(1,:);
133 p2 = plot(1:length(UA),UA(1,1:length(UA)),'b');
134 hold on;
135 grid on;
136 UA = OR_err(1,:);
137 p3 = plot(1:length(UA),UA(1,1:length(UA)),'g');
138 title(['Roll']);
139 legend([p1 p2 p3],'Aiding System', 'Driving System', 'Quaternion based error');
140 subplot(3,1,3)
141 UA = OR_aid(3,:);
142 p1 = plot(1:length(UA),UA(1,1:length(UA)),'r');
143 hold on;
144 grid on;
145 UA = OR_driv(3,:);
146 p2 = plot(1:length(UA),UA(1,1:length(UA)),'b');
147 hold on;
148 grid on;
149 UA = OR_err(3,:);
150 p3 = plot(1:length(UA),UA(1,1:length(UA)),'g');
151 title(['Yaw']);
152 legend([p1 p2 p3],'Aiding System', 'Driving System', 'Quaternion based error');