effectively emulating a laser pointer for my cat using Matlab
Asked Answered
F

1

7

I'm trying to write a code using matlab that emulates a laser pointer in a way that my cat will enjoy chasing it on the screen. This is what I've done so far:

figure('menubar','none','color','k')
h = plot(0,'r.','MarkerSize',20);
xlim([-1 1]);  ylim([-1 1])
axis off
phi1=(1+sqrt(5))/2;
phi2=sqrt(3);
step= 0.0001; % change according to machine speed
for t=0:step:100
    set(h,'xdata',sin(t+phi1*t),'ydata',cos(phi2*t))
    drawnow
end

The "issues" with this code are the following:

  1. the pointer moves more or less at a constant speed and doesn't slow to a near stop and then unexpectedly proceed.

  2. The trajectory is somewhat repeating itself, though I tried to make it using irrational numbers, the overall motions is continuous from right to left. I think a sharper trajectory change will help.

I know this is not a traditional programming question but still I want to solve a programming issue. I'd appreciate your help and of course open to new ways to answer my question that doesn't use the code I added.

Finney answered 20/7, 2013 at 0:35 Comment(11)
Fantastic project. But at the cost of potentially sacrificing a monitor?!Chrominance
you could implement the notion of position, velocity, and acceleration, with parameters to control each. Look for inspirations in gamedev.stackexchange.comBenco
Fun. You might even learn some new stuff in doing it. Simulate a strange attractor or other chaotic attractor. :-) The famous Lorenz attractor is pretty easy to do. Or look into 2-D random walks and Wiener processes for ways to add noise to some other system/process.Ammons
By the way, to maximize the potential for claw damage, you can make your figure window full screen via: set(gcf,'Position',get(0,'Screensize')); or set(gcf,'Units','Normalized','OuterPosition',[0 0 1 1]);.Ammons
A chaotic oscillator should suit your needs. Perhaps The Rossler oscillator with appropriate parameters. If that's not weird enough, you can use a more complicated system.Quincentenary
Don't get too complicated. I'm positive all it would talk is a random walk (still using sine and cosine functions) to address issue 2, and to address issue 1 you can use a series of simple pauses (at random intervals of course) to slow the dot down.Chrominance
you can add a slow random sign variable to create a discontinuous trajectory via sign flips, and also add a slow random step size modification that will change the effective speed of the pointer...Halfhour
What sort of cat is it? Siamese cats are typically satisfied by the Lorenz attractor paths, whereas the discerning Russian Blue is more partial to Rossler oscillators proposed by @Nigel. Random Walks would only be suitable for a common moggy.Herl
Maybe try something like 2D Brown noise? (with bounds that will "kick" the dot if it gets too close to screen borders)Donndonna
@Herl Wow, how did you know that?Donndonna
Also, consider using projector instead of monitor. It is safer. One issue though - cat's shadow, but it is not worse than with real laser pointer.Donndonna
G
3

Brilliant question, so good I thought I'd take 15 minutes of my life to have a go myself. After extensive YouTube research on laser technique i thought using the equations of motion to move between random points would work well:

n = 20; %number of steps
pos = [0,0]; % initial position
vel = 4; % laser velocity
acc = 400; % laser acelertation
dt = 0.01; % timestep interval
figure
set(gcf,'Position',get(0,'Screensize'));
for i=1:n
    point = rand(1,2);
    dist = 1;
    while dist > 0.05 % loop until we reach the point
        plot(pos(1),pos(2),'o','color','r','MarkerFaceColor','r')
        axis equal
        xlim([0,1])
        ylim([0,1])
        drawnow
        % create random point to move towards
        dist = pdist([point;pos],'euclidean');
        % calculate the direction & mag vector to the point
        dir = (point-pos)/norm((point-pos));
        mag = norm(point-pos);
        % update position
        displ = vel*dt - 0.5*acc*mag*dt^2;
        pos = pos + dir*displ;
    end
end

Play around with the parameters till you find something your cat likes :0)

Getaway answered 20/7, 2013 at 19:26 Comment(2)
For more natural movements, I suggest not using a normal distribution, but a power law, or Levy distribution. A log-normal distribution would work in a pinch, too.Quincentenary
I tried the normal distribution, but I still prefer the uniform distribution! I suppose the real arbiter has to be the cat, although I'm not sure how you would setup a quantitative experiment to asses this...!Getaway

© 2022 - 2024 — McMap. All rights reserved.