0

I'm building an Android app that uses the smartphone's sensors and GPS to rotate a camera (Pivo device) to face a specific GPS coordinate.

  1. Azimuth Calculation My phone is in portrait mode, and I want to get the azimuth based on the rear camera direction (which is the negative Z-axis, opposite of the screen).

Here's the code I'm using:

val rotationMatrix = FloatArray(9)
val remappedRotationMatrix = FloatArray(9)
val orientation = FloatArray(3)

if (SensorManager.getRotationMatrix(rotationMatrix, null, gravity, geomagnetic)) {
SensorManager.remapCoordinateSystem(
rotationMatrix,
SensorManager.AXIS_MINUS_X,
SensorManager.AXIS_MINUS_Z,
remappedRotationMatrix
)

SensorManager.getOrientation(remappedRotationMatrix, orientation)
var azimuth = Math.toDegrees(orientation[0].toDouble())
if (azimuth < 0) azimuth += 360
}

Is this remapping logic correct for aligning with the rear camera direction in portrait mode?

  1. Rotation Angle Toward a Target Given a fixed current GPS position, I want to rotate the phone to face a target GPS location. I'm calculating the angle like this:
   fun calculateDirectionAzimuth(
   lat1: Double, lon1: Double,
   lat2: Double, lon2: Double
   ): Int {
   val dLon = Math.toRadians(lon2 - lon1)
   val rLat1 = Math.toRadians(lat1)
   val rLat2 = Math.toRadians(lat2)

   val y = sin(dLon) * cos(rLat2)
   val x = cos(rLat1) * sin(rLat2) - sin(rLat1) * cos(rLat2) * cos(dLon)
   return ((Math.toDegrees(atan2(y, x)) + 360) % 360).toInt()
   }

fun calculateRotationAngle(
phoneAzimuth: Double,
currentLat: Double, currentLon: Double,
targetLat: Double, targetLon: Double
): Double {
val targetAzimuth = calculateDirectionAzimuth(currentLat, currentLon, targetLat, targetLon)
return (((targetAzimuth - phoneAzimuth + 540) % 360) - 180)
}

Does this logic look mathematically and conceptually correct?

My current implementation gives unstable results. I'm not sure if the issue comes from the azimuth logic, GPS noise, or something else. Any guidance would be greatly appreciated.

I used SensorManager to get azimuth based on the phone's orientation in portrait mode. I expected the azimuth to reflect the direction the back camera (−Z axis) was pointing. I also used GPS coordinates to calculate the angle needed to rotate the phone to face a target location. However, the calculated rotation angle often seems incorrect or unstable even when the phone is fixed.

1
  • If there is no problem with the above code, is it because of the reliability of the GPS received by the bl device? Commented Jun 17 at 9:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.