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,087
Last edited:

apty

Active Member
Licensed User
Longtime User
how can i run a service that displays x,y,z values of the sensor (accelerometer only)? i.e instead of activity module, i make it service module
 

flightnet

Member
Licensed User
Longtime User
Hey,
I have an additional question, how can I find the g. If I propebly swing the phone so that it Shows me how meny g are given in this Moment.
 

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: 633

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.
 
Top