zhunk 4 месяцев назад
Родитель
Сommit
3af8fce5da

+ 180 - 4
Application_cbd_apm32f1/applications/hmi.c

@@ -240,6 +240,36 @@ typedef struct {
 
 static auto_focus_t g_af;
 
+/* ========== 景深融合(Depth of Field Fusion)控制块 ========== */
+typedef struct {
+    rt_bool_t enabled;            /* 景深融合功能使能 */
+    int       best_pos;           /* 最佳对焦位置 */
+    int       move_count;         /* 当前移动次数 (0-5: 前后各3次) */
+    int       total_moves;        /* 总移动次数 (默认6次: 前后各3次) */
+    int       move_step;          /* 每次移动的步长(脉冲数) */
+    int       current_capture;    /* 当前拍照序号 */
+    int       total_captures;     /* 总拍照数 (对焦1次 + 前后各3次 = 7次) */
+    rt_bool_t is_last_capture;    /* 是否是最后一次拍照 */
+    rt_bool_t capture_done;       /* 当前拍照完成标志 */
+    volatile rt_bool_t in_progress; /* 融合流程进行中 */
+} depth_fusion_t;
+
+static depth_fusion_t g_fusion;
+
+/* 景深融合步骤名称 */
+static const char* fusion_step_name(int step) {
+    switch(step) {
+        case 0: return "focus_ok";           /* 对焦完成基准拍照 */
+        case 1: return "back_1";            /* 向后1 */
+        case 2: return "back_2";            /* 向后2 */
+        case 3: return "back_3";            /* 向后3 */
+        case 4: return "forward_1";         /* 向前1 */
+        case 5: return "forward_2";         /* 向前2 */
+        case 6: return "forward_3";         /* 向前3 */
+        default: return "unknown";
+    }
+}
+
 /* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
 void thread1_signal_handler(int sig)
 {
@@ -281,13 +311,150 @@ static void auto_focus_move(void)
     rt_timer_start(timer_takephoto);
 }
 
-/* 完成对焦 */
+/* 初始化景深融合状态 */
+static void depth_fusion_init(int best_pos)
+{
+    memset(&g_fusion, 0, sizeof(g_fusion));
+    g_fusion.enabled = RT_TRUE;
+    g_fusion.best_pos = best_pos;
+    g_fusion.move_count = 0;
+    g_fusion.total_moves = 6;      /* 前后各移动3次 */
+    g_fusion.move_step = 30;       /* 每次移动30脉冲(可根据景深需求调整) */
+    g_fusion.current_capture = 0;
+    g_fusion.total_captures = 7;   /* 1次对焦 + 6次移动 = 7次 */
+    g_fusion.is_last_capture = RT_FALSE;
+    g_fusion.capture_done = RT_FALSE;
+    g_fusion.in_progress = RT_TRUE;
+
+    LOG_I("[景深融合] 初始化完成! 最佳位置:%d 步长:%d", best_pos, g_fusion.move_step);
+}
+
+/* 景深融合移动电机一步 */
+static void depth_fusion_move(void)
+{
+    int offset;
+    int dir;
+
+    /* 计算偏移量: 先向负方向移动3次,再向正方向移动3次 */
+    if (g_fusion.move_count < 3) {
+        /* 向后(负方向)移动 */
+        dir = 0;
+        offset = -(g_fusion.move_count + 1) * g_fusion.move_step;
+    } else {
+        /* 向前(正方向)移动 */
+        dir = 1;
+        offset = (g_fusion.move_count - 2) * g_fusion.move_step;
+    }
+
+    /* 计算绝对位置: 最佳位置 + 偏移量 */
+    int target_pos = g_fusion.best_pos + offset;
+
+    LOG_I("[景深融合] 移动#%d/%d 方向:%s 目标位置:%d",
+           g_fusion.move_count + 1, g_fusion.total_moves,
+           dir ? "前" : "后", target_pos);
+
+    slide_para.pulse_period = mapSpeed(1);
+    slide_para.ystep = target_pos;
+    slide_para.ydir = dir;
+    slide_para.ydis = g_fusion.move_step;
+    slide_para.en_slide = 6;
+
+    while (slide_para.en_slide != 0) {
+        rt_thread_delay(100);
+    }
+
+    LOG_I("[景深融合] 电机到位,准备拍照");
+    rt_timer_start(timer_takephoto);
+}
+
+/* 景深融合拍照完成回调 - 由定时器或信号处理调用 */
+void depth_fusion_on_capture_ready(void)
+{
+    if (!g_fusion.in_progress || g_fusion.capture_done) {
+        return;
+    }
+
+    g_fusion.current_capture++;
+    g_fusion.capture_done = RT_TRUE;
+
+    /* 发送拍照指令,标记是否最后一次 */
+    char *cmd = makeJson_Warn("fusion_capture", fusion_step_name(g_fusion.current_capture));
+    if (cmd != RT_NULL) {
+        DTU_Write("%s", cmd);
+        rt_free(cmd);
+        LOG_I("[景深融合] 发送拍照指令[%d/%d]: %s",
+               g_fusion.current_capture, g_fusion.total_captures,
+               fusion_step_name(g_fusion.current_capture));
+    }
+}
+
+/* 景深融合拍照完成回调 - 内部使用 */
+static void depth_fusion_capture_done(void)
+{
+    depth_fusion_on_capture_ready();
+}
+
+/* 景深融合流程处理(在接收端通知保存完成后调用) */
+static void depth_fusion_process(void)
+{
+    g_fusion.capture_done = RT_FALSE;
+
+    if (g_fusion.current_capture == 0) {
+        /* 第一次: 对焦完成后的基准位置拍照 */
+        LOG_I("[景深融合] 执行对焦基准位置拍照");
+        depth_fusion_capture_done();
+        return;
+    }
+
+    g_fusion.move_count++;
+
+    if (g_fusion.move_count > g_fusion.total_moves) {
+        /* 全部移动完成,执行融合 */
+        g_fusion.in_progress = RT_FALSE;
+        g_fusion.enabled = RT_FALSE;
+        LOG_I("[景深融合] 全部拍照完成,执行融合!");
+        char *cmd = makeJson_Warn("fusion_start", "do_fusion");
+        if (cmd != RT_NULL) {
+            DTU_Write("%s", cmd);
+            rt_free(cmd);
+        }
+        return;
+    }
+
+    /* 继续移动到下一个位置,移动完成后自动拍照 */
+    depth_fusion_move();
+}
+
+/* 设置景深融合使能(由外部调用) */
+void depth_fusion_set_enabled(rt_bool_t enable)
+{
+    g_fusion.enabled = enable;
+    LOG_I("[景深融合] 功能%s", enable ? "使能" : "禁用");
+}
+
+/* 获取景深融合是否在进行中 */
+rt_bool_t depth_fusion_is_active(void)
+{
+    return g_fusion.in_progress;
+}
+
+/* 完成对焦 - 触发景深融合流程 */
 static void auto_focus_done(void)
 {
     LOG_I("[对焦] 完成! 最佳位置:%d 清晰度:%.2f", g_af.best_pos, g_af.best_sharpness);
-    char *c0 = makeJson_Warn("auto_focus","auto_focus");
-    DTU_Write("%s",c0);
-    rt_free(c0);
+
+    if (g_fusion.enabled) {
+        /* 启动景深融合流程 */
+        LOG_I("[景深融合] 对焦完成,启动景深融合流程");
+        depth_fusion_init(g_af.best_pos);
+        depth_fusion_process();  /* 执行第一次拍照 */
+    } else {
+        /* 原有逻辑: 发送对焦完成消息 */
+        char *c0 = makeJson_Warn("auto_focus","auto_focus");
+        DTU_Write("%s",c0);
+        rt_free(c0);
+    }
+
     device_status.autotakepic = 0;
 }
 
@@ -603,6 +770,15 @@ static void Ctr_Mortor_entry(void* parameter)
             calibrate_init();
             slide_para.ystep = 0;
         }
+        /* 景深融合: 拍照保存完成后继续下一步 */
+        if(device_status.autotakepic == 5)
+        {
+            if (depth_fusion_is_active()) {
+                LOG_I("[景深融合] 收到保存完成,继续下一步");
+                depth_fusion_process();
+            }
+            device_status.autotakepic = 0;
+        }
         if(device_status.fsl_sw==1)
         {
             ctr_Petroleum_jelly(10);

+ 5 - 0
Application_cbd_apm32f1/applications/hmi.h

@@ -36,4 +36,9 @@ void bubblesort(int a[], int n);
 
 void send_signal(int sig);
 
+/* 景深融合功能接口 */
+void depth_fusion_on_capture_ready(void);
+rt_bool_t depth_fusion_is_active(void);
+void depth_fusion_set_enabled(rt_bool_t enable);
+
 #endif /* APPLICATIONS_INC_HMI_H_ */

+ 6 - 0
Application_cbd_apm32f1/applications/selfcheck.c

@@ -38,6 +38,12 @@ static int calculate_average(int arr[], int n) {
 
 static void t_takephoto_callback(void *parameter)
 {
+    /* 景深融合模式下,触发拍照指令 */
+    if (depth_fusion_is_active()) {
+        depth_fusion_on_capture_ready();
+        return;
+    }
+
     device_status.autotakepic = 0;
     LOG_E("t_takephoto_callback");
     //关闭补光灯