Arc Gauge
Lab 9: Real Arc Gauge (Roll Angle)
Part 2 - Sensor Visualization
1. โครงสร้างภาพรวมของ Lab
Why? - ทำไมต้องเรียนรู้เรื่องนี้
Sensor Fusion: เข้าใจวิธีการรวมข้อมูลจาก sensor 2 ตัว (Accelerometer + Gyroscope) เข้าด้วยกันเพื่อให้ได้ผลลัพธ์ที่ดีกว่าการใช้ sensor ตัวเดียว
Complementary Filter: เรียนรู้ algorithm พื้นฐานที่ใช้จริงในอุตสาหกรรม (drone, robot, motion tracker)
Tilt API: ใช้ module
aic_tiltที่ abstract ความซับซ้อนของ filter ให้เรียกใช้ง่ายArc Gauge + Real Data: นำ Arc Gauge จาก Lab 2 มาแสดงข้อมูล Roll Angle จริงจาก IMU
What? - จะได้เรียนรู้อะไร
Complementary Filter: หลักการรวม Accel (ช้าแต่ stable) กับ Gyro (เร็วแต่ drift)
Tilt API: ใช้
aic_tilt_init(),aic_tilt_update_from_imu(),aic_tilt_get_roll(),aic_tilt_get_roll_percent()Arc Gauge Integration: แสดงมุมเอียง (Roll Angle) บน Arc widget แบบ real-time
Percentage Mapping: แปลงมุม (-90 to +90) เป็นเปอร์เซ็นต์ (0-100%) สำหรับ widget
Hardware Init Pattern:
aic_sensors_init()+aic_tilt_init(NULL)ก่อนสร้าง UI
How? - จะทำอย่างไร
เรียก
aic_sensors_init()เพื่อ initialize BMI270 IMUเรียก
aic_tilt_init(NULL)เพื่อ initialize Complementary Filter (ค่า default: alpha=0.98, dt=0.1s)สร้าง Arc Gauge UI เหมือน Lab 2 (part2_ex2_arc_gauge)
ใน timer callback (100ms): เรียก
aic_tilt_update_from_imu()แล้วอ่านaic_tilt_get_roll()+aic_tilt_get_roll_percent()อัพเดท Arc value + labels ด้วยค่ามุมจริง

2. หลักการทำงานและ Flowchart
2.1 Why Complementary Filter?
2.2 Complementary Filter Formula
2.3 Roll Angle Measurement
3. ฟังก์ชันสำคัญ
3.1 Tilt API Functions
aic_tilt_init(config)
bool
Initialize Complementary Filter (NULL = defaults)
aic_tilt_update_from_imu()
bool
อ่าน Accel+Gyro แล้วอัพเดท filter (เรียกทุก 100ms)
aic_tilt_get_roll()
float
Roll angle (degrees, -180 to +180)
aic_tilt_get_pitch()
float
Pitch angle (degrees, -90 to +90)
aic_tilt_get_roll_percent()
uint8_t
Roll mapped to 0-100%
aic_tilt_get_pitch_percent()
uint8_t
Pitch mapped to 0-100%
aic_tilt_reset()
void
Reset filter state to zero
aic_tilt_set_alpha(alpha)
void
ตั้งค่า filter coefficient (0.0-1.0)
aic_tilt_set_dt(dt)
void
ตั้งค่า sample period (seconds)
3.2 Tilt Init API Signature
3.3 Tilt Update API Signature
3.4 Key Differences: Lab 2 (Sim) vs Lab 9 (HW)
Init
ไม่ต้อง
aic_sensors_init() + aic_tilt_init(NULL)
Data Source
Counter (0-100 loop)
Complementary Filter (Accel+Gyro)
Update
value = (value + 1) % 101
aic_tilt_update_from_imu()
Value
Simulated percentage
aic_tilt_get_roll_percent()
Error Check
ไม่มี
ตรวจ return false จาก update
Data Range
0-100 fixed
-90 to +90 (mapped to 0-100)
Behavior
Linear sweep
Real physical tilt angle
3.5 Include Files
4. โค้ดตัวอย่าง
4.1 Global Variables
4.2 Timer Callback - Tilt Analysis (Complementary Filter)
ภายใน aic_tilt_update_from_imu() จะทำ 3 ขั้นตอน: อ่าน Accel -> อ่าน Gyro -> คำนวณ Complementary Filter โดยอัตโนมัติ ถ้า sensor ตัวใดตัวหนึ่งไม่พร้อมจะ return false
4.3 Main Function
5. องค์ความรู้และเทคนิค
5.1 Complementary Filter - ทำไมต้องใช้?
ปัญหาของการใช้ sensor ตัวเดียว:
5.2 Tilt API Usage Pattern
5.3 Alpha Parameter - ผลกระทบต่อการตอบสนอง
5.4 Complementary Filter - สูตรคำนวณภายใน (tilt.c)
5.5 Calibration Tips - เมื่อค่าไม่ตรง
5.6 Comparison: Tilt API vs Manual Calculation
6. แบบฝึกหัด
Exercise 1: Dual Arc Gauge (Roll + Pitch)
โจทย์: สร้างหน้าจอที่แสดง Arc Gauge 2 วงสำหรับ Roll และ Pitch พร้อมกัน
Requirements:
Arc 2 วง: Roll (ซ้าย, สี Cyan) และ Pitch (ขวา, สี Orange)
ขนาด 150x150 pixels ต่อวง
แต่ละวงมี percentage label ตรงกลาง + angle label ใต้วง
ใช้
aic_tilt_get_roll_percent()และaic_tilt_get_pitch_percent()เปลี่ยนสี indicator เมื่อเอียงเกิน 45 องศา (สีแดง)
ใช้ timer 100ms เดียวอัพเดททั้ง 2 วง
Layout:
การประยุกต์ใช้งานจริง:
Drone Flight Controller: แสดงมุม Roll (เอียงซ้าย-ขวา) และ Pitch (เอียงหน้า-หลัง) ของ quadcopter เพื่อให้ pilot เห็น attitude ของ drone แบบ real-time
เครน/รถยก (Crane/Forklift): ระบบ Anti-Tipping ที่แจ้งเตือนเมื่อรถเอียงเกินมุมปลอดภัย ป้องกันการพลิกคว่ำขณะยกของหนัก
Marine Vessel Monitoring: แสดง Roll/Pitch ของเรือเพื่อตรวจสอบเสถียรภาพ
Hint:
วาง Arc ด้วย
LV_ALIGN_CENTERoffset X = -90 (Roll) และ +90 (Pitch)เปลี่ยนสี:
lv_obj_set_style_arc_color(arc, color, LV_PART_INDICATOR)ตรวจมุมเกิน:
if(fabsf(roll) > 45.0f) { /* change to red */ }
Exercise 2: Tilt Alarm with History Chart
โจทย์: สร้างระบบตรวจจับมุมเอียงเกินกำหนดพร้อม Line Chart แสดงประวัติ:
Requirements:
Arc Gauge แสดง Roll angle ปัจจุบัน (เหมือน Lab 9)
Line Chart (350x120) ใต้ Arc แสดง history 50 จุดของ Roll angle
ระบบเตือน 3 ระดับ:
Safe: |roll| < 15 deg (สีเขียว)
Warning: |roll| 15-30 deg (สีเหลือง)
Danger: |roll| > 30 deg (สีแดง + label กระพริบ)
แสดง alarm count, max tilt angle ที่เคยวัดได้
ปุ่ม "RESET" สำหรับ clear alarm count + tilt history
Layout:
การประยุกต์ใช้งานจริง:
Structural Health Monitoring: ติดตั้งบนเสา/อาคาร ตรวจจับความเอียงและแจ้งเตือนเมื่อเกินเกณฑ์ปลอดภัย เก็บข้อมูลย้อนหลังเพื่อวิเคราะห์แนวโน้ม
Industrial Equipment Leveling: ตรวจสอบว่าเครื่องจักร CNC หรือเครื่องพิมพ์ 3D อยู่ระดับหรือไม่ แจ้งเตือนเมื่อพื้นทรุดตัว
Vehicle Roll-over Prevention: ระบบ ESP/ESC ในรถยนต์ที่ตรวจจับมุมเอียงเพื่อป้องกันรถพลิกคว่ำ
Hint:
Chart range:
-90to+90(raw degrees, not percentage)lv_chart_set_next_value(chart, ser, (int32_t)roll)กระพริบ label: toggle visibility ใน timer
lv_obj_add_flag()/lv_obj_remove_flag(LV_OBJ_FLAG_HIDDEN)Peak tracking:
if(fabsf(roll) > max_tilt) max_tilt = fabsf(roll);
7. References
Last updated