Mobile two or three "four": Gyroscope (gravity sensor) for mobile phone position, acceleration sensing and common applications.

Source: Internet
Author: User

First of all, the gyroscope sensor needs to be debugged in the real machine environment, the PC end has no effect.

1. Get the Sensor

The devicemotion event is monitored on the window, and the accelerationincludinggravity (built-in gravitational acceleration sensor) object is obtained through the event object. The code is as follows:

function (e) {        var motion = e.accelerationincludinggravity;         var x = motion.x;         var motion.y;         var z = motion.z;         // the gravitational acceleration of x, Y, z three axes         Console.log (x);        Console.log (y);        Console.log (z);});

The x, y, Z axes are not good enough to cover. (z-axis perpendicular to the phone):

2. Position and acceleration of gravity:

It can be learned from the above that the obtained values are in different directions of the gravitational acceleration, so different positions of X, Y, Z are different:

The locations mentioned here include location and mobile phone placement:

Shijiazhuang (here for Home call) geographical location (north latitude 37°27′~38°47′, longitude 113°30′~115°20′), the gravitational acceleration of 9.7997.

Now assume that the gravity of the position is 9.8:, then:

Put the phone flat on the table, the x-axis and the y-axis of the gravitational acceleration of the 0,z axis of the value of 9.8;

The mobile phone is erected to prevent (completely upright), then the x-axis and z-axis of the gravitational acceleration of the 0,y axis is more than 9.8 of the value of gravity;

When the phone is sideways (completely sideways), the gravitational acceleration of the y-axis and z-axis is 0,x axis of 9.8.

* You can use the phone to run the following code in a server environment to experience:

<!DOCTYPE HTML><HTMLLang= "en"><Head><MetaCharSet= "UTF-8"><Metaname= "Viewport"content= "Width=device-width,user-scalable=no" /><title>Document</title><styletype= "Text/css">#box{width:200px;Height:200px;background:Red;Color:#fff;font-size:20px;}    </style></Head><Body><DivID= "box"></Div><Scripttype= "Text/javascript">    varBox=Document.queryselector ('#box'); Window.addeventlistener ('devicemotion', function(e) {varMotion=e.accelerationincludinggravity; varx=motion.x.tofixed (3); vary=motion.y.tofixed (3); varZ=motion.z.tofixed (3); Box.innerhtml= "x:"+x; Box.innerhtml+= "<br/>y:"+y; Box.innerhtml+= "<br/>z:"+Z; });</Script></Body></HTML>

Mobile two or three "four": Gyroscope (gravity sensor) for mobile phone position, acceleration sensing and common applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.