[Android] Accelerometer
Accelerometer คืออะไร ?
Accelerometer เป็นเซ็นเซอร์วัดความเร่ง ที่อยู่ในมือถือแอนดรอยด์แทบทุกรุ่น
โดยเป็นความเร่งที่กระทำต่อมือถือของเรา ซึ่งก็รวมถึงแรงดึงดูดของโลก หรือ gravity ด้วยนั้นเอง
ประโยชน์ที่เอามาใช้แล้วเห็นภาพที่สุด ก็คือมาตรวจจับการหมุนของหน้าจอ (แนวตั้งหรือแนวนอน)
จากฟิสิกส์พื้นฐาน ตามกฏของนิวตัน แรง (F) = มวล (m) x ความเร่ง (a)
[math] F= ma [/math]
จึงกล่าวได้ว่า ค่าจากเซนเซอร์วัดความเร่งจะได้จากความเร่งที่กระทำกับมือถือ ([math]A_d[/math])
ซึ่งวัดจากแรงที่กระทำกับตัวเซนเซอร์เอง ([math]F_s[/math]) ดังสมการ
[math] A_d = — \sum{F_s} / mass[/math]
แต่อย่างไรก็ตาม แรงดึงดูดของโลกก็จะมีผลต่อการวัดความเร่งเสมอ ดังสมมการ
[math] A_d = -g — \sum{F} / mass[/math]
ด้วยเหตุนี้ เมื่อวางมือถือเฉยๆบนโต๊ะ (ไม่มีความเร่ง) จึงอ่านค่าขนาดของความเร่ง (magnitude)
(ขนาดของความเร่งคือ [math]magnitude =\sqrt{X²+Y²+Z²} [/math])
ได้เท่ากับค่า g หรือ 9.81 [math]m/s² [/math] (กระทำกับแกน Z ของมือถือ ในแนวดึงลง)
แต่ถ้ามือถือตกลงสู่พื้นแบบอิสระ (free fall) จะมีค่าความเร่งที่กระทำกับมือถือในแนวแกน Z ในทิศทางสวนกลับ
ทำให้อ่านขนาดของความเร่งได้เท่ากับ 0 [math]m/s² [/math] นั้นเอง
ดังนั้น ถ้าต้องการวัดค่าความเร่งจริงของมือถือ จึงจำเป็นต้องลบแรงดึงดูดของโลกออกไปด้วย
เจ้า accelerometer ที่อยู่ในมือถือเป็นชิพซิลิคอนเล็กๆที่เรียกกว่า MEMS (Micro Electro-Mecahnical Systems)
การทำงานจะมีหลักการคล้ายๆกับการนำลูกเหล็กมาผูกกับสปริงไว้ภาพด้านล่างนะครับ
ภาพ A : เมื่อเราวางมือถือเราไว้เฉยๆ บนโต๊ะ ก็มีแรงดึงดูดของโลกกระทำต่อมือถือของเราเพียงอย่างเดียว
ภาพ B : เมื่อเราแขว่งมือถือไปทางขวา ก็จะมีความเร่งไปทางขวาและค่าแรงดึงดูดของโลกกระทำพร้อมกัน
ภาพ C : เมือเราปล่อยมือถือตกแบบ freefall ก็จะไม่มีแรงใดๆมากระทำ
หลักการทำงานคร่าวๆดูได้จาก Youtube อันนี้นะครับ
[youtube]http://www.youtube.com/watch?v=KZVgKu6v808#t=254[/youtube]
- ระบบแกน (Coordinate Systems)
ระบบแกนที่เกี่ยวกับการหมุนและการเคลื่อนที่ในแอนดรอยด์จะประกอบไปด้วย 2 ระบบแกนคือ
- ระบบแกนของโลก [math](X_e,Y_e, Z_e)[/math]
- ระบบแกนของมือถือ [math](X,Y, Z)[/math]
ระบบแกนของเซนเซอร์ต่างๆ ในมือถือเรา หลักๆก็มี 3 แกนคือ X,Y และ Z
แต่เมื่อเราใช้งานจริง เราจำเป็นต้องเปรียบเทียบแกนของโลกด้วย
ก็คือแกนของมือถือเรานั้นเองครับ
X คือ แกนนอน (ด้านกว้าง)
Y คือ แกนตั้ง (ด้านยาว)
Z คือ แกนที่พุ่งออกมาจากหน้าจอ
ระบบแกนของโลก ก็จะมี 3 แกนเช่นกันคือ [math]X_e[/math], [math]Y_e[/math] และ [math]Z_e[/math]
แต่จะไม่เหมือนกับระบบแกนของมือถือนะครับ
[math]X_e[/math] คือ แกนที่พุ่งไปตามแนวตะวันออก — ตก
[math]Y_e[/math] คือ แกนที่พุ่งไปตามแนวเหนือ — ใต้
[math]Z_e[/math] คือ แกนที่พุ่งออกจากโลก
ระบบแกนของมือถือจะใช้กับเซนซอร์ดังต่อไปนี้ครับ
- Acceleration sensor
- Gravity sensor
- Gyroscope
- Linear Acceleration sensor
- Geomagnetic field sensor
การใช้งาน Accelerometer
มาลองเขียนโค๊ดง่ายๆ อ่านค่า Accelerometer ทั้ง 3 แกนกันครับ
- โค๊ดทั้งหมด MainActivity.java
package com.scuta.accelerometer;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.widget.TextView;public class MainActivity extends Activity implements SensorEventListener {private SensorManager mSensorManager;
private Sensor mAccelerometer;
private TextView axisX, axisY, axisZ;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);axisX = (TextView) findViewById(R.id.acc_x);
axisY = (TextView) findViewById(R.id.acc_y);
axisZ = (TextView) findViewById(R.id.acc_z);mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];axisX.setText("X: "+x);
axisY.setText("Y: "+y);
axisZ.setText("Z: "+z);
}@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
- โค๊ดทั้งหมด main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" ><TextView
android:id="@+id/acc_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accelerometer" /><TextView
android:id="@+id/acc_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X:" /><TextView
android:id="@+id/acc_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y:" /><TextView
android:id="@+id/acc_z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z:" /></LinearLayout>อ้างอิงhttp://developer.android.com/guide/topics/sensors/sensors_overview.html
http://developer.android.com/guide/topics/sensors/sensors_motion.html
http://th.mouser.com/new/stmicroelectronics/stmemsaccelerometers/
http://en.wikipedia.org/wiki/Accelerometer