[Android] Gyroscope

Sathittham (Phoo) Sangthong
SS Blog
Published in
2 min readFeb 8, 2014

Gyroscope คืออะไร ?

“ไจโรสโคป” ที่เราหลายๆคนอาจจะคุ้นเคยกัน หรือผ่านหูผ่านตามาบ้าง
ในวิชาฟิสิกส์ เรื่องการหมุนและโมเมนตั้มนั้นเองครับ
และเจ้าไจโรสโคป นั้นก็คือ ลูกข่าง หรือวงล้อ ดังวีดีโอข้างล่างนี้ครับ

[youtube]http://www.youtube.com/watch?v=cquvA_IpEsA[/youtube]
โดยสรุปเลยแล้ว มันคืออุปกรณ์ที่ใช้วัดความเร็วเชิงมุม (Angular Velocity)
แล้วไอ้ความเร็วเชิงมุมคืออะไร??? เอาไปทำอะไร ???

  • ความเร็วเชิงมุม คืออะไร ?

จากพื้นฐานการเคลื่อนที่แบบหมุนในฟิสิกส์ (เจ้าเก่า)
การเคลื่อนที่แบบหมุน คือ การเคลื่อนที่โดยการหมุนรอบตัวเอง รอบจุดใดจุดหนึ่ง หรือ แกนใดแกนหนึ่งในตัวมันเอง เช่น ลูกฟุตบอล ลูกข่าง พัดลม ล้อรถ เป็นต้น

สำหรับวัตถุที่หมุนรอบแกนหมุน ทุก ๆ จุดบนวัตถุจะมีความเร็วเชิงมุมเท่ากัน ความเร็วในแนวเส้นสัมผัสของทุก ๆ จุดบนวัตถุจะแปรผันตามความเร็วเชิงมุมและระยะทางจากจุดหมุนมายังตำแหน่งนั้น ๆ

[math] v = \omega r [/math] หรือ [math] \omega = \frac {v}{r} [/math]

ความเร็วเชิงมุม (Angular Velocity : [math]\omega[/math]) คือ การกระจัดเชิงมุม (Angular Displacement:[math]\theta[/math]) ที่เปลี่ยนไปในหนึ่งหน่วยเวลา มีทิศเดียวกับการกระจัดเชิงมุม เป็นปริมาณเวคเตอร์ มีหน่วยเป็น rad/s

[math] \omega= \frac {\Delta\theta}{\Delta t}[/math] [math]\frac{rad}{s}[/math]

หรือ

[math] \omega_{average }= \frac {\Delta\theta}{\Delta T} = \frac{\theta- \theta_0}{t}[/math] [math]\frac{rad}{s}[/math]

Note : ความเร็วเชิงมุมเป็นปริมาณเวกเตอร์ ที่มีทิศเดียวกับแกนหมุน หาทิศได้จากกฎมือขวา

ความเร่งเชิงมุม (Angular Acceleration : [math]\alpha[/math]) คือ ความเร็วเชิงมุมที่เปลี่ยนไปในหนึ่งหน่วยเวลา

กำหนดให้
[math] \alpha[/math] = ความเร่งเชิงมุม
[math] \Delta\omega[/math] = ความเร็วเชิงมุมที่เปลี่ยนไป
[math] \Delta t[/math] = เวลาที่เปลี่ยนไป

เราจะได้

[math] \alpha = \frac{\Delta \omega}{\Delta t}[/math] [math]\frac{rad}{s²}[/math]
หรือ
[math] \alpha = \frac{\omega — \omega_0}{t}[/math] [math]\frac{rad}{s²}[/math]

  • เอา Gyroscope ไปทำอะไร ?
  • ใช้ทำ image stabilization ในกล้องดิจิตอล
  • สามารถใช้ในระบบควบคุมเบรคในรถยนต์ เพื่อช่วยลดอุบัติเหตุ เวลารถมีการหักเลี้ยวอย่างกระทันหัน
  • สามารถใช้ในการเปิดใช้งาน airbags เมื่อรถมีการพลิกคว่ำได้
  • ใช้ในระบบนำทางภายในอาคาร หรือสถานที่ๆไม่สามารถใช้ GPS ได้ (ระบบนี้เรียกกว่า dead-reckoning)
  • ใช้สำหรับควบคุมหุ่นยนต์และ UAV (เช่น AR Droin)
  • ใช้ใน IMU(Inertial Measurement Units) ติดกับแขน ขา เพื่อทำการติดตามการเคลื่อนไหวของร่างกาย
  • ทำ air mouse (เมาส์คอมพิวเตอร์ที่ไม่ได้ใช้ระบบแทร็ก) อุปกรณ์ควบคุมต่างๆ เช่น เกม
  • Gyroscope ในโทรศัพท์

กลับมาสู่โทรศัพท์ Android ของเรากันครับ…แน่นอนว่าคงไม่ใช่เจ้าลูกข่างแน่ๆที่จะไปอยู่ในโทรศัพท์ของเรา
ด้วยเทคโลยีที่เรียกว่า MEMS ที่ทำให้กลไกทางแมคานิกส์ สามารถไปอยู่ในชิปอิเล็กทรอนิกส์ได้
ที่ทั้งมีประสิทธิภาพสูงและประหยัดพลังงาน

โดยเจ้าไจโรสโคปแบบ MEMS นี้ใช้หลักการ Coriolis Effect ในการวัดความเร่งเชิงมุม (Angular rate)
ซึ่งจะไม่ขออธิบายต่อให้ยืดยาวในหัวข้อนี้นะครับ โปรดดูหลักการคร่าวๆจากวีดีโอด้านล่างนะครับ

[youtube]http://www.youtube.com/watch?v=zwe6LEYF0j8[/youtube]

  • Gyroscope ใน Android
  • Android สนับสนุนการใช้งานตั้งแต่ API ก่อน 1.0
  • มีใน Nexus S เป็นรุ่นแรกของ Android (ประมาณปี 2010)
  • ตัวอย่างเช่น ชิพจาก InvenSense MPU-3000
  • ระบบ Sensor Coordinate
  • ใช้ระบบเดียวกับ Accelerometer
  • การหมุนจะเป็นค่าบวก เมื่อหมุนทิศทางทวนเข็มนาฬิกา
  • ถ้าโทรศัพท์ไม่ได้เคลื่อนที่ ค่าที่อ่านได้จะเป็น 0
  • Pitch คือ ค่าที่อ่านได้จาก x
  • Roll คือ ค่า y
  • Azimuth คือ ค่า z
  • ข้อดีข้อเสียของ Gyroscope
  • ข้อดี
  • ไจโรสโปคตอบสนองได้ดี (ในช่วงเวลาสั้นๆ)
  • ข้อเสีย
  • ไจโรสโคปไม่ได้มีบนมือถือทุกรุ่น
  • ค่าดิปที่ได้จากไจโรสโคป (raw rotational data) มีค่า Noise และ Drift สูง (เมือเวลาผ่านไป)

การใช้งาน Gyroscope บน Android

TYPE_GYROSCOPE
สามารถเรียกใช้ได้จาก

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
  • โค๊ดดัวอย่าง

activity_main.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/gyro_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gyroscope Raw Data (rad/s)" />
<TextView
android:id="@+id/gyro_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X:" />
<TextView
android:id="@+id/gyro_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y:" />
<TextView
android:id="@+id/gyro_z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z:" />
</LinearLayout>MainActivity.javapackage sathittham.sangthong.mygyroscope;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 mGyroscope;
private TextView axisX, axisY, axisZ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
axisX = (TextView) findViewById(R.id.gyro_x);
axisY = (TextView) findViewById(R.id.gyro_y);
axisZ = (TextView) findViewById(R.id.gyro_z);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
}
@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, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
อ้างอิงhttp://en.wikipedia.org/wiki/Gyroscope
http://th.wikipedia.org/wiki/ไจโรสโคป
http://www.rmutphysics.com/charud/howstuffwork/gyro/gyroscopethai.htm
http://www.pieter-jan.com/node/7
http://electroiq.com/blog/2010/11/introduction-to-mems-gyroscopes/
http://hyperphysics.phy-astr.gsu.edu/hbase/rotq.html
http://developer.android.com/guide/topics/sensors/sensors_motion.html
http://mylifewithandroid.blogspot.com/2011/10/my-presentation-about-motion.html

--

--

Sathittham (Phoo) Sangthong
SS Blog

Hi! It's me Phoo! I’m a Software Developer 👨‍💻 , a Startup Entrepreneur 📱 and a Runner 🏃 . Currently, I’m a Co-Founder and CTO of a Startup name “Urbanice”.