clear all; clc; close all;
global Cc Co Ct tau fmax fmin i_ww
global rthr gthr bthr
tau = 10;
rthr = 60;
bthr = 60;
gthr = 60;
frr = 2;
frb = 1;
Cc = [1 1 .1 .1 .01 .01];
fmax = 0.144;
fmin = -0.045;
TLB = .4*0;
if (frr+frb~=3), error('Wrong front/rear combination'); end
i_ww = input('Enter 0 for no wave and wind or 1 for both: ');
delete(imaqfind);
vid = videoinput('dcam');
vid.ReturnedColorSpace = 'bayer';
vid.BayerSensorAlignment = 'rggb';
vid.ROIPosition = [60 40 960 720];
vid.FramesPerTrigger = 1;
triggerconfig(vid,'manual')
vid.TriggerRepeat = Inf;
src = getselectedsource(vid);
src.Gain = 500;
start(vid)
pause(0.5)
gflag = 0;
i = 0;
ij = 0;
time = 0;
while time <= tau;
trigger(vid)
[img time] = getdata(vid);
[rrpts crpts rgpts_t cgpts_t rbpts cbpts] = gsearch_rgb(img);
if (length(rrpts) ~= 2) && (length(rbpts) ~= 2) || (length(rrpts) ~= 1) && (length(rbpts) ~= 1)
display([num2str(time) 's red:' num2str(length(rrpts)) ' blue:' num2str(length(rbpts))])
ij = ij + 1
missed_leds(:,:,:,ij) = img;
else
i = i + 1;
if i == 1
time0 = time;
end
t(i,1) = time - time0;
if length(rrpts) == 2
r_tr = rrpts(1) + crpts(1);
r_tr2 = rrpts(2) + crpts(2);
b_tr = rbpts(1) + cbpts(1);
b_tr2 = rbpts(2) + cbpts(2);
if i == 1
frr1 = 1; frr2 = 2;
frb1 = 1; frb2 = 2;
else
if abs(r_tr-r_tr_old) < abs(r_tr2-r_tr_old)
frr1 = 1; frr2 = 2;
else
frr1 = 2; frr2 = 1;
end
if abs(b_tr-b_tr_old) < abs(b_tr2-b_tr_old)
frb1 = 1; frb2 = 2;
else
frb1 = 2; frb2 = 1;
end
end
ip(frr) = crpts(frr1);
jp(frr) = rrpts(frr1);
ip(frb) = cbpts(frb1);
jp(frb) = rbpts(frb1);
ijp = [ip jp];
[x(i) y(i) theta(i)] = cam_calib(ijp);
ip(frr) = crpts(frr2);
jp(frr) = rrpts(frr2);
ip(frb) = cbpts(frb2);
jp(frb) = rbpts(frb2);
ijp = [ip jp];
[xt(i) yt(i) thetat(i)] = cam_calib(ijp);
xt(i) = xt(i) - TLB*cos(thetat(i));
yt(i) = yt(i) - TLB*sin(thetat(i));
else
ip(2) = crpts(1);
jp(2) = rrpts(1);
ip(1) = cbpts(1);
jp(1) = rbpts(1);
ijp = [ip jp];
[x(i) y(i) theta(i)] = cam_calib(ijp);
xt(i) = 1.5 + 0.5*cos(2*pi*t(i)/20);
yt(i) = 2 + 0.5*sin(2*pi*t(i)/20);
thetat(i) = 2*pi*t(i)/20 + pi/2;
end
Cos(i,1) = length(cgpts)/4;
n_o = Cos(i,1);
if i == 1
if n_o == 0
Ct = [0 0 0 0 0 .2 .05 .05 0 0 0 .1 .1 1 .25 .25];
else
Ct = [0 0 0 0 0 .2 .1 .1 0 0 0 .1 .1 1 .1 .1];
end
Ct(1) = x(1); Ct(2) = y(1); Ct(3)=theta(1);
Ct(4) = xt(1); Ct(5) = yt(1);
end
Co(1) = n_o;
if n_o > 0
[pxobs pyobs] = cam_calib_obs(cgpts,rgpts);
d_obs_old = 1e10;
obs_grp(1:n_o,1:4) = 0;
obs_grp(1,1) = 1;
if i==1
cgpr = 1:4*n_o;
for i1 = 1:n_o-1
for ii1 = 1:length(cgpr)
for ii2 = 1:length(cgpr)
d_obs(ii2) = sqrt((pxobs(cgpr(ii2)) - pxobs(cgpr(ii1)))^2 + ...
(pyobs(cgpr(ii2)) - pyobs(cgpr(ii1)))^2);
end
end
[d_o_s id_o_s] = sort(d_obs(1:length(cgpr)));
obsn(i1,:) = id_o_s(1:4);
clear cgpr;
cgpr = id_o_s(5:end);
end
if n_o > 0
obsn(n_o,:) = cgpr;
end
end
for ii1 = 1:Cos(i,1)
ii3 = 8*(ii1-1);
Cos(i,ii3+2) = sum(pxobs(obsn(ii1,:)))/4;
Cos(i,ii3+3) = sum(pyobs(obsn(ii1,:)))/4;
Cos(i,ii3+4) = 0;
Cos(i,ii3+5) = 0;
disd(1) = sqrt((pyobs(obsn(ii1,1))-pyobs(obsn(ii1,4)))^2+(pxobs(obsn(ii1,1))-pxobs(obsn(ii1,4)))^2);
disd(2) = sqrt((pyobs(obsn(ii1,2))-pyobs(obsn(ii1,4)))^2+(pxobs(obsn(ii1,2))-pxobs(obsn(ii1,4)))^2);
disd(3) = sqrt((pyobs(obsn(ii1,3))-pyobs(obsn(ii1,4)))^2+(pxobs(obsn(ii1,3))-pxobs(obsn(ii1,4)))^2);
[disd idisd] = sort(disd);
Cos(i,ii3+6) = disd(2)*1.2;
Cos(i,ii3+7) = disd(1)*1.2;
Cos(i,ii3+8) = atan((pyobs(obsn(ii1,idisd(2)))-pyobs(obsn(ii1,idisd(1))))/...
(pxobs(obsn(ii1,idisd(2)))-pxobs(obsn(ii1,idisd(1)))));
Cos(i,ii3+9) = 0;
end
Co = Cos(i,:);
end
r_tr_old = r_tr;
b_tr_old = b_tr;
end
end
stop(vid);