Sensor Topic (3)——Acceleration/Gyro Sensor
Introduction to this section:
This section continues to discuss the sensors in Android. This section brings the acceleration sensor (Accelerometer sensor) and Gyroscope sensor, like the direction sensor in the previous section, has three axes: x, y, and z. One more thing to say: the coordinates of the x and y axes must be distinguished from the x and y axes of the drawing! The sensor is in the lower left corner For the origin! x goes to the right, y goes up! Okay, let’s learn the sensors in this section with our routine!
In addition, what I want to say is that we are not specialized in this. We just write things for fun and gain experience. There are many things. Don't be too serious!
PS: The direction sensor actually uses an acceleration sensor and a magnetic field sensor to obtain the orientation. It has been abandoned since 2.2~
1. Accelerometer sensor
1) Noun concept:
- unit of acceleration sensor:Acceleration (m/s^2)
- The acceleration obtained by the direction sensor is: The combined acceleration of the acceleration of the mobile phone and the acceleration of gravity (9.81m/s^2)
- In addition, the acceleration of gravity is vertically downward!
The calculation of the total acceleration in different directions seems quite complicated, so we won’t worry about it here! Let’s first take a look at the values of the three numbers in the acceleration value array~ It’s still the code in the previous section, just change the sensor~
Place it horizontally: Vertical and flat placement: Vertical and horizontal placement:
From the above we know that the three values of the value array correspond to the X, Y, and Z axes respectively The acceleration! Okay, now that we have a rough idea, let’s write a simple pedometer to get familiar with its usage!
2). Implementation of simple pedometer
Operation renderings:
##Code implementation:
Layout code:activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:text="简易计步器" android:textSize="25sp" /> <TextView android:id="@+id/tv_step" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:text="0" android:textColor="#DE5347" android:textSize="100sp" android:textStyle="bold" /> <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="64dp" android:text="开始" android:textSize="25sp" /></LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SensorEventListener { private SensorManager sManager; private Sensor mSensorAccelerometer; private TextView tv_step; private Button btn_start; private int step = 0; //步数 private double oriValue = 0; //原始值 private double lstValue = 0; //上次的值 private double curValue = 0; //当前值 private boolean motiveState = true; //是否处于运动状态 private boolean processState = false; //标记当前是否已经在计步 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sManager = (SensorManager) getSystemService(SENSOR_SERVICE); mSensorAccelerometer = sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sManager.registerListener(this, mSensorAccelerometer, SensorManager.SENSOR_DELAY_UI); bindViews(); } private void bindViews() { tv_step = (TextView) findViewById(R.id.tv_step); btn_start = (Button) findViewById(R.id.btn_start); btn_start.setOnClickListener(this); } @Override public void onSensorChanged(SensorEvent event) { double range = 1; //设定一个精度范围 float[] value = event.values; curValue = magnitude(value[0], value[1], value[2]); //计算当前的模 //向上加速的状态 if (motiveState == true) { if (curValue >= lstValue) lstValue = curValue; else { //检测到一次峰值 if (Math.abs(curValue - lstValue) > range) { oriValue = curValue; motiveState = false; } } } //向下加速的状态 if (motiveState == false) { if (curValue range) { //检测到一次峰值 oriValue = curValue; if (processState == true) { step++; //步数 + 1 if (processState == true) { tv_step.setText(step + ""); //读数更新 } } motiveState = true; } } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} @Override public void onClick(View v) { step = 0; tv_step.setText("0"); if (processState == true) { btn_start.setText("开始"); processState = false; } else { btn_start.setText("停止"); processState = true; } } //向量求模 public double magnitude(float x, float y, float z) { double magnitude = 0; magnitude = Math.sqrt(x * x + y * y + z * z); return magnitude; } @Override protected void onDestroy() { super.onDestroy(); sManager.unregisterListener(this); } }Okay, it’s really a very simple pedometer...The number of steps on it was taken by me while sitting down...
, after all, it’s just for fun~
2. Gyroscope sensor
1) Noun concept:
Gyroscope is also called an angular velocity sensor and is generally used to detect mobile phones. Attitude, it seems that the gyroscope sensors in mobile phones are generally three-axis! Motion-sensing games are most commonly used in mobile phone camera stabilization, GPS inertial navigation, and adding some motion sensing to the APP (such as gently shaking the phone). Turn off the incoming call ringtone), etc. For details, you can go to Baidu yourself~
- Unit of the gyroscope sensor:Angular velocity (radians/second) radians/second
- The sensor is obtained using: Sensor.TYPE_GYROSCOPE
Its three values are rotated along the X-axis, Y-axis, and Z-axis. Angular velocity, when the mobile phone rotates counterclockwise, the angular velocity value is positive, while clockwise it is negative! Often used to calculate the angle the phone has turned! This is a piece of code on the Internet~
private static final float NS2S = 1.0f / 1000000000.0f; private float timestamp; public void onSensorChanged(SensorEvent event) { if (timestamp != 0) { // event.timesamp表示当前的时间,单位是纳秒(1百万分之一毫秒) final float dT = (event.timestamp - timestamp) * NS2S; angle[0] += event.values[0] * dT; angle[1] += event.values[1] * dT; angle[2] += event.values[2] * dT; } timestamp = event.timestamp; }
Using the time difference (dT) between two consecutive data acquisitions by the gyroscope sensor, the angle of rotation of the mobile phone along the X, Y, and Z axes during this period of time is calculated, and Accumulate the values to different elements of the angle array respectively
3. Download the sample code of this section:
SensorDemo4.zip
Summary of this section:
Okay, this section briefly introduces the acceleration sensor and gyroscope to you, and writes a simple pedometer. I feel like I haven’t played with sensors much, so there’s nothing to write about. Forget it, I’ll briefly introduce the remaining sensors in the next section. Forget it, just treat it as popular science. If you need to use it in the future, you can study it in depth~