![]() * If the roll is positive, you're in reverse landscape (landscape right), and if the roll is negative you're in landscape (landscape left) SensorManager.getOrientation(R, orientation) ![]() If (mGravity != null & mGeomagnetic != null)īoolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic) ![]() Check the actual blog post for a more complete solution. This currently only works if the phone is held horizontally. You can call this method in your OnSensorChanged method to detect if the phone was tilt upwards. You can use the accelerometer and magnetic field sensor to accomplish this. The pictures was taken from this article, wich i recomend to read if you want to improve the accuracy and consider 2 o 3 axis for the tilt For sure you can find some libraries or code snippets to help you out with this, but i think is good, as you say, to know the logic of inferring the tilt from the readings But if you dont get a good value and the time is consumed, you reset everything: let your reference value be the last one, reset the timers, reset the half-tilt-done boolean to false, and keep measuring. if you find a valid value (accomplishing value difference and time conditions ), you have a tilt.so start taking values comparing the new reference value with the new values coming from the sensor, and see if one accomplish what you decided in point 3. if the last difference was positive, now you need a negative difference, and if the last difference was negative, now you need a positive difference this is, coming back.if you detect two measurements that accomplish those conditions, you take note of half tilt done (in a boolean for instance), and start measuring again, but now, the new reference value is the value that was considered half tilt.a big rotation is good but only if it is fast enough, and a fast movement is good only if the difference in the angle is big enough) Decide a maximum time and a minimum sensor difference, that you will consider a valid movement (e.g.write a function to get the difference in the sensor value for that axis between one function call, and the next.So, if you take the value over the time, and compare to each other, you can figure out how big the rotation was. Since the values are more or less proportional, as you can see in the table below, you can work directly with the value of the sensor, without taking much care of what angle represents, if you don't need it to be much accurate, since a change in that value means a proportional change in the angle, so with a few test, you will find out how big should be the change in order to be relevant to you. ![]() This means that the value given to you by the sensor, is = to 9,8 * sine of the angle, so doing the maths you can get the actual angle.īut don't worry, you don't even have to do this. This means that having the values of an axis (those are acceleration values) you can calculate the angle in which the device is. Referring to basic trigonometry, the projection of the gravity vector on the x-axis produces an output acceleration equal to the sine of the angle between the accelerometer x-axis and the horizon. Using just one axis, lets say, axis x, will be enough, since you can detect accurately a change in that axis position, since even any small movement will do a change in the axis.īut, if your application is only reading that axis, and the user has the phone almost vertical, the difference in x axis will be really small even rotating the phone a big angle.Īnyways,for applications that only need coarse resolution, a single-axis can be used. Think the mobile is completely horizontal, and you move it like this: If you use only one axis, it is quite simple. Depending on how accurate you want it, and how much you feel like fighting with maths. You can take into account 1 axis, 2 axis, or the 3 axis. A tilt can be detected in a sort of diferent ways.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |