zhunk 1 год назад
Родитель
Сommit
7f18c68a40
1 измененных файлов с 34 добавлено и 8 удалено
  1. 34 8
      Application_cbd_apm32f1/applications/hmi.c

+ 34 - 8
Application_cbd_apm32f1/applications/hmi.c

@@ -26,7 +26,8 @@
 #include "dtu_uart2.h"
 #include "dtu_uart2.h"
 #include <fal.h>
 #include <fal.h>
 #include "selfcheck.h"
 #include "selfcheck.h"
- #include <float.h>
+#include <float.h>
+#include <math.h>
 #define DBG_ENABLE
 #define DBG_ENABLE
 #define DBG_COLOR
 #define DBG_COLOR
 #define DBG_TAG "hmi"
 #define DBG_TAG "hmi"
@@ -1357,11 +1358,12 @@ static float g_last_sharpness = -FLT_MAX;    // 上一次移动后的清晰度
 static bool g_initial_move = true;           // 初始方向判断标志
 static bool g_initial_move = true;           // 初始方向判断标志
 int maxIterations = 100;
 int maxIterations = 100;
 uint8_t dir = 1;
 uint8_t dir = 1;
-uint8_t stepSize = 4;     // 初始步长(单位:步进电机脉冲数)
+uint8_t stepSize = 20;     // 初始步长(单位:步进电机脉冲数)
 int current_pos = 0;  // 根据实际类型修改(int/float等)
 int current_pos = 0;  // 根据实际类型修改(int/float等)
 static int g_peak_counter = 0;      // 连续上升后下降的计数器
 static int g_peak_counter = 0;      // 连续上升后下降的计数器
 static bool g_last_rising = false;   // 记录上次是否上升
 static bool g_last_rising = false;   // 记录上次是否上升
-static int counter_sharpness_reduce = 0;
+static int counter_sharpness_reduce = 0;    //清晰度降低累计
+static int counter_sharpness_diff = 0;      //清晰度累计插值 次数变化
 #define MAX(a, b) ((a) > (b) ? (a) : (b))  // 手动定义宏
 #define MAX(a, b) ((a) > (b) ? (a) : (b))  // 手动定义宏
 
 
 void thread1_signal_handler(int sig)
 void thread1_signal_handler(int sig)
@@ -1390,7 +1392,24 @@ void thread1_signal_handler(int sig)
             }
             }
             g_initial_move = false;
             g_initial_move = false;
         } else {
         } else {
-            if(current_sharpness< (g_last_sharpness+0.5f)){    //持续3次清晰度降低,调整行进方向
+
+            double diff_sharpness =  fabs(current_sharpness-g_last_sharpness);
+            if(diff_sharpness<1.0)
+            {
+                counter_sharpness_diff++;
+                if(counter_sharpness_diff>=10){ //持续10次
+                    stepSize = 20;
+                }
+            }
+
+            if(diff_sharpness > 3.5)  //动态调整,发生明显变化,降低步进幅度
+            {
+                counter_sharpness_diff = 0;
+                stepSize = 4;
+            }
+            LOG_I("[对焦] 清晰度变化累计%d次小于1.0",counter_sharpness_diff);
+
+            if(current_sharpness< (g_last_sharpness-0.3f)){    //持续3次清晰度降低,调整行进方向
                 counter_sharpness_reduce++;
                 counter_sharpness_reduce++;
                 LOG_I("[对焦] 清晰度下降,次数:%d", counter_sharpness_reduce);
                 LOG_I("[对焦] 清晰度下降,次数:%d", counter_sharpness_reduce);
                 if(counter_sharpness_reduce>=3){
                 if(counter_sharpness_reduce>=3){
@@ -1416,7 +1435,8 @@ void thread1_signal_handler(int sig)
                 }
                 }
                 g_last_rising = false;
                 g_last_rising = false;
                 dir = !dir;             // 清晰度下降,反转方向
                 dir = !dir;             // 清晰度下降,反转方向
-                LOG_I("[对焦] 清晰度下降,反转方向:%d 新步长:%d", dir, stepSize);
+                stepSize = 4;
+                LOG_I("[对焦] 清晰度下降,反转方向:%d 步长:%d", dir, stepSize);
             }
             }
         }
         }
 
 
@@ -1427,6 +1447,11 @@ void thread1_signal_handler(int sig)
         current_pos += dir ? stepSize : -stepSize;
         current_pos += dir ? stepSize : -stepSize;
         LOG_I("[对焦] 方向:%d 移动步长:%d 新位置:%d", dir, stepSize, current_pos);
         LOG_I("[对焦] 方向:%d 移动步长:%d 新位置:%d", dir, stepSize, current_pos);
 
 
+        if(counter_sharpness_diff>=30){
+            dir = !dir;
+            counter_sharpness_diff = 0;
+        }
+
         slide_para.ydir = dir;
         slide_para.ydir = dir;
         slide_para.ydis = stepSize;
         slide_para.ydis = stepSize;
         slide_para.en_slide = 6;  // 触发移动
         slide_para.en_slide = 6;  // 触发移动
@@ -1436,7 +1461,7 @@ void thread1_signal_handler(int sig)
 
 
 
 
         /* 终止条件检测 */
         /* 终止条件检测 */
-        if (maxIterations <= 0 || g_peak_counter >= 3) {
+        if (counter_sharpness_diff>=100||maxIterations <= 0 || g_peak_counter >= 3) {
             LOG_I("[对焦] 完成搜索,最佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
             LOG_I("[对焦] 完成搜索,最佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
             if (g_peak_counter >= 3) {
             if (g_peak_counter >= 3) {
                 LOG_I("[对焦] 连续三次波动确认峰值");
                 LOG_I("[对焦] 连续三次波动确认峰值");
@@ -1445,10 +1470,11 @@ void thread1_signal_handler(int sig)
             LOG_I("[对焦] 已到达最佳对焦位置");
             LOG_I("[对焦] 已到达最佳对焦位置");
             if(maxIterations==0)
             if(maxIterations==0)
             {
             {
-                maxIterations = 120;
+                stepSize = 20;
                 dir = !dir;
                 dir = !dir;
                 current_pos = 0;
                 current_pos = 0;
             }else{
             }else{
+                counter_sharpness_diff = 0;
                 char *c0 = makeJson_Warn("auto_focus","auto_focus");
                 char *c0 = makeJson_Warn("auto_focus","auto_focus");
                 DTU_Write("%s",c0);
                 DTU_Write("%s",c0);
                 rt_free(c0);
                 rt_free(c0);
@@ -1539,7 +1565,7 @@ static void Ctr_Mortor_entry(void* parameter)
             g_initial_move = true;           // 初始方向判断标志
             g_initial_move = true;           // 初始方向判断标志
             maxIterations = 100;
             maxIterations = 100;
             dir = 1;
             dir = 1;
-            stepSize = 4;     // 初始步长(单位:步进电机脉冲数)
+            stepSize = 20;     // 初始步长(单位:步进电机脉冲数)
             current_pos = 0;  // 根据实际类型修改(int/float等)
             current_pos = 0;  // 根据实际类型修改(int/float等)
 
 
             g_peak_counter = 0;
             g_peak_counter = 0;