无垠的广袤 发表于 2025-11-20 12:26

【英飞凌 CY8CKIT-062S2-AI评测】IMU 动作识别

<h1>【英飞凌 CY8CKIT-062S2-AI评测】IMU 动作识别</h1>
<p>本文介绍了英飞凌 CY8CKIT-062S-AI 开发板结合板载 IMU 传感器收集加速度计数据,并通过机器学习模型预测和推理特定运动,实现动作识别的项目设计。</p>
<h2>项目介绍</h2>
<p>该项目使用板载 IMU 传感器收集加速度计数据,发送至 ML 模型以检测特定运动,如振动或圆圈等行为;</p>
<ul>
<li>环境搭建:安装相关软件和机器学习工具,用以生成对应的模型代码;</li>
<li>工程创建:使用 ModusToolbox 软件快速加载和编译固件及调试;</li>
<li>工程代码:给出项目方案落地实现的关键代码,包括流程图等;</li>
<li>效果演示:通过串口显示动作行为的概率,推理并给出预测结果。</li>
</ul>
<h2>环境搭建</h2>
<ul>
<li>
<p>在 CY8CKIT-062S2-AI 设备 官方网站 下载对应的开发工具和 IDE 软件,包括</p>
<ul>
<li>ModusToolbox ;</li>
<li>DEEPCRAFT™ Studio 或 Imagimob ;</li>
</ul>
</li>
<li>
<p>可通过 ModusToolbox Setup 软件安装相关软件和工具链;</p>
</li>
<li>
<p>使用 ModusToolbox Programmer 软件烧录固件。</p>
</li>
</ul>
<h2>工程测试</h2>
<p>加载 CY8CKIT-062S-AI 开发板 Demo 工程,演示了部署由 DEEPCRAFT™ Studio 生成的机器学习(ML)模型。<br />
<img src="data/attachment/forum/202511/20/122416x7zk0jwkzjtu25d7.jpg" alt="ML_motion_create.jpg" title="ML_motion_create.jpg" /></p>
<ul>
<li>代码示例从惯性测量单元(IMU)收集加速度计数据,然后发送给ML模型以检测特定运动,如震动或圆圈。</li>
<li>直接使用了 DEEPCRAFT™ Studio 内部生成的 model.c/h 文件。</li>
<li>基于运动检测项目的新模型可以直接加入项目。</li>
</ul>
<h3>工程创建</h3>
<ul>
<li>进入 Eclipse for ModusToolbox 软件;</li>
<li>在 Quick Package 标签界面下选择 Start - New Application;</li>
<li>待加载出设备目录后(需要科学上网),在检索框中输入 <code>CY8CKIT-062S2-AI</code> 获取对应设备;</li>
<li>勾选 Machine Learning 目录下的 <code>DEEPCRAFT Deploy Model Motion</code> 工程,点击 Create 按钮;</li>
</ul>
<p><img src="data/attachment/forum/202511/20/122503euyauyu9ady0ata6.jpg" alt="deepcraft_deploy_motion.jpg" title="deepcraft_deploy_motion.jpg" /></p>
<ul>
<li>待完成 Demo 创建,右键项目,构建工程,确认无报错;</li>
</ul>
<p>详见:Infineon/mtb-example-ml-deepcraft-deploy-motion .</p>
<h3>流程图</h3>
<p><img src="data/attachment/forum/202511/20/122522q91z6f0fs410mfh9.png" alt="flowchart_motion.png" title="flowchart_motion.png" /></p>
<h3>工程代码</h3>
<p>打开工程目录中的 <code>main.c</code> 文件,代码如下</p>
<pre><code class="language-c++">#include &lt;float.h&gt;
#include &lt;stdbool.h&gt;
#include &quot;cyhal.h&quot;
#include &quot;cybsp.h&quot;
#include &quot;bmi270.h&quot;
#include &quot;cy_retarget_io.h&quot;

/* Model to use */
#include &lt;models/model.h&gt;

/*******************************************************************************
* Macros
*******************************************************************************/
#define BMI270_ADDRESS (BMI2_I2C_PRIM_ADDR)

/* X, Y and Z axes */
#define AXIS_COUNT            (3)

/* Total number of sensors - Accelerometer &amp; Gyroscope */
#define SENSOR_COUNT          (2)

/* Earth's gravity in m/s^2 */
#define GRAVITY_EARTH         (9.80665f)

/* Accelerometer range in G. Must be one of 2, 4, 8, 16 */
#define IMU_ACCEL_RANGE_G   (8)

/* Gyro range in degrees per second. Must be one of 125, 250, 500, 1000, 2000 */
#define IMU_GYRO_RANGE_DPS    (500)

/* IMU Sample frequency (Hz), must be one of 25, 50, 100, 200, 400 */
#define IMU_FREQ            (50)

/* I2C Config */
#define _I2C_TIMEOUT_MS            (10U)
#define _READ_WRITE_LEN            (46U)
#define _SOFT_RESET_DELAY_US       (300)

/*******************************************************************************
* Function Prototypes
*******************************************************************************/
static void init_board(void);
static void halt_error(int code);
static void imu_init(struct bmi2_dev* imu);
static float imu_lsb_to_mps2(int16_t val, float g_range, uint8_t bit_width);
static float imu_lsb_to_dps(int16_t val, float dps, uint8_t bit_width);
static bool imu_read(struct bmi2_dev* imu, float* dest);
static BMI2_INTF_RETURN_TYPE _bmi2_i2c_read(
      uint8_t reg_addr,
      uint8_t* reg_data,
      uint32_t len,
      void* intf_ptr);
static BMI2_INTF_RETURN_TYPE _bmi2_i2c_write(
      uint8_t reg_addr,
      const uint8_t* reg_data,
      uint32_t len,
      void* intf_ptr);
static void _bmi2_delay_us(uint32_t us, void* intf_ptr);


/*************************************************
* Function Name: main
*************************************************/
int main(void)
{
    float data_buffer;
    float label_scores;
    char *label_text[] = IMAI_DATA_OUT_SYMBOLS;
    struct bmi2_dev imu = {0};
    cy_rslt_t result;
    int16_t best_label;
    float max_score;

    /* Basic board setup */
    init_board();

    /* ANSI ESC sequence for clear screen */
    printf(&quot;\x1b[2J\x1b[;H\x1b[?25l&quot;);

    /* Initialize model */
    result = IMAI_init();
    halt_error(result);

    /* Initialize IMU sampling */
    imu_init(&amp;imu);

    for (;;)
    {
      /* Move cursor home */
      printf(&quot;\033[H&quot;);
      printf(&quot;DEEPCRAFT IMU Model Example\r\n\n&quot;);

      /* Read sensor data */
      memset(data_buffer, 0,sizeof(data_buffer));
      if(!imu_read(&amp;imu, data_buffer))
      {
            continue;
      }
      /* Give sensor data to model */
      result = IMAI_enqueue(data_buffer);
      halt_error(result);

      /* Check if there is any model output */
      best_label = 0;
      max_score = -1000.0f;
      switch(IMAI_dequeue(label_scores))
      {
            case IMAI_RET_SUCCESS:      /* We have data, display it */
                for(int i = 0; i &lt; IMAI_DATA_OUT_COUNT; i++)
                {
                  printf(&quot;label: %-10s: score: %f\r\n&quot;, label_text, label_scores);
                  if (label_scores &gt; max_score)
                  {
                        max_score = label_scores;
                        best_label = i;
                  }
                }
                printf(&quot;\r\n&quot;);
                printf(&quot;Output: %-30s\r\n&quot;, label_text);
                break;
            case IMAI_RET_NODATA:   /* No new output, continue with sampling */
                break;
            case IMAI_RET_ERROR:    /* Abort on error */
                halt_error(IMAI_RET_ERROR);
                break;
      }
    }
}
</code></pre>
<p>保存代码。</p>
<h3>固件上传</h3>
<ul>
<li>连接开发板和电脑,点击菜单栏的运行按钮,完成固件上传;</li>
<li>或使用 <code>ModusToolbox Programmer</code> 工具烧录固件</li>
</ul>
<p><img src="data/attachment/forum/202511/20/122541qv89cm6rne8l69mp.jpg" alt="MT_programmer_run.jpg" title="MT_programmer_run.jpg" /></p>
<ul>
<li>加载固件,配置烧录器、开发板型号;</li>
<li>点击 Program 即可。</li>
</ul>
<h3>效果</h3>
<ul>
<li>运行 <code>Tera Term</code> 软件,连接设备串口,配置波特率为 <code>115200</code>;</li>
<li>短按板载 RESET 键,终端显示 IMU 例程,并进行动作推理;</li>
</ul>
<p><img src="data/attachment/forum/202511/20/122554trhggxlgelhsaoo0.gif" alt="demo_motion.gif" title="demo_motion.gif" /></p>
<ul>
<li>使开发板做摆动、绕圈等运动,开发板可根据 IMU 数据识别出相应的行为动作;</li>
</ul>
<p><img src="data/attachment/forum/202511/20/122606hlt682104yrt9jy9.gif" alt="motion_move.gif" title="motion_move.gif" /></p>
<h2>总结</h2>
<p>本文介绍了英飞凌 CY8CKIT-062S-AI 开发板结合板载 IMU 传感器收集加速度计数据,并通过机器学习模型预测和推理特定运动,实现动作识别的项目设计,为相关产品在边缘 AI 领域的快速开发和设计应用提供了参考。</p>
页: [1]
查看完整版本: 【英飞凌 CY8CKIT-062S2-AI评测】IMU 动作识别