ภาพรวม
ตัวอย่างนี้สาธิตวิธีตรวจสอบการรองรับเซ็นเซอร์ Gyroscope (ยีโรสโคป) ซึ่งวัดความเร็วเชิงมุม (Angular velocity) รอบ 3 แกน ใช้สำหรับการตรวจจับการหมุน การเล่นเกม VR/AR และการนำทาง
ขั้นตอนการสร้างโปรเจกต์
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → Empty Activity
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/actionEvent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:textSize="40sp"
android:gravity="center" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
SensorManager sensorManager;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView actionEvent = findViewById(R.id.actionEvent);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// ตรวจสอบเซ็นเซอร์ Gyroscope
Sensor gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
if (gyroscope != null) {
actionEvent.setText("รองรับ Gyroscope (ยีโรสโคป)");
String info = String.format(
"Name: %s\nVendor: %s\nVersion: %d\n" +
"Max Range: %.2f rad/s\nResolution: %.2f rad/s\nPower: %.2f mA\nMin Delay: %d µs",
gyroscope.getName(),
gyroscope.getVendor(),
gyroscope.getVersion(),
gyroscope.getMaximumRange(),
gyroscope.getResolution(),
gyroscope.getPower(),
gyroscope.getMinDelay()
);
actionEvent.append("\n\n" + info);
} else {
actionEvent.setText("ไม่รองรับ Gyroscope");
}
}
}
ข้อมูลเซ็นเซอร์ Gyroscope
| คุณสมบัติ | รายละเอียด |
|---|---|
| Constant | Sensor.TYPE_GYROSCOPE |
| หน่วยวัด | Radian per second (rad/s) |
| แกน | 3 แกน (X, Y, Z) |
| วัดค่า | ความเร็วเชิงมุม (Angular velocity) |
| อุปกรณ์ที่มี | สมาร์ทโฟนส่วนใหญ่ (Mid-range ขึ้นไป) |
การใช้งานจริง
- Gaming: ควบคุมเกมด้วยการเอียง/หมุนโทรศัพท์ (Racing, FPS)
- VR/AR: Head tracking สำหรับ Google Cardboard, Daydream
- Navigation: Dead reckoning เมื่อ GPS ไม่ทำงาน
- Camera Stabilization: ป้องกันการสั่น (OIS/EIS)
- Gesture Recognition: การหมุนข้อมือ, Shake detection
Gyroscope vs Accelerometer
| คุณสมบัติ | Gyroscope | Accelerometer |
|---|---|---|
| วัด | การหมุน (Rotation) | การเร่ง (Linear acceleration + Gravity) |
| Drift | มี Drift สะสมตามเวลา | ไม่มี Drift (แต่มี Noise) |
| ความแม่นยำระยะสั้น | สูงมาก | ปานกลาง |
| ความแม่นยำระยะยาว | ต่ำ (ต้อง Fuse กับ Accel/Mag) | ดีกว่า |
Sensor Fusion (แนะนำ)
ใช้ Sensor.TYPE_GAME_ROTATION_VECTOR หรือ Sensor.TYPE_ROTATION_VECTOR ซึ่งรวม Gyro + Accel (+ Mag) ให้ค่า Orientation ที่แม่นยำและไม่ Drift
ข้อควรระวัง
- อุปกรณ์ราคาถูก/Entry-level อาจไม่มี Gyroscope
- Gyroscope ใช้พลังงานสูงกว่า Accelerometer
- ค่า Raw ต้องผ่าน Filter (Complementary/Kalman) ก่อนใช้งานจริง
- ใน Manifest:
<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="false" />