| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266 |
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-26 Administrator the first version
- */
- #include <rtthread.h>
- #include "hmi.h"
- #include <board.h>
- #include <control.h>
- #include <rtdevice.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stdarg.h"
- #include "string.h"
- #include "sht30_collect.h"
- #include "beep.h"
- #include "easyflash.h"
- #include "sound.h"
- #include "ymodem.h"
- #include <shell.h>
- #include "dtu_uart2.h"
- #include <fal.h>
- #include "selfcheck.h"
- #include <float.h>
- #include <math.h>
- #include "app_flashdb.h"
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_TAG "hmi"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
- #define HMI_THREAD_PRIORITY 5 //线程优先级
- #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
- #define HMI_THREAD_TIMESLICE 10 //时间片
- #define CONTROL_MOTOR_PRIORITY 7
- #define CONTROL_MOTOR_STACK_SIZE 2048
- #define CONTROL_MOTOR_TIMESLICE 5
- static char msg_pool[512];
- static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
- void list_mem(void);
- int mb_master_samlpe();
- /**/
- Device_Status_typedef device_status;
- Device_slide_typedef slide_para;
- time_t time(time_t *t);
- /* 串口接收消息结构*/
- struct rx_msg
- {
- rt_device_t dev;
- rt_size_t size;
- };
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
- ////
- static rt_thread_t hmi1 = RT_NULL;
- static rt_thread_t ctr_motor = RT_NULL;
- rt_device_t serial; /*串口设备句柄*/
- rt_timer_t timer_uart2 = RT_NULL;
- /* 消息队列控制块 */
- static struct rt_messagequeue rx_mq;//接收数据消息队列
- static void HMI_Decode(void);
- static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
- static int HMI_uart4_init(void)
- {
- serial = rt_device_find(UART_NAME);
- /*修改串口配置参数*/
- config.baud_rate = BAUD_RATE_115200; //修改波特率9600
- config.data_bits = DATA_BITS_8; //数据位8
- config.stop_bits = STOP_BITS_1; //停止位1
- #ifdef RT_USING_SERIAL_V2
- config.rx_bufsz = 512;
- config.tx_bufsz = 128;
- #else
- config.bufsz = 512; //缓冲区128字节
- #endif
- config.parity = PARITY_NONE; //无奇偶校验
- /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
- rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
- // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
- //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
- /* 以 DMA 接收及轮询发送模式打开串口设备 */
- #ifdef RT_USING_SERIAL_V2
- rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
- #else
- rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
- #endif
- if(uart_res!=RT_EOK)
- {
- LOG_E("%s打开失败!",UART_NAME);
- }else{
- LOG_I("%s Open Success!",UART_NAME);
- }
- return RT_EOK;
- }
- /*串口屏写数据*/
- void HMI_WRITE(char *fmt, ...)
- {
- va_list ap;
- //末尾需要拼接的3个ff
- const char hmi_hex[3]={0xff,0xff,0xff};
- static char str[MAX_SIZE];
- memset(str,'\0',sizeof(str));
- rt_size_t str_length;
- va_start(ap,fmt);
- str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
- if (str_length > MAX_SIZE - 1)
- str_length = MAX_SIZE - 1;
- rt_device_write(serial, 0, str, str_length);
- va_end(ap);
- rt_device_write(serial, 0,hmi_hex,3);
- }
- /* 接收数据回调函数 */
- static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
- {
- struct rx_msg msg;
- rt_err_t result;
- msg.dev = dev;
- msg.size = size;
- result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
- if ( result == -RT_EFULL)
- {
- /* 消息队列满 */
- LOG_I("hmi message queue full!\n");
- }
- return result;
- }
- /*入口函数*/
- static void Hmi_thread_entry(void *parameter)
- {
- struct rx_msg msg;
- rt_err_t result;
- rt_uint32_t rx_length;
- if(device_status.dtype==3){
- device_status.collt_box_sta = 0;
- device_status.run_sta = 0;
- }
- device_status.camsta = 1;
- while(1)
- {
- rt_memset(&msg, 0, sizeof(msg));
- /* 从消息队列中读取消息*/
- 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';
- //LOG_I("%s",rx_buffer);
- //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
- //HMI_Decode();
- }
- /*********************************************************************************************************************/
- //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);
- //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
- }
- }
- /*******************************************************************************************/
- static void t_uart2_callback(void *parameter)
- {
- HMI_WRITE("sysSet.va0.txt=\"指令超时,请重试...\"");
- LOG_E("t_uart2_callback");
- }
- void Start_timer_Uart2(void)
- {
- if(timer_uart2!=RT_NULL)
- {
- rt_timer_start(timer_uart2);
- }
- else
- {
- LOG_E("timer_uart2 start Failed!");
- }
- }
- /***************************** 解析从屏幕收到的数据 begin **********************************/
- #define EPS 1e-3
- // 找出一维数组中最大数的索引
- int findMaxIndex(double arr[], int size) {
- int maxIndex = 0; // 假设最大值的索引为0
- for (int i = 1; i < size; i++) {
- if ((arr[i] - arr[maxIndex])>EPS) {
- maxIndex = i; // 更新最大值的索引
- }
- }
- return maxIndex;
- }
- void send_signal(int sig)
- {
- rt_thread_kill(ctr_motor, sig);
- }
- /* ========== 自动对焦(Auto 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; /* 信号处理程序写入的新清晰度值 */
- volatile bool fusion_active; /* 景深融合正在进行标志 */
- } auto_focus_t;
- static auto_focus_t g_af;
- /* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
- void thread1_signal_handler(int sig)
- {
- rt_timer_stop(timer_takephoto);
- /* 景深融合期间仍然记录清晰度,但 auto_focus_process 会忽略处理 */
- g_af.sharpness_updated = true;
- g_af.new_sharpness = device_status.focus_value;
- }
- /* 初始化自动对焦状态 */
- 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.dir = 1;
- g_af.step_size = 20;
- g_af.current_pos = 0;
- g_af.max_iterations = 100;
- }
- /* 移动电机一步 */
- 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);
- 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;
- while (slide_para.en_slide != 0) {
- rt_thread_delay(100);
- }
- LOG_I("[对焦] 电机到位");
- rt_timer_start(timer_takephoto);
- }
- /* 景深融合:步进20次后再回到最清晰位置
- * 流程:dir=1步进10次 -> 回到最佳位置 -> dir=0步进10次 -> 回到最佳位置
- * 每次步进后等待清晰度更新再继续
- */
- static void depth_of_field_fusion(void)
- {
- int i;
- int best_pos = g_af.best_pos;
- int step_size = g_af.step_size;
- int wait_count;
- LOG_I("[景深融合] 开始: 最佳位置=%d", best_pos);
- /* ========== 第一阶段:往 dir=1 方向步进 10 次 ========== */
- g_af.dir = 1;
- for (i = 0; i < 10; i++) {
- g_af.current_pos += step_size;
- LOG_I("[景深融合] 正向步进[%d/10]: 位置=%d", i + 1, g_af.current_pos);
- slide_para.pulse_period = mapSpeed(1);
- slide_para.ystep = step_size;
- slide_para.ydir = g_af.dir;
- slide_para.ydis = step_size;
- slide_para.en_slide = 6;
- /* 等待电机到位 */
- while (slide_para.en_slide != 0) {
- rt_thread_delay(100);
- }
- /* 等待清晰度更新 */
- g_af.sharpness_updated = false;
- rt_timer_start(timer_takephoto);
- wait_count = 0;
- while (!g_af.sharpness_updated && wait_count < 200) {
- rt_thread_delay(50);
- wait_count++;
- }
- if (g_af.sharpness_updated) {
- LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
- g_af.sharpness_updated = false;
- } else {
- LOG_W("[景深融合] 等待清晰度超时,继续执行");
- }
- }
- LOG_I("[景深融合] 正向步进完成: 位置=%d", g_af.current_pos);
- /* ========== 第一阶段:回到最清晰位置 ========== */
- if (g_af.current_pos > best_pos) {
- g_af.dir = 0; // 反向移动
- int steps_back = (g_af.current_pos - best_pos) / step_size;
- if ((g_af.current_pos - best_pos) % step_size != 0) {
- steps_back++; // 向上取整
- }
- for (i = 0; i < steps_back && g_af.current_pos > best_pos; i++) {
- g_af.current_pos -= step_size;
- LOG_I("[景深融合] 返回最佳位置[%d/%d]: 位置=%d", i + 1, steps_back, g_af.current_pos);
- slide_para.pulse_period = mapSpeed(1);
- slide_para.ystep = step_size;
- slide_para.ydir = g_af.dir;
- slide_para.ydis = step_size;
- slide_para.en_slide = 6;
- /* 等待电机到位 */
- while (slide_para.en_slide != 0) {
- rt_thread_delay(100);
- }
- /* 等待清晰度更新 */
- g_af.sharpness_updated = false;
- rt_timer_start(timer_takephoto);
- wait_count = 0;
- while (!g_af.sharpness_updated && wait_count < 200) {
- rt_thread_delay(50);
- wait_count++;
- }
- if (g_af.sharpness_updated) {
- LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
- g_af.sharpness_updated = false;
- }
- }
- }
- g_af.current_pos = best_pos;
- LOG_I("[景深融合] 回到正向起点: 位置=%d", g_af.current_pos);
- /* ========== 第二阶段:往 dir=0 方向步进 10 次 ========== */
- g_af.dir = 0;
- for (i = 0; i < 10; i++) {
- g_af.current_pos -= step_size;
- LOG_I("[景深融合] 反向步进[%d/10]: 位置=%d", i + 1, g_af.current_pos);
- slide_para.pulse_period = mapSpeed(1);
- slide_para.ystep = step_size;
- slide_para.ydir = g_af.dir;
- slide_para.ydis = step_size;
- slide_para.en_slide = 6;
- /* 等待电机到位 */
- while (slide_para.en_slide != 0) {
- rt_thread_delay(100);
- }
- /* 等待清晰度更新 */
- g_af.sharpness_updated = false;
- rt_timer_start(timer_takephoto);
- wait_count = 0;
- while (!g_af.sharpness_updated && wait_count < 200) {
- rt_thread_delay(50);
- wait_count++;
- }
- if (g_af.sharpness_updated) {
- LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
- g_af.sharpness_updated = false;
- } else {
- LOG_W("[景深融合] 等待清晰度超时,继续执行");
- }
- }
- LOG_I("[景深融合] 反向步进完成: 位置=%d", g_af.current_pos);
- /* ========== 第二阶段:回到最清晰位置 ========== */
- if (g_af.current_pos < best_pos) {
- g_af.dir = 1; // 正向移动
- int steps_forward = (best_pos - g_af.current_pos) / step_size;
- if ((best_pos - g_af.current_pos) % step_size != 0) {
- steps_forward++; // 向上取整
- }
- for (i = 0; i < steps_forward && g_af.current_pos < best_pos; i++) {
- g_af.current_pos += step_size;
- LOG_I("[景深融合] 返回最佳位置[%d/%d]: 位置=%d", i + 1, steps_forward, g_af.current_pos);
- slide_para.pulse_period = mapSpeed(1);
- slide_para.ystep = step_size;
- slide_para.ydir = g_af.dir;
- slide_para.ydis = step_size;
- slide_para.en_slide = 6;
- /* 等待电机到位 */
- while (slide_para.en_slide != 0) {
- rt_thread_delay(100);
- }
- /* 等待清晰度更新 */
- g_af.sharpness_updated = false;
- rt_timer_start(timer_takephoto);
- wait_count = 0;
- while (!g_af.sharpness_updated && wait_count < 200) {
- rt_thread_delay(50);
- wait_count++;
- }
- if (g_af.sharpness_updated) {
- LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
- g_af.sharpness_updated = false;
- }
- }
- }
- g_af.current_pos = best_pos;
- LOG_I("[景深融合] 完成: 已回到最佳位置=%d", g_af.current_pos);
- /* ========== 发送景深融合完成消息 ========== */
- char *focus_done_msg = makeJson_Warn("focus_done","focus_done");
- DTU_Write("%s", focus_done_msg);
- rt_free(focus_done_msg);
- device_status.autotakepic = 0;
- }
- /* 完成对焦 */
- static void auto_focus_done(void)
- {
- LOG_I("[对焦] 完成! 最佳位置:%d 清晰度:%.2f", g_af.best_pos, g_af.best_sharpness);
- /* 停止 timer_takephoto,避免景深融合期间产生新的清晰度更新 */
- rt_timer_stop(timer_takephoto);
- /* 标记景深融合开始,auto_focus_process 会忽略清晰度处理 */
- g_af.fusion_active = true;
- g_af.sharpness_updated = false;
- /* 执行景深融合 */
- depth_of_field_fusion();
- g_af.fusion_active = false;
- }
- /* 处理一次清晰度值,执行对焦算法 */
- static void auto_focus_process(void)
- {
- /* 景深融合期间不处理对焦 */
- if (g_af.fusion_active) {
- return;
- }
-
- float current_sharpness = g_af.new_sharpness;
- g_af.sharpness_updated = false;
- g_af.max_iterations--;
- 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_af.initial_move) {
- g_af.initial_move = false;
- if (current_sharpness > g_af.last_sharpness + 0.5f) {
- LOG_I("[对焦] 初始方向正确");
- } else {
- g_af.dir = !g_af.dir;
- LOG_I("[对焦] 初始方向错误,反转");
- }
- g_af.last_sharpness = current_sharpness;
- auto_focus_move();
- return;
- }
- /* 计算清晰度变化 */
- float diff = current_sharpness - g_af.last_sharpness;
- /* 动态调整步长 */
- if (fabs(diff) > 3.0f) {
- /* 清晰度大幅变化 -> 缩小步长精细搜索 */
- g_af.step_size = 4;
- 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;
- }
- LOG_I("[对焦] 平坦[%d/10] 步长=%d", g_af.diff_counter, g_af.step_size);
- } else {
- g_af.diff_counter = 0;
- }
- /* 方向判断 */
- bool need_reverse = false;
- if (diff > 0.5f) {
- /* 上升:保持方向,缩小步长逼近峰值 */
- g_af.reduce_counter = 0;
- g_af.last_rising = true;
- if (g_af.step_size > 4) g_af.step_size -= 2;
- LOG_I("[对焦] 上升↑ 步长=%d", g_af.step_size);
- } else if (diff < -0.3f) {
- /* 下降:累计,超过阈值反转方向 */
- g_af.reduce_counter++;
- g_af.last_rising = false;
- LOG_I("[对焦] 下降↓[%d/3]", g_af.reduce_counter);
- if (g_af.reduce_counter >= 3) {
- need_reverse = true;
- g_af.reduce_counter = 0;
- g_af.peak_counter++;
- LOG_I("[对焦] 峰值检测[%d/3]", g_af.peak_counter);
- }
- } else {
- g_af.reduce_counter = 0;
- LOG_I("[对焦] 平稳=");
- }
- g_af.last_sharpness = current_sharpness;
- if (need_reverse) {
- g_af.dir = !g_af.dir;
- LOG_I("[对焦] 反转方向");
- }
- /* 平坦区域超时:强制反转并增大步长 */
- 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 >= 3 && current_sharpness > g_af.best_sharpness * 0.95f) {
- done = true;
- LOG_I("[对焦] 峰值确认完成");
- }
- if (g_af.max_iterations <= 0) {
- done = true;
- 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);
- g_cal.sharpness_updated = true;
- g_cal.new_sharpness = device_status.focus_value;
- }
- /* 初始化校准状态 */
- 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;
- }
- /* 移动电机一步 */
- 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);
- rt_timer_start(timer_takephoto);
- }
- /* 处理一次校准清晰度值 */
- 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();
- }
- //入口函数-电机控制及
- static void Ctr_Mortor_entry(void* parameter)
- {
- /*孢子仪2.0*/
- rt_signal_install(SIGUSR1, thread1_signal_handler); //自动对焦信号
- rt_signal_install(SIGUSR2, thread2_signal_handler); //坐标校准信号
- rt_signal_unmask(SIGUSR1);
- rt_signal_unmask(SIGUSR2);
- static uint32_t t_takephoto = 0;
- while(1)
- {
- if(device_status.calibrate_slide==2)
- {
- device_status.autotakepic=1;
- device_status.calibrate_slide = 20;
- }
- if(device_status.autotakepic==2)
- {
- t_takephoto++;
- if(t_takephoto==18000)
- {
- t_takephoto = 0;
- device_status.autotakepic = 0;
- }
- /* 自动对焦:有新清晰度值时处理 */
- if (g_af.sharpness_updated) {
- auto_focus_process();
- }
- /* Y轴校准:有新清晰度值时处理 */
- if (g_cal.sharpness_updated) {
- calibrate_process();
- }
- }else {
- t_takephoto = 0;
- }
- /*对焦拍照,滑台初始化*/
- if(device_status.autotakepic==1)
- {
- device_status.step_focus = 1;
- slide_para.en_slide = 7; //Y轴复位
- slide_para.pulse_period = mapSpeed(50);
- while(slide_para.en_slide!=0){
- LOG_D("等待Y轴电机完成复位!");
- rt_thread_delay(1000);
- }
- if(device_status.calibrate_slide==0){
- device_status.autotakepic = 3;
- }else{
- slide_para.en_slide = 7; //Y轴复位
- slide_para.pulse_period = mapSpeed(50);
- while(slide_para.en_slide!=0){
- LOG_D("等待Y轴电机完成复位!");
- rt_thread_delay(1000);
- }
- rt_thread_delay(1000);
- //生成JSON指令
- char *photo = makeJson_Warn("mov_init_y","takephoto_done");
- DTU_Write("%s",photo);
- rt_free(photo);
- device_status.autotakepic = 4;
- rt_timer_start(timer_takephoto);
- }
- }
- if(device_status.autotakepic==3)
- {
- slide_para.en_slide = 7; //Y轴复位
- slide_para.pulse_period = mapSpeed(50);
- while(slide_para.en_slide!=0){
- LOG_D("等待Y轴电机完成复位!");
- rt_thread_delay(1000);
- }
- rt_thread_delay(1000);
- //Y轴电机运行到指定距离
- slide_para.ydir = 1;
- //slide_para.ydis = 3350;
- slide_para.ydis = device_status.y_init_dis; //9000
- slide_para.pulse_period = mapSpeed(50);
- slide_para.en_slide = 6;
- while(slide_para.en_slide!=0){
- LOG_D("Again:等待y轴电机到位!");
- rt_thread_delay(1000);
- }
- //生成JSON指令
- char *photo = makeJson_Warn("mov_init_y","takephoto_done");
- 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;
- auto_focus_init();
- calibrate_init();
- slide_para.ystep = 0;
- }
- if(device_status.fsl_sw==1)
- {
- ctr_Petroleum_jelly(10);
- }
- //一键测试
- if(device_status.auto_test_sw==1)
- {
- //开始采集风机持续5S
- ctr_router(ON);
- rt_thread_delay(1000*60);
- //关闭采集风机
- ctr_router(OFF);
- //胶带传送
- turnlimit();
- rt_thread_delay(1000);
- turnlimit(); //再转
- //滑台归位,自动对焦
- //拍一张照片上传到平台
- device_status.autotakepic=1;
- device_status.auto_test_sw=0;
- char *take_start = makeJson_Warn("start", "autofocus");
- DTU_Write(take_start);
- rt_free(take_start);
- }
- rt_thread_delay(100);
- }
- }
- /*屏幕写数据*/
- int Hmi_Fresh(void)
- {
- #if 0
- //初始化串口4
- HMI_uart4_init();
- rt_err_t ret = RT_EOK;
- /* 初始化消息队列 */
- rt_mq_init(&rx_mq, "rx_mq",
- msg_pool, /* 存放消息的缓冲区 */
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- /* 设置接收回调函数 */
- rt_device_set_rx_indicate(serial, uart_input);
- /* 创建线程*/
- hmi1 = rt_thread_create("Hmi_Fresh_thread",
- Hmi_thread_entry,
- RT_NULL,
- HMI_THREAD_STACK_SIZE,
- HMI_THREAD_PRIORITY,
- HMI_THREAD_TIMESLICE);
- /* 如果获得线程控制块,启动这个线程 */
- if (hmi1 != RT_NULL)
- {
- rt_thread_startup(hmi1);
- }
- else
- {
- LOG_E("rt_thread_startup hmi1 failed!");
- }
- #endif
- /*部分控制的线程*/
- ctr_motor = rt_thread_create("Ctr motor",
- Ctr_Mortor_entry,
- RT_NULL,
- CONTROL_MOTOR_STACK_SIZE,
- CONTROL_MOTOR_PRIORITY,
- CONTROL_MOTOR_TIMESLICE);
- if(ctr_motor != RT_NULL)
- {
- rt_thread_startup(ctr_motor);
- }
- //定时器,串口通信超时监测20S
- timer_uart2 = rt_timer_create("t_uart2",t_uart2_callback,RT_NULL, 20000, RT_TIMER_FLAG_ONE_SHOT);
- if(timer_uart2==RT_NULL)
- {
- LOG_E("rt_timer_create timer_uart2 failed!");
- }
- return 0;
- }
- void Check_ParamConfig(void)
- {
- time_t now = time(RT_NULL);
- static struct tm tm;
- gmtime_r(&now, &tm);
- if(easyflash_init()==EF_NO_ERR) //初始化成功
- {
- ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
- ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
- ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
- device_status.rebootcnt++;
- ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
- LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
- }
- else {
- LOG_E("easyflash init ERRPR!!!");
- }
- //保存默认配置
- if(device_status.iSave!=FLASH_FLAG)
- {
- device_status.iSave=FLASH_FLAG;
- /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
- device_status.dtype = 7;
- device_status.version_type = 7; //4.0
- device_status.Power_Type = 0; //0-交流供电;1-直流供电
- device_status.tph = 60; //高温保护阈值
- device_status.tpl = 10; //低温保护阈值
- device_status.dattim = 10; //数据上传间隔
- device_status.ds = 1; //设备状态
- device_status.update_flag = 0;
- /*孢子仪*/
- device_status.collecting_sh1 = 9;
- device_status.collecting_eh1 = 10;
- device_status.collecting_sh2 = 14;
- device_status.collecting_eh2 = 16;
- device_status.cold_sw = 0;
- device_status.heat_sw =0;
- device_status.cul_time = 1;
- device_status.run_sta = 0;
- sprintf(device_status.driver, "%s",VERSION_BZY);
- sprintf(device_status.deviceId,"0");
- rt_sprintf(device_status.tempture, "-15");
- rt_sprintf(device_status.humidity, "31");
- rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
- device_status.x_init_dis = 500;
- device_status.y_init_dis = 6000;
- device_status.tape_used = 0;
- ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
- ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
- ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
- ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
- ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
- ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
- ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
- ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
- ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
- ef_set_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3)); //采集时间
- ef_set_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3)); //采集时间
- ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
- ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
- ef_set_env_blob("x_init_dis", &device_status.x_init_dis, sizeof(device_status.x_init_dis));
- ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
- ef_set_and_save_env("collt_min_sh1", "0");
- ef_set_and_save_env("collt_min_eh1", "0");
- ef_set_and_save_env("collt_min_sh2", "0");
- ef_set_and_save_env("collt_min_eh2", "0");
- ef_set_and_save_env("collt_min_sh3", "0");
- ef_set_and_save_env("collt_min_eh3", "0");
- LOG_I("SOFTWARE VERSION:%s",device_status.driver);
- LOG_I("Default paramconf is:");
- LOG_I("开机次数:%d",device_status.rebootcnt);
- LOG_I("高温保护:%d",device_status.tph);
- LOG_I("低温保护:%d",device_status.tpl);
- LOG_I("数据上传间隔:%d",device_status.dattim);
- LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
- LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
- device_status.collecting_sh1,device_status.collt_min_sh1,
- device_status.collecting_eh1,device_status.collt_min_eh1,
- device_status.collecting_sh2,device_status.collt_min_sh2,
- device_status.collecting_eh2,device_status.collt_min_eh2,
- device_status.collecting_sh3,device_status.collt_min_sh3,
- device_status.collecting_eh3,device_status.collt_min_eh3);
- LOG_I("PowerType:%d",device_status.Power_Type);
- LOG_I("x_init_dis:%d",device_status.x_init_dis);
- LOG_I("y_init_dis:%d",device_status.y_init_dis);
- }//从easyflash读取参数配置
- else if (device_status.iSave==FLASH_FLAG) {
- ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
- ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
- ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
- ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
- ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
- ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
- ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
- ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
- ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
- ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
- ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
- ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
- ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
- ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
- ef_get_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1),NULL); //采集开始1
- ef_get_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1),NULL); //采集结束1
- ef_get_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2),NULL); //采集开始2
- ef_get_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2),NULL); //采集结束2
- ef_get_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3),NULL); //采集开始3
- ef_get_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3),NULL); //采集结束3
- ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
- ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
- ef_get_env_blob("x_init_dis",&device_status.x_init_dis,sizeof(device_status.x_init_dis),NULL);
- ef_get_env_blob("y_init_dis",&device_status.y_init_dis,sizeof(device_status.y_init_dis),NULL);
- ef_get_env_blob("collt_min_sh1", &device_status.collt_min_sh1, sizeof(device_status.collt_min_sh1), NULL);
- ef_get_env_blob("collt_min_eh1", &device_status.collt_min_eh1, sizeof(device_status.collt_min_eh1), NULL);
- ef_get_env_blob("collt_min_sh2", &device_status.collt_min_sh2, sizeof(device_status.collt_min_sh2), NULL);
- ef_get_env_blob("collt_min_eh2", &device_status.collt_min_eh2, sizeof(device_status.collt_min_eh2), NULL);
- ef_get_env_blob("collt_min_sh3", &device_status.collt_min_sh3, sizeof(device_status.collt_min_sh3), NULL);
- ef_get_env_blob("collt_min_eh3", &device_status.collt_min_eh3, sizeof(device_status.collt_min_eh3), NULL);
- //光控计数
- device_status.work_done = 0;
- //工作完成标志位
- if(device_status.dattim==0)
- {
- device_status.dattim = 10;
- }
- /*设备类型,7孢子仪*/
- device_status.dtype = 7;
- device_status.version_type = 7;
- if (device_status.version_type==7) {
- rt_sprintf(device_status.driver, "%s",VERSION_BZY);
- //异常处理:20230331,修复第一段开始时间等于第二段结束时间的情况
- if(device_status.collecting_eh1==device_status.collecting_eh2&&device_status.collecting_sh2!=device_status.collecting_eh2)
- {
- device_status.collecting_eh2 = device_status.collecting_sh2 + 2;
- }
- }
- app_flashdb_init();
- LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
- LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
- LOG_I("开机次数:%d",device_status.rebootcnt);
- LOG_I("高温保护:%d",device_status.tph);
- LOG_I("低温保护:%d",device_status.tpl);
- LOG_I("数据上传间隔:%d",device_status.dattim);
- LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
- LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
- device_status.collecting_sh1,device_status.collt_min_sh1,
- device_status.collecting_eh1,device_status.collt_min_eh1,
- device_status.collecting_sh2,device_status.collt_min_sh2,
- device_status.collecting_eh2,device_status.collt_min_eh2,
- device_status.collecting_sh3,device_status.collt_min_sh3,
- device_status.collecting_eh3,device_status.collt_min_eh3);
- LOG_I("PowerType:%d",device_status.Power_Type);
- LOG_I("x_init_dis:%d",device_status.x_init_dis);
- LOG_I("y_init_dis:%d",device_status.y_init_dis);
- }
- //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
- {
- device_status.update_flag = 0;
- //初始化联网状态
- rt_sprintf(device_status.net,"1");
- rt_sprintf(device_status.csq,"0");
- rt_sprintf(device_status.sim,"1");
- rt_sprintf(device_status.gps, "0");
- Hmi_Fresh();
- dtu_data_play();
- sht30_collect();
- slide_Fresh();
- ctr_photo(ON);
- #if SENSOR_EN==1
- mb_master_samlpe();
- #endif
- #if ROBOT_EN == 0
- uint16_t connect_num=0;
- //等待联网,
- while(atoi(device_status.net)==1)
- {
- connect_num++;
- LOG_I("联网中......%d",connect_num);
- //超时时间为100S
- if(connect_num==60)
- {
- LOG_W("Time not set:%04d-%02d-%02d %02d:%02d:%02d",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
- break;
- }
- rt_thread_delay(2000);
- Beep_Tip(100);
- }
- connect_num = 0;
- rt_thread_delay(2000);
- DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
- rt_thread_delay(1000);
- UpLoad_Data();
- Workstatus_check();
- Work_Loop_Task();
- #endif
- }
- }
- void ResetParamDefault(void)
- {
- device_status.dtype = 7;
- device_status.ds = 1; //设备状态开机
- device_status.thsp = 300;
- device_status.dattim = 10;
- device_status.collecting_sh1 = 9;
- device_status.collecting_eh1 = 17;
- device_status.collecting_sh2 = 00;
- device_status.collecting_eh2 = 00;
- device_status.collecting_sh3 = 00;
- device_status.collecting_eh3 = 00;
- device_status.collt_min_sh1 = 0;
- device_status.collt_min_eh1 = 0;
- device_status.collt_min_sh2 = 0;
- device_status.collt_min_eh2 = 0;
- device_status.collt_min_sh3 = 0;
- device_status.collt_min_eh3 = 0;
- device_status.Power_Type = 0;
- device_status.tph = 70; //高温保护阈值
- device_status.tpl = 10; //低温保护阈值
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
- ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
- ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
- device_status.run_sta = 0;
- device_status.heatvalue = 25; //培养温度
- ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
- ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
- ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
- ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
- ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
- ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
- ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
- ef_set_and_save_env("collt_min_sh1", "0");
- ef_set_and_save_env("collt_min_eh1", "0");
- ef_set_and_save_env("collt_min_sh2", "0");
- ef_set_and_save_env("collt_min_eh2", "0");
- ef_set_and_save_env("collt_min_sh3", "0");
- ef_set_and_save_env("collt_min_eh3", "0");
- ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
- rt_thread_delay(1000);
- }
- void ResetParamDefault_DC(void)
- {
- //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
- rt_sprintf(device_status.tempture,"%d",-15);
- device_status.dattim = 10; //数据上传间隔
- device_status.tph = 70; //高温保护阈值
- device_status.tpl = 10; //低温保护阈值
- device_status.ds = 1; //设备状态开机
- device_status.collecting_sh1 = 9;
- device_status.collecting_eh1 = 17;
- device_status.collecting_sh2 = 00;
- device_status.collecting_eh2 = 00;
- device_status.collecting_sh3 = 00;
- device_status.collecting_eh3 = 00;
- device_status.collt_min_sh1 = 0;
- device_status.collt_min_eh1 = 0;
- device_status.collt_min_sh2 = 0;
- device_status.collt_min_eh2 = 0;
- device_status.collt_min_sh3 = 0;
- device_status.collt_min_eh3 = 0;
- device_status.Power_Type = 1;
- ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
- ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
- ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
- ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
- ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
- ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
- ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
- ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
- ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
- ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
- ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
- ef_set_and_save_env("collt_min_sh1", "0");
- ef_set_and_save_env("collt_min_eh1", "0");
- ef_set_and_save_env("collt_min_sh2", "0");
- ef_set_and_save_env("collt_min_eh2", "0");
- ef_set_and_save_env("collt_min_sh3", "0");
- ef_set_and_save_env("collt_min_eh3", "0");
- rt_thread_delay(1000);
- }
- //固件备份
- void app_partion_backup(void)
- {
- rt_kprintf("固件恢复!!!\n");
- const char *dst_part_name = "df_area";
- const char *src_part_name = "app";
- const struct fal_partition *dst_part;
- const struct fal_partition *src_part;
- dst_part = fal_partition_find(dst_part_name);
- src_part = fal_partition_find(src_part_name);
- if (dst_part == RT_NULL || src_part == RT_NULL)
- {
- if (dst_part == RT_NULL)
- rt_kprintf("%s partition is not exist!\n", dst_part_name);
- if (src_part == RT_NULL)
- rt_kprintf("%s partition is not exist!\n", src_part_name);
- }
- else
- {
- rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
- if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
- {
- int clone_pos = 0;
- int clone_len = 0, clone_tol_len;
- rt_uint8_t *buf = rt_malloc(4096);
- if (dst_part->len < src_part->len)
- clone_tol_len = dst_part->len;
- else
- clone_tol_len = src_part->len;
- while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
- {
- clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
- if (clone_len < 0)
- {
- rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
- break;
- }
- if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
- {
- rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
- break;
- }
- HMI_WRITE("#");
- rt_kprintf("#");
- clone_pos += clone_len;
- }
- if (clone_pos >= clone_tol_len)
- {
- rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
- }
- else
- {
- rt_kprintf("Clone partition failed!");
- }
- if (buf)
- rt_free(buf);
- }
- }
- }
- void Debug_log_ON(void)
- {
- rt_console_set_device("uart4");
- finsh_set_device("uart4");
- }
- void Debug_log_OFF(void)
- {
- rt_console_set_device("uart1");
- finsh_set_device("uart1");
- rt_mq_init(&rx_mq, "rx_mq",
- msg_pool, /* 存放消息的缓冲区 */
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
- rt_device_set_rx_indicate(serial, uart_input);
- hmi1 = rt_thread_create("Hmi_Fresh_thread",
- Hmi_thread_entry,
- RT_NULL,
- HMI_THREAD_STACK_SIZE,
- HMI_THREAD_PRIORITY,
- HMI_THREAD_TIMESLICE);
- if (hmi1 != RT_NULL)
- {
- rt_thread_startup(hmi1);
- }
- }
- int delete_thread_ctr_motor()
- {
- if(ctr_motor!=RT_NULL)
- {
- rt_thread_delete(ctr_motor);
- }
- }
- MSH_CMD_EXPORT(ResetParamDefault,ResetParamDefault);
- MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
- MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
- //
- //INIT_BOARD_EXPORT(HMI_uart4_init);
|