產品分類

      當前位置: 首頁 > 傳感測量產品 > 工業傳感器 > 力傳感器

      類型分類:
      科普知識
      數據分類:
      力傳感器

      android 加速傳感器 重力傳感器:android 加速度傳感器,重力傳感器,線性加速度傳感器

      發布日期:2022-10-09 點擊率:70


      android 加速傳感器 重力傳感器:android <a title=加速度傳感器,重力傳感器,線性加速度傳感器 第1張" title="android 加速傳感器 重力傳感器:android 加速度傳感器,重力傳感器,線性加速度傳感器 第1張-傳感器知識網"/>

      android 加速傳感器 重力傳感器:android 加速度傳感器,重力傳感器,線性加速度傳感器

      加速度傳感器檢測物體傾角的原理
      檢物體傾角的一種常用方法是對陀螺儀輸出的角速度進行積分。雖然這種方法直截了當,但誤差會隨著積分時間的增加而快速累積。在某些應用中,若整個時間范圍內物體運動緩慢(忽略慣性力等因素的影響,物體只受重力作用),那么可以使用加速度計來測量物體的傾斜角度。該方法利用重力矢量及其在加速度計軸上的投影來確定傾斜角度。
      以兩輪平衡小車(倒立擺模型)為例,當檢測到傾角存在時要控制小車以一定的加速度運動,才能維持平衡而不至于倒下。因此傾角的測量成為控制小車直立的關鍵。而在實際小車運動過程

      android 加速傳感器 重力傳感器:Android基于Sensor感應器獲取重力感應加速度的方法

      package uni.sensor;
      import java.util.Iterator;
      import java.util.List;
      import android.app.Activity;
      import android.content.Context;
      import android.hardware.Sensor;
      import android.hardware.SensorEvent;
      import android.hardware.SensorEventListener;
      import android.hardware.SensorManager;
      import android.os.Bundle;
      import android.util.Log;
      public class SensorDemoActivity extends Activity {

      //設置LOG標簽
       private static final String TAG="sensor";
       private SensorManager sm;
       @Override
       public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      //創建一個SensorManager來獲取系統的傳感器服務
      sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
      //選取加速度感應器
      int sensorType=Sensor.TYPE_ACCELEROMETER;

      sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
       }

      final SensorEventListener myAccelerometerListener=new SensorEventListener(){
      //復寫onSensorChanged方法
      public void onSensorChanged(SensorEvent sensorEvent){
       if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
      Log.i(TAG,"onSensorChanged");
      //圖解中已經解釋三個值的含義
      float X_lateral=sensorEvent.values[0];
      float Y_longitudinal=sensorEvent.values[1];
      float Z_vertical=sensorEvent.values[2];
      Log.i(TAG,"
      heading "+X_lateral);
      Log.i(TAG,"
      pitch "+Y_longitudinal);
      Log.i(TAG,"
      roll "+Z_vertical);
       }
      }
      //復寫onAccuracyChanged方法
      public void onAccuracyChanged(Sensor sensor , int accuracy){
       Log.i(TAG, "onAccuracyChanged");
      }
       };
       public void onPause(){

      sm.unregisterListener(myAccelerometerListener);
      super.onPause();
       }
      }

      android 加速傳感器 重力傳感器:Android開發之重力傳感器

      重力傳感器與方向傳感器的開發步驟類似,只要理清了期中的x,y,z的值之后就可以根據他們的變化來進行編程了,首先來看一副圖

      假設當地的重力加速度值為g
      當手機正面朝上的時候,z的值為q,反面朝上的時候,z的值為-g
      當手機右側面朝上的時候,x的值為g,右側面朝上的時候,x的值為-g
      當手機上側面朝上的時候,y的值為g,右側面朝上的時候,y的值為-g
      了解了重力傳感器中X,Y,Z的含義之后下面我們就開始學習如何使用
      首先我們創建一個傳感器管理器和一個傳感器監聽器,管理器用來管理傳感器以及創建各種各樣的傳感器,監聽器用來監視傳感器的變化并且進行相應的操作
      private SensorManager sensorManager;
      private MySensorEventListener mySensorEventListener;
      mySensorEventListener=new MySensorEventListener();//這個監聽器當然是我們自己定義的,在重力感     應器感應到手機位置有變化的時候,我們可以采取相應的操作,這里緊緊是將x,y,z的值打印出來
      private final class MySensorEventListener implements  SensorEventListener{

      @Override
      //可以得到傳感器實時測量出來的變化值
      public void onSensorChanged(SensorEvent event) {
      //重力傳感器
      if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
      float x=event.values[SensorManager.DATA_X];
      float y=event.values[SensorManager.DATA_Y];
      float z=event.values[SensorManager.DATA_Z];
      //tv_accelerometer是界面上的一個TextView標簽,不再贅述
      tv_orientation.setText("Orientation:"+x+","+y+","+z);
      }
      }

      我們在onResume方法中創建重力傳感器,并向系統注冊監聽器
      protected void onResume() {
      Sensor sensor_accelerometer=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      sensorManager.registerListener(mySensorEventListener,sensor_accelerometer,   SensorManager.SENSOR_DELAY_UI);

      super.onResume();
      }
      最后我們在onPause()中注銷所有傳感器的監聽,釋放重力感應器資源!
      protected void onPause() {
      /注銷所有傳感器的監聽
      sensorManager.unregisterListener(mySensorEventListener);
      super.onPause();
      }
      到此,有關重力傳感器的介紹完畢!
      android 加速傳感器 重力傳感器:android 加速度傳感器,重力傳感器,線性加速度傳感器  第2張

      android 加速傳感器 重力傳感器:Android的重力傳感器(3軸加速度傳感器)簡單實例

      重力感應主要是依靠手機的加速度傳感器(accelerometer)來實現

      在Android的開發中一共有八種傳感器但是不一定每一款真機都支持這些傳感器。因為很多功能用戶根本不care的所以可能開發商會把某些功能屏蔽掉。還是得根據真機的實際情況來做開發,今天我們主要來討論加速度傳感器的具體實現方式。

      傳感器名稱如下:

      加速度傳感器(accelerometer)
      陀螺儀傳感器(gyroscope)
      環境光照傳感器(light)
      磁力傳感器(magnetic field)
      方向傳感器(orientation)
      壓力傳感器(pressure)
      距離傳感器(proximity)
      溫度傳感器(temperature)

      上面的是程序的運行圖

      遇到的問題:
      1、當在與球相同的布局里面調用TextView時,球就不能移動了。最后我把球和數據分離開,用兩個布局處理的。
      2、不明白super.setframe()函數到底是什么意思
      有哪位大神看懂了,告訴我一下

      這個是主程序代碼:
      package cn.itcast.accelerometer;
      import cn.itcast.accelerometer.view.BallView;
      import android.app.Activity;
      import android.graphics.Color;
      import android.hardware.Sensor;
      import android.hardware.SensorEvent;
      import android.hardware.SensorEventListener;
      import android.hardware.SensorManager;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.TextView;
      public class AccelerometerActivity extends Activity {
      private static final float MAX_ACCELEROMETER=9.81f;
      private SensorManager sensorManager;
      private BallView ball;
      private boolean success=false;
      private boolean init=false;
      private int container_width=0;
      private int container_height=0;
      private int ball_width=0;
      private int ball_height=0;
      private TextView prompt;
      private TextView tv1;
      private TextView tv2;
      private TextView tv3;

      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      //獲取感應器管理器
      sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
      prompt=(TextView) findViewById(R.id.ball_prompt);
      tv1=(TextView)findViewById(R.id.tv1);
      tv2=(TextView)findViewById(R.id.tv2);
      tv3=(TextView)findViewById(R.id.tv3);
      }
      @Override
      public void onWindowFocusChanged(boolean hasFocus) {//ball_container控件顯示出來后才能獲取其寬和高,所以在此方法得到其寬高
      super.onWindowFocusChanged(hasFocus);
      if(hasFocus && !init){
      View container=findViewById(R.id.ball_container);
      container_width=container.getWidth();
      container_height=container.getHeight();
      ball=(BallView) findViewById(R.id.ball);
      ball_width=ball.getWidth();
      ball_height=ball.getHeight();
      moveTo(0f, 0f);
      init=true;
      }
      }
      @Override
      protected void onResume() {
      Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//獲取重力加速度感應器
      success=sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);//注冊listener,第三個參數是檢測的精確度
      super.onResume();
      }

      @Override
      protected void onPause() {
      if(success) sensorManager.unregisterListener(listener);
      super.onPause();
      }
      private SensorEventListener listener=new SensorEventListener() {
      @Override
      public void onSensorChanged(SensorEvent event) {

      if (!init) return ;
      float x=event.values[SensorManager.DATA_X];
      float y=event.values[SensorManager.DATA_Y];
      float z=event.values[SensorManager.DATA_Z];
      prompt.setText("X=" + x + ",Y=" + y + ", Z=" + z);
      //當重力x,y為0時,球處于中心位置,以y為軸心(固定不動),轉動手機,x會在(0-9.81)之間變化,負號代表方向
      moveTo(-x, y);//x方向取反

              if(x>0){
      tv1.setTextColor(Color.WHITE);
      tv1.setText("向左");
      }

              if(x

              if(y

              if(z<0){         tv3.setTextColor(Color.YELLOW);         tv3.setText("向下");         }                 } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; private void moveTo(float x, float y) {        int max_x=(container_width - ball_width) / 2;//在x軸可移動的最大值        int max_y=(container_height - ball_height) / 2;//在y軸可移動的最大值        //手機沿x、y軸垂直擺放時,自由落體加速度最大為9.81,當手機沿x、y軸成某個角度擺放時,變量x和y即為該角度的加速度        float percentageX=x / MAX_ACCELEROMETER;//得到當前加速度的比率,如果手機沿x軸垂直擺放,比率為100%,即球在x軸上移動到最大值        float percentageY=y / MAX_ACCELEROMETER;                        int pixel_x=(int) (max_x * percentageX);//得到x軸偏移量        int pixel_y=(int) (max_y * percentageY);//得到y軸偏移量        //以球在中心位置的坐標為參考點,加上偏移量,得到球的對應位置,然后移動球到該位置                int x3=max_x + pixel_x;//屏幕中心位置+x軸偏移        int y3=max_y + pixel_y;//屏幕中心位置+y軸偏移                ball.moveTo(x3, y3);                  } } 球的代碼移動: package cn.itcast.accelerometer.view; import android.content.Context; import android.util.AttributeSet; import android.widget.ImageView; public class BallView extends ImageView { public BallView(Context context) { super(context); }    public BallView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public BallView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public void moveTo(int x, int y) {//沒有弄明白什么意思,誰懂了告訴我一下啊,我加一個TextView,球就不能移動了    itas109        super.setframe(x, y, x + getWidth(), y + getHeight());//繪制視圖,由左上角與右下角確定視圖矩形位置    } }   整個程序的下載地址:

      下一篇: PLC、DCS、FCS三大控

      上一篇: 電氣控制線路圖控制原

      主站蜘蛛池模板: 中文乱码精品一区二区三区| 国产在线一区二区视频| 亚洲成在人天堂一区二区| 无码人妻一区二区三区一| 大伊香蕉精品一区视频在线| 国产丝袜一区二区三区在线观看| 91video国产一区| 中文字幕VA一区二区三区| 日韩电影在线观看第一区| 午夜视频在线观看一区| 极品人妻少妇一区二区三区| 国产精品区一区二区三在线播放| 一区二区三区免费视频播放器| 日韩aⅴ人妻无码一区二区| 精品亚洲av无码一区二区柚蜜| 精品国产一区二区三区四区| 亚洲AV无一区二区三区久久| 国产午夜精品一区二区三区| 国产午夜精品一区二区三区| 国产精品无圣光一区二区 | 国产内射999视频一区| 色精品一区二区三区| 一区二区中文字幕在线观看| 无码毛片一区二区三区中文字幕| 免费人妻精品一区二区三区| 变态调教一区二区三区| 亚洲一区精品伊人久久伊人| 精品人妻少妇一区二区| 熟女性饥渴一区二区三区| 久久精品一区二区三区四区| 久久人妻内射无码一区三区| 日韩在线不卡免费视频一区| 亚洲综合一区二区精品久久| 无码国产精品久久一区免费 | 成人区精品一区二区不卡 | 久久一区二区免费播放| 亚洲一区二区三区AV无码| 亚欧色一区W666天堂| 亚洲AV无码一区二区三区性色| 亚洲日韩中文字幕一区| 国产一区二区在线观看麻豆|