Hardware Interfacing Workshops

Part II: Sensor Hardware Integration

เอกสารเตรียมความพร้อมสำหรับนิสิต

หัวข้อ: การเชื่อมต่อ LVGL กับ Sensor Hardware จริง (ADC, IMU) LVGL Version: v9.2.0

ความต่อเนื่อง: ต่อจาก Part I (Simulated Data) → Real Sensor Data


สารบัญ

  1. ภาพรวม Part II

  2. aic-eec Sensor API

  3. ตัวอย่างที่ 7: Real IMU Visualization

  4. ตัวอย่างที่ 8: Real Sensor Dashboard

  5. ตัวอย่างที่ 9: Real Arc Gauge with Tilt Analysis

  6. ตัวอย่างที่ 10: Real Scale Gauge with Tilt Analysis 6B. ตัวอย่างที่ 11: Real Chart Dashboard (4 Chart Types)

  7. Complementary Filter - การวิเคราะห์มุมเอียง

  8. แบบฝึกหัด


1. ภาพรวม Part II

Part I vs Part II

รายการ
Part I (Ex1-6)
Part II (Ex7-9)

UI Widgets

Chart, Arc, Scale, Bar

เหมือนกัน

ข้อมูล ADC

simulate_adc_read()

aic_adc_read()

ข้อมูล IMU

simulate_imu_accel()

aic_imu_read_accel()

ไฟล์โค้ด

week4_examples.c

week4_hw_examples.c

Hardware

ไม่ต้องมี

ต้องมี Potentiometer, BMI270

โครงสร้างไฟล์

Sensor Hardware บนบอร์ด

Sensor
Chip
Interface
ข้อมูล

ADC

SAR ADC

Analog

Potentiometer (0-3.3V)

IMU

BMI270

I2C

Accelerometer, Gyroscope

Temperature

Internal

ADC

Chip temperature


2. aic-eec Sensor API

2.1 ADC API

2.2 IMU API (BMI270)

API Summary

Function
หน้าที่
Return

aic_sensors_init()

เริ่มต้น Sensors

void

aic_adc_read(ch)

อ่าน ADC raw

0-4095

aic_adc_read_voltage(ch)

อ่าน Voltage

0.0-3.3V

aic_adc_read_percent(ch)

อ่าน Percent

0-100%

aic_imu_read_accel(ax,ay,az)

อ่าน Accelerometer

true/false

aic_imu_read_gyro(gx,gy,gz)

อ่าน Gyroscope

true/false

aic_imu_get_orientation()

คำนวณ Orientation

enum

2.3 Tilt Analysis API (NEW)

Function
หน้าที่
Return

aic_tilt_init(config)

เริ่มต้น Complementary Filter

true/false

aic_tilt_update_from_imu()

อ่าน IMU และอัพเดท filter

true/false

aic_tilt_get_roll()

มุม Roll (เอียงซ้าย-ขวา)

-180..+180°

aic_tilt_get_pitch()

มุม Pitch (เอียงหน้า-หลัง)

-90..+90°

aic_tilt_get_roll_percent()

Roll เป็นเปอร์เซ็นต์

0-100%

aic_tilt_get_pitch_percent()

Pitch เป็นเปอร์เซ็นต์

0-100%

WHY: ทำไมใช้ aic-eec API?

  1. Abstraction - ซ่อนความซับซ้อนของ HAL/Driver

  2. Consistency - API รูปแบบเดียวกันทั้งโปรเจค

  3. Testability - สามารถ mock ได้ง่าย

  4. Reusability - ใช้ซ้ำได้หลาย examples


3. ตัวอย่างที่ 7: Real ADC Visualization

เป้าหมาย

แสดงค่า ADC จาก Potentiometer จริงด้วย Slider และ Bar

Comparison: Part I vs Part II

โค้ดหลัก

Layout


4. ตัวอย่างที่ 8: Real IMU Visualization

เป้าหมาย

แสดงข้อมูล Accelerometer 3 แกน จาก BMI270 แบบ Real-time

Comparison: Part I vs Part II

โค้ดหลัก

Data Mapping

WHY: ทำไมต้อง Map ค่า?

  • LVGL Chart รับค่าเป็น integer (0-100)

  • IMU Accelerometer ส่งค่าเป็น float (-2.0 ถึง +2.0 g)

  • ต้อง map เพื่อแสดงผลได้ถูกต้อง


5. ตัวอย่างที่ 9: Real Sensor Dashboard

เป้าหมาย

รวม ADC + IMU + Orientation + Temperature ในหน้าเดียว

Layout

โค้ดโครงสร้าง


5B. ตัวอย่างที่ 9: Real Arc Gauge (Roll Angle)

เป้าหมาย

แสดงมุม Roll (เอียงซ้าย-ขวา) ด้วย Arc Gauge โดยใช้ Complementary Filter

Algorithm

Code Pattern

Status: ✅ Approved


6. ตัวอย่างที่ 10: Real Scale Gauge (Pitch Angle)

เป้าหมาย

แสดงมุม Pitch (เอียงหน้า-หลัง) ด้วย Scale Gauge with Needle

Pitch vs Roll

Angle
Axis
Motion
Example

Roll

X-axis

เอียงซ้าย-ขวา

เครื่องบินเอียงปีก

Pitch

Y-axis

เอียงหน้า-หลัง

เครื่องบินก้ม-เงย

Percentage Mapping

Code Pattern

Layout

Status: ✅ Approved


6B. ตัวอย่างที่ 11: Real Chart Dashboard (4 Chart Types)

เป้าหมาย

แสดง Real Sensor Data ใน 4 รูปแบบ Chart ผ่าน TabView

4 Tabs Overview

Tab
Chart Type
Data Source
Technique

Bar

Bar Chart

Accel X/Y/Z

3 Colored Series (R/G/B)

Area

Faded Area

Tilt Magnitude

lv_example_chart_5 pattern

Scatter

Scatter Plot

Roll vs Pitch

Tilt Ball effect

Line

Line Chart

Gyroscope

Ex7 stable pattern

Tab 1: Bar Chart (Accel X/Y/Z)

Tab 2: Faded Area Chart (Tilt Magnitude)

เทคนิค lv_example_chart_5:

Data: Tilt Magnitude

Tab 3: Scatter Chart (Roll vs Pitch)

แนวคิด: Tilt Ball

  • เอียงบอร์ดเหมือนกระดานกลิ้งลูกบอล

  • Roll = ซ้าย-ขวา, Pitch = หน้า-หลัง

Tab 4: Line Chart (Gyroscope)

ใช้ Ex7 Pattern ที่ stable:

Optimization: Update Active Tab Only

เหตุผล: ลด CPU load เพราะไม่ต้องอัพเดท 4 charts ทุก 100ms

Status: ✅ Approved


7. Complementary Filter - การวิเคราะห์มุมเอียง

ทำไมต้องใช้ Complementary Filter?

Sensor
ข้อดี
ข้อเสีย

Accelerometer

วัดทิศทาง gravity ได้ตรง

มี noise และถูก vibration รบกวน

Gyroscope

ตอบสนองเร็ว, ไม่มี noise

Drift ตลอดเวลา (integration ของ bias)

Complementary

รวมข้อดีทั้งสอง

ต้อง tune alpha parameter

Algorithm

การคำนวณ Accel Angle

ข้อควรระวัง

  1. Gimbal Lock: เมื่อ Pitch ใกล้ ±90° จะมีปัญหาในการคำนวณ Roll

  2. Initial Condition: ต้องให้ accel angle เป็นค่าเริ่มต้น

  3. Sampling Rate: dt ต้องตรงกับ timer interval


8. แบบฝึกหัด

แบบฝึกหัดที่ 1: ADC Threshold Alert

  • ตั้ง threshold ด้วย Slider (0-100%)

  • เมื่อ ADC เกิน threshold → LED ติด + เปลี่ยนสี Bar

  • แสดงสถานะ "NORMAL" / "ALERT"

แบบฝึกหัดที่ 2: IMU Tilt Indicator

  • แสดง orientation ด้วยรูปภาพ/icon

  • เปลี่ยนสีพื้นหลังตาม orientation

  • เพิ่ม Gyroscope chart (angular velocity)

แบบฝึกหัดที่ 3: Multi-Channel ADC

  • แสดง ADC หลาย channels (ถ้ามี)

  • เปรียบเทียบค่าด้วย grouped bar chart

  • Export data ผ่าน UART

แบบฝึกหัดที่ 4: Data Logger

  • บันทึกข้อมูล ADC/IMU ทุก 1 วินาที

  • แสดงกราฟย้อนหลัง 60 วินาที

  • เพิ่มปุ่ม Start/Stop/Clear


5A. CM33-CM55 IPC Architecture (Ex7-11)

สถาปัตยกรรม Dual-Core

PSoC Edge E84 ใช้ dual-core architecture:

  • CM33 (Non-Secure): อ่าน BMI270 ผ่าน I2C, เขียนลง Shared Memory

  • CM55 (FreeRTOS + LVGL): อ่าน Shared Memory, แสดงผลบน Display

Shared Memory Structure (imu_shared.h)

Synchronization Pattern

Last updated

Was this helpful?