Android Question tilt to degrees

Levit

Member
Licensed User
Longtime User
You should use PhoneSensors from the Phone library.
Thanks Erel. That is a start point.
I think that a class named "tilt2degrees.bas" or "titlt2angle.bas" would be a good contribution in Basic4android library.
As a beginer I'm not able to do that, i still have a lot to learn.
Maybe somebody more skilled can do it.
thanks
 
Upvote 0

Linostar

Member
Licensed User
Longtime User
Thanks Erel. That is a start point.
I think that a class named "tilt2degrees.bas" or "titlt2angle.bas" would be a good contribution in Basic4android library.
As a beginer I'm not able to do that, i still have a lot to learn.
Maybe somebody more skilled can do it.
thanks

The PhoneSensors, particularly the orientation sensor from the PhoneSensors, already give you that.
Since it is only the tilt angles you want (i.e. orientation angles), you can use directly a PhoneOrientation object from the Phone library.

Below an example that shows how to use a PhoneOrientation object:

B4X:
Sub Globals
    Dim PO As PhoneOrientation
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim label1 As Label
    label1.Initialize("")
    Activity.AddView(label1, 0, 0, 100%x, 100%y)
End Sub

Sub Activity_Resume
    PO.StartListening("PO")
End Sub

Sub Activity_Pause
    PO.StopListening
End Sub

Sub PO_OrientationChanged(Azimuth As Float, Pitch As Float, Roll As Float)
    label1.Text = Azimuth & CRLF & Pitch & CRLF & Roll
End Sub

Note that Azimuth is the tilt angle in the horizontal plane, Roll is the tilt angle in the vertical plane, and Pitch is the tilt angle between the horizontal and vertical planes.
 
Upvote 0

Linostar

Member
Licensed User
Longtime User
I should add that if the measured values do not match with the actual tilt angles of the phone, your phone sensors need calibration in that case.
 
Upvote 0

Levit

Member
Licensed User
Longtime User
The PhoneSensors, particularly the orientation sensor from the PhoneSensors, already give you that.
Since it is only the tilt angles you want (i.e. orientation angles), you can use directly a PhoneOrientation object from the Phone library.
Thank you Linostar
I tried you code and didn't have a reading.
Then i learnt that 'orientation' is the result of math computation of data from accelerometer and geomagnetic sensors.
Most tablets don't have geomagnetic sensor, that's why i couldn't have a reading.
It looks like with math computation of accelerometer data we can get tilt angles related to ground, that's what i'm looking for.
I need those math formulas to compute accelerometer data
thanks
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
The PhoneSensors, particularly the orientation sensor from the PhoneSensors, already give you that.
Since it is only the tilt angles you want (i.e. orientation angles), you can use directly a PhoneOrientation object from the Phone library.

Below an example that shows how to use a PhoneOrientation object:

B4X:
Sub Globals
    Dim PO As PhoneOrientation
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim label1 As Label
    label1.Initialize("")
    Activity.AddView(label1, 0, 0, 100%x, 100%y)
End Sub

Sub Activity_Resume
    PO.StartListening("PO")
End Sub

Sub Activity_Pause
    PO.StopListening
End Sub

Sub PO_OrientationChanged(Azimuth As Float, Pitch As Float, Roll As Float)
    label1.Text = Azimuth & CRLF & Pitch & CRLF & Roll
End Sub

Note that Azimuth is the tilt angle in the horizontal plane, Roll is the tilt angle in the vertical plane, and Pitch is the tilt angle between the horizontal and vertical planes.
Why does it give this error when the app is closed with the back button (I am using Phone library V2.26):

java.lang.Exception: Sub activity_pause signature does not match expected signature.
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Avoid using the geomagnetic sensor if you can, it's usually crap anyway.

As for the accelerometer, assuming the device is at rest (ie not accelerating in any direction), you should have an acceleration in one direction of slightly less than 10 (the exact value depends on where you are on earth, for example, here in central Sweden, it's 9.82, and 9.83 the poles, while only 9.78 at the equator) from the gravity. The sensors aren't that exact anyway, so I'd probably go with anything above 8. Anyway, that direction is down.

However, this assumes the device is either flat, standing or sideways. If you have any degrees of tilt between these that you wish to measure, you'll have to break out your math books and check for Pythagoras theorem and trigonometry.
 
Upvote 0
Top