简体   繁体   中英

Arduino calculating angle accuracy problem?

I'm trying to calculate the angle of a point I'm measuring.

Using: https://www.calculator.net/triangle-calculator.html?vc=90&vx=50&vy=50&va=&vz=&vb=&angleunits=d&x=101&y=9 My a = 50, b = 50 and c = unknown (varies along with a), I am currently testing it with a fixed distance of 50.

See the link for visualisation, it will probably help a lot.

I am using the following code:

float calculateAngle(float distance)
{
  float a = TSThorizontalDistanceToProjector - (distance + TSTdistanceFromFrontToProjection);
  float b = TSTverticalDistanceToProjector;
  float C = 90;

  float c = sqrt(pow(TSTverticalDistanceToProjector, 2) + pow(a,2) - ((2 * b * a) * cos(C)));
  Serial.println("float c is: ");
  Serial.println(c);

 float A = acos((pow(c,2) + pow(b,2) - pow(a,2)) / (2 * c * b));
 Serial.println("float A is: ");
 Serial.println(A);
}

I first calculate c so I can calculate the A angle. I only want the A angle however, so if I can calculate it right away please say so. The problem with this code is however that it outputs the wrong numbers.

According to the site linked above angle A should be: 45 degrees and side c should be 70.711. My code outputs angle A = 0.40 and side C = 68.88. Both are wrong, am I using the formula incorrectly? Or does it have to do something with the variables I'm using?

EDIT:

I'm now using the following code:

float calculateAngle(float distance)
{
  float a = TSThorizontalDistanceToProjector - (distance + TSTdistanceFromFrontToProjection); //120 - (70 + 20) = 30
  Serial.println(a);
  float b = TSTverticalDistanceToProjector;
  float C = 90;

  float c = sqrt(pow(a,2) + pow(b,2));
  Serial.println("float c: ");
  Serial.println(c);

  float tanX = (a / b);
  Serial.println("float tanX is: ");
  Serial.println(tanX);
  
  float tanXresult = atan(tanX);
  Serial.println("float tanXresult: ");
  Serial.println(tanXresult);
}

I also saw that a = 30 and not 50 but b still is 50.

I can now calculate c with enough accuracy, the angle of A is still a problem though.

Don't use degrees as input to trigonmic functions. Use radians!

Also if a and b join at 90° and you know a and b , c is simply sqrt(a^2+b^2) as

c^2 = a^2 + b^2

Not sure what that cos is supposed to do here.

As you already know a and b you can simply calculate A via the arcustangens or a/b. You don't need c for that.

I suggest you revisit basic trigonometry.

I had to convert my atan from radians to degrees.

float calculateAngle(float distance)
{
  float a = TSThorizontalDistanceToProjector - (distance + TSTdistanceFromFrontToProjection); //120 - (70 + 20) = 30
  //Serial.println(a);
  float b = TSTverticalDistanceToProjector;
  float C = 90;

  float c = sqrt(pow(a,2) + pow(b,2));
  Serial.println("float c: ");
  Serial.println(c);

  float tanX = (a / b);
  Serial.println("float tanX is: ");
  Serial.println(tanX);
  
  float tanXresult = ((atan(tanX)) * 180) / Pi;
  
  Serial.println("float tanXresult: ");
  Serial.println(tanXresult);
}

I did that with:

float tanXresult = ((atan(tanX)) * 180) / Pi;

printed result: 30.96 degrees, which is correct!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM