Android uses directional sensors to get a relative angle to the phone

Source: Internet
Author: User
Tags final range relative stub

The 1.android coordinate system defines the x, y z axes.

The direction of the x-axis is the horizontal direction of the screen from left to right, if the cell phone is not a square, the shorter side needs to be placed horizontally, the longer side needs to be placed vertically.

The y-axis is directed from the bottom left corner of the screen to the top of the screen along the vertical direction of the screen.

Put the phone on the table, the direction of the z axis from the phone pointing to the sky.

2. Direction sensor

The 3 values of the value variables in the directional sensor represent degrees, and their meanings are as follows:

VALUES[0]: This value represents the orientation, which is the angle at which the phone rotates around the z axis. 0 represents northern (north), 90 for East (eastern), 180 for South (South), and 270 for West (western). If the value of values[0] is exactly these 4 values, and the cell phone is placed horizontally, it means that the phone is in front of the 4 directions. This feature can be used to achieve the electronic compass, example 76 will detail the implementation of the electronic compass process.

VALUES[1]: This value indicates the inclination, or the extent to which the phone is tilted. The value changes when the phone is tilted around the x axis. The value range of values[1] is -180≤values[1]≤180.

If you put your phone screen up on the table, at this point, if the table is completely horizontal, the value of values[1] should be 0 (since very few tables are absolutely horizontal, the value is probably not 0, but typically a value between 5 and 5). Lift it from the top of the phone until you rotate the phone 180 degrees along the x axis (the screen is horizontally on the desktop). In this rotation, values[1] will change between 0 and 180, that is to say, when lifted from the top of the phone, the value of values[1 will gradually become smaller until it equals 180. If you start at the bottom of your phone until you rotate the phone 180 degrees along the x axis, values[1 will change between 0 and 180. The value of values[1] increases gradually until it equals 180. You can use Values[1] and the values[2 described below to measure the tilt of objects such as tables.

VALUES[2]: Indicates the mobile phone's scrolling angle along the y axis. The value range is -90≤values[2]≤90. Suppose you put your phone screen up on the desktop, and if the desktop is flat, the value of values[2] should be 0. When the left side of the phone is gradually raised, the value of values[2] gradually becomes smaller until the phone is placed perpendicular to the desktop, when the value of values[2] is 90. When the right side of the phone is gradually raised, the value of values[2] gradually increases until the phone is placed perpendicular to the desktop, when the value of values[2] is 90. Continue scrolling to the right or left at the vertical position, the value of values[2] continues to change between 90 and 90.

Here is an example of how to apply it

Package com.example.sensortest;  
      
Import java.util.List;  
Import android.app.Activity;  
Import Android.hardware.Sensor;  
Import android.hardware.SensorEvent;  
Import Android.hardware.SensorEventListener;  
Import Android.hardware.SensorManager;  
Import Android.os.Bundle;  
Import Android.util.Log;  
Import Android.view.Menu;  
Import Android.view.View;  
Import Android.view.View.OnClickListener;  
Import Android.widget.Button;  
      
Import Android.widget.TextView; public class Sensortest extends activity implements sensoreventlistener{private Sensormanager Sensorman  
        ager = null;  
        Private Sensor gyrosensor = null;  
        Private TextView VX;  
        Private TextView VY;  
        Private TextView VZ;  
        Private TextView V;  
        Private button button;  
        Private static final float ns2s = 1.0f/1000000000.0f;  
        private float timestamp;    
      
    Private float[] angle = new FLOAT[3]; @SuppreSswarnings ("deprecation") @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreat  
        E (savedinstancestate);  
        Setcontentview (r.layout.activity_sensor_test);  
        VX = (TextView) Findviewbyid (R.ID.VX);  
        VY = (TextView) Findviewbyid (R.id.vy);  
        VZ = (TextView) Findviewbyid (R.ID.VZ);   
        v = (TextView) Findviewbyid (R.ID.V);  
        Button = (button) Findviewbyid (R.id.button);  
        Sensormanager = (Sensormanager) getsystemservice (Sensor_service);  
        GyroSensor = Sensormanager. Getdefaultsensor (sensor.type_orientation);  
        Vx.settext ("!!!!!!"); Button.setonclicklistener (New Onclicklistener () {@Override public void oncli CK (View arg0) {//TODO auto-generated Method Stub//declare variable string STRINGB  
                Uffer sb = new StringBuffer (); Get all the sensors on the phone List<sensor> sensors = sensormanager.getsensorlist (Sensor.type_all); The iteration output obtains the sensor for (Sensor sensor:sensors) {//system.out.println (Sensor.getname (). To  
                String ());  
                Sb.append (Sensor.getname (). toString ());  
                Sb.append ("\ n");  
                LOG.I ("Sensor", Sensor.getname (). toString ());  
            ///Assign value to Text control V.settext (sb.tostring ());  
    }  
        });  
        Public Sensortest () {//TODO auto-generated constructor stub angle[0] = 0;  
        ANGLE[1] = 0;  
        ANGLE[2] = 0;  
    timestamp = 0; @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the ' menu; this ad  
        DS items to the action bar if it is present.  
        Getmenuinflater (). Inflate (r.menu.activity_sensor_test, menu);  
    return true; } @Override protected void OnpaUse () {//TODO auto-generated Method Stub super.onpause (); Sensormanager.unregisterlistener (this);  
        Dismiss Listener Registration} @Override protected void Onresume () {//TODO auto-generated method stub  
         Super.onresume ();  Sensormanager.registerlistener (This, gyrosensor, sensormanager.sensor_delay_normal); Registering listeners for sensors} @Override public void onaccuracychanged (Sensor Sensor, int accuracy) {//T Odo auto-generated Method stub} @Override public void onsensorchanged (Sensorevent E VENT) {//TODO auto-generated Method Stub//if (event.accuracy = = Sensormanager.sensor_status_unreliabl    
E)//{//return; }//if (timestamp!= 0) {//final float DT = (event.timestamp-timestamp) *  
Ns2s;  
Angle[0] + = event.values[0] * DT * 100;       //   ANGLE[1] + = event.values[1] * DT * 100;  
ANGLE[2] + = event.values[2] * DT * 100;  
}//timestamp = Event.timestamp;  
//Vx.settext ("X:" + float.tostring (angle[0));   
Vy.settext ("Y:" + float.tostring (angle[1));  
              
Vz.settext ("Z:" + float.tostring (angle[2));  
The directional sensor provides three data, namely azimuth, pitch and roll.  
Azimuth: Azimuth, returns the angle between the magnetic north and the Y axis at a horizontal time, ranging from 0° to 360°.  
0°= North, 90°= East, 180°= South, 270°= West.  
The angle between the pitch:x axis and the horizontal plane, ranging from -180° to 180°.  
When the z axis rotates to the y-axis, the angle is positive.  
The angle between the roll:y axis and the horizontal plane, for historical reasons, ranges from -90° to 90°.  
              
         The angle is positive when the x axis moves to the z-axis.  
         Vx.settext ("Orientation X:" + event.values[0]);  
         Vy.settext ("Orientation Y:" + event.values[1]);  
              
    Vz.settext ("Orientation Z:" + event.values[2]); }  
      
}

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.