Various problems with sensors used together

214 views
Skip to first unread message

madlyR

unread,
Mar 27, 2015, 5:33:39 AM3/27/15
to androi...@googlegroups.com
I created test program to see, how behaves multiple sensors in one DS app.

The program is below. It creates all known sensors and displays their results. To be sure that sensors are properly initialised I even started them by setTimeout function. There is a sensorSpeed variable to ease see, how app is working with faster sensor readings. While sensors send a lot debug info, I disabled debug output to not slow down app. On most sensors I invoked SetMinChange method to filter some sensor "noise" minimal changes.

I checked this program on two phones, Samsung Galaxy S5 (4 x 2.5 GHz CPU, Android 4.4.2), and Sony Xperia P (2 x 1.0 GHz CPU, Android 4.1.2). 

OK. What are the problems?

On Samsung Galaxy S5:
  • All works fine and reliable, but when setting sensorSpeed to Slow or Medium. 
  • On Fast and Fastest speed, when phone is shaking I get: "Script error: Maximum call stack size exceeded".

On Sony Xperia P:
  • Proximity sensor doesn't work. After CreateSenor it's not null, but it almost always doesn't call callback method. Almost, but very rarely it calls callback with readings 1 or less often 0. But it's rather "noise" and it's not responding to proximity change.
  • Magnetic field sensor is constantly and very fast called. It doesn't react on Slow or Medium speed mode.
  • Application is not responsive, load is very big. Orientation readings are sometimes strange. Sometimes app freezes, sometimes I get get message that script is working to slow and prompt to quit or wait for the app.
  • Sometimes readings are ridiculous - ie few readings constantly increases for a while, it seems, that something messed up callbacks.
  • When I comment out few sensors, all starts to work better and reliable.

Please install this program and check on other devices to see, if it is problem with one device type (Sony Xperia P, Android 4.1.2) or it is more common problem with multiple sensor readings. Let's share some info here.

Best regards,
Radek

function OnStart()
{
  app
.SetDebugEnabled( false );
  app
.SetOrientation("Portrait");


 
// Choose one fo the sensor gathering speed:
 
// “Fastest”, “Fast”, “Medium” or “Slow”. Slow is default.
 
var sensorSpeed = "Slow";


 
// Create app layout  
  lay
= app.CreateLayout( "Linear", "VCenter,FillXY" );
  txto
= app.CreateText( "", 0.8, 0.2, "Multiline" );
  lay
.AddChild( txto );
  txta
= app.CreateText( "", 0.8, 0.2, "Multiline" );
  lay
.AddChild( txta );
  txtl
= app.CreateText( "", 0.8, 0.1 );
  lay
.AddChild( txtl );
  txtp
= app.CreateText( "", 0.8, 0.1 );
  lay
.AddChild( txtp );
  txtm
= app.CreateText( "", 0.8, 0.1 );
  lay
.AddChild( txtm );
 
  app
.AddLayout( lay );
 
 
// Create available sensors.
 
// Sensor start is delayed to ensure that sensor is fully initialised.
 
// It should work seamlessy hust without setTimeout by calling sns.Start() method.
  snso
= app.CreateSensor( "Orientation", sensorSpeed );
  snso
.SetMinChange( 0.5 );
  snso
.SetOnChange( sns_OnOrientationChange );
  setTimeout
( snso.Start, 100 );
 
//snso.Start();


  snsa
= app.CreateSensor( "Accelerometer", sensorSpeed );
  snsa
.SetMinChange( 0.5 );
  snsa
.SetOnChange( sns_OnAccelerometerChange );
  setTimeout
( snsa.Start, 110 );
 
//snsa.Start();


  snsl
= app.CreateSensor( "Light", sensorSpeed );
  snsl
.SetMinChange( 1.0 );
  snsl
.SetOnChange( sns_OnLightChange );
  setTimeout
( snsl.Start, 120 );
 
//snsl.Start();


  snsp
= app.CreateSensor( "Proximity", sensorSpeed );  
  snsp
.SetOnChange( sns_OnProximityChange );
  setTimeout
( snsp.Start, 130 );
 
//snsp.Start();


  snsm
= app.CreateSensor( "MagneticField", sensorSpeed );
  snsm
.SetMinChange( 0.5 );
  snsm
.SetOnChange( sns_OnMagneticFieldChange );
  setTimeout
( snsm.Start, 140 );
 
//snsm.Start();  
 
}
 
function sns_OnOrientationChange( azimuth, pitch, roll, time )
{
  txto
.SetText( "Orientation:\nazimuth = " + azimuth.toFixed(1) + "\n pitch = " + pitch.toFixed(1) + "\n roll = " + roll.toFixed(1) );
}


function sns_OnAccelerometerChange( x, y, z, time )
{
  txta
.SetText( "Acceleration:"+"\n x="+x.toFixed(2) + "\n y="+y.toFixed(2) + "\n z="+z.toFixed(2) );
}
 
function sns_OnLightChange( lux )
{
  txtl
.SetText( "Light level = " + lux + " lux" );
}


function sns_OnProximityChange( value )
{
  txtp
.SetText( "Proximity = " + value );
}


function sns_OnMagneticFieldChange( value )
{
  txtm
.SetText( "Magnetic field = " + value.toFixed(2) );
}





Dave Smart

unread,
Mar 27, 2015, 1:36:25 PM3/27/15
to androi...@googlegroups.com
Hi,  try putting a try/catch around the code in your callback handlers and see if that affects the badly behaving phones.

madlyR

unread,
Mar 27, 2015, 5:17:31 PM3/27/15
to androi...@googlegroups.com
Dave, try / catch it doesn't change anything.

I checked my two phones again and I have some thoughts and observations.

1. There is problem with MagneticField sensor. It seems that this sensor gives enormously fast changing results. On Xperia P last text with magnetic fields readings are constantly changing with maximum speed (several times per second).
2. If I comment out MagneticField sensor, all becomes much more responsive.
3. It seems that all sensor readings are in a queue and the faster changes, the more results in queue, then there is a bigger lag in visible UI. When I shake several seconds Xperia P with MagneticField enabled, and leave phone without movement, then queue empties after 45-60 seconds!!! Without MagneticFields on Slow speed lag could be 4-10 seconds... With severe changes, app responds good. On much faster phone (Galaxy S5) in Fast mode queue start to be too long and range error occurs (try/catch on handler traps range error). 
4. When you have simple app with one sensor readings and not too much to change on screen, all seems to work perfectly. But no more under bigger load.
5. On Xperia P Proximity sensor works, but to initialise itself it needs to be covered for 1-2 second and then it starts to show results. It's interesting. 

I think, that the problem is in sending sensor results to the message queue without checking readings lag. 
It should be possibility to skip sending or reading message queue by default. If you need to log all readings, this behaviour is OK, but not in other situations. 

Dave, on callbacks for Accelerometer and Orientation sensor are parameters time. Is it time (timestamp?) of this event or sensor readings? How to count real lag between sensor readings and callback invoke? I tried code:

function sns_OnAccelerometerChange( x, y, z, time )
{

 
try {
   
var d = new Date();
    txta
.SetText( "Acceleration:"+"\nx="+x.toFixed(2) + "\ny="+y.toFixed(2) + "\nz="+z.toFixed(2)  + "\nlag="+(d-time) );
 
} catch ( err ) {
      app
.ShowPopup( err );
 
}
}


and on Galaxy S5 lag is shown between 5, and 15, but on Xperia P lag results are meaningless. It's 12 digit number... Strange.

Cheers!
Radek



Steve Garman

unread,
Mar 27, 2015, 9:06:32 PM3/27/15
to androi...@googlegroups.com
On my Moto G XT1068, all 5 sensors seem to work well except that the proximity sensor only returns 3 or 100

The magnetic field changes pretty fast but my phone seems to cope with it without any noticeable degradation.

madlyR

unread,
Mar 28, 2015, 3:55:18 AM3/28/15
to androi...@googlegroups.com
Steve, 
Proximity sensor doesn't show 0, when something is near (2-5 cm) to the proximity sensor? 
All other devices reported 0 at "near"state. It complicates detection as:
1. we can't trust if we get any initial value at the start (Xperia P, Jelly Bean 4.1)
2. we have to check Proximity value changes and assume lowest value as a "near" state, and maximum reading as "far" state. On some devices Proximity sensor could return "in between" state or "initial state" as stated by one user at Lenovo K5 / Jelly bean 4.1.


Steve, do you tried on "Fast" and "Fastest" speedMode?

madlyR

unread,
Mar 28, 2015, 4:01:21 AM3/28/15
to androi...@googlegroups.com
Dave,

time on Orientation and Acceleration callbacks on Android Jelly Bean 4.1 doesn't get typical JS time (milliseconds since midnight Jan 1, 1970). On KitKat.
I get results on JB 4.1 = 93642681, while Date() gives time: 1427529515386
Those two values should give almost the same or the same time.

Radek

madlyR

unread,
Mar 28, 2015, 10:37:51 AM3/28/15
to androi...@googlegroups.com
OK, next device report. Asus Nexus 7 (2013) - KitKat 4.4.
- there is no Proximity sensor inside Nexus,
- all works smoothly in slow and medium speedMode,
- in Fast mode - after shaking tablet, the same rang error script error... 
- Magnetic Field sensor works perfectly, screen isn't updated constantly,
- time returned in callbacks from Accelerometer and Orientation sensors is different than Date object, same as Sony Xperia P and Android 4.1.

As time vs. Date war. It;s rather device dependent, not Android version. Some devices (Samsung Galaxy S5)  returns time in Date format, others not.

Steve Garman

unread,
Mar 28, 2015, 11:20:39 AM3/28/15
to androi...@googlegroups.com
Radek,

I think I must have spelled "Fastest" wrong originally because it now falls over if I use "Fastest" (but not "Fast")

I''ll double-check about proximity value 3 being closest but I,m pretty sure it was.

Dave Smart

unread,
Mar 29, 2015, 1:24:24 PM3/29/15
to androi...@googlegroups.com
Hi Guys,

I just tried putting a try catch around my callback handler and it got rid of the "Maximum call stack size" error.


function sns_OnChange( x, y, z, time )
{
    try
    {
        txt.SetText( "x="+x + "\n y="+y + "\n z="+z );
    }
    catch(e) {}
}

Rodrigo Faustino

unread,
Jun 30, 2016, 6:45:03 PM6/30/16
to DroidScript
the question is how this sensor, it actually measures the magnectico field relamente give to distinguish between iron and wood?
Reply all
Reply to author
Forward
0 new messages