當(dāng)前位置: 首頁 > 傳感測量產(chǎn)品 > 工業(yè)傳感器 > 力傳感器
發(fā)布日期:2022-10-09 點擊率:63
加速度傳感器,重力傳感器,線性加速度傳感器 第1張" title="android 加速傳感器 重力傳感器:android 加速度傳感器,重力傳感器,線性加速度傳感器 第1張-傳感器知識網(wǎng)"/>
加速度傳感器檢測物體傾角的原理
檢物體傾角的一種常用方法是對陀螺儀輸出的角速度進行積分。雖然這種方法直截了當(dāng),但誤差會隨著積分時間的增加而快速累積。在某些應(yīng)用中,若整個時間范圍內(nèi)物體運動緩慢(忽略慣性力等因素的影響,物體只受重力作用),那么可以使用加速度計來測量物體的傾斜角度。該方法利用重力矢量及其在加速度計軸上的投影來確定傾斜角度。
以兩輪平衡小車(倒立擺模型)為例,當(dāng)檢測到傾角存在時要控制小車以一定的加速度運動,才能維持平衡而不至于倒下。因此傾角的測量成為控制小車直立的關(guān)鍵。而在實際小車運動過程
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 {
//設(shè)置LOG標(biāo)簽
private static final String TAG="sensor";
private SensorManager sm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//創(chuàng)建一個SensorManager來獲取系統(tǒng)的傳感器服務(wù)
sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
//選取加速度感應(yīng)器
int sensorType=Sensor.TYPE_ACCELEROMETER;
sm.registerListener(myAccelerometerListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
}
final SensorEventListener myAccelerometerListener=new SensorEventListener(){
//復(fù)寫onSensorChanged方法
public void onSensorChanged(SensorEvent sensorEvent){
if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
Log.i(TAG,"onSensorChanged");
//圖解中已經(jīng)解釋三個值的含義
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);
}
}
//復(fù)寫onAccuracyChanged方法
public void onAccuracyChanged(Sensor sensor , int accuracy){
Log.i(TAG, "onAccuracyChanged");
}
};
public void onPause(){
sm.unregisterListener(myAccelerometerListener);
super.onPause();
}
}
重力傳感器與方向傳感器的開發(fā)步驟類似,只要理清了期中的x,y,z的值之后就可以根據(jù)他們的變化來進行編程了,首先來看一副圖
假設(shè)當(dāng)?shù)氐闹亓铀俣戎禐間
當(dāng)手機正面朝上的時候,z的值為q,反面朝上的時候,z的值為-g
當(dāng)手機右側(cè)面朝上的時候,x的值為g,右側(cè)面朝上的時候,x的值為-g
當(dāng)手機上側(cè)面朝上的時候,y的值為g,右側(cè)面朝上的時候,y的值為-g
了解了重力傳感器中X,Y,Z的含義之后下面我們就開始學(xué)習(xí)如何使用
首先我們創(chuàng)建一個傳感器管理器和一個傳感器監(jiān)聽器,管理器用來管理傳感器以及創(chuàng)建各種各樣的傳感器,監(jiān)聽器用來監(jiān)視傳感器的變化并且進行相應(yīng)的操作
private SensorManager sensorManager;
private MySensorEventListener mySensorEventListener;
mySensorEventListener=new MySensorEventListener();//這個監(jiān)聽器當(dāng)然是我們自己定義的,在重力感 應(yīng)器感應(yīng)到手機位置有變化的時候,我們可以采取相應(yīng)的操作,這里緊緊是將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標(biāo)簽,不再贅述
tv_orientation.setText("Orientation:"+x+","+y+","+z);
}
}
我們在onResume方法中創(chuàng)建重力傳感器,并向系統(tǒng)注冊監(jiān)聽器
protected void onResume() {
Sensor sensor_accelerometer=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(mySensorEventListener,sensor_accelerometer, SensorManager.SENSOR_DELAY_UI);
super.onResume();
}
最后我們在onPause()中注銷所有傳感器的監(jiān)聽,釋放重力感應(yīng)器資源!
protected void onPause() {
/注銷所有傳感器的監(jiān)聽
sensorManager.unregisterListener(mySensorEventListener);
super.onPause();
}
到此,有關(guān)重力傳感器的介紹完畢!
重力感應(yīng)主要是依靠手機的加速度傳感器(accelerometer)來實現(xiàn)
在Android的開發(fā)中一共有八種傳感器但是不一定每一款真機都支持這些傳感器。因為很多功能用戶根本不care的所以可能開發(fā)商會把某些功能屏蔽掉。還是得根據(jù)真機的實際情況來做開發(fā),今天我們主要來討論加速度傳感器的具體實現(xiàn)方式。
傳感器名稱如下:
加速度傳感器(accelerometer)
陀螺儀傳感器(gyroscope)
環(huán)境光照傳感器(light)
磁力傳感器(magnetic field)
方向傳感器(orientation)
壓力傳感器(pressure)
距離傳感器(proximity)
溫度傳感器(temperature)
上面的是程序的運行圖
遇到的問題:
1、當(dāng)在與球相同的布局里面調(diào)用TextView時,球就不能移動了。最后我把球和數(shù)據(jù)分離開,用兩個布局處理的。
2、不明白super.setframe()函數(shù)到底是什么意思
有哪位大神看懂了,告訴我一下
這個是主程序代碼:
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);
//獲取感應(yīng)器管理器
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);//獲取重力加速度感應(yīng)器
success=sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);//注冊listener,第三個參數(shù)是檢測的精確度
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);
//當(dāng)重力x,y為0時,球處于中心位置,以y為軸心(固定不動),轉(zhuǎn)動手機,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,當(dāng)手機沿x、y軸成某個角度擺放時,變量x和y即為該角度的加速度 float percentageX=x / MAX_ACCELEROMETER;//得到當(dāng)前加速度的比率,如果手機沿x軸垂直擺放,比率為100%,即球在x軸上移動到最大值 float percentageY=y / MAX_ACCELEROMETER; int pixel_x=(int) (max_x * percentageX);//得到x軸偏移量 int pixel_y=(int) (max_y * percentageY);//得到y(tǒng)軸偏移量 //以球在中心位置的坐標(biāo)為參考點,加上偏移量,得到球的對應(yīng)位置,然后移動球到該位置 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三大控
上一篇: 電氣控制線路圖控制原