Recently a quick look at the sensors of the Android system. The more commonly used is the accelerometer accelerometer, which can be used to achieve "shake a shake."
At first, the accelerometer is the name itself to understand, that is, the measurement of acceleration, that is, if the phone is still, the accelerometer should return 0.
It turns out that the external support of the phone is the acceleration of the force exerted by the cell phone, and its direction is relative to the local coordinate system of the phone. Simply put, if the phone is stationary, the accelerometer has a value of-g. The advantage is that this value can reflect the direction of the phone at rest.
Phone local coordinate system
In other words, the cell phone in the process of falling,the value of accelerometer 0 (depending on the hardware, there will be different range of errors). Well, it's kind of interesting.
We can make use of this, by dropping the phone, recording the time of the fall, and then using a simple physical formula to figure out the distance of the fall.
Simple implementation in the NDK environment. The impact strength is estimated by the way after a collision has been detected.
Enum State{ready = 0,dropping,colliding,idle,};int state= ready;int64_t timestamp= 0;float dropHeight= 0.f;float Averageimpact = 0.f;int Nimpact = 0;//Use accelerometer track the dropping process//measuring time and estimate dropping Heightvoid trackdropping (const asensorevent& event) {vector3f V (event.acceleration.x, EVENT.ACCELERATION.Y, EVENT.ACCELERATION.Z); Float lenth = V.length (); if (state = = Ready) {//Detect Weightlessnessif (Lenth < 1.0f) {Timestam p = Event.timestamp;state = Dropping;}} if (state = = dropping) {//Detect Collideif (Lenth > Asensor_standard_gravity) {//in milliseconds, timestamp are in Nan osecondsint64_t during = (event.timestamp-timestamp)/1000000;float sec = During/1000.0f;dropheight = 0.5f * ASENSOR_ Standard_gravity * sec * sec;state = Colliding;return;}} if (state = = colliding) {averageimpact + = Lenth;nimpact++;if (Lenth < asensor_standard_gravity) {averageimpact/= NImpa Ct Glog.loginfo ("Drop Height%.2f impact%.2f", Dropheight, AverageimpACT); averageimpact = 0.f;nimpact = 0;timestamp = Event.timestamp;state = Idle;} return;} if (state = = Idle) {if ((event.timestamp-timestamp)/1000000 > +) {state = Ready;}}}
while (ident = Alooper_pollall (engine.animating 0:-1, NULL, &events, (void**) &source)) >= 0) { // Process this event. if (Source = NULL) { source->process (state, source); } If a sensor has data, process it now.if (ident = = Looper_id_user) {if (engine.accelerometersensor! = NULL) {Asensoreven T Event;while (asensoreventqueue_getevents (Engine.sensoreventqueue, &event, 1) > 0) {
Trackdropping (event); }}} Check if we are exiting. if (state->destroyrequested! = 0) { engine_term_display (&engine); return; } }
In fact, the implementation of Java can be, but do not know the sampling rate and delay will not be different.
After testing my test machine Coolpad great god F1, I found that there was a significant delay (greater than 0.1s) from the start of the fall to detecting a drop through the sensor, much larger than the sampling interval (10ms). Similarly, there is a noticeable delay in detecting collisions. The most important thing is that these two intervals are not as large and seriously affect the accuracy of the measurement results.
Later, a method was used to calculate the correction value of a delay time with a standard drop (e.g. 1 m high). With the delay correction, the measurement accuracy is significantly increased (within 10%).
Warm tips:
Small partners can try it themselves, but it's a good idea to back up the important data in your phone to avoid unnecessary losses.
Measuring height with cell phone sensors