Android Question Orientation is not supported

AlpVir

Well-Known Member
Licensed User
Longtime User
I launched the Erel "SensorExample" app from the "Orientation and accelerometer" post (https://www.b4x.com/android/forum/threads/orientation-and-accelerometer.6647/#content) on a Huawei Honor 7S smartphone.
However, it is indicated that the ORIENTATION sensor is not supported.

sensor.jpg


However, by installing any of the numerous applications available on Google Play entitled "clinometer" on the same smartphone, these apps all manage to measure the inclination of the smartphone, in other words its orientation.
How do they do it ?
Perhaps by interpreting the accelerometer data? If yes, how?
Thanks in advance.
 

agraham

Expert
Licensed User
Longtime User
Yes. The accelerometer, when the phone static in the earth's intertial frame of reference is effectively measuring the force gravity acting along each axis of the phone so it the data can be used to calculate orientation. In fact I believe that orientation is a pseudo sensor whose values are calculated for you from the accelerometer readings.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The accelerometer values should be in units of 'g' the gravitational constant of 9.8 m/s^2. A phone lying flat should have a Z value of 9.8 indicating an angle of zero degrees to the earths gravitational field. The X and Y should both be zero indicating an angle of 90 degrees each so having no component of the field along their axes. Simple trigonometry gives you the angular values.

In practice these readings are usually very noisy and need smoothing and the max and min values for each axis may be different from each other and from the nominal 9.8 so I would build in some dynamic re-scaling by recording and using the max and min values of each axis as I encounter it, perhaps starting with getting the user to do an orientation test to get starting values when running the app for the first time.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
It doesn't seem simple to me.
By modifying very little of Erel's code
B4X:
    If sd.ThreeValues Then
        Dim Incl As Double
        Incl= ACosD(Values(2)/9.81)
        Incl=(Incl-10) * 1.1  '
        lbl.Text = sd.Name &  "   " & NumberFormat(Incl, 0, 1) & "°  X=" & NumberFormat(Values(0), 0, 3) & ", Y=" & NumberFormat(Values(1), 0, 3) _
        & ", Z=" & NumberFormat(Values(2), 0, 3)
    Else
        lbl.Text = sd.Name & " = " & NumberFormat(Values(0), 0, 3)
    End If

it is had that when the smartphone is horizontal it marks around 10 degrees and when it is vertical it marks around 90 degrees. If it were just a matter of calibration it would be easy to provide.
At this point, however, we need to think of an easy way for calibration that is probably different from device to device. And it becomes complicated again.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This line of code wrong, I don't know what you mean to achieve with it but this is where the incorrect 10 degrees when horizontal comes from.
Incl=(Incl-10) * 1.1
this line should be sufficient
Incl= ACosD(Values(2)/9.81)
The code should work but the constant value of 9.81 may not be the actual max reading as I stated above.
 
Upvote 0

AlpVir

Well-Known Member
Licensed User
Longtime User
The line
Incl = (Incl-10) * 1.1
I inserted it to make sure that when the smartphone is horizontal Incl is equal to 0 and when the smartphone is vertical Incl is equal to 90.
The values 10 and 1.1 are approximate, experimental, , identified by trial and error.
By shaking the smartphone the maximum value of Values (2) is 19.61. The minimum is -19.46. This should allow for calibration and refinement of values 10 and 1.1.
I still don't know how.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This is so wrong I don't know where to begin. This is my last attempt to explain.

Offsetting the inclination value is just going to screw things up. Accelerometer is a linear reading, it should be zero by definition when there is no acceleration along the axis and positive or negative depending on whether is it measuring an acceleration or a deceleration.

Shaking the smartphone is adding additional acceleration to each axis over and above that contributed by the earth's gravity field which is what you need to measure to get orientation. The phone needs to be stationary in the earth's reference frame to measure its orientation to that frame of reference. Is this not obvious?
 
Upvote 0
Top