Air defense system โ€” Find the position of an object if it is specified degrees upwards and sideways from a point

I was making an "Air defense system" using arduino uno. The idea is the have two servos (vertical and horizontal) attached to an Ultrasonic sensor and called "radar" which will rotate and scan the area, then calculate the position of the closest object it found and send this to rotate a "laser" which also has two servos to move. It's supposed to use some trigonometry and vector math to calculate both the position of object and the angles the "laser" has to move both upwards and sideways.

This is what the "radar" looks like (don't worry, I'll have it decorated):

radar stuff image

You can see the sensor attached to a servo on side (it'll move it upwards), and another servo at the bottom (it'll move it sideways), and when the vertical angle is 0, it points straight ahead, and currently the horizontal angle is around 60 degrees, i.e., 0=point left-ish (towards arduino) and 120 = point right-ish (away from arduino)


I had this code:

#include <Servo.h>

const int pinRadarServoV = 2; // V=vertical (i.e. upwards rotating servo)
const int pinRadarServoH = 3; // H=horizontal (i.e. sideways rotating servo)
const int pinRadarSensorTrig = 9;
const int pinRadarSensorEcho = 10;

Servo radarServoV;
Servo radarServoH;

const int radarX = 0; // X
const int radarY = 7; // Y
const int radarZ = 0; // and Z positions of the radar's center 

const float pi = 3.14159;
int adderV = 2; // it will alternate between 2 and -2
int adderH = 2; // both to be added to H and V angles per loop.
int angleV = 0;
int angleH = 0;
float minDistance = 0; // save the minimum distance per revolution
int minDistAngleH = 0; // and have it H
int minDistAngleV = 0; // and V angles stored

// Use the Ultrasonic sensor.
int radarSensorDistance() {
  digitalWrite(pinRadarSensorTrig, LOW);
  delayMicroseconds(2);
  digitalWrite(pinRadarSensorTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(pinRadarSensorTrig, LOW);
  long duration = pulseIn(pinRadarSensorEcho, HIGH);
  float distance = (duration * 0.0343) / 2;
  return distance;
}

// the function that needs help.
void triggerAction() {
  if (minDistance>0) {
    int X = radarX + minDistance * cos(angleV*(pi/180)) * cos(angleH*(pi/180));
    int Y = radarY + minDistance * cos(angleV*(pi/180)) * sin(angleH*(pi/180));
    int Z = radarZ + minDistance * sin(angleV*(pi/180));
    Serial.print("object (");
    Serial.print(X);
    Serial.print(",");
    Serial.print(Y);
    Serial.print(",");
    Serial.print(Z);
    Serial.print(") at d:");
    Serial.print(minDistance);
    Serial.print(", angleH: ");
    Serial.print(minDistAngleH);
    Serial.print(", and angleV: ");
    Serial.println(minDistAngleV);
    minDistance = 0.0f;
    minDistAngleH = 0;
    minDistAngleV = 0;
  }
}

void setup() {
  Serial.begin(9600);
  radarServoV.attach(pinRadarServoV);
  radarServoH.attach(pinRadarServoH);
  pinMode(pinRadarSensorTrig, OUTPUT);
  pinMode(pinRadarSensorEcho, INPUT);

  radarServoV.write(0); // reset both servos
  radarServoH.write(0); // for good measures
  delay(1000);
}

void loop() {
  if (angleV>=75)   {angleV=75; adderV=-2;} // limit it to 75 upwards and 120 sideways
  if (angleH>=120) {angleH=120; adderH=-2;} // and go back to 0 on both.
  if (angleV<=0)   {angleV=0; adderV=+2; triggerAction();} // trigger that function
  if (angleH<=0)   {angleH=0; adderH=+2; triggerAction();} // on reaching 0
  
  radarServoV.write(angleV);
  radarServoH.write(angleH);
  delay(15);

  float distanceNow = 0; // temporary variable to store Sensor data.
  if (angleH%20==0 || angleV%15==0) distanceNow = radarSensorDistance();
  if (distanceNow>0 && distanceNow<20) { // work only if distance is less than 20cm.
    if (minDistance==0) minDistance = distanceNow; // setting first time.
    if (distanceNow<=minDistance) {
      // updated global variables if current detection has least distance.
      distanceNow = minDistance;
      minDistAngleH = angleH;
      minDistAngleV = angleV;
    }
  }

  angleV+=adderV;
  angleH+=adderH;
}

Here, upon running it, I get awkward result, such as:
Serial Monitor data

It's awkward because, in many of these readings, the Z value turns out to be 0 which isn't possible because the object can't be "on" the sensor.


lastly, I want to have an accurate way to calculate the point of object if the "radar"'s sensor's center is at (radarX, radarY, radarZ), and also calculate angles the "laser" has to point to, if its center is at, say, (p,q,r).

Thanks :slight_smile:

That would be the case if "angleV" is zero, for example. Add in additional Serial.print statements to investigate.

I want to have an accurate way to calculate the point of object if the "radar"'s sensor's center is at (radarX, radarY, radarZ), and also calculate angles the "laser" has to point to, if its center is at, say, (p,q,r).

That is elementary trigonometry, similar in outline to the equations below. Did you work these out or copy them from somewhere else?

    int X = radarX + minDistance * cos(angleV*(pi/180)) * cos(angleH*(pi/180));
    int Y = radarY + minDistance * cos(angleV*(pi/180)) * sin(angleH*(pi/180));
    int Z = radarZ + minDistance * sin(angleV*(pi/180));

See Spherical coordinate system - Wikipedia

Read the documentation for pulseln() and see what is returned when no echo is found. Your system would also run faster if you included a short time-out parameter.

1 Like

"elevation and azimuth"

The "cone" of the SR04 is 15 degrees. That means a 30 degree error in both AZ and EL.

Try averaging the first syllable and last syllable of detection.

1 Like

I should've investigated more on my side regarding this. thanks for pointing out :blush:

I actually got that as a bit of help from AI, although I should've been able to find my way out, what for am I a science student :flushed:... also, do you mean that my equations are valid or those are wrong?


Also, are any of these equations related to what I'm trying to achieve? (taken from Wikipedia)

That is usually a mistake, because "AIs" don't pay attention to the details and make many errors, based on incorrect assumptions. They should only be used as a time saver by people who can easily spot those mistakes.

The equations in your code (which I quoted) are correct for certain definitions of angleV and angelH but not others. for example, NOT the one used in the linked Wikipedia article:

To get started on the proper path, you need to make a drawing like the above, showing exactly how angleV and angleH are defined with respect to X, Y and Z.

I'd take care of your words thanks for them.


What you have defined does not appear to be a conventional polar coordinate system. Please show how the servos are arranged, as I don't understand your drawing.

For a polar coordinate system, one of the servo angle origins depends on the other servo angle.

I'm terribly sorry... thanks a million times for being an eye opener :pensive::pray:
I've deleted the question from SO. we get to learn a lot of ethics each day, don't we...

2 Likes

I had a picture of the arrangement in the question itself, the horizontal movement servo is right below the piece of cardboard holding the vertical servo and sensor, the two strips of yellow tape is where it's affixed... I hope u get what I mean...

The picture does not help me understand how the servos are supported and arranged. I doubt your angle diagram is correct.

What you should be using is something similar to this pan/tilt mount:

@whmsft

This drawing is showing one direction (left to right) of one plane (AZ or EL) of recording the first syllable (when range decreases from nominal) and last syllable (when range increases to nominal) to obtain the average angle. "Nominal" being "nothing in the cone."

2 Likes

thank you, I now understood what you meant... I'll have it implemented when I'm able to

Note; The AZ first/last detection should note the EL position. That is to say, with the AZ readings, the top and the bottom of the object will be effected by the height of the object. Similar on the EL detection being effected by the width of the object.

That would be the "illuminated width" of the object. Shape is the problem.

That kind of talk takes me "Mountain Home."

As in AFB?

Activities on and around it.

I knew a guy who had been a radar tech at the Blaine Washington station. He told they used to warm their lunches by placing then inside the transmitter housing for a short time. When I was Oregon AF MARS director I used to go to the radar station on Mt. Hebo on the Oregon coast to pick up issued equipment. Pretty impressive, all run from generators. All gone, now.

Out of sight. All that old stuff is pocket sized. Some form still exists where "we" do not. I once worked on a 320lbs/150kg "tape recorder"... It's a keyfob now.