ftctechnh / ftc_app

FTC Android Studio project to create FTC Robot Controller app.
761 stars 3.16k forks source link

RecognitionImpl.estimateAngleToObject() only defined in Portait mode #658

Closed AN-2001 closed 4 years ago

AN-2001 commented 5 years ago

Hi, After a bit of testing withRecognitionImpl.estimateAngleToObject() in land-scape mode, I realized that it only outputs the angle relative to the X axis, this works perfectly in portrait mode but when the phone is flipped the X axis gets flipped with the Y axis, this of-course will always return the same angle along the X axis.

It could be a problem with my phones Gyro-scope but in-case:

I think the most efficient would be to offset the X axis by 90 degrees when it's flipped. I did a bit of digging and tried to fix it myself, -the code below works when the phone is in land-scape mode -the code below is hard-coded, it doesn't flip the axis it just works according to the Y axis.

public double calcAngle(Recognition recognition,AngleUnit angleUnit) {
        //get vertical focal length
        double adjacentSideLength = vuforia.getCameraCalibration().getFocalLength().getData()[1;

        //calculate the distance from the vertical center line
       //NOTE: getTop() and getBottom() are used here since top and bottom
       // in portrait mode are actually left and right in landScape mode
        double oppositeSideLength = (recognition.getTop()+recognition.getBottom())*0.5f
        - 0.5f * recognition.getImageHeight();

        //calculate the angle
        double tangent = oppositeSideLength / adjacentSideLength;
        double angle = angleUnit.fromRadians(Math.atan(tangent));
        return angle;
    }