> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/interfacing-with-infineon-psoc-tm-edge/sensor-interfacing/workshops.md).

# Workshops

## 1. ภาพรวม IMU Sensor

### 1.1 BMI270 - 6-Axis IMU

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FttLgwwRkMRCJ7ahdbpzg%2FEmbedded%20Systems%20and%20IoT-Page-9.drawio-3.png?alt=media&amp;token=05568641-172e-4f31-b4af-9c6452cc3971" alt=""><figcaption></figcaption></figure>

### 1.2 หลักการทำงาน Accelerometer

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FRc96yWyhtS8XzF2rlb9S%2FScreenshot%202569-01-18%20at%2012.06.24.png?alt=media&amp;token=baa83501-b066-4f44-85e0-8fecbbf4f67d" alt=""><figcaption></figcaption></figure>

เมื่อวางราบ (face up):

* acc\_x ≈ 0 m/s²
* acc\_y ≈ 0 m/s²
* acc\_z ≈ +9.81 m/s² (1g pointing up)

เมื่อเอียง:

* acc ถูกแบ่งไปตามแกนต่างๆ
* Magnitude (ขนาด) ยังคง ≈ 9.81 m/s²

### 1.3 I2C Communication

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F7kEQQYDyeb8DbTUhoJq1%2FEmbedded%20Systems%20and%20IoT-Page-9.drawio-5.png?alt=media&amp;token=7f80e272-c6a5-4ee0-85f5-87c42e78b83e" alt=""><figcaption></figcaption></figure>

### 1.4 การแปลงค่า Raw → Physical Units

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FkgUOPvf1ZQc1bxL4rznp%2FScreenshot%202569-01-18%20at%2018.36.00.png?alt=media&amp;token=03abd82b-174f-4166-85c5-06b780011527" alt="" width="375"><figcaption></figcaption></figure>

```c
/* BMI270 Raw Data: int16_t (-32768 to +32767) */

/* สูตรแปลง Accelerometer (Range ±2g) */
               raw_value × g_range × 9.80665
acc (m/s²) = ─────────────────────────────────
                     2^(bit_width - 1)

/* สูตรแปลง Gyroscope (Range ±2000 dps) */
              raw_value × dps_range × 0.01745
gyro (rad/s) = ────────────────────────────────
                     2^(bit_width - 1)
```

```c
/* BMI270 Raw Data: int16_t (-32768 to +32767) */

/* สูตรแปลง Accelerometer (Range ±2g) */
acc (m/s²) = (raw_value × g_range × 9.80665) / 2^(bit_width - 1)

/* สูตรแปลง Gyroscope (Range ±2000 dps) */
gyro (rad/s) = (raw_value × dps_range × 0.01745) / 2^(bit_width - 1)

```

***

## 2. C Programming Review

### 2.1 Arrays สำหรับ Sensor Buffer

```c
/* Array declaration */
#define BUFFER_SIZE  100
float sensor_buffer[BUFFER_SIZE];    /* 100 samples */
int16_t imu_data[6];                 /* ax,ay,az,gx,gy,gz */

/* Array size macro - ใช้บ่อยมาก! */
#define ARRAY_SIZE(arr)  (sizeof(arr) / sizeof((arr)[0]))

/* ตัวอย่าง: circular buffer index */
uint32_t index = 0;
sensor_buffer[index] = new_value;
index = (index + 1) % BUFFER_SIZE;  /* Wrap around */
```

### 2.2 Structs สำหรับ Sensor Data

```c
/* Vector 3D - ใช้บ่อยกับ sensor data */
typedef struct {
    float x;
    float y;
    float z;
} vector3f_t;

/* IMU Complete Data */
typedef struct {
    vector3f_t acceleration;  /* m/s² */
    vector3f_t gyroscope;     /* rad/s */
    uint32_t timestamp_ms;
} imu_data_t;

/* Usage */
imu_data_t sensor;
sensor.acceleration.x = 0.0f;
sensor.acceleration.z = 9.81f;
```

### 2.3 Pointers และ Pass-by-Reference

```c
/* Pass by pointer - efficient for structs */
void process_data(const imu_data_t *data)  /* read-only */
{
    printf("X: %.2f\n", data->acceleration.x);
}

void update_data(imu_data_t *data)  /* can modify */
{
    data->timestamp_ms = get_time_ms();
}

/* Usage */
imu_data_t my_data;
process_data(&my_data);  /* Pass address */
update_data(&my_data);
```
