# Robot Arm, Three Stationary Ultrasonic Sensors. Maths Problem?!

I have a robot arm that I want to rotate by using three ultrasonic sensors. I’m using newping so all three sensors are working correctly. When a person approaches, the arm should react by rotating a single servo to the persons location, in a 180 deg arc. What that means is I need to take the cm measurements from all three sensors and translate that into a value between 0 and 180, relative to the person.

Here’s a diagram.

I can easily make the lowest cm value correspond to one of three positions, but it must be possible to translate the cm values into a value between 0 and 180 thus achieving a more dynamic response. If a little jittery, which is fine. I want this thing to be threatening.

The person will be in view of one or two of the sensors, never three. So that makes it easy already.

When in view of one sensor only, it's easy: 0, 90 or 180 degree. Maybe add a 20 degree randomness to it if you like your jitter.

If in view of two sensors, it's basic trigonometry, calculate the tangent using the two distance values. Or even simpler, the ratio between the two should work quite well (by the time this is seriously off, your subject will be in view of just one of the beams).

Another potential problem: most ultrasonic sensors have a beam way narrower than 90 degrees, so there is probably not much if any overlap between the sensors. Someone approaching this contraption at a 45 degree angle may end up in a blind spot.

Thanks for the reply. Looks like this is what I need:

atan2() seems likt the right track. Thoughts?

It’s almost there! Here’s my code:

`````` // TANGENT CALCULATIONS
L = (round ( atan2 (cm[0], cm[1]) * 180 / 3.14159265 )); // radians to degrees and rounding
R = (round ( atan2 (cm[1], cm[2]) * 180 / 3.14159265 )); // radians to degrees and rounding

// ARM ROTATION
if (L < 90) {
myservoA.write(L);
Serial.println(L);
}
else {
myservoA.write(R);
Serial.println(R);
}
``````

I get values between 0 and 90 from each set of two sensors. I can’t figure out how to effectively use both values for a 180 degree arc. I’m mathematically inept, so any help would be appreciated.