0

I want to detect if the user has taken a turn on the road while driving using the sensors on the android phone. How do I code this? I am collecting data live from all the sensors(accelerometer,location,rotation,geomagnetic) and storing them on the sd card. So now i just want to know whether the user has a taken a turn and in which direction he has turned.

Anirudh GP
  • 345
  • 1
  • 4
  • 15
  • Add important code segments of the project please. – Dorukhan Arslan May 31 '15 at 12:46
  • 1
    I'm not sure why you don't use gps to track the exactly position of the user. The next step would be to compair if there is a street nearby, but you need to keep an eye on the accuracy while doing that. – rekire May 31 '15 at 12:51
  • It is also possible but I think he asks for a sensor-based solution so that if he turns the phone west, it outputs "left" in a way. However, I agree with you to use such a method that considers location instead of magnetic or orientation sensor. – Dorukhan Arslan May 31 '15 at 13:13
  • Ok. I wanted to detect a turn even if GPS signal is lost. How do i do this? – Anirudh GP Jun 01 '15 at 10:07

2 Answers2

3

I assume the registration of the sensor is done properly. You can detect the direction by using the orientation sensor (deprecated) as follows:

@Override
public void onSensorChanged(SensorEvent event) {

    float azimuth_angle = event.values[0];
    int precision = 2;

    if (prevAzimuth - azimuth_angle < precision * -1)
        Log.v("->", "RIGHT");

    else if (prevAzimuth - azimuth_angle > precision)
        Log.v("<-", "LEFT");

    prevAzimuth = azimuth_angle;

}

Note: The variable of "prevAzimuth" is declared as global. You can change "precision" value to whatever you want. We need this value because we do not want to see output after each trivial change in azimuth angle. However, too large precision gives imprecise results. To me, "2" is optimum.

Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42
  • I'll try this. Thanks! – Anirudh GP Jun 01 '15 at 10:03
  • I'm making an assumption that the device is going to be docked in the landscape mode with the camera facing the front of the car. This method will work for this assumption right? – Anirudh GP Jun 03 '15 at 07:01
  • Even the position of the phone is not changed but the phone is turned more than 2 degree, it calculates the new direction according to previous azimuth value. – Dorukhan Arslan Jun 03 '15 at 11:58
  • Ok and what does precision =2 mean? Is it 2 radians? – Anirudh GP Jun 05 '15 at 07:08
  • As you know, 1 radian equals about 57.3 degrees. I mean 2 degrees there. 2 radians (114.6 degrees) change is too much. however, 2 degrees may be too small in your case (driving). 20 degree should be proper. Test and change precision value. It is not a crucial constant. – Dorukhan Arslan Jun 05 '15 at 09:15
  • How would you use this to know if user turns to left or rigt for a GPS-Like App, @DorukhanArslan ? This is only working for me if the device changes from horizontal to vertial or vice-versa, could you check https://stackoverflow.com/questions/52626704/android-know-when-user-turns-to-left-or-right?noredirect=1#comment92186760_52626704 ? Thanks. – Wrong Oct 03 '18 at 12:30
0

If you are tracking location coordinates, you can also track shifts between the angle from previous locations.

angle = arctan((Y2 - Y1) / (X2 - X1)) * 180 / PI

See this answer for calculating x and y.

Decision to use sensor values is based on an unrealistic assumption that the device is never rotated with respect to the vehicle.

Community
  • 1
  • 1
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • Ok I can do this if location coordinates are available. But i want to continue detecting a turn even if the GPS signal is lost for sometime. – Anirudh GP Jun 01 '15 at 10:32