IMU Sensor Hub

Lab 1: ทำความเข้าใจโค้ด Sensor Hub

วัตถุประสงค์

  • อ่านและเข้าใจโครงสร้างโค้ด sensor_hub_daq_task.c

  • เข้าใจฟังก์ชันแปลงค่า lsb_to_mps2() และ lsb_to_rps()

  • เข้าใจฟังก์ชัน motion_sensor_update_orientation()

ขั้นตอน

1.1 เปิดโปรเจกต์ sensor-hub-imu

# ใน ModusToolbox
1. File  New  ModusToolbox Application
2. เลือก KIT_PSE84_EVAL_EPC2
3. ค้นหา "sensor-hub-imu"
4. สร้างโปรเจกต์

1.2 ศึกษาไฟล์ sensor_hub_daq_task.c

เปิดไฟล์ proj_cm33_ns/sensor_hub_daq_task.c และศึกษาโครงสร้าง:

/***********************************************************
* ส่วนที่ 1: Macros (ค่าคงที่สำคัญ)
***********************************************************/
#define GRAVITY_EARTH       (9.80665f)   /* ค่า g มาตรฐาน */
#define DEG_TO_RAD          (0.01745f)   /* แปลง degree → radian */
#define GYR_RANGE_DPS       (2000.0f)    /* ±2000 degrees/sec */
#define ACC_RANGE_2G        (2.0f)       /* ±2g */

/***********************************************************
* ส่วนที่ 2: ฟังก์ชันแปลงค่า Accelerometer
***********************************************************/
static float lsb_to_mps2(int16_t val, int8_t g_range, uint8_t bit_width)
{
    float half_scale = (float)(1u << (bit_width - 1u));
    return ((GRAVITY_EARTH) * val * g_range) / half_scale;
}

/*
คำอธิบาย:
- val: ค่า raw จาก sensor (-32768 to +32767)
- g_range: ช่วงวัด (2 = ±2g)
- bit_width: 16 bits

ตัวอย่าง:
- val = 16384, g_range = 2, bit_width = 16
- half_scale = 2^15 = 32768
- result = (9.80665 × 16384 × 2) / 32768 = 9.80665 m/s² ≈ 1g
*/

/***********************************************************
* ส่วนที่ 3: ฟังก์ชันแปลงค่า Gyroscope
***********************************************************/
static float lsb_to_rps(int16_t val, float dps, uint8_t bit_width)
{
    float half_scale = (float)(1u << (bit_width - 1u));
    return ((DEG_TO_RAD) * ((dps) / (half_scale)) * (val));
}

/*
คำอธิบาย:
- dps: degrees per second range (2000)
- DEG_TO_RAD: แปลงเป็น radians

ตัวอย่าง:
- val = 1000, dps = 2000, bit_width = 16
- result = 0.01745 × (2000/32768) × 1000 ≈ 1.065 rad/s
*/

/***********************************************************
* ส่วนที่ 4: ฟังก์ชันตรวจจับทิศทาง
***********************************************************/
static cy_rslt_t motion_sensor_update_orientation(void)
{
    int16_t abs_x, abs_y, abs_z;

    /* อ่านค่า sensor */
    result = mtb_bmi270_read(&bmi270, &bmi270_data);

    /* คำนวณค่า absolute */
    abs_x = abs(bmi270_data.sensor_data.acc.x);
    abs_y = abs(bmi270_data.sensor_data.acc.y);
    abs_z = abs(bmi270_data.sensor_data.acc.z);

    /* เปรียบเทียบหาแกนที่ตั้งฉากกับพื้น */
    if ((abs_z > abs_x) && (abs_z > abs_y))
    {
        if (bmi270_data.sensor_data.acc.z < 0)
            printf("Orientation = ORIENTATION_DOWN\r\n");
        else
            printf("Orientation = ORIENTATION_UP\r\n");
    }
    /* ... เงื่อนไขอื่นๆ ... */
}

1.3 ศึกษา Main Task Loop

1.4 Build และ Flash

1.5 ทดลองหมุนบอร์ด

ลองหมุนบอร์ดไปในทิศทางต่างๆ และสังเกต:

  • ค่า accelerometer เปลี่ยนอย่างไร

  • Orientation เปลี่ยนอย่างไร


จบ Session 2 | ต่อไป: Session 3: LVGL Display

Last updated

Was this helpful?