|
|
@@ -39,7 +39,7 @@
|
|
|
#define HMI_THREAD_TIMESLICE 10 //时间片
|
|
|
|
|
|
#define CONTROL_MOTOR_PRIORITY 7
|
|
|
-#define CONTROL_MOTOR_STACK_SIZE 2048
|
|
|
+#define CONTROL_MOTOR_STACK_SIZE 8192
|
|
|
#define CONTROL_MOTOR_TIMESLICE 5
|
|
|
|
|
|
|
|
|
@@ -163,9 +163,8 @@ static void Hmi_thread_entry(void *parameter)
|
|
|
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
|
|
|
if (result == RT_EOK)
|
|
|
{
|
|
|
-
|
|
|
- rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
|
|
|
- rx_buffer[rx_length] = '\0';
|
|
|
+ rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
|
|
|
+ rx_buffer[rx_length] = '\0';
|
|
|
}
|
|
|
/*********************************************************************************************************************/
|
|
|
//rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
|
|
|
@@ -214,210 +213,317 @@ void send_signal(int sig)
|
|
|
rt_thread_kill(ctr_motor, sig);
|
|
|
}
|
|
|
|
|
|
-// 全局状态变量(需在合适位置初始化)
|
|
|
-static float g_best_sharpness = -FLT_MAX; // 全局最佳清晰度
|
|
|
-static int g_best_pos = 0; // 全局最佳位置
|
|
|
-static float g_last_sharpness = 0.0; // 上一次移动后的清晰度
|
|
|
-static bool g_initial_move = true; // 初始方向判断标志
|
|
|
-static bool focus_done = false;
|
|
|
-int maxIterations = 100;
|
|
|
-uint8_t dir = 1;
|
|
|
-uint8_t stepSize = 20; // 初始步长(单位:步进电机脉冲数)
|
|
|
-int current_pos = 0; // 根据实际类型修改(int/float等)
|
|
|
-static int g_peak_counter = 0; // 连续上升后下降的计数器
|
|
|
-static bool g_last_rising = false; // 记录上次是否上升
|
|
|
-static int counter_sharpness_reduce = 0; //清晰度降低累计
|
|
|
-static int counter_sharpness_diff = 0; //清晰度累计插值 次数变化
|
|
|
-#define MAX(a, b) ((a) > (b) ? (a) : (b)) // 手动定义宏
|
|
|
-
|
|
|
+/* ========== 自动对焦(Auto Focus)控制块 ========== */
|
|
|
+#define AF_FOCUS_TIMEOUT_MS 5000 /* 等待 focus 回复超时时间(ms) */
|
|
|
+#define AF_FOCUS_RETRY_MAX 3 /* 等待 focus 超时补发指令次数上限 */
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ float best_sharpness; /* 全局最佳清晰度 */
|
|
|
+ int best_pos; /* 全局最佳位置 */
|
|
|
+ float last_sharpness; /* 上一次移动后的清晰度 */
|
|
|
+ int dir; /* 行进方向: 1=正向, 0=反向 */
|
|
|
+ int step_size; /* 步进电机脉冲数 */
|
|
|
+ int current_pos; /* 当前位置 */
|
|
|
+ int peak_counter; /* 连续上升后下降的峰值计数 */
|
|
|
+ int reduce_counter; /* 清晰度连续降低计数 */
|
|
|
+ int diff_counter; /* 清晰度变化微小累计次数 */
|
|
|
+ bool last_rising; /* 上次是否上升 */
|
|
|
+ int max_iterations; /* 最大迭代次数 */
|
|
|
+ bool initial_move; /* 初始方向判断标志 */
|
|
|
+ volatile bool sharpness_updated; /* 信号处理程序置位: 有新清晰度值 */
|
|
|
+ float new_sharpness; /* 信号处理程序写入的新清晰度值 */
|
|
|
+ bool active; /* 对焦进行中标志(用于超时重试判定) */
|
|
|
+ rt_tick_t wait_start_tick; /* 开始等待 focus 回复的时间 */
|
|
|
+ int retry_cnt; /* 已连续超时补发指令次数 */
|
|
|
+} auto_focus_t;
|
|
|
+
|
|
|
+static auto_focus_t g_af;
|
|
|
+/* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
|
|
|
void thread1_signal_handler(int sig)
|
|
|
{
|
|
|
- rt_timer_stop(timer_takephoto);
|
|
|
- LOG_I("[对焦] 清晰度值更新:%.2f", device_status.focus_value);
|
|
|
- slide_para.pulse_period = mapSpeed(1); // 更新电机速度参数
|
|
|
- maxIterations--;
|
|
|
-
|
|
|
- if (device_status.autotakepic == 2 && maxIterations >= 0) {
|
|
|
- float current_sharpness = device_status.focus_value;
|
|
|
-
|
|
|
- /* 更新全局最佳状态 */
|
|
|
- if (current_sharpness > g_best_sharpness) {
|
|
|
- g_best_sharpness = current_sharpness;
|
|
|
- g_best_pos = current_pos;
|
|
|
- LOG_I("[对焦] 发现更佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
|
|
|
- }
|
|
|
-
|
|
|
- /* 初始方向判断(首步移动后) */
|
|
|
- if (g_initial_move) {
|
|
|
- if (current_sharpness > g_last_sharpness) {
|
|
|
- LOG_I("[对焦] 初始方向正确,保持方向:%d", dir);
|
|
|
- } else {
|
|
|
- dir = !dir; // 反转方向
|
|
|
- LOG_I("[对焦] 初始方向错误,反转方向:%d", dir);
|
|
|
- }
|
|
|
- g_initial_move = false;
|
|
|
- } else {
|
|
|
+ g_af.sharpness_updated = true;
|
|
|
+ g_af.new_sharpness = device_status.focus_value;
|
|
|
+}
|
|
|
|
|
|
- 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++;
|
|
|
- LOG_I("[对焦] 清晰度下降,次数:%d", counter_sharpness_reduce);
|
|
|
- if(counter_sharpness_reduce>=3){
|
|
|
- counter_sharpness_reduce = 0;
|
|
|
- dir = !dir; // 清晰度下降,反转方向
|
|
|
- LOG_I("[对焦] 连续清晰度下降,调转行进方向!");
|
|
|
- }
|
|
|
- }else {
|
|
|
- counter_sharpness_reduce = 0;
|
|
|
- }
|
|
|
+/* 初始化自动对焦状态 */
|
|
|
+static void auto_focus_init(void)
|
|
|
+{
|
|
|
+ memset(&g_af, 0, sizeof(g_af));
|
|
|
+ g_af.best_sharpness = -FLT_MAX;
|
|
|
+ g_af.last_sharpness = 0.0f;
|
|
|
+ g_af.initial_move = true;
|
|
|
+ g_af.last_rising = false;
|
|
|
+ g_af.dir = 1;
|
|
|
+ g_af.step_size = 20;
|
|
|
+ g_af.current_pos = 0;
|
|
|
+ g_af.max_iterations = 100;
|
|
|
+ g_af.peak_counter = 0;
|
|
|
+ g_af.best_pos = 0;
|
|
|
+ g_af.reduce_counter = 0;
|
|
|
+ /* 超时重试状态 */
|
|
|
+ g_af.active = true;
|
|
|
+ g_af.wait_start_tick = rt_tick_get();
|
|
|
+ g_af.retry_cnt = 0;
|
|
|
+}
|
|
|
|
|
|
- /* 方向策略调整,检测到清晰度变大 */
|
|
|
- if (current_sharpness > (g_last_sharpness - 3.5f)) { // 噪声容忍阈值3.0
|
|
|
- // 保持方向,当接近峰值时自动减小步长
|
|
|
- LOG_I("保持方向,当接近峰值时自动减小步长...");
|
|
|
- g_last_rising = true;
|
|
|
- stepSize += 2; //动态调整步幅
|
|
|
- } else {
|
|
|
- if(g_last_rising){
|
|
|
- g_peak_counter++;
|
|
|
- LOG_I("[对焦] 检测到清晰度波动[%d/3]", g_peak_counter);
|
|
|
- stepSize = 2;
|
|
|
- }else {
|
|
|
- g_peak_counter = 0; // 连续下降则重置
|
|
|
- }
|
|
|
- g_last_rising = false;
|
|
|
- dir = !dir; // 清晰度下降,反转方向
|
|
|
- LOG_I("[对焦] 清晰度下降,反转方向:%d 步长:%d", dir, stepSize);
|
|
|
- }
|
|
|
- }
|
|
|
+/* 移动电机一步 */
|
|
|
+static void auto_focus_move(void)
|
|
|
+{
|
|
|
+ g_af.current_pos += g_af.dir ? g_af.step_size : -g_af.step_size;
|
|
|
+ LOG_I("[对焦] 移动:方向=%d 步长=%d 位置=%d", g_af.dir, g_af.step_size, g_af.current_pos);
|
|
|
|
|
|
- /* 记录本次清晰度值 */
|
|
|
- g_last_sharpness = current_sharpness;
|
|
|
+ slide_para.pulse_period = mapSpeed(1);
|
|
|
+ slide_para.ystep = g_af.step_size;
|
|
|
+ slide_para.ydir = g_af.dir;
|
|
|
+ slide_para.ydis = g_af.step_size;
|
|
|
+ slide_para.en_slide = 6;
|
|
|
|
|
|
- /* 计算并执行下一步移动 */
|
|
|
- current_pos += dir ? stepSize : -stepSize;
|
|
|
- LOG_I("[对焦] 方向:%d 移动步长:%d 新位置:%d", dir, stepSize, current_pos);
|
|
|
+ while (slide_para.en_slide != 0) {
|
|
|
+ rt_thread_delay(100);
|
|
|
+ }
|
|
|
|
|
|
- if(counter_sharpness_diff>=30){
|
|
|
- dir = !dir;
|
|
|
- stepSize += 10; //增大步幅
|
|
|
- counter_sharpness_diff = 0;
|
|
|
- }
|
|
|
+ LOG_I("[对焦] 电机到位");
|
|
|
+ g_af.wait_start_tick = rt_tick_get(); /* 移动完成,开始等待 focus 回复 */
|
|
|
+}
|
|
|
|
|
|
- LOG_I("[对焦] 最佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
|
|
|
+/* 完成对焦 */
|
|
|
+static void auto_focus_done(void)
|
|
|
+{
|
|
|
+ LOG_I("[对焦] 完成! 最佳位置:%d 清晰度:%.2f", g_af.best_pos, g_af.best_sharpness);
|
|
|
+ g_af.active = false; /* 对焦完成,停止超时重试检测 */
|
|
|
+ char *c0 = makeJson_Warn("auto_focus","auto_focus");
|
|
|
+ DTU_Write("%s",c0);
|
|
|
+ rt_free(c0);
|
|
|
+}
|
|
|
|
|
|
+/* 处理一次清晰度值,执行对焦算法 */
|
|
|
+static void auto_focus_process(void)
|
|
|
+{
|
|
|
+ float current_sharpness = g_af.new_sharpness;
|
|
|
+ g_af.sharpness_updated = false;
|
|
|
+ g_af.max_iterations--;
|
|
|
+ g_af.retry_cnt = 0; /* 收到 focus 回复,清零超时重试计数 */
|
|
|
+ g_af.wait_start_tick = rt_tick_get(); /* 重置等待计时 */
|
|
|
+
|
|
|
+ LOG_I("[对焦] 清晰度:%.2f 位置:%d 剩余:%d",
|
|
|
+ current_sharpness, g_af.current_pos, g_af.max_iterations);
|
|
|
+
|
|
|
+ /* 更新最佳位置 */
|
|
|
+ if (current_sharpness > g_af.best_sharpness) {
|
|
|
+ g_af.best_sharpness = current_sharpness;
|
|
|
+ g_af.best_pos = g_af.current_pos;
|
|
|
+ LOG_I("[对焦] 新最佳:位置%d 清晰度%.2f", g_af.best_pos, g_af.best_sharpness);
|
|
|
+ }
|
|
|
|
|
|
- if(g_peak_counter>=3&¤t_sharpness>g_best_sharpness*0.95){
|
|
|
- focus_done = true;
|
|
|
- }else{
|
|
|
- focus_done = false;
|
|
|
- g_best_sharpness -= 2.0f; //降低阈值,防止找不到
|
|
|
+ /* 初始方向判断:比较首次移动前后的清晰度 */
|
|
|
+ if (g_af.initial_move) {
|
|
|
+ g_af.initial_move = false;
|
|
|
+ if (current_sharpness > g_af.last_sharpness) {
|
|
|
+ LOG_I("[对焦] 初始方向正确");
|
|
|
+ } else {
|
|
|
+ g_af.dir = !g_af.dir;
|
|
|
+ LOG_I("[对焦] 初始方向错误,反转");
|
|
|
}
|
|
|
+ g_af.last_sharpness = current_sharpness;
|
|
|
+ auto_focus_move();
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- /* 终止条件检测 */
|
|
|
- if (counter_sharpness_diff>=100||maxIterations <= 0 || focus_done==true) {
|
|
|
- counter_sharpness_reduce = 0;
|
|
|
- maxIterations = 100;
|
|
|
- LOG_I("[对焦] 完成搜索,最佳位置:%d (清晰度:%.2f)", g_best_pos, g_best_sharpness);
|
|
|
- if (g_peak_counter >= 3) {
|
|
|
- LOG_I("[对焦] 连续三次波动确认峰值");
|
|
|
- }
|
|
|
- focus_done = false;
|
|
|
- LOG_I("[对焦] 已到达最佳对焦位置");
|
|
|
- if(maxIterations==0)
|
|
|
- {
|
|
|
- stepSize = 20;
|
|
|
- dir = !dir;
|
|
|
- current_pos = 0;
|
|
|
- }else{
|
|
|
- counter_sharpness_diff = 0;
|
|
|
- char *c0 = makeJson_Warn("auto_focus","auto_focus");
|
|
|
- DTU_Write("%s",c0);
|
|
|
- rt_free(c0);
|
|
|
- device_status.autotakepic = 0; // 退出自动对焦模式
|
|
|
- return;
|
|
|
- }
|
|
|
+ /* 计算清晰度变化 */
|
|
|
+ float diff = current_sharpness - g_af.last_sharpness;
|
|
|
+
|
|
|
+ /* 动态调整步长 */
|
|
|
+ if (fabs(diff) > 3.0f) {
|
|
|
+ /* 清晰度大幅变化 -> 缩小步长精细搜索 */
|
|
|
+ g_af.step_size = 10;
|
|
|
+ g_af.diff_counter = 0;
|
|
|
+ LOG_I("[对焦] 大幅变化,步长=%d", g_af.step_size);
|
|
|
+ } else if (fabs(diff) < 1.0f) {
|
|
|
+ /* 清晰度变化微小 -> 累计平坦区域 */
|
|
|
+ g_af.diff_counter++;
|
|
|
+ if (g_af.diff_counter >= 10) {
|
|
|
+ g_af.step_size = 20;
|
|
|
}
|
|
|
- slide_para.ystep = stepSize;
|
|
|
- slide_para.ydir = dir;
|
|
|
- slide_para.ydis = stepSize;
|
|
|
- slide_para.en_slide = 6; // 触发移动
|
|
|
- while (slide_para.en_slide != 0) {
|
|
|
- rt_thread_delay(500); // 等待移动完成
|
|
|
+ LOG_I("[对焦] 平坦[%d/10] 步长=%d", g_af.diff_counter, g_af.step_size);
|
|
|
+ } else {
|
|
|
+ g_af.diff_counter = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (current_sharpness > (g_af.last_sharpness - 3.5f)) { // 噪声容忍阈值3.0
|
|
|
+ // 保持方向,当接近峰值时自动减小步长
|
|
|
+ LOG_I("保持方向,当接近峰值时自动减小步长...");
|
|
|
+ g_af.last_rising = true;
|
|
|
+ g_af.step_size += 2; //动态调整步幅
|
|
|
+ } else {
|
|
|
+ if(g_af.last_rising){
|
|
|
+ g_af.peak_counter++;
|
|
|
+ LOG_I("[对焦] 检测到清晰度波动[%d/2]", g_af.peak_counter);
|
|
|
+ g_af.step_size = 2;
|
|
|
+ }else {
|
|
|
+ g_af.peak_counter = 0; // 连续下降则重置
|
|
|
}
|
|
|
+ g_af.last_rising = false;
|
|
|
+ g_af.dir = !g_af.dir; // 清晰度下降,反转方向
|
|
|
+ LOG_I("[对焦] 清晰度下降,反转方向:%d 步长:%d", g_af.dir, g_af.step_size);
|
|
|
+ }
|
|
|
+
|
|
|
+ g_af.last_sharpness = current_sharpness;
|
|
|
+
|
|
|
+
|
|
|
+ /* 平坦区域超时:强制反转并增大步长 */
|
|
|
+ if (g_af.diff_counter >= 30) {
|
|
|
+ g_af.dir = !g_af.dir;
|
|
|
+ g_af.step_size += 10;
|
|
|
+ g_af.diff_counter = 0;
|
|
|
+ LOG_I("[对焦] 平坦超时,增大步长=%d", g_af.step_size);
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_I("[对焦] 最佳:位置%d 清晰度%.2f", g_af.best_pos, g_af.best_sharpness);
|
|
|
+
|
|
|
+ /* 终止条件 */
|
|
|
+ bool done = false;
|
|
|
+ if (g_af.peak_counter >= 2 && current_sharpness > g_af.best_sharpness * 0.90f) {
|
|
|
+ done = true;
|
|
|
+ LOG_I("[对焦] 峰值确认完成");
|
|
|
+ g_af.max_iterations = 100;
|
|
|
+ g_af.reduce_counter = 0;
|
|
|
+ g_af.diff_counter = 0;
|
|
|
}
|
|
|
+ if (g_af.max_iterations <= 0) {
|
|
|
+ done = true;
|
|
|
+ g_af.step_size = 20;
|
|
|
+ g_af.dir = !g_af.dir;
|
|
|
+ g_af.current_pos = 0;
|
|
|
+ LOG_I("[对焦] 迭代次数耗尽");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (done) {
|
|
|
+ auto_focus_done();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 移动电机 */
|
|
|
+ auto_focus_move();
|
|
|
}
|
|
|
|
|
|
+/* ========== Y轴校准(Calibration)控制块 ========== */
|
|
|
+typedef struct {
|
|
|
+ float best_sharpness;
|
|
|
+ float last_sharpness;
|
|
|
+ int diff_counter;
|
|
|
+ bool initial_move;
|
|
|
+ int dir;
|
|
|
+ int ystep;
|
|
|
+ volatile bool sharpness_updated;
|
|
|
+ float new_sharpness;
|
|
|
+} calibrate_t;
|
|
|
+
|
|
|
+static calibrate_t g_cal;
|
|
|
+
|
|
|
+/* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
|
|
|
void thread2_signal_handler(int sig)
|
|
|
{
|
|
|
- rt_timer_stop(timer_takephoto);
|
|
|
- maxIterations--;
|
|
|
- LOG_I("[Y] 清晰度值更新:%.2f", device_status.focus_value);
|
|
|
- slide_para.pulse_period = mapSpeed(50); // 更新电机速度参数
|
|
|
- if(device_status.calibrate_slide==20&& maxIterations >= 0){
|
|
|
- float current_sharpness = device_status.focus_value;
|
|
|
- /* 更新全局最佳状态 */
|
|
|
- if (current_sharpness > g_best_sharpness) {
|
|
|
- g_best_sharpness = current_sharpness;
|
|
|
- LOG_I("[Y] 发现更佳位置(清晰度:%.2f)",g_best_sharpness);
|
|
|
- }
|
|
|
- /* 初始方向判断(首步移动后) */
|
|
|
- if (g_initial_move) {
|
|
|
- if (current_sharpness > g_last_sharpness) {
|
|
|
- LOG_I("[对焦] 初始方向正确,保持方向:%d", dir);
|
|
|
- }
|
|
|
- g_initial_move = false;
|
|
|
- }else{
|
|
|
- double diff_sharpness = fabs(current_sharpness-g_last_sharpness);
|
|
|
- if(diff_sharpness<1.0)
|
|
|
- {
|
|
|
- counter_sharpness_diff++;
|
|
|
- }
|
|
|
+ g_cal.sharpness_updated = true;
|
|
|
+ g_cal.new_sharpness = device_status.focus_value;
|
|
|
+}
|
|
|
|
|
|
- LOG_I("[Y] 清晰度变化累计%d次小于1.0",counter_sharpness_diff);
|
|
|
- if(counter_sharpness_diff>1&&diff_sharpness>100.0){
|
|
|
- //停止,保存当前位置
|
|
|
- device_status.calibrate_slide = 0;
|
|
|
- device_status.y_init_dis = slide_para.ystep;
|
|
|
- LOG_I("设置Y轴初始位置:%d",slide_para.ystep);
|
|
|
- ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
|
|
|
- return;
|
|
|
- }
|
|
|
- /* 记录本次清晰度值 */
|
|
|
- g_last_sharpness = current_sharpness;
|
|
|
- }
|
|
|
+/* 初始化校准状态 */
|
|
|
+static void calibrate_init(void)
|
|
|
+{
|
|
|
+ memset(&g_cal, 0, sizeof(g_cal));
|
|
|
+ g_cal.best_sharpness = -FLT_MAX;
|
|
|
+ g_cal.last_sharpness = 0.0f;
|
|
|
+ g_cal.initial_move = true;
|
|
|
+ g_cal.dir = 1;
|
|
|
+ g_cal.ystep = 0;
|
|
|
+}
|
|
|
|
|
|
- /* 终止条件检测 */
|
|
|
- if (maxIterations <= 0) {
|
|
|
- device_status.calibrate_slide = 0;
|
|
|
- return;
|
|
|
- }
|
|
|
- slide_para.ydir = dir;
|
|
|
- slide_para.ydis = 80;
|
|
|
- slide_para.ystep += 80;
|
|
|
- slide_para.en_slide = 6; // 触发移动
|
|
|
- while (slide_para.en_slide != 0) {
|
|
|
- rt_thread_delay(500); // 等待移动完成
|
|
|
+/* 移动电机一步 */
|
|
|
+static void calibrate_move(void)
|
|
|
+{
|
|
|
+ g_cal.ystep += 80;
|
|
|
+ slide_para.pulse_period = mapSpeed(50);
|
|
|
+ slide_para.ydir = g_cal.dir;
|
|
|
+ slide_para.ydis = 80;
|
|
|
+ slide_para.ystep = g_cal.ystep;
|
|
|
+ slide_para.en_slide = 6;
|
|
|
+
|
|
|
+ while (slide_para.en_slide != 0) {
|
|
|
+ rt_thread_delay(500);
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_I("[Y] 电机到位 位置:%d", g_cal.ystep);
|
|
|
+}
|
|
|
+
|
|
|
+/* 处理一次校准清晰度值 */
|
|
|
+static void calibrate_process(void)
|
|
|
+{
|
|
|
+ float current_sharpness = g_cal.new_sharpness;
|
|
|
+ g_cal.sharpness_updated = false;
|
|
|
+
|
|
|
+ LOG_I("[Y] 清晰度值:%.2f 位置:%d", current_sharpness, g_cal.ystep);
|
|
|
+
|
|
|
+ /* 更新最佳位置 */
|
|
|
+ if (current_sharpness > g_cal.best_sharpness) {
|
|
|
+ g_cal.best_sharpness = current_sharpness;
|
|
|
+ LOG_I("[Y] 更佳位置(清晰度:%.2f) 位置:%d", g_cal.best_sharpness, g_cal.ystep);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 初始方向判断 */
|
|
|
+ if (g_cal.initial_move) {
|
|
|
+ g_cal.initial_move = false;
|
|
|
+ if (current_sharpness > g_cal.last_sharpness) {
|
|
|
+ LOG_I("[Y] 初始方向正确");
|
|
|
}
|
|
|
+ g_cal.last_sharpness = current_sharpness;
|
|
|
+ calibrate_move();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 计算变化 */
|
|
|
+ double diff_sharpness = fabs(current_sharpness - g_cal.last_sharpness);
|
|
|
+ if (diff_sharpness < 1.0) {
|
|
|
+ g_cal.diff_counter++;
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_I("[Y] 平坦累计:%d 变化量:%.2f", g_cal.diff_counter, diff_sharpness);
|
|
|
+
|
|
|
+ /* 从平坦区域到突变 -> 特征点,保存当前位置 */
|
|
|
+ if (g_cal.diff_counter > 1 && diff_sharpness > 100.0) {
|
|
|
+ device_status.calibrate_slide = 0;
|
|
|
+ device_status.y_init_dis = g_cal.ystep;
|
|
|
+ LOG_I("设置Y轴初始位置:%d", g_cal.ystep);
|
|
|
+ ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
+ g_cal.last_sharpness = current_sharpness;
|
|
|
+
|
|
|
+ /* 继续移动 */
|
|
|
+ calibrate_move();
|
|
|
}
|
|
|
|
|
|
+/* 对焦拍照开始计时时刻(文件级): 供收到关闭LED/触发超时时上报持续时间 */
|
|
|
+static rt_tick_t takephoto_start_tick = 0;
|
|
|
+
|
|
|
+/* 上报对焦拍照持续时间: 在收到关闭LED指令(led_off)或触发超时(拍照超时/focus超时)时调用 */
|
|
|
+void report_takephoto_duration(void)
|
|
|
+{
|
|
|
+ if (takephoto_start_tick == 0) {
|
|
|
+ return; /* 未开始拍照计时或已上报, 不重复上报 */
|
|
|
+ }
|
|
|
+ rt_uint32_t duration_s = (rt_tick_get() - takephoto_start_tick) / RT_TICK_PER_SECOND;
|
|
|
+ LOG_I("[拍照] 结束, 持续时间 %u 秒", duration_s);
|
|
|
+ char duration_str[16];
|
|
|
+ rt_sprintf(duration_str, "%dS", duration_s);
|
|
|
+ char *dur_warn = makeJson_Warn("takephoto_duration", duration_str);
|
|
|
+ if(dur_warn != RT_NULL) {
|
|
|
+ DTU_Write("%s", dur_warn);
|
|
|
+ rt_free(dur_warn);
|
|
|
+ }
|
|
|
+ takephoto_start_tick = 0; /* 复位计时, 防止重复上报 */
|
|
|
+}
|
|
|
|
|
|
//入口函数-电机控制及
|
|
|
static void Ctr_Mortor_entry(void* parameter)
|
|
|
@@ -430,6 +536,13 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
static uint32_t t_takephoto = 0;
|
|
|
while(1)
|
|
|
{
|
|
|
+ /* ========== 对焦拍照持续时间计时 ==========
|
|
|
+ * autotakepic 非0且未在计时: 开始计时;
|
|
|
+ * 上报时机: 收到关闭LED指令(led_off)或触发超时(拍照超时/focus超时)时调用 report_takephoto_duration() 上报 */
|
|
|
+ if((device_status.autotakepic != 0) && (takephoto_start_tick == 0)) {
|
|
|
+ takephoto_start_tick = rt_tick_get();
|
|
|
+ LOG_I("[拍照] 开始计时");
|
|
|
+ }
|
|
|
if(device_status.calibrate_slide==2)
|
|
|
{
|
|
|
device_status.autotakepic=1;
|
|
|
@@ -438,11 +551,45 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
if(device_status.autotakepic==2)
|
|
|
{
|
|
|
t_takephoto++;
|
|
|
- if(t_takephoto==36000)
|
|
|
+ if(t_takephoto==360000)
|
|
|
{
|
|
|
+ ctr_led(OFF);
|
|
|
t_takephoto = 0;
|
|
|
device_status.autotakepic = 0;
|
|
|
+ report_takephoto_duration(); /* 拍照超时, 上报对焦拍照持续时间 */
|
|
|
+ }
|
|
|
+ /* 自动对焦:等待 focus 回复超时则补发指令(最多重试3次),
|
|
|
+ * 防止串口乱码导致 focus 帧丢失而对焦中断 */
|
|
|
+ if (g_af.active &&
|
|
|
+ (rt_tick_get() - g_af.wait_start_tick) >
|
|
|
+ rt_tick_from_millisecond(AF_FOCUS_TIMEOUT_MS))
|
|
|
+ {
|
|
|
+ if (g_af.retry_cnt < AF_FOCUS_RETRY_MAX) {
|
|
|
+ g_af.retry_cnt++;
|
|
|
+ g_af.wait_start_tick = rt_tick_get(); /* 重新计时,等待下一个5s */
|
|
|
+ LOG_W("[对焦] 等待focus超时,补发指令(%d/%d)",
|
|
|
+ g_af.retry_cnt, AF_FOCUS_RETRY_MAX);
|
|
|
+ char *p1 = makeJson_Warn("movy", "4");
|
|
|
+ DTU_Write("%s", p1);
|
|
|
+ rt_free(p1);
|
|
|
+ DTU_Write("{\"cmd\": \"takephoto\" }");
|
|
|
+ } else {
|
|
|
+ LOG_E("[对焦] focus超时,重试%d次无回复,中止对焦", AF_FOCUS_RETRY_MAX);
|
|
|
+ g_af.active = false;
|
|
|
+ device_status.autotakepic = 0;
|
|
|
+ ctr_led(OFF);
|
|
|
+ slidelock();
|
|
|
+ report_takephoto_duration(); /* focus超时中止, 上报对焦拍照持续时间 */
|
|
|
+ }
|
|
|
}
|
|
|
+ /* 自动对焦:有新清晰度值时处理 */
|
|
|
+ if (g_af.sharpness_updated) {
|
|
|
+ auto_focus_process();
|
|
|
+ }
|
|
|
+ /* Y轴校准:有新清晰度值时处理 */
|
|
|
+ if (g_cal.sharpness_updated) {
|
|
|
+ calibrate_process();
|
|
|
+ }
|
|
|
}else {
|
|
|
t_takephoto = 0;
|
|
|
}
|
|
|
@@ -501,7 +648,6 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
DTU_Write("%s",photo);
|
|
|
rt_free(photo);
|
|
|
device_status.autotakepic = 4;
|
|
|
- rt_timer_start(timer_takephoto);
|
|
|
}
|
|
|
}
|
|
|
if(device_status.autotakepic==3)
|
|
|
@@ -528,25 +674,18 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
DTU_Write("%s",photo);
|
|
|
rt_free(photo);
|
|
|
device_status.autotakepic = 4;
|
|
|
- rt_timer_start(timer_takephoto);
|
|
|
}
|
|
|
if(device_status.autotakepic == 4)
|
|
|
{
|
|
|
- device_status.autotakepic = 2;
|
|
|
- g_best_sharpness = 0.0; // 全局最佳清晰度
|
|
|
- g_best_pos = 0; // 全局最佳位置
|
|
|
- g_last_sharpness = 0.0; // 上一次移动后的清晰度
|
|
|
- g_initial_move = true; // 初始方向判断标志
|
|
|
- maxIterations = 100;
|
|
|
- counter_sharpness_diff = 0;
|
|
|
- dir = 1;
|
|
|
- stepSize = 20; // 初始步长(单位:步进电机脉冲数)
|
|
|
- current_pos = 0; // 根据实际类型修改(int/float等)
|
|
|
+ /* 根据当前模式只初始化对应的状态机:
|
|
|
+ * 校准模式(custom calibrate_slide!=0)下不应激活自动对焦的重试检测 */
|
|
|
+ if (device_status.calibrate_slide == 0) {
|
|
|
+ auto_focus_init();
|
|
|
+ } else {
|
|
|
+ calibrate_init();
|
|
|
+ }
|
|
|
slide_para.ystep = 0;
|
|
|
- g_peak_counter = 0;
|
|
|
- g_last_rising = false;
|
|
|
- focus_done = false;
|
|
|
- //启动超时检测定时器
|
|
|
+ device_status.autotakepic = 2;
|
|
|
}
|
|
|
if(device_status.fsl_sw==1)
|
|
|
{
|
|
|
@@ -556,10 +695,10 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
if(device_status.auto_test_sw==1)
|
|
|
{
|
|
|
//开始采集风机持续5S
|
|
|
- ctr_router(ON);
|
|
|
+ ctr_Heat(ON);
|
|
|
rt_thread_delay(1000*60);
|
|
|
//关闭采集风机
|
|
|
- ctr_router(OFF);
|
|
|
+ ctr_Heat(OFF);
|
|
|
//胶带传送
|
|
|
turnlimit();
|
|
|
rt_thread_delay(1000);
|
|
|
@@ -571,6 +710,7 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
DTU_Write(take_start);
|
|
|
rt_free(take_start);
|
|
|
}
|
|
|
+
|
|
|
rt_thread_delay(100);
|
|
|
}
|
|
|
}
|