function newlog = compute_errors(log, res, perc, max_theta) m3 = -res.est_r_l / res.est_d; m4 = res.est_r_r / res.est_d; n = size(log.T,2); for k=1:n T_k = log.T(k); w0 = ( m3 * log.phi_l(k) + m4 * log.phi_r(k) ); o_theta(k) = T_k * w0; c3 = 0.5 * T_k * (-m3 * log.phi_l(k) + m4 * log.phi_r(k)); if abs(w0*T_k) > 1e-7 c4 = c3 * ( sin(w0*T_k) / (w0*T_k) ); c5 = c3 * ( (1-cos(w0*T_k)) / (w0*T_k) ); else c4 = c3 * 1; c5 = c3 * 0; end o_x(k) = c4 * res.est_d; o_y(k) = c5 * res.est_d; o_k = [o_x(k);o_y(k);o_theta(k)]; sm_k = log.sm(:,k); e_k = oplus(res.est_laser, sm_k) - oplus( o_k, res.est_laser); errors(:,k) = e_k; end worst_indexes = []; threshold = perc; for j=1:2 if j==1 err = abs(errors(3,:)); else err = sqrt(errors(1,:).^2 + errors(2,:).^2); end cut = floor(threshold*size(err,2)); err_sorted = sort(err); th = err_sorted(cut); worst = find(err > th); fprintf('%d: cutting %d / %d\n', j, size(worst,2), size(err,2)); worst_indexes = [worst_indexes worst]; end worst = find(rad2deg(abs(o_theta)) < max_theta); worst_indexes = [worst_indexes worst]; worst = find(rad2deg(abs(log.sm(3,:))) < max_theta); worst_indexes = [worst_indexes worst]; worst_indexes = unique(worst_indexes); fprintf('Unique: %d \n', size(worst_indexes,2)); good_indexes = setdiff(1:n, worst_indexes); newlog = select_indexes(log, good_indexes); newlog.good_indexes = good_indexes; newlog.worst_indexes = worst_indexes; newlog.old_residuals = errors; newlog.o_theta = o_theta; % plot errors if false figure subplot(3,1,1); plot(rad2deg(o_theta), rad2deg(log.sm(3,:)), 'r.'); axis('equal'); title('odometry \theta vs laser \theta'); %polyfit(rad2deg(o_theta), rad2deg(log.sm(3,:)), 1) xlabel('odometry \theta (deg)'); ylabel('laser \theta (deg)'); subplot(3,1,2); plot(rad2deg(o_theta), rad2deg(errors(3,:)), '.k'); axis('equal'); title('odometry \theta vs \theta error'); xlabel('odometry \theta (deg)'); ylabel('\theta error (deg)'); subplot(3,1,3); plot(1000*errors(1,:), 1000*errors(2,:), '.k'); axis('equal'); title('x,y error'); xlabel('x errors (mm) '); ylabel('y errors (mm) '); end if false figure; hold on; for i=1:n x1 = o_x(i); y1 = o_y(i); x2 = log.sm(1,i); y2 = log.sm(2,i); plot([x1 x2],[y1 y2],'r-') end axis('equal'); end if false figure hist(rad2deg(o_theta), -5:.2:5); title('o_theta'); figure hist(rad2deg(log.sm(3,:)), -5:.2:5); title('sm_theta'); figure hist(mod(rad2deg(log.sm(3,:)), 4 * 270/1024), 100); title('sm_theta'); end