Hey guys, I’m trying to build a project that involves 2 SRF-02 ultrasonic sensors that are mounted onto a rotating platform (calling this servo the pan servo for now). The platform also holds a nerf gun which is attached to a servo (tilt servo), so that once an object is sensed/detected the model will stop, move to the correct angle and fire. What is the best way of detecting the position of this object? And is it even possible to get a precise tilt angle on the gun without using MATLAB/a grid?

# Best approach to a motion detection project

Hi Kirk,

I am able to give any advice on the algorithm, but I can give you the library of SRF02 and Servo, hope these help.

Regards,

Desmond

Short answer is that it’s not easy.

You can measure distance to the closest object somewhere in your field of view from two different points. That’s not really enough to calculate that objects location.

You could mount the two sensors on either side of the gun, the further out to the sides the better. You then measure distance from both sensors and then turn in the direction that gives you the shortest range until they both give the same reading. At this point you should be pointing at the target.

In theory you could use the difference in range measurements together with the sensor separation to tell you how far you need to rotate but it may be easier to just just keep turning slowly until they read the same.

Elevation angle you can’t calculate with only two sensors, you’d need a 3rd mounted above or below the other two and use the same technique. Or you could set the elevation angle based on distance, the further away the higher you aim.

Quite how well this will work I’m not sure. It’ll depend on the amount of noise in the sensor measurements. The further apart they are the better it should work but their are practical limitations on that.

Thanks for the reply! We’ve simplified the project a little now, instead of calculating the appropriate tilt angle we’ll just be setting it to 15 degrees, then fire the gun. But back to the location, could you possibly help with code with this? I know I can use laws of cosines to mathematically calculate the distance, but the conversion of that to an angle at which the pan servo needs to turn is the difficult part. I know from using C that there was a map function to change the cm to an appropriate angle, not sure if there’s anything like that applicable here.

Hopefully this makes sense…

You have two sensors each a distance d from the nerf. They measure ranges r1 and r2. You want to calculate the angle x that you need to rotate by to aim at the target.

First find t, the angle at the target using the cosine rule

double t =acos( ( r1*r1 + r2*r2 - 4*d*d ) / (2*r1 *r2) );

Then using the fact that the angle to the centre point will be half of t we can use the sin rule to find the angle n.

double n = asin ( r1 * sin (t/2) / d );

Finally calculate the angle x in degrees

double x = 90 - (n*180/3.14159365);

I think that I’ve got the maths correct, you may want to double check, it’s been a while since I’ve done something like this.