• ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度


    一、简介

    二、代码
    1.java

    (1)MainActivity.java

     1 import android.app.Activity;
     2 import android.content.Context;
     3 import android.hardware.Sensor;
     4 import android.hardware.SensorEvent;
     5 import android.hardware.SensorEventListener;
     6 import android.hardware.SensorManager;
     7 import android.os.Bundle;
     8 
     9 public class MainActivity extends Activity {
    10     /** Called when the activity is first created. */
    11     
    12     private SensorManager sensorManager;
    13     private Sensor accelerometerSensor;
    14     private float gravity [] = new float[3];
    15     private float linear_acceleration [] = new float[3];
    16     @Override
    17     public void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20         
    21         sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    22         accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    23         sensorManager.registerListener(new SensorEventListener() {
    24             
    25             public void onSensorChanged(SensorEvent event) {
    26                 
    27                 /*System.out.println("x---" + event.values[0]);
    28                 System.out.println("y---" + event.values[1]);
    29                 System.out.println("z---" + event.values[2]);*/
    30                   final float alpha = 0.8f;
    31 
    32                   //用滤波器过滤掉杂质
    33                   gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
    34                   gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
    35                   gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
    36 
    37                   linear_acceleration[0] = event.values[0] - gravity[0];
    38                   linear_acceleration[1] = event.values[1] - gravity[1];
    39                   linear_acceleration[2] = event.values[2] - gravity[2];
    40             
    41                   System.out.println(linear_acceleration[0]);
    42                   System.out.println(linear_acceleration[1]);
    43                   System.out.println(linear_acceleration[2]);
    44 //                  System.out.println(gravity[0]);
    45 //                  System.out.println(gravity[1]);
    46 //                  System.out.println(gravity[2]);
    47             }
    48             
    49             public void onAccuracyChanged(Sensor sensor, int accuracy) {
    50                 
    51             }
    52         }, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    53     }
    54 }

     

  • 相关阅读:
    02 createJS中关于图片优化的问题(推荐一个图片优化的网站)
    01 createJS代码作用域
    文件读取一些payload
    SQL注入笔记-updatexml与extractvalue
    四位数六位数字典生成
    XShell上传文件到Linux服务器上
    centos彻底删除文件夹创建文件
    2020.6.18 时间记录 总学习5.15
    2020.6.14 时间记录
    五种IT职位的介绍
  • 原文地址:https://www.cnblogs.com/shamgod/p/5207549.html
Copyright © 2020-2023  润新知