Scale Gauge
Lab 10: Real Scale Gauge (Pitch Angle)
Part 2 - Sensor Visualization
1. โครงสร้างภาพรวมของ Lab
Why? - ทำไมต้องเรียนรู้เรื่องนี้
Real Gauge + Real Sensor: นำ Scale widget จาก Lab 4 (simulation) มาใช้กับข้อมูล Pitch Angle จริงจาก IMU
Pitch Angle: วัดการเอียงหน้า/หลังของบอร์ด ซึ่งใช้งานได้จริงในหลายแอปพลิเคชัน เช่น ระบบเตือนความลาดชัน
Complementary Filter: เรียนรู้อัลกอริทึมที่รวม Accelerometer + Gyroscope เพื่อผลลัพธ์ที่แม่นยำ
Scale Widget Mastery: เข้าใจการใช้ lv_scale แบบ ROUND_OUTER กับ needle line, color sections, custom labels
Roll vs Pitch: เข้าใจความแตกต่างระหว่าง Roll (Lab 9) กับ Pitch (Lab 10)
What? - จะได้เรียนรู้อะไร
Tilt API (Pitch): ใช้
aic_tilt_get_pitch()และaic_tilt_get_pitch_percent()อ่านมุมเอียงหน้า/หลังScale Widget: สร้าง round gauge ด้วย ticks, labels, sections, needle
Color Sections: กำหนดโซนสี (75-100 = แดง) สำหรับ warning zone
Needle Positioning: ใช้
lv_scale_set_line_needle_value()ขยับเข็มตามค่า sensorCustom Labels: แสดง "0", "25", "50", "75", "100" แทน default labels
Style Layers: จัดการ 3 layers ของ scale: MAIN (arc), INDICATOR (major ticks), ITEMS (minor ticks)
How? - จะทำอย่างไร
Initialize hardware ด้วย
aic_sensors_init()+aic_tilt_init(NULL)สร้าง Scale widget ในโหมด
LV_SCALE_MODE_ROUND_OUTERขนาด 200x200กำหนด 21 ticks, major ทุก 5 ticks, range 0-100
เพิ่ม color section สำหรับ high zone (75-100 = สีแดง)
สร้าง needle line ด้วย
lv_line_create()บน scaleTimer callback (100ms) เรียก
aic_tilt_update_from_imu()แล้วอัพเดท needle + labels

2. หลักการทำงานและ Flowchart
2.1 Pitch Angle Measurement
2.2 Scale Widget Structure
2.3 UI Layout
3. ฟังก์ชันสำคัญ
3.1 Tilt API Functions
aic_sensors_init()
void
Initialize sensor subsystem (เรียกครั้งเดียว)
aic_tilt_init(NULL)
bool
Initialize Complementary Filter (defaults: alpha=0.98, dt=0.1)
aic_tilt_update_from_imu()
bool
อัพเดท filter จาก BMI270 (true = สำเร็จ)
aic_tilt_get_pitch()
float
Pitch angle in degrees (-90 to +90)
aic_tilt_get_pitch_percent()
uint8_t
Pitch as percentage (0-100%)
aic_tilt_get_roll()
float
Roll angle in degrees (-180 to +180)
aic_tilt_get_roll_percent()
uint8_t
Roll as percentage (0-100%)
aic_tilt_reset()
void
Reset filter state to zero
aic_tilt_set_alpha(float)
void
เปลี่ยน filter coefficient (0.0-1.0)
3.2 Scale Widget APIs
lv_scale_create(parent)
สร้าง Scale widget
lv_scale_set_mode(scale, LV_SCALE_MODE_ROUND_OUTER)
ตั้ง mode เป็น round gauge
lv_scale_set_total_tick_count(scale, 21)
จำนวน tick ทั้งหมด
lv_scale_set_major_tick_every(scale, 5)
Major tick ทุก N ticks
lv_scale_set_range(scale, 0, 100)
กำหนดช่วงค่า min-max
lv_scale_set_label_show(scale, true)
แสดง labels
lv_scale_set_text_src(scale, labels)
ใช้ custom labels
lv_scale_set_line_needle_value(scale, line, len, val)
ตั้งตำแหน่งเข็ม
lv_scale_add_section(scale)
เพิ่ม color section
lv_scale_section_set_range(section, min, max)
กำหนดช่วงของ section
lv_scale_section_set_style(section, part, style)
ตั้ง style ของ section
3.3 Pitch Angle to Percentage Mapping
3.4 Include Files
4. โค้ดตัวอย่าง
4.1 Global Variables
4.2 Timer Callback - อ่านค่า Pitch จาก Complementary Filter
4.3 Main Function - สร้าง UI ทั้งหมด
5. องค์ความรู้และเทคนิค
5.1 Scale Style Layers - 3 ชั้นของ Style
Scale widget มี 3 ชั้นที่ต้องกำหนด style แยกกัน:
1
LV_PART_MAIN
เส้น arc หลัก
arc_color, arc_width
2
LV_PART_INDICATOR
Major ticks + Labels
line_color, text_color, width, line_width
3
LV_PART_ITEMS
Minor ticks
line_color, width, line_width
5.2 Section Coloring - เพิ่มโซนสี
5.3 Needle Line vs Image Needle
5.4 Pitch vs Roll - เมื่อไหร่ใช้อันไหน
ทิศทาง
เอียงหน้า/หลัง
เอียงซ้าย/ขวา
Range
-90 to +90 deg
-180 to +180 deg
สูตร
atan2(-ax, sqrt(ay^2+az^2))
atan2(ay, az)
Use Case
Slope, crane arm, nose up/down
Anti-tip, balance, bank L/R
Widget
Scale (lv_scale)
Arc (lv_arc)
5.5 Custom Labels vs Auto Labels
5.6 Complementary Filter Tuning
6. แบบฝึกหัด
Exercise 1: Multi-Zone Scale Gauge
โจทย์: เพิ่ม color sections ให้ Scale gauge ครอบคลุมทั้ง 3 โซน:
Requirements:
Low zone (0-25): สีเขียว - บอร์ดเอียงหลังมาก
Normal zone (25-75): สีน้ำเงิน (default) - บอร์ดอยู่ในช่วงปกติ
High zone (75-100): สีแดง - บอร์ดเอียงหน้ามาก
เปลี่ยนสี value label ตาม zone ปัจจุบัน (เขียว/ขาว/แดง)
เพิ่ม status label: "BACKWARD", "LEVEL", "FORWARD"
การประยุกต์ใช้งานจริง:
Road Grade Warning, Anti-Rollover, Digital Level
Hint:
สร้าง section เพิ่มสำหรับ low zone (0-25)
ใน timer callback: ตรวจ
pct < 25,pct > 75เพื่อเปลี่ยนสี labellv_obj_set_style_text_color(label, color, 0)เปลี่ยนสี label
Exercise 2: Dual Scale Dashboard (Roll + Pitch)
โจทย์: สร้าง dashboard แสดง 2 Scale gauges: Roll (ซ้าย) กับ Pitch (ขวา) พร้อมกัน
Requirements:
Scale 2 อัน ขนาด 160x160 วางซ้าย-ขวา
Scale ซ้าย: Roll angle (สี Cyan)
Scale ขวา: Pitch angle (สี Orange)
แต่ละอันมี needle line + value label
เพิ่ม label กลาง: "Tilt Dashboard"
Tilt magnitude label ด้านล่าง: "Tilt: XX.X deg"
การประยุกต์ใช้งานจริง:
Flight Instrument: Artificial Horizon
Crane Safety Monitor: ตรวจสอบความเอียง 2 แกน
Platform Leveling: ปรับระดับแท่นรองรับอุปกรณ์
Hint:
Scale ซ้าย:
lv_obj_align(scale, LV_ALIGN_CENTER, -110, -20)Scale ขวา:
lv_obj_align(scale, LV_ALIGN_CENTER, 110, -20)ขนาด 160x160 (เล็กกว่า 200x200 ของ Lab 10 เดิม)
Tilt magnitude:
sqrtf(roll*roll + pitch*pitch)ทั้งคู่ใช้
aic_tilt_update_from_imu()ครั้งเดียวใน timer
7. References
Last updated
Was this helpful?