1 function [q gyro accel rpy time] = analyzeINSGPS(fn)
2 % Analyzes data collected from SerialLogger while DUMP_FRIENDLY
5 % [q gyro accel time] = analyzeINSGPS(fn)
12 while ischar(tline) && ~isempty(tline)
15 c = textscan(tline,'q: %f %f %f %f');
16 data(i).q = [c{:}] / 1000;
18 c = textscan(tline,'b: %f');
22 c = textscan(tline,'m: %f %f %f');
25 c = textscan(tline,'a: %f %f %f %f');
26 data(i).accel = [c{:}] / 1000;
28 c = textscan(tline,'g: %f %f %f %f');
29 data(i).gyro = [c{:}] / 1000;
36 b = [data.block]; % get block counts
37 gaps = find(diff(b) ~= 1);
38 if(gaps) % get biggest contiguous chunk
39 gaps = [1 gaps length(b)];
41 [foo idx] = max(lengths);
42 idx = gaps(idx):gaps(idx+1);
46 data(end) = []; % delete in case partial update
49 accel = cat(1,data.accel);
50 gyro = cat(1,data.gyro);
51 time = (1:size(q,1)) / 50;
53 rpy = Quaternion2RPY(q);
60 ylabel('RPY'); legend('Roll','Pitch','Yaw');
71 function rpy = Quaternion2RPY(q)
75 q = bsxfun(@rdivide,q,sqrt(sum(q.^2,2)));
78 R13 = 2*(q(:,2).*q(:,4) -q(:,1).*q(:,3)); %2*(q[1]*q[3]-q[0]*q[2]);
79 R11 = qs(:,1) + qs(:,2) - qs(:,3) - qs(:,4); %q0s+q1s-q2s-q3s;
80 R12 = 2*(q(:,2).*q(:,3) + q(:,1).*q(:,4)); %2*(q[1]*q[2]+q[0]*q[3]);
81 R23 = 2*(q(:,3).*q(:,4) + q(:,1) .* q(:,2)); %2*(q[2]*q[3]+q[0]*q[1]);
82 R33 = qs(:,1)-qs(:,2)-qs(:,3)+qs(:,4); %q0s-q1s-q2s+q3s;
84 rpy(:,2)=RAD2DEG*asin(-R13); % pitch always between -pi/2 to pi/2
85 rpy(:,3)=RAD2DEG*atan2(R12,R11);
86 rpy(:,1)=RAD2DEG*atan2(R23,R33);