function angle = sensor_model(x0,y0,d)
%d: diameter of red circle in pixels.  (x0,y0): coordinates of base

img = vcapg2;  %grab camera image
%/4:image is downsampled by 4; -1: a circle is not a square.
x0=x0/4; y0=y0/4;  d = d/4 - 1; 
%downsample image, normalize, highlight red, smooth with convolution.
img = squeeze(sum(sum(reshape(double(img),[4 size(img,1)/4 4 size(img,2)/4 3]), 3)));
img = img./max(img(:));
img = min(5, bsxfun(@rdivide, img.^2, max(.2, mean(img,3)).^2) ); %max(.2,_: dark spots are noisy!
[~,ind] = max(reshape(conv2(img(:,:,1),ones(d),'same'),[],1));  %point est is max
[y1,x1] = ind2sub([size(img,1) size(img,2)], ind);
angle = 180/pi*atan2(y1-y0, x1-x0);  %return angle in degrees.
