Android Question Phone Sensor Orientation x starting point

Discussion in 'Android Questions' started by Scantech, Jun 24, 2019.

  1. Scantech

    Scantech Well-Known Member 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?
  2. emexes

    emexes Well-Known Member Licensed User

    This post mentions:
    which sounds like perhaps it relates to your question.
    Shelby and Scantech like this.
  3. Scantech

    Scantech Well-Known Member Licensed 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.
  4. emexes

    emexes Well-Known Member 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.
    Shelby and Scantech like this.
  5. emexes

    emexes Well-Known Member 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:
    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 ;-)
    Shelby and Scantech like this.
  6. emexes

    emexes Well-Known Member 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 ;-)

    '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(
        Y = Values(
    '''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
    '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: Jun 24, 2019
    Shelby and Scantech like this.
  7. Scantech

    Scantech Well-Known Member Licensed User

    As always, thanks. I will give it a try and post a video result.
    emexes likes this.
  8. Scantech

    Scantech Well-Known Member 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.

    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(
        dbY = Values(
        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
    '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
    End If
    End Sub

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

    If isPortraitLayout = True Then
                    strAngle =  Angle - (Angle * 
    End If
                strAngle = 
    NumberFormat2(strAngle, 100False)
    If Starter.GaugeNb > 0 Then                       
    For x = 0 To Starter.GaugeNb - 1
                        xGauge(x).asView.Rotation = strAngle
    End If
    End If
    End Try
    End Sub
  9. emexes

    emexes Well-Known Member Licensed User

    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:
    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 = 
    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.
    Scantech likes this.
  10. Scantech

    Scantech Well-Known Member Licensed User

    Not sure per second, but coming in really fast. The behavior with slow rates is not as jittery then fast rates.

    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: Jun 25, 2019
  11. Scantech

    Scantech Well-Known Member Licensed User

    Can you upload the source code? The damping code is not working on either of my 2 phones.
  12. emexes

    emexes Well-Known Member Licensed User

    That seems odd. The damping is done in these two lines here:
    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.

    Attached Files:

  13. emexes

    emexes Well-Known Member Licensed User

    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.
  14. Scantech

    Scantech Well-Known Member Licensed User

    My X and Y is declared in wrong spot. should work. I'll test it later on.
  15. Scantech

    Scantech Well-Known Member Licensed 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.
  16. Scantech

    Scantech Well-Known Member Licensed 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: Jun 26, 2019
  17. emexes

    emexes Well-Known Member Licensed User

    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.
    Scantech and Shelby like this.
  18. emexes

    emexes Well-Known Member 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.
    Scantech and Shelby like this.
  19. emexes

    emexes Well-Known Member Licensed User

    Accelerometer readings/second:
    .                Rate 1  Rate 2  Rate 3
    Phone         50.0    15.2     5.0
    Phone      49.3    14.2    14.2
    Phone       74.1    16.7     7.5
    Rates are same in release and debug modes.
    Shelby likes this.
  20. Scantech

    Scantech Well-Known Member Licensed User

    Are Tablets Accelerometer different then Phone? Check the image out. Y > 7 and reports as Portrait with accelerometer readings?


    I wish i had a physical Tablet to test with. From my understanding, the image position should be Landscape Left.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice