Android Question Phone Sensor Orientation x starting point

Scantech

Well-Known Member
Licensed User
Longtime User
Is Phone Sensor Orientation Type X value ranges from 0 to 360? x seems a bit off. Do we have to calibrate it to have it set at x = 0 when phone is portrait mode?
 

emexes

Expert
Licensed User
Is Phone Sensor Orientation Type X value ranges from 0 to 360? x seems a bit off. Do we have to calibrate it to have it set at x = 0 when phone is portrait mode?
This post mentions:
I chose to use the accelerometer instead of orientation, as I found orientation to be very "noisy" and generally finicky to handle
which sounds like perhaps it relates to your question.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I just found out Orientation sensor is deprecated.

Meanwhile, i need to find an equation for phone angles (0 to 360 degrees). The accelerometer x and y max values is influenced by z value. Im not sure how to get the equation properly with that sensor.

The purpose for this, is Steering Wheel Mode Project we have discussed in the other thread.
 
Upvote 0

emexes

Expert
Licensed User
I would expect that you can calculate the "steering wheel angle" of the phone just using the x and y accelerations. As the phone is tilted backwards, more and more of the gravity vector will show up in the z acceleration, until when the phone is horizontal ie screen pointing to sky, all of the gravity vector is in the z acceleration and none in the x and y accelerations, and thus the reason you cannot get a reliable portrait/landscape orientation when the phone is horizontal.

I'm pretty sure that it's a case of using atan(x/y), just not sure about the signs. Hang on a few minutes, I'll give it a burl.
 
Upvote 0

emexes

Expert
Licensed User
Righto, step one is these measurements, where the angle is clockwise (like a compass) ie negative = tilted left, positive = tilted right, with 0 degrees being phone upright aka portrait:
B4X:
angle   x    y

-135    7   -7
 -90   10    0
 -45    7    7
   0    0   10
  45   -7    7
  90  -10    0
 135   -7   -7

Accelerations in metres-per-second-per-second, where earth's gravity is (close enough to) 10. Hallelujah! One small step in the long journey of dragging youse Yanks towards the glorious coherence of SI ;-)
 
Upvote 0

emexes

Expert
Licensed User
Well, that was mildly painful. Internet says use atan(x/y), but (i) there will obviously be a problem when y nears 0, and (ii) atan only returns half the required range of angles. When it's me alone vs the internet, I'd usually go with the internet, but in this case I'm betting on me ;-)

B4X:
'these were all globals in my test program so's could display them on screen
Dim X, Y, Z As Float
Dim R As Float
Dim ArcSinX, ArcSinY, ArcCosX, ArcCosY As Float
Dim Angle As Float

Private Sub Accelerometer_SensorChanged (Values() As Float)
 
    X = Values(0)
    Y = Values(1)
    '''Z = Values(2)
 
    R = Sqrt(X * X + Y * Y )    'length of gravity vector = approx. 9.8 ms-2 if no Z tilt
                                
    If R > 1.234 Then    'arbitrary cut-off of orientation detection as phone is tilted towards flat horizontal
 
        '''ArcSinX = ASin(X / R) / cPI * 180
        '''ArcSinY = ASin(Y / R) / cPI * 180
        '''ArcCosX = ACos(X / R) / cPI * 180
        ArcCosY = ACos(Y / R) / cPI * 180
 
        If X < 0 Then
            Angle = ArcCosY    'returns angles 0 .. 180
        Else
            'choose one of the below, depending on whether prefer angles -180 .. 180 or 0 .. 360
            Angle = -ArcCosY    'returns angles -180 .. 0
            Angle = 360 - ArcCosY   'returns angles 180 .. 360
        End If

    End If
 
End Sub
 
Last edited:
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Work in Progress.. Jitters alot with Samsung S6 held by my hand and rotating it. I need some kind of filter to eliminate the jitters. Did not try at Steering Wheel. I assume it should be less vibration with steering wheel mount then hand.

https://www.dropbox.com/s/5qjailv6vgkfmu8/1.mp4?dl=0

B4X:
Private Sub Accelerometer2_SensorChanged (Values() As Float)
    If blnSteeringMode = False Then Return
    
    Dim dbX,dbY As Float
    Dim dbR As Float
    Dim ArcCosY As Float
    Dim dbAngle As Float
    
    dbX = Values(0)
    dbY = Values(1)
 
    dbR = Sqrt(dbX * dbX + dbY * dbY )    'length of gravity vector = approx. 9.8 ms-2 if no Z tilt
                                
    If dbR > 1.234 Then    'arbitrary cut-off of orientation detection as phone is tilted towards flat horizontal

        ArcCosY = ACos(dbY / dbR) / cPI * 180
 
        If dbX < 0 Then
            dbAngle = ArcCosY    'returns angles 0 .. 180
        Else
            'choose one of the below, depending on whether prefer angles -180 .. 180 or 0 .. 360
            dbAngle = -ArcCosY    'returns angles -180 .. 0
            dbAngle = 360 - ArcCosY   'returns angles 180 .. 360
        End If
        
        RotateDashboardSteering(dbAngle)
        
    End If
 
End Sub

Sub RotateDashboardSteering(Angle As Float)
    Try
        If StateManager.blnNewDashboard = True Then
            Dim strAngle As String

            If isPortraitLayout = True Then
                strAngle =  Angle - (Angle * 2)
            Else
                    
            End If
    
            strAngle = NumberFormat2(strAngle, 1, 0, 0, False)
                        
            If Starter.GaugeNb > 0 Then                       
                For x = 0 To Starter.GaugeNb - 1
                    xGauge(x).asView.Rotation = strAngle
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

emexes

Expert
Licensed User
Work in Progress.. Jitters alot with Samsung S6 held by my hand and rotating it. I need some kind of filter to eliminate the jitters. Did not try at Steering Wheel. I assume it should be less vibration with steering wheel mount then hand.

I tried it here, using the pre-Scanteched Klaus gauges, and it seemed ok, certainly not as jittery as the video of your phone at the end, where I assume you were holding the phone upright. Could be difference in sensors, although I have a cheap brand, not a Samsung.

Thoughts are:

1/ How fast are the accelerometer readings coming in? Perhaps rather than updating the gauges for each new acceleration reading, just update a general DeviceAngle global, and then use that when you are updating the gauges anyway ie the needle angles.

2/ If you do filter (average) to get rid of the jitters, probably simpler and possibly better to do it on the accelerations (X and Y) and not on the angle. If you do average the angle, watch out when the phone is hovering near upright ie 359..0..1 degrees, where the average will be ~180 degrees (no prizes for guessing how I found that out).

3/ I am not sure of the timing of the acceleration readings, ie, are they regular like clockwork, or are there more of them when the readings are changing?

4/ I assume you've got the phone relatively "upright" so the Z axis is closer to horizontal than vertical. As the phone is tilted towards being flat horizontal, the X and Y accelerations will become smaller, and their relative errors larger, causing more jitter.

On my phone, and done on an actual steering wheel because I was running out of hands...

No damping:

Too much damping:

My damping code looks like:
B4X:
Private Sub Accelerometer_SensorChanged (Values() As Float)
 
    Dim Damping As Int = DampingSeekBar.Value
    DampingLabel.Text = Damping
 
    Dim Threshold As Int = ThresholdSeekBar.Value
    ThresholdLabel.Text = Threshold
 
    Dim TotalChange As Float = Abs(Values(0) - X) + Abs(Values(1) - Y)
    TotalChange = TotalChange * 10   'approximate %G
 
    If TotalChange > Threshold Then
        Damping = 0
    End If
 
    X = (X * Damping + Values(0)) / (Damping + 1)
    Y = (Y * Damping + Values(1)) / (Damping + 1)
   
    Dim R As Float = Sqrt(X * X + Y * Y )
The idea with the threshold was that, if the rotation changed by more than a specified amount, then no need to apply damping. Damping would only be applied to steady the gauges when the phone was steady.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
How fast are the accelerometer readings coming in?
Not sure per second, but coming in really fast. The behavior with slow rates is not as jittery then fast rates.

assume you've got the phone relatively "upright" so the Z axis is closer to horizontal than vertical
There was a slight tilt, maybe more then 10degrees towards horizontal.

That was really impressive to watch. I liked the rotation behavior of the second video. Are you calling SetRotationAnimated?
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Can you upload the source code?
Done.
The damping code is not working on either of my 2 phones.
That seems odd. The damping is done in these two lines here:
B4X:
X = (X * Damping + Values(0)) / (Damping + 1)
Y = (Y * Damping + Values(1)) / (Damping + 1)
and the amount of damping is specified by Damping, must be zero or positive (ie, NOT negative), Damping = 0 means no damping. Doesn't have to be an integer number, eg Damping = 1.42 is no problem. What is a small trap to watch out for with this type of damping (exponential?), is that it is possible the damped value never quite gets to the raw value, eg, if your raw reading was a steady zero, the damped value would get smaller and smaller and closer to zero, but in theory never quite get there (although in practice, is usually does, thanks to floating point numbers running out of precision and rounding off).

We were using different variable names, and I've slipped in a direct reference to the Values() array that would be easy to miss in transcription... perhaps the damping's working great but the gauge routines are still using the undamped variables or something.
 

Attachments

  • TestManyGaugesSteeringWheel2.zip
    16.6 KB · Views: 337
Upvote 0

emexes

Expert
Licensed User
The damping code is not working on either of my 2 phones.
The amount of damping is controlled by the two sliders (top and bottom of screen). I think they default to 50. For no damping, slide the bottom one to zero (and the top one too if you like, to be sure, to be sure). For maximum damping, like they're in oil or something, slide both sliders to 100.

Note that even with large thresholds (the top slider), large quick turns can still "escape" damping and the gauges rotate immediately to the raw rotation angle.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I got it working, but i noticed some issues not using 20 ms timer. You did the right thing using 20 ms timer to call rotations. Without it, the example video demo 2 has a huge delay on with rotation (both above 50). it can take up to 10 seconds to complete the transition. I was calling rotation in accelerometer change event which is not recommended.

Thanks again.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Nevermind, it was not the timer causing huge delay. It is the Damping setting it over 20. I had to restart my phone now damping 100 value is doing what it is suppose to do.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
You did the right thing using 20 ms timer to call rotations. ... I was calling rotation in accelerometer change event which is not recommended.

Perhaps your phone returns heaps more accelerometer readings than my el cheapo's, especially if it's one intended for virtual-reality use.

Updating the panel angles faster than the screen frame rate (30 .. 60 Hz?) is getting into diminishing-returns zone anyway.
 
Upvote 0

emexes

Expert
Licensed User
Also, I remembered today that the damping effect will be affected by the acceleration reading rate. Eg, if you get 100 readings/second, then your phone would settle 5x quicker than a phone getting 20 readings/second, and appear 5x more responsive.

Easily fixed by slipping time into the equation.

Having said that, I'm off to measure exactly how fast these sensor readings are coming in.
 
Upvote 0

emexes

Expert
Licensed User
Accelerometer readings/second:
B4X:
.                Rate 1  Rate 2  Rate 3
Big Phone         50.0    15.2     5.0
Middle Phone      49.3    14.2    14.2
Small Phone       74.1    16.7     7.5
Rates are same in release and debug modes.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Are Tablets Accelerometer different then Phone? Check the image out. Y > 7 and reports as Portrait with accelerometer readings?

1.jpg

I wish i had a physical Tablet to test with. From my understanding, the image position should be Landscape Left.
 
Upvote 0
Top