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

MiniDemonic

Member
Licensed User
Longtime User
Is it possible to track the movement of the phone, rather than the orientation?

Like if I move my phone to right, can I track that movement? Think of PSMove or WiiMote, move the phone in different directions to make stuff happen.
 

SlavaVB

Member
Licensed User
Longtime User
Hello Erel

The orientation sensor seems to have an impact on the overall timing of an app. It seems to steal quite a bit of operation time
Could you look at adding options to set the polling rate?

Have a look at this example to see what I mean
Here, a ball is set to roll when the phone is tilted (NOTE: operate in landcape mode). On my Samsung Galaxy S there are notable delays as the ball travels
I have set the timer to 10ms interval in order to allow some leeway for the orientation sensor but there is still a significant impact when polling the sensor

B4X:
'AccelBall
Sub Process_Globals
    Dim Orientation As PhoneOrientation
   Dim time As Timer
End Sub

Sub Globals
   Dim can As Canvas
   Dim ballx,bally,ballsize As Float
   Dim targety As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ballx=80 : bally=Activity.Height-Activity.Height/2
   ballsize=42 : targety=bally
   can.Initialize(Activity)
   time.Initialize("Timer1",10)
End Sub

Sub Activity_Resume
    Orientation.StartListening("Orientation") 
   time.Enabled=True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Orientation.StopListening
   time.Enabled=False
End Sub

Sub Timer1_Tick
   can.DrawColor(Colors.Black)
   bally=bally+(targety-bally)/2.0
   can.DrawLine(ballx,0,ballx,Activity.Height,Colors.Red,1.0)
   can.DrawCircle(ballx+ballsize,bally,ballsize,Colors.White,True,0.0)
   Activity.Invalidate
End Sub

Sub Orientation_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)
   targety=targety-Pitch
   If targety<ballsize Then targety=ballsize
   If targety>(Activity.Height-ballsize) Then targety=(Activity.Height-ballsize)
End Sub




How do you import the PhoneOrientation class in basic4android?
 

SlavaVB

Member
Licensed User
Longtime User
You should try to only refresh the parts required.

Refreshing the whole screen each time can be done. In your code you also drew the whole screen each tick.


i figured out why it didnt work... i was in a loop when doing msgbox, works great now!
 

latcc

Banned
I notice that the accelerometer readouts (x,y,z) are not discrete values but are in some way interactive. When you move the device in one axis only the other two axes are changed. So it is not possible to use the (as an example) z axis data to give info on how far on that axis the device has moved, without interfering with the othe two axis values.

Also the values for X,Y,Z are offsets from some initial value, rather than genuine acceleration values. An accelerometer value would always return to zero when the device returns to 'rest'.
 

jjmcc2

New Member
Licensed User
Longtime User
I have two Android device to test with a Xoom and HTC EVO. When I run the Sensor program I get different readings with landscape. Does anyone know this true for all phones and tablets?

Example:
Xoom when I tilt forward and back Y value changes

EVO when I tilt forward and back Z value changes


Xoom when I tilt Left or Right Z value changes

EVO when I tilt Left or Right Y value changes
 

metrick

Active Member
Licensed User
Longtime User
Light Sensor:
My Samsung Galaxy S only show the following light readings;
6,5000,9000,&15000.
6-dark area
5000-light on
9000-on sunny day in the shade
15000-Sunny day(outside) point phone toward the sun
Is there a way to get a better light sensor reading from the phone?
eg increment of 100 between the above numbers base on intensity of light.
 

Gsquared

Member
Licensed User
Longtime User
Sensor fusion supported

Hi Erel,

Thanks for your useful sensor program. Are there plans to support the "fused" sensor types, "TYPE_LINEAR_ACCELERATION", "TYPE_GRAVITY", "TYPE_ROTATION_VECTOR"? Support for these types were introduced in API 10. These are supposed to give much better results for determining rotation angles.

Thanks,

G^2
 

Gsquared

Member
Licensed User
Longtime User
Sensor fusion supported

That did the trick!

Thanks Erel for your quick and helpful response.
 

booster55

New Member
Licensed User
Longtime User
I have a problem with the light sensor:

B4X:
Sub Activity_Create(FirstTime As Boolean)

Dim ps As PhoneSensors
   ps.Initialize(ps.TYPE_LIGHT)   
   ps.StartListening("Sensor")

End Sub

Sub Sensor_SensorChanged(Values() As Float)

Dim s As PhoneSensors
s=Sender

ToastMessageShow(s.TYPE_LIGHT, False)

End Sub

The sensor itself seems to be monitored: I do get a ToastMessage whenever the light changes, however this ToastMessage always just shows "5" and I don't get why. Is something wrong with my code? I have a Galaxy SII and it is certainly capable of measuring the light. I tested the light sensor with an app called "AndroSensor" from the market and in this app, I can see more values coming from the light sensor.


EDIT: Dammit, I'm amazingly stupid... :D


B4X:
Sub Sensor_SensorChanged(Values() As Float)

ToastMessageShow(Values(0), False)

End Sub

And it works like a charm...
 
Last edited:

Robert28

New Member
Licensed User
Longtime User
Hi all,
first off all, I must say that I am new to android and B4A :sign0013: . I tried the example above wich is running without problem on a samsung ace.
I am just astonished by the fact that, with the phone perfectly still, the datas are always changing. (Acc, Mag and orientation) and, for the accelerometer are not 0 (ex: x=0.153, Y = 0.153, Z = 9.807). Strange for a phone on a table... Is it coming from bad sensors?

By advance, thanks!

Best regards,

Robert.

BTW : what are the units for the accelerometer? m/s² ?
 

xmayeur

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.
Can you please explain the values returned by the accelerometer? According to the library documentation, it returns meters per square seconds. OK. But, if my phone stands still on a table, I expected to get zero values returns for the three X-Y-Z axes (or does it measure the movement of our galaxy in the universe in expansion?). I get the impression that the sensor returns some kind of inclination of the phone with respect to one of the axis, which is not an acceleration... Do I miss something?
 

klaus

Expert
Licensed User
Longtime User
The accelerometer is an absolute accelerometer which measures also the earth gravity.
So if you put your device flat on the table the z axis should show values around 9.8 m/s2.
If you turn it upside down you would see a value about -9.8 m/s2.

You could have a look here
Sensors Overview | Android Developers

Best regards.
 
Top