Android Tutorial Orientation and accelerometer

The attached program displays the values of the different sensors:

sensors.png


It creates a PhoneSensors object for each type of sensor.
As you can see in the attached code, two Maps are used to hold the phone sensors and the other required data. This way there is no need to write any duplicate code.

A better method for finding the orientation values: https://www.b4x.com/android/forum/threads/orientation-and-accelerometer.6647/page-6#post-476271
 

Attachments

  • SensorsExample.zip
    7.5 KB · Views: 1,792
Last edited:

flightnet

Member
Licensed User
Longtime User
Thank you very much!
Must I multiplicate the 3 acceleration Parameters?
X*Y*Z /9.81?
 

klaus

Expert
Licensed User
Longtime User
No you need to devide each direction by 9.81 !
gx = X / 9.81
gy = Y / 9.81
gz = Z / 9.81

Multiplying the three values doesn't give you the total acceleration you need to take into account their orientations and use vector addition.
You must also be aware that the accelerometers take into account the earth gravity.
 

Kevin L. Johnson

Member
Licensed User
Longtime User
The attached program displays the values of the different sensors:

sensors.png


It creates a PhoneSensors object for each type of sensor.
As you can see in the attached code, two Maps are used to hold the phone sensors and the other required data. This way there is no need to write any duplicate code.

Erel,

I would like to support both landscape and portrait orientations, therefore, I am trying to get proper / correct sensor data out of the Device in landscape orientation, however, I think that I need to call 'RemapCoordinateSystem' in order for the ORIENTATION sensor (as seen on screen above) to reflect the change or rotation and display the proper Yaw, Pitch and Roll. Can I do this using the Reflection Library?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

chrjak

Active Member
Licensed User
Longtime User
Hey Erel. My Phone has a temperature sensor but it shows "not supported"!!?
 

Croïd

Active Member
Licensed User
Longtime User
Is it possible move a bitmap with sensor data X - Y ?

Thank you in advance (example in zip)
 

Attachments

  • bille.zip
    498.6 KB · Views: 839

Croïd

Active Member
Licensed User
Longtime User
Please, it exist a simple example to begin ? this model is a little high for me :)
 

Vincenzo Fabiano

Member
Licensed User
Longtime User
@Erel is possible to understand the rotation of the screen using only the sensors? I do not want to use libraries already posted as they depend on the fact that the screen itself "rotates". I would understand the approach even if the screen is fixed in portrait mode
 

Vincenzo Fabiano

Member
Licensed User
Longtime User
It should be possible to find the orientation from the sensors. However it is not simple.

What exactly are you trying to do?

I need to solve the problem of the rotation of photos of cameraEx. By accessing the exif data I always retrieves 6 in all rotations of the device (ie I have to rotate 90 ° to restore the orientation. I have checked and this is because I'm stuck in portrait orientation, unlocking and activating the automatic rotation of the value that stores data in the exif becomes right). The issue is that I do not want the screen to rotate. So use the sensors seems to me the only solution ..

Sorry for my bad english.
 

Vincenzo Fabiano

Member
Licensed User
Longtime User
After several attempts and tests I found my own solution using the accelerometer. Moreover, given that I was "the only one" to implement this solution (on the forum I have not found anything like this except for the part of the code relating to the exif data) plus the rotation of the photo always in the right direction, I would suggest adding all the official library or in some way to stand out. Can return certainly useful to someone else..

Sub FotocameraLib_PictureTaken (Data() As Byte)

...
...
...

ModificaOrientamento(PercorsoFile, NomeFile, DatiSensore)

End sub

Sub Accellerometro_AccelerometerChanged (X As Float, Y As Float, Z As Float)
'Log(DatiSensore)
DatiSensore = Round(ATan2D(Y, X))
End Sub

Public Sub ModificaOrientamento(Dir As String, Filename As String, DatiSensore As Double)

Dim bm As Bitmap
bm=LoadBitmapSample(Dir, Filename, 100%x, 100%y)

Dim bm2 As BitmapExtended
Dim exifdata1 As ExifData
bm2.Initialize("")

Try

exifdata1.Initialize(Dir, Filename)
'http://sylvana.net/jpegcrop/exif_orientation.html
' 1) transform="";;
' 2) transform="-flip horizontal";;
' 3) transform="-rotate 180";;
' 4) transform="-flip vertical";;
' 5) transform="-transpose";;
' 6) transform="-rotate 90";;
' 7) transform="-transverse";;
' 8) transform="-rotate 270";;

If DatiSensore >= -20 AND DatiSensore <= 20 Then

exifdata1.setAttribute(exifdata1.TAG_ORIENTATION, 1)
exifdata1.saveAttributes

Else If DatiSensore >= 75 AND DatiSensore <= 100 Then

exifdata1.setAttribute(exifdata1.TAG_ORIENTATION, 1)
exifdata1.saveAttributes
bm = bm2.rotateBitmap(bm, 90)

Else If (DatiSensore >= 165 AND DatiSensore <= 180) OR (DatiSensore >= -180 AND DatiSensore <= -170) Then

exifdata1.setAttribute(exifdata1.TAG_ORIENTATION, 1)
exifdata1.saveAttributes
bm = bm2.rotateBitmap(bm, 180)

End If

' Riscrivo l'immagine con il giusto orientamento
Dim Out As OutputStream
Out = File.OpenOutput(Dir, "Temp2.jpg", False)
bm.WriteToStream(Out, 100, "JPEG")
Out.Close

Catch
Msgbox(LastException, "Errore Immagine")
End Try

End Sub
 

Mery

New Member
Hi,

I was wondering how can we transfer these information (from Android sensors) to a PC over wifi? I'll appreciate any hint on that.

Thanks.
 

Troberg

Well-Known Member
Licensed User
Longtime User
Just to make it as simple as possible, a simple example for accessing only one sensor, as bare bones as it gets:

B4X:
Sub Process_Globals
    Dim ps As PhoneSensors
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        ps.Initialize(ps.TYPE_ACCELEROMETER)
    End If
End Sub

Sub Activity_Resume
    If ps.StartListening("Sensor") = False Then
        Log("Sensor is not supported.")
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    ps.StopListening
End Sub

Sub Sensor_SensorChanged (Values() As Float)
    Values(2) < -8 Then 'Check Z value
        Log("Device face down")
    Else
        Log("Device face up")
    End If
End Sub

I hope this makes it a little simpler. There was nothing wrong with the example in the first post, but for a simple usage like this (and, typically, you only want to watch one sensor), it took a while to reverse engineer, so hopefully, I've saved someone else the work.

By the way, while this code works, I chose to wait until I had two consecutive values of -8 or less, just to avoid triggering on random shakes and bumps. Also, in this specific application, I chose to use the accelerometer instead of orientation, as I found orientation to be very "noisy" and generally finicky to handle.
 

Turbo3

Active Member
Licensed User
Longtime User
I don't understand. What you people are calling the accelerometer looks like a gyroscope to me. When the device is sitting on a table an accelerometer should read zero (almost) for x, y, and z but that is not what is reported. Instead x, y, and z are the phone's orientation which is a gyroscope not an accelerometer. What am I missing?

How does one read the accelerometer?

I am trying to map over the B4i motion function which returns true acceleration (x,y,z) by using "GetUserAcceleration". What is the equivalent function is B4A?
 
Last edited:
Top