lugl4313820 发表于 2024-11-2 15:47

【STM32H7S78-DK】基于TouchGFX的可视测距仪

<p>【硬件】</p>

<p>1、STM32H7S78-DK</p>

<p>2、X-NUCLEO-53L4A3 TOF评估板</p>

<p>【软件环境】</p>

<p>1、STM32CubeMX6.12</p>

<p>2、X-CUBE-TOF</p>

<p>3、X-CUBE_TOUCHGFX</p>

<p>4、TouchGFX-Designer</p>

<p>5、MDK5.38</p>

<p>【实现步步骤】</p>

<p>1、使用TouchGFX-Designer生成基于STM32H7S78L-DK的TouchGFX工程,设计界面如下:</p>

<p>&nbsp;&nbsp;</p>

<p>2、使用STM32CubeMX打开工程,配置X-CBUE-TOF如下:</p>

<p> &nbsp;</p>

<p>3、生成MDK工程,由于TouchGFX已经打开了I2C1,并且53L4A3也是使用I2C1所以无需重新配置。</p>

<p>4、打开MDK工程,可以看到STM32CubeMX已经给生成了53L4A3的工程文件:</p>

<p> &nbsp;</p>

<p>【用户代码实现】</p>

<p>1、我们添加app_tof.h/c,来实现对53L4A3的初始化以及读取任务。</p>

<p>app_tof.c内存如下:</p>

<pre>
<code>
/**
******************************************************************************
* <a href="home.php?mod=space&amp;uid=1307177" target="_blank">@File </a>          : app_tof.c
* <a href="home.php?mod=space&amp;uid=1315547" target="_blank">@author </a>      : IMG SW Application Team
* <a href="home.php?mod=space&amp;uid=159083" target="_blank">@brief </a>         : This file provides code for the configuration
*                  of the STMicroelectronics.X-CUBE-TOF1.3.4.2 instances.
******************************************************************************
*
* @attention *
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/

#ifdef __cplusplus
extern &quot;C&quot; {
#endif



/* Includes ------------------------------------------------------------------*/
#include &quot;app_tof.h&quot;
#include &quot;main.h&quot;
#include &lt;stdio.h&gt;
#include &quot;cmsis_os2.h&quot;

#include &quot;53l4a3_ranging_sensor.h&quot;

#define TIMING_BUDGET (100U) /* 10 ms &lt; TimingBudget &lt; 200 ms */
#define POLLING_PERIOD (250U) /* refresh rate for polling mode (ms, shall be consistent with TimingBudget value) */

/* Private variables ---------------------------------------------------------*/
static RANGING_SENSOR_Capabilities_t Cap;
static RANGING_SENSOR_ProfileConfig_t Profile;
static int32_t status = 0;

volatile uint8_t ToF_EventDetected = 0;

/* Private function prototypes -----------------------------------------------*/
static void MX_53L4A3_SimpleRanging_Init(void);
static void MX_53L4A3_SimpleRanging_Process(void);
static void print_result(RANGING_SENSOR_Result_t *Result);
static int32_t decimal_part(float_t x);


void MX_TOF_Init(void)
{
/* USER CODE BEGIN SV */

/* USER CODE END SV */

/* USER CODE BEGIN TOF_Init_PreTreatment */

/* USER CODE END TOF_Init_PreTreatment */

/* Initialize the peripherals and the TOF components */

MX_53L4A3_SimpleRanging_Init();

/* USER CODE BEGIN TOF_Init_PostTreatment */

/* USER CODE END TOF_Init_PostTreatment */
}

static void MX_53L4A3_SimpleRanging_Init(void)
{



/* reset XSHUT (XSDN) pin */
HAL_GPIO_WritePin(VL53L4A3_XSHUT_C_PORT, VL53L4A3_XSHUT_C_PIN, GPIO_PIN_RESET);
HAL_Delay(2);
HAL_GPIO_WritePin(VL53L4A3_XSHUT_C_PORT, VL53L4A3_XSHUT_C_PIN, GPIO_PIN_SET);
HAL_Delay(2);

printf(&quot;53L4A3 Simple Ranging demo application\n&quot;);
status = VL53L4A3_RANGING_SENSOR_Init(VL53L4A3_DEV_CENTER);

if (status != BSP_ERROR_NONE)
{
    printf(&quot;VL53L4A3_RANGING_SENSOR_Init failed\n&quot;);
    while (1);
}
}


/*
* LM background task
*/
void MX_TOF_Process(void)
{
/* USER CODE BEGIN TOF_Process_PreTreatment */

/* USER CODE END TOF_Process_PreTreatment */

MX_53L4A3_SimpleRanging_Process();

/* USER CODE BEGIN TOF_Process_PostTreatment */

/* USER CODE END TOF_Process_PostTreatment */
}

static void MX_53L4A3_SimpleRanging_Process(void)
{
uint8_t i;
uint32_t cal_distance_mm = 0; /* target distance used for offset calibration */
uint32_t Id;

RANGING_SENSOR_Result_t Result;

VL53L4A3_RANGING_SENSOR_ReadID(VL53L4A3_DEV_CENTER, &amp;Id);
VL53L4A3_RANGING_SENSOR_GetCapabilities(VL53L4A3_DEV_CENTER, &amp;Cap);



Profile.RangingProfile = VL53L4ED_PROFILE_CONTINUOUS;
Profile.TimingBudget = TIMING_BUDGET;
Profile.Frequency = 0; /* Induces intermeasurement period, NOT USED for normal ranging */
Profile.EnableAmbient = 1; /* Enable: 1, Disable: 0 */
Profile.EnableSignal = 1; /* Enable: 1, Disable: 0 */

/* set the profile if different from default one */
VL53L4A3_RANGING_SENSOR_ConfigProfile(VL53L4A3_DEV_CENTER, &amp;Profile);



printf(&quot;--- BEGIN OFFSET CALIBRATION ---\n&quot;);
/* make sure that a target is placed at cal_distance_mm (100 mm is the default value in this example)
   * the application will perform some measure in order to have a log of some pre-calibration values
   */
status = VL53L4A3_RANGING_SENSOR_Start(VL53L4A3_DEV_CENTER, RS_MODE_BLOCKING_CONTINUOUS);

        printf(&quot;--- Start testing ---\n&quot;);
while (1)
{
    /* interrupt mode */


      status = VL53L4A3_RANGING_SENSOR_GetDistance(VL53L4A3_DEV_CENTER, &amp;Result);

      if (status == BSP_ERROR_NONE)
      {
      print_result(&amp;Result);
      }

                osDelay(100);
}
}


static void print_result(RANGING_SENSOR_Result_t *Result)
{
uint8_t i;
uint8_t j;

for (i = 0; i &lt; RANGING_SENSOR_MAX_NB_ZONES; i++)
{
    printf(&quot;\nTargets = %lu&quot;, (unsigned long)Result-&gt;ZoneResult.NumberOfTargets);

    for (j = 0; j &lt; Result-&gt;ZoneResult.NumberOfTargets; j++)
    {
      printf(&quot;\n |---&gt; &quot;);

      printf(&quot;Status = %ld, Distance = %5ld mm &quot;,
             (long)Result-&gt;ZoneResult.Status,
             (long)Result-&gt;ZoneResult.Distance);

      if (Profile.EnableAmbient)
      printf(&quot;, Ambient = %ld.%02ld kcps/spad&quot;,
               (long)Result-&gt;ZoneResult.Ambient,
               (long)decimal_part(Result-&gt;ZoneResult.Ambient));

      if (Profile.EnableSignal)
      printf(&quot;, Signal = %ld.%02ld kcps/spad&quot;,
               (long)Result-&gt;ZoneResult.Signal,
               (long)decimal_part(Result-&gt;ZoneResult.Signal));
    }
}
printf(&quot;\n&quot;);
}

static int32_t decimal_part(float_t x)
{
int32_t int_part = (int32_t) x;
return (int32_t)((x - int_part) * 100);
}


#ifdef __cplusplus
}
#endif

</code></pre>

<p>同时添加app_tof.h声明两个函数,用个Inint与任务</p>

<pre>
<code>#ifndef __APP_TOF_H
#define __APP_TOF_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/

/* Exported defines ----------------------------------------------------------*/

/* Exported functions --------------------------------------------------------*/
void MX_TOF_Init(void);
void MX_TOF_Process(void);

#ifdef __cplusplus
}
#endif

#endif /* __APP_TOF_H */</code></pre>

<p>3、在freertos的任务中执行初始化,并运行检测任务。</p>

<pre>
<code>void StartDefaultTask(void *argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
        MX_TOF_Init();
        MX_TOF_Process();
for(;;)
{
    osDelay(1);
}
/* USER CODE END StartDefaultTask */
}
</code></pre>

<p>使用串口助手,可以看到可以打印出测距信息:</p>

<p> &nbsp;</p>

<p>4、为了在tochGFX中显示,我们在app_tof.c中添加一个函数,用于与TouchGFX的数据交换。</p>

<pre>
<code>int get_distance(long * value)
{
        RANGING_SENSOR_Result_t Result;
       
        status = VL53L4A3_RANGING_SENSOR_GetDistance(VL53L4A3_DEV_CENTER, &amp;Result);

        if (status == BSP_ERROR_NONE)
        {
                *value = (long)Result.ZoneResult.Distance;
                return 0;
        }
        return -1;
       
}</code></pre>

<p>5、在ScreenView.hpp中添加handleTickEvent函数。</p>

<p> &nbsp;</p>

<p>并在Screen1view.cpp中实现:</p>

<pre>
<code>void Screen1View::handleTickEvent()
{
        static int tick;
        tick ++;
        long value;
        int ret;
        if( 0 == (tick%20))
        {
                ret = get_distance(&amp;value);
                if(0 == ret)
                {
                        Unicode::snprintf(textArea3Buffer,TEXTAREA3_SIZE,"%d",value);
                        textArea3.invalidate();
                }
        }
       
}</code></pre>

<p>烧录后,可以成功的实现测距:</p>

<p>&nbsp; 【总结】</p>

<p>使用TouchGFX+X-CBUE-Tof可以快速的实现一个可视化的测距工具。</p>

<p>&nbsp;</p>

秦天qintian0303 发表于 2024-11-2 23:10

<p>你这不错,正好可以连个活动结合,不过53L4A3是几点检测?</p>

lugl4313820 发表于 2024-11-2 23:22

秦天qintian0303 发表于 2024-11-2 23:10
你这不错,正好可以连个活动结合,不过53L4A3是几点检测?

<p>不过53L4A3是几点检测?只有一个点,我原来以为有4*4,8*8的,后来查了一下,只有一个。</p>

秦天qintian0303 发表于 2024-11-2 23:34

lugl4313820 发表于 2024-11-2 23:22
不过53L4A3是几点检测?只有一个点,我原来以为有4*4,8*8的,后来查了一下,只有一个。

<p>53L8应该是多点的,那就可以跟热成像是的高边缘锐化了&nbsp;</p>
页: [1]
查看完整版本: 【STM32H7S78-DK】基于TouchGFX的可视测距仪