hmi.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-26 Administrator the first version
  9. */
  10. #include <rtthread.h>
  11. #include "hmi.h"
  12. #include <board.h>
  13. #include <control.h>
  14. #include <rtdevice.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "stdarg.h"
  19. #include "string.h"
  20. #include "sht30_collect.h"
  21. #include "beep.h"
  22. #include "easyflash.h"
  23. #include "sound.h"
  24. #include "ymodem.h"
  25. #include <shell.h>
  26. #include "dtu_uart2.h"
  27. #include <fal.h>
  28. #include "selfcheck.h"
  29. #include <float.h>
  30. #include <math.h>
  31. #include "app_flashdb.h"
  32. #define DBG_ENABLE
  33. #define DBG_COLOR
  34. #define DBG_TAG "hmi"
  35. #define DBG_LEVEL DBG_LOG
  36. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  37. #define HMI_THREAD_PRIORITY 5 //线程优先级
  38. #define HMI_THREAD_STACK_SIZE 2048 //线程栈空间大小
  39. #define HMI_THREAD_TIMESLICE 10 //时间片
  40. #define CONTROL_MOTOR_PRIORITY 7
  41. #define CONTROL_MOTOR_STACK_SIZE 2048
  42. #define CONTROL_MOTOR_TIMESLICE 5
  43. static char msg_pool[512];
  44. static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
  45. void list_mem(void);
  46. int mb_master_samlpe();
  47. /**/
  48. Device_Status_typedef device_status;
  49. Device_slide_typedef slide_para;
  50. time_t time(time_t *t);
  51. /* 串口接收消息结构*/
  52. struct rx_msg
  53. {
  54. rt_device_t dev;
  55. rt_size_t size;
  56. };
  57. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
  58. ////
  59. static rt_thread_t hmi1 = RT_NULL;
  60. static rt_thread_t ctr_motor = RT_NULL;
  61. rt_device_t serial; /*串口设备句柄*/
  62. rt_timer_t timer_uart2 = RT_NULL;
  63. /* 消息队列控制块 */
  64. static struct rt_messagequeue rx_mq;//接收数据消息队列
  65. static void HMI_Decode(void);
  66. static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
  67. static int HMI_uart4_init(void)
  68. {
  69. serial = rt_device_find(UART_NAME);
  70. /*修改串口配置参数*/
  71. config.baud_rate = BAUD_RATE_115200; //修改波特率9600
  72. config.data_bits = DATA_BITS_8; //数据位8
  73. config.stop_bits = STOP_BITS_1; //停止位1
  74. #ifdef RT_USING_SERIAL_V2
  75. config.rx_bufsz = 512;
  76. config.tx_bufsz = 128;
  77. #else
  78. config.bufsz = 512; //缓冲区128字节
  79. #endif
  80. config.parity = PARITY_NONE; //无奇偶校验
  81. /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  82. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  83. // /* step4:打开串口设备。以中断接收及轮询发送模式打开串口设备 */
  84. //rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  85. /* 以 DMA 接收及轮询发送模式打开串口设备 */
  86. #ifdef RT_USING_SERIAL_V2
  87. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
  88. #else
  89. rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
  90. #endif
  91. if(uart_res!=RT_EOK)
  92. {
  93. LOG_E("%s打开失败!",UART_NAME);
  94. }else{
  95. LOG_I("%s Open Success!",UART_NAME);
  96. }
  97. return RT_EOK;
  98. }
  99. /*串口屏写数据*/
  100. void HMI_WRITE(char *fmt, ...)
  101. {
  102. va_list ap;
  103. //末尾需要拼接的3个ff
  104. const char hmi_hex[3]={0xff,0xff,0xff};
  105. static char str[MAX_SIZE];
  106. memset(str,'\0',sizeof(str));
  107. rt_size_t str_length;
  108. va_start(ap,fmt);
  109. str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
  110. if (str_length > MAX_SIZE - 1)
  111. str_length = MAX_SIZE - 1;
  112. rt_device_write(serial, 0, str, str_length);
  113. va_end(ap);
  114. rt_device_write(serial, 0,hmi_hex,3);
  115. }
  116. /* 接收数据回调函数 */
  117. static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
  118. {
  119. struct rx_msg msg;
  120. rt_err_t result;
  121. msg.dev = dev;
  122. msg.size = size;
  123. result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
  124. if ( result == -RT_EFULL)
  125. {
  126. /* 消息队列满 */
  127. LOG_I("hmi message queue full!\n");
  128. }
  129. return result;
  130. }
  131. /*入口函数*/
  132. static void Hmi_thread_entry(void *parameter)
  133. {
  134. struct rx_msg msg;
  135. rt_err_t result;
  136. rt_uint32_t rx_length;
  137. if(device_status.dtype==3){
  138. device_status.collt_box_sta = 0;
  139. device_status.run_sta = 0;
  140. }
  141. device_status.camsta = 1;
  142. while(1)
  143. {
  144. rt_memset(&msg, 0, sizeof(msg));
  145. /* 从消息队列中读取消息*/
  146. result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
  147. if (result == RT_EOK)
  148. {
  149. rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
  150. rx_buffer[rx_length] = '\0';
  151. //LOG_I("%s",rx_buffer);
  152. //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
  153. //HMI_Decode();
  154. }
  155. /*********************************************************************************************************************/
  156. //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);
  157. //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
  158. }
  159. }
  160. /*******************************************************************************************/
  161. static void t_uart2_callback(void *parameter)
  162. {
  163. HMI_WRITE("sysSet.va0.txt=\"指令超时,请重试...\"");
  164. LOG_E("t_uart2_callback");
  165. }
  166. void Start_timer_Uart2(void)
  167. {
  168. if(timer_uart2!=RT_NULL)
  169. {
  170. rt_timer_start(timer_uart2);
  171. }
  172. else
  173. {
  174. LOG_E("timer_uart2 start Failed!");
  175. }
  176. }
  177. /***************************** 解析从屏幕收到的数据 begin **********************************/
  178. #define EPS 1e-3
  179. // 找出一维数组中最大数的索引
  180. int findMaxIndex(double arr[], int size) {
  181. int maxIndex = 0; // 假设最大值的索引为0
  182. for (int i = 1; i < size; i++) {
  183. if ((arr[i] - arr[maxIndex])>EPS) {
  184. maxIndex = i; // 更新最大值的索引
  185. }
  186. }
  187. return maxIndex;
  188. }
  189. void send_signal(int sig)
  190. {
  191. rt_thread_kill(ctr_motor, sig);
  192. }
  193. /* ========== 自动对焦(Auto Focus)控制块 ========== */
  194. typedef struct {
  195. float best_sharpness; /* 全局最佳清晰度 */
  196. int best_pos; /* 全局最佳位置 */
  197. float last_sharpness; /* 上一次移动后的清晰度 */
  198. int dir; /* 行进方向: 1=正向, 0=反向 */
  199. int step_size; /* 步进电机脉冲数 */
  200. int current_pos; /* 当前位置 */
  201. int peak_counter; /* 连续上升后下降的峰值计数 */
  202. int reduce_counter; /* 清晰度连续降低计数 */
  203. int diff_counter; /* 清晰度变化微小累计次数 */
  204. bool last_rising; /* 上次是否上升 */
  205. int max_iterations; /* 最大迭代次数 */
  206. bool initial_move; /* 初始方向判断标志 */
  207. volatile bool sharpness_updated; /* 信号处理程序置位: 有新清晰度值 */
  208. float new_sharpness; /* 信号处理程序写入的新清晰度值 */
  209. volatile bool fusion_active; /* 景深融合正在进行标志 */
  210. } auto_focus_t;
  211. static auto_focus_t g_af;
  212. /* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
  213. void thread1_signal_handler(int sig)
  214. {
  215. rt_timer_stop(timer_takephoto);
  216. /* 景深融合期间仍然记录清晰度,但 auto_focus_process 会忽略处理 */
  217. g_af.sharpness_updated = true;
  218. g_af.new_sharpness = device_status.focus_value;
  219. }
  220. /* 初始化自动对焦状态 */
  221. static void auto_focus_init(void)
  222. {
  223. memset(&g_af, 0, sizeof(g_af));
  224. g_af.best_sharpness = -FLT_MAX;
  225. g_af.last_sharpness = 0.0f;
  226. g_af.initial_move = true;
  227. g_af.dir = 1;
  228. g_af.step_size = 20;
  229. g_af.current_pos = 0;
  230. g_af.max_iterations = 100;
  231. }
  232. /* 移动电机一步 */
  233. static void auto_focus_move(void)
  234. {
  235. g_af.current_pos += g_af.dir ? g_af.step_size : -g_af.step_size;
  236. LOG_I("[对焦] 移动:方向=%d 步长=%d 位置=%d", g_af.dir, g_af.step_size, g_af.current_pos);
  237. slide_para.pulse_period = mapSpeed(1);
  238. slide_para.ystep = g_af.step_size;
  239. slide_para.ydir = g_af.dir;
  240. slide_para.ydis = g_af.step_size;
  241. slide_para.en_slide = 6;
  242. while (slide_para.en_slide != 0) {
  243. rt_thread_delay(100);
  244. }
  245. LOG_I("[对焦] 电机到位");
  246. rt_timer_start(timer_takephoto);
  247. }
  248. /* 景深融合:步进20次后再回到最清晰位置
  249. * 流程:dir=1步进10次 -> 回到最佳位置 -> dir=0步进10次 -> 回到最佳位置
  250. * 每次步进后等待清晰度更新再继续
  251. */
  252. static void depth_of_field_fusion(void)
  253. {
  254. int i;
  255. int best_pos = g_af.best_pos;
  256. int step_size = g_af.step_size;
  257. int wait_count;
  258. LOG_I("[景深融合] 开始: 最佳位置=%d", best_pos);
  259. /* ========== 第一阶段:往 dir=1 方向步进 10 次 ========== */
  260. g_af.dir = 1;
  261. for (i = 0; i < 10; i++) {
  262. g_af.current_pos += step_size;
  263. LOG_I("[景深融合] 正向步进[%d/10]: 位置=%d", i + 1, g_af.current_pos);
  264. slide_para.pulse_period = mapSpeed(1);
  265. slide_para.ystep = step_size;
  266. slide_para.ydir = g_af.dir;
  267. slide_para.ydis = step_size;
  268. slide_para.en_slide = 6;
  269. /* 等待电机到位 */
  270. while (slide_para.en_slide != 0) {
  271. rt_thread_delay(100);
  272. }
  273. /* 等待清晰度更新 */
  274. g_af.sharpness_updated = false;
  275. rt_timer_start(timer_takephoto);
  276. wait_count = 0;
  277. while (!g_af.sharpness_updated && wait_count < 200) {
  278. rt_thread_delay(50);
  279. wait_count++;
  280. }
  281. if (g_af.sharpness_updated) {
  282. LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
  283. g_af.sharpness_updated = false;
  284. } else {
  285. LOG_W("[景深融合] 等待清晰度超时,继续执行");
  286. }
  287. }
  288. LOG_I("[景深融合] 正向步进完成: 位置=%d", g_af.current_pos);
  289. /* ========== 第一阶段:回到最清晰位置 ========== */
  290. if (g_af.current_pos > best_pos) {
  291. g_af.dir = 0; // 反向移动
  292. int steps_back = (g_af.current_pos - best_pos) / step_size;
  293. if ((g_af.current_pos - best_pos) % step_size != 0) {
  294. steps_back++; // 向上取整
  295. }
  296. for (i = 0; i < steps_back && g_af.current_pos > best_pos; i++) {
  297. g_af.current_pos -= step_size;
  298. LOG_I("[景深融合] 返回最佳位置[%d/%d]: 位置=%d", i + 1, steps_back, g_af.current_pos);
  299. slide_para.pulse_period = mapSpeed(1);
  300. slide_para.ystep = step_size;
  301. slide_para.ydir = g_af.dir;
  302. slide_para.ydis = step_size;
  303. slide_para.en_slide = 6;
  304. /* 等待电机到位 */
  305. while (slide_para.en_slide != 0) {
  306. rt_thread_delay(100);
  307. }
  308. /* 等待清晰度更新 */
  309. g_af.sharpness_updated = false;
  310. rt_timer_start(timer_takephoto);
  311. wait_count = 0;
  312. while (!g_af.sharpness_updated && wait_count < 200) {
  313. rt_thread_delay(50);
  314. wait_count++;
  315. }
  316. if (g_af.sharpness_updated) {
  317. LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
  318. g_af.sharpness_updated = false;
  319. }
  320. }
  321. }
  322. g_af.current_pos = best_pos;
  323. LOG_I("[景深融合] 回到正向起点: 位置=%d", g_af.current_pos);
  324. /* ========== 第二阶段:往 dir=0 方向步进 10 次 ========== */
  325. g_af.dir = 0;
  326. for (i = 0; i < 10; i++) {
  327. g_af.current_pos -= step_size;
  328. LOG_I("[景深融合] 反向步进[%d/10]: 位置=%d", i + 1, g_af.current_pos);
  329. slide_para.pulse_period = mapSpeed(1);
  330. slide_para.ystep = step_size;
  331. slide_para.ydir = g_af.dir;
  332. slide_para.ydis = step_size;
  333. slide_para.en_slide = 6;
  334. /* 等待电机到位 */
  335. while (slide_para.en_slide != 0) {
  336. rt_thread_delay(100);
  337. }
  338. /* 等待清晰度更新 */
  339. g_af.sharpness_updated = false;
  340. rt_timer_start(timer_takephoto);
  341. wait_count = 0;
  342. while (!g_af.sharpness_updated && wait_count < 200) {
  343. rt_thread_delay(50);
  344. wait_count++;
  345. }
  346. if (g_af.sharpness_updated) {
  347. LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
  348. g_af.sharpness_updated = false;
  349. } else {
  350. LOG_W("[景深融合] 等待清晰度超时,继续执行");
  351. }
  352. }
  353. LOG_I("[景深融合] 反向步进完成: 位置=%d", g_af.current_pos);
  354. /* ========== 第二阶段:回到最清晰位置 ========== */
  355. if (g_af.current_pos < best_pos) {
  356. g_af.dir = 1; // 正向移动
  357. int steps_forward = (best_pos - g_af.current_pos) / step_size;
  358. if ((best_pos - g_af.current_pos) % step_size != 0) {
  359. steps_forward++; // 向上取整
  360. }
  361. for (i = 0; i < steps_forward && g_af.current_pos < best_pos; i++) {
  362. g_af.current_pos += step_size;
  363. LOG_I("[景深融合] 返回最佳位置[%d/%d]: 位置=%d", i + 1, steps_forward, g_af.current_pos);
  364. slide_para.pulse_period = mapSpeed(1);
  365. slide_para.ystep = step_size;
  366. slide_para.ydir = g_af.dir;
  367. slide_para.ydis = step_size;
  368. slide_para.en_slide = 6;
  369. /* 等待电机到位 */
  370. while (slide_para.en_slide != 0) {
  371. rt_thread_delay(100);
  372. }
  373. /* 等待清晰度更新 */
  374. g_af.sharpness_updated = false;
  375. rt_timer_start(timer_takephoto);
  376. wait_count = 0;
  377. while (!g_af.sharpness_updated && wait_count < 200) {
  378. rt_thread_delay(50);
  379. wait_count++;
  380. }
  381. if (g_af.sharpness_updated) {
  382. LOG_I("[景深融合] 收到清晰度:%.2f", g_af.new_sharpness);
  383. g_af.sharpness_updated = false;
  384. }
  385. }
  386. }
  387. g_af.current_pos = best_pos;
  388. LOG_I("[景深融合] 完成: 已回到最佳位置=%d", g_af.current_pos);
  389. /* ========== 发送景深融合完成消息 ========== */
  390. char *focus_done_msg = makeJson_Warn("focus_done","focus_done");
  391. DTU_Write("%s", focus_done_msg);
  392. rt_free(focus_done_msg);
  393. device_status.autotakepic = 0;
  394. }
  395. /* 完成对焦 */
  396. static void auto_focus_done(void)
  397. {
  398. LOG_I("[对焦] 完成! 最佳位置:%d 清晰度:%.2f", g_af.best_pos, g_af.best_sharpness);
  399. /* 停止 timer_takephoto,避免景深融合期间产生新的清晰度更新 */
  400. rt_timer_stop(timer_takephoto);
  401. /* 标记景深融合开始,auto_focus_process 会忽略清晰度处理 */
  402. g_af.fusion_active = true;
  403. g_af.sharpness_updated = false;
  404. /* 执行景深融合 */
  405. depth_of_field_fusion();
  406. g_af.fusion_active = false;
  407. }
  408. /* 处理一次清晰度值,执行对焦算法 */
  409. static void auto_focus_process(void)
  410. {
  411. /* 景深融合期间不处理对焦 */
  412. if (g_af.fusion_active) {
  413. return;
  414. }
  415. float current_sharpness = g_af.new_sharpness;
  416. g_af.sharpness_updated = false;
  417. g_af.max_iterations--;
  418. LOG_I("[对焦] 清晰度:%.2f 位置:%d 剩余:%d",
  419. current_sharpness, g_af.current_pos, g_af.max_iterations);
  420. /* 更新最佳位置 */
  421. if (current_sharpness > g_af.best_sharpness) {
  422. g_af.best_sharpness = current_sharpness;
  423. g_af.best_pos = g_af.current_pos;
  424. LOG_I("[对焦] 新最佳:位置%d 清晰度%.2f", g_af.best_pos, g_af.best_sharpness);
  425. }
  426. /* 初始方向判断:比较首次移动前后的清晰度 */
  427. if (g_af.initial_move) {
  428. g_af.initial_move = false;
  429. if (current_sharpness > g_af.last_sharpness + 0.5f) {
  430. LOG_I("[对焦] 初始方向正确");
  431. } else {
  432. g_af.dir = !g_af.dir;
  433. LOG_I("[对焦] 初始方向错误,反转");
  434. }
  435. g_af.last_sharpness = current_sharpness;
  436. auto_focus_move();
  437. return;
  438. }
  439. /* 计算清晰度变化 */
  440. float diff = current_sharpness - g_af.last_sharpness;
  441. /* 动态调整步长 */
  442. if (fabs(diff) > 3.0f) {
  443. /* 清晰度大幅变化 -> 缩小步长精细搜索 */
  444. g_af.step_size = 4;
  445. g_af.diff_counter = 0;
  446. LOG_I("[对焦] 大幅变化,步长=%d", g_af.step_size);
  447. } else if (fabs(diff) < 1.0f) {
  448. /* 清晰度变化微小 -> 累计平坦区域 */
  449. g_af.diff_counter++;
  450. if (g_af.diff_counter >= 10) {
  451. g_af.step_size = 20;
  452. }
  453. LOG_I("[对焦] 平坦[%d/10] 步长=%d", g_af.diff_counter, g_af.step_size);
  454. } else {
  455. g_af.diff_counter = 0;
  456. }
  457. /* 方向判断 */
  458. bool need_reverse = false;
  459. if (diff > 0.5f) {
  460. /* 上升:保持方向,缩小步长逼近峰值 */
  461. g_af.reduce_counter = 0;
  462. g_af.last_rising = true;
  463. if (g_af.step_size > 4) g_af.step_size -= 2;
  464. LOG_I("[对焦] 上升↑ 步长=%d", g_af.step_size);
  465. } else if (diff < -0.3f) {
  466. /* 下降:累计,超过阈值反转方向 */
  467. g_af.reduce_counter++;
  468. g_af.last_rising = false;
  469. LOG_I("[对焦] 下降↓[%d/3]", g_af.reduce_counter);
  470. if (g_af.reduce_counter >= 3) {
  471. need_reverse = true;
  472. g_af.reduce_counter = 0;
  473. g_af.peak_counter++;
  474. LOG_I("[对焦] 峰值检测[%d/3]", g_af.peak_counter);
  475. }
  476. } else {
  477. g_af.reduce_counter = 0;
  478. LOG_I("[对焦] 平稳=");
  479. }
  480. g_af.last_sharpness = current_sharpness;
  481. if (need_reverse) {
  482. g_af.dir = !g_af.dir;
  483. LOG_I("[对焦] 反转方向");
  484. }
  485. /* 平坦区域超时:强制反转并增大步长 */
  486. if (g_af.diff_counter >= 30) {
  487. g_af.dir = !g_af.dir;
  488. g_af.step_size += 10;
  489. g_af.diff_counter = 0;
  490. LOG_I("[对焦] 平坦超时,增大步长=%d", g_af.step_size);
  491. }
  492. LOG_I("[对焦] 最佳:位置%d 清晰度%.2f", g_af.best_pos, g_af.best_sharpness);
  493. /* 终止条件 */
  494. bool done = false;
  495. if (g_af.peak_counter >= 3 && current_sharpness > g_af.best_sharpness * 0.95f) {
  496. done = true;
  497. LOG_I("[对焦] 峰值确认完成");
  498. }
  499. if (g_af.max_iterations <= 0) {
  500. done = true;
  501. LOG_I("[对焦] 迭代次数耗尽");
  502. }
  503. if (done) {
  504. auto_focus_done();
  505. return;
  506. }
  507. /* 移动电机 */
  508. auto_focus_move();
  509. }
  510. /* ========== Y轴校准(Calibration)控制块 ========== */
  511. typedef struct {
  512. float best_sharpness;
  513. float last_sharpness;
  514. int diff_counter;
  515. bool initial_move;
  516. int dir;
  517. int ystep;
  518. volatile bool sharpness_updated;
  519. float new_sharpness;
  520. } calibrate_t;
  521. static calibrate_t g_cal;
  522. /* 轻量级信号处理程序:仅存储清晰度值,不阻塞 */
  523. void thread2_signal_handler(int sig)
  524. {
  525. rt_timer_stop(timer_takephoto);
  526. g_cal.sharpness_updated = true;
  527. g_cal.new_sharpness = device_status.focus_value;
  528. }
  529. /* 初始化校准状态 */
  530. static void calibrate_init(void)
  531. {
  532. memset(&g_cal, 0, sizeof(g_cal));
  533. g_cal.best_sharpness = -FLT_MAX;
  534. g_cal.last_sharpness = 0.0f;
  535. g_cal.initial_move = true;
  536. g_cal.dir = 1;
  537. g_cal.ystep = 0;
  538. }
  539. /* 移动电机一步 */
  540. static void calibrate_move(void)
  541. {
  542. g_cal.ystep += 80;
  543. slide_para.pulse_period = mapSpeed(50);
  544. slide_para.ydir = g_cal.dir;
  545. slide_para.ydis = 80;
  546. slide_para.ystep = g_cal.ystep;
  547. slide_para.en_slide = 6;
  548. while (slide_para.en_slide != 0) {
  549. rt_thread_delay(500);
  550. }
  551. LOG_I("[Y] 电机到位 位置:%d", g_cal.ystep);
  552. rt_timer_start(timer_takephoto);
  553. }
  554. /* 处理一次校准清晰度值 */
  555. static void calibrate_process(void)
  556. {
  557. float current_sharpness = g_cal.new_sharpness;
  558. g_cal.sharpness_updated = false;
  559. LOG_I("[Y] 清晰度值:%.2f 位置:%d", current_sharpness, g_cal.ystep);
  560. /* 更新最佳位置 */
  561. if (current_sharpness > g_cal.best_sharpness) {
  562. g_cal.best_sharpness = current_sharpness;
  563. LOG_I("[Y] 更佳位置(清晰度:%.2f) 位置:%d", g_cal.best_sharpness, g_cal.ystep);
  564. }
  565. /* 初始方向判断 */
  566. if (g_cal.initial_move) {
  567. g_cal.initial_move = false;
  568. if (current_sharpness > g_cal.last_sharpness) {
  569. LOG_I("[Y] 初始方向正确");
  570. }
  571. g_cal.last_sharpness = current_sharpness;
  572. calibrate_move();
  573. return;
  574. }
  575. /* 计算变化 */
  576. double diff_sharpness = fabs(current_sharpness - g_cal.last_sharpness);
  577. if (diff_sharpness < 1.0) {
  578. g_cal.diff_counter++;
  579. }
  580. LOG_I("[Y] 平坦累计:%d 变化量:%.2f", g_cal.diff_counter, diff_sharpness);
  581. /* 从平坦区域到突变 -> 特征点,保存当前位置 */
  582. if (g_cal.diff_counter > 1 && diff_sharpness > 100.0) {
  583. device_status.calibrate_slide = 0;
  584. device_status.y_init_dis = g_cal.ystep;
  585. LOG_I("设置Y轴初始位置:%d", g_cal.ystep);
  586. ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
  587. return;
  588. }
  589. g_cal.last_sharpness = current_sharpness;
  590. /* 继续移动 */
  591. calibrate_move();
  592. }
  593. //入口函数-电机控制及
  594. static void Ctr_Mortor_entry(void* parameter)
  595. {
  596. /*孢子仪2.0*/
  597. rt_signal_install(SIGUSR1, thread1_signal_handler); //自动对焦信号
  598. rt_signal_install(SIGUSR2, thread2_signal_handler); //坐标校准信号
  599. rt_signal_unmask(SIGUSR1);
  600. rt_signal_unmask(SIGUSR2);
  601. static uint32_t t_takephoto = 0;
  602. while(1)
  603. {
  604. if(device_status.calibrate_slide==2)
  605. {
  606. device_status.autotakepic=1;
  607. device_status.calibrate_slide = 20;
  608. }
  609. if(device_status.autotakepic==2)
  610. {
  611. t_takephoto++;
  612. if(t_takephoto==18000)
  613. {
  614. t_takephoto = 0;
  615. device_status.autotakepic = 0;
  616. }
  617. /* 自动对焦:有新清晰度值时处理 */
  618. if (g_af.sharpness_updated) {
  619. auto_focus_process();
  620. }
  621. /* Y轴校准:有新清晰度值时处理 */
  622. if (g_cal.sharpness_updated) {
  623. calibrate_process();
  624. }
  625. }else {
  626. t_takephoto = 0;
  627. }
  628. /*对焦拍照,滑台初始化*/
  629. if(device_status.autotakepic==1)
  630. {
  631. device_status.step_focus = 1;
  632. slide_para.en_slide = 7; //Y轴复位
  633. slide_para.pulse_period = mapSpeed(50);
  634. while(slide_para.en_slide!=0){
  635. LOG_D("等待Y轴电机完成复位!");
  636. rt_thread_delay(1000);
  637. }
  638. if(device_status.calibrate_slide==0){
  639. device_status.autotakepic = 3;
  640. }else{
  641. slide_para.en_slide = 7; //Y轴复位
  642. slide_para.pulse_period = mapSpeed(50);
  643. while(slide_para.en_slide!=0){
  644. LOG_D("等待Y轴电机完成复位!");
  645. rt_thread_delay(1000);
  646. }
  647. rt_thread_delay(1000);
  648. //生成JSON指令
  649. char *photo = makeJson_Warn("mov_init_y","takephoto_done");
  650. DTU_Write("%s",photo);
  651. rt_free(photo);
  652. device_status.autotakepic = 4;
  653. rt_timer_start(timer_takephoto);
  654. }
  655. }
  656. if(device_status.autotakepic==3)
  657. {
  658. slide_para.en_slide = 7; //Y轴复位
  659. slide_para.pulse_period = mapSpeed(50);
  660. while(slide_para.en_slide!=0){
  661. LOG_D("等待Y轴电机完成复位!");
  662. rt_thread_delay(1000);
  663. }
  664. rt_thread_delay(1000);
  665. //Y轴电机运行到指定距离
  666. slide_para.ydir = 1;
  667. //slide_para.ydis = 3350;
  668. slide_para.ydis = device_status.y_init_dis; //9000
  669. slide_para.pulse_period = mapSpeed(50);
  670. slide_para.en_slide = 6;
  671. while(slide_para.en_slide!=0){
  672. LOG_D("Again:等待y轴电机到位!");
  673. rt_thread_delay(1000);
  674. }
  675. //生成JSON指令
  676. char *photo = makeJson_Warn("mov_init_y","takephoto_done");
  677. DTU_Write("%s",photo);
  678. rt_free(photo);
  679. device_status.autotakepic = 4;
  680. rt_timer_start(timer_takephoto);
  681. }
  682. if(device_status.autotakepic == 4)
  683. {
  684. device_status.autotakepic = 2;
  685. auto_focus_init();
  686. calibrate_init();
  687. slide_para.ystep = 0;
  688. }
  689. if(device_status.fsl_sw==1)
  690. {
  691. ctr_Petroleum_jelly(10);
  692. }
  693. //一键测试
  694. if(device_status.auto_test_sw==1)
  695. {
  696. //开始采集风机持续5S
  697. ctr_router(ON);
  698. rt_thread_delay(1000*60);
  699. //关闭采集风机
  700. ctr_router(OFF);
  701. //胶带传送
  702. turnlimit();
  703. rt_thread_delay(1000);
  704. turnlimit(); //再转
  705. //滑台归位,自动对焦
  706. //拍一张照片上传到平台
  707. device_status.autotakepic=1;
  708. device_status.auto_test_sw=0;
  709. char *take_start = makeJson_Warn("start", "autofocus");
  710. DTU_Write(take_start);
  711. rt_free(take_start);
  712. }
  713. rt_thread_delay(100);
  714. }
  715. }
  716. /*屏幕写数据*/
  717. int Hmi_Fresh(void)
  718. {
  719. #if 0
  720. //初始化串口4
  721. HMI_uart4_init();
  722. rt_err_t ret = RT_EOK;
  723. /* 初始化消息队列 */
  724. rt_mq_init(&rx_mq, "rx_mq",
  725. msg_pool, /* 存放消息的缓冲区 */
  726. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  727. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  728. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  729. /* 设置接收回调函数 */
  730. rt_device_set_rx_indicate(serial, uart_input);
  731. /* 创建线程*/
  732. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  733. Hmi_thread_entry,
  734. RT_NULL,
  735. HMI_THREAD_STACK_SIZE,
  736. HMI_THREAD_PRIORITY,
  737. HMI_THREAD_TIMESLICE);
  738. /* 如果获得线程控制块,启动这个线程 */
  739. if (hmi1 != RT_NULL)
  740. {
  741. rt_thread_startup(hmi1);
  742. }
  743. else
  744. {
  745. LOG_E("rt_thread_startup hmi1 failed!");
  746. }
  747. #endif
  748. /*部分控制的线程*/
  749. ctr_motor = rt_thread_create("Ctr motor",
  750. Ctr_Mortor_entry,
  751. RT_NULL,
  752. CONTROL_MOTOR_STACK_SIZE,
  753. CONTROL_MOTOR_PRIORITY,
  754. CONTROL_MOTOR_TIMESLICE);
  755. if(ctr_motor != RT_NULL)
  756. {
  757. rt_thread_startup(ctr_motor);
  758. }
  759. //定时器,串口通信超时监测20S
  760. timer_uart2 = rt_timer_create("t_uart2",t_uart2_callback,RT_NULL, 20000, RT_TIMER_FLAG_ONE_SHOT);
  761. if(timer_uart2==RT_NULL)
  762. {
  763. LOG_E("rt_timer_create timer_uart2 failed!");
  764. }
  765. return 0;
  766. }
  767. void Check_ParamConfig(void)
  768. {
  769. time_t now = time(RT_NULL);
  770. static struct tm tm;
  771. gmtime_r(&now, &tm);
  772. if(easyflash_init()==EF_NO_ERR) //初始化成功
  773. {
  774. ef_get_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt), NULL); //读出reboot_time的值
  775. ef_get_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag), NULL); //读出updateflag的值
  776. ef_get_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave), NULL); //判断是否用户配置了程序
  777. device_status.rebootcnt++;
  778. ef_set_env_blob("reboot_time", &device_status.rebootcnt, sizeof(device_status.rebootcnt));
  779. LOG_I("REBOOT_TIME:%d-%d-%d",device_status.rebootcnt,device_status.iSave,device_status.update_flag);
  780. }
  781. else {
  782. LOG_E("easyflash init ERRPR!!!");
  783. }
  784. //保存默认配置
  785. if(device_status.iSave!=FLASH_FLAG)
  786. {
  787. device_status.iSave=FLASH_FLAG;
  788. /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
  789. device_status.dtype = 7;
  790. device_status.version_type = 7; //4.0
  791. device_status.Power_Type = 0; //0-交流供电;1-直流供电
  792. device_status.tph = 60; //高温保护阈值
  793. device_status.tpl = 10; //低温保护阈值
  794. device_status.dattim = 10; //数据上传间隔
  795. device_status.ds = 1; //设备状态
  796. device_status.update_flag = 0;
  797. /*孢子仪*/
  798. device_status.collecting_sh1 = 9;
  799. device_status.collecting_eh1 = 10;
  800. device_status.collecting_sh2 = 14;
  801. device_status.collecting_eh2 = 16;
  802. device_status.cold_sw = 0;
  803. device_status.heat_sw =0;
  804. device_status.cul_time = 1;
  805. device_status.run_sta = 0;
  806. sprintf(device_status.driver, "%s",VERSION_BZY);
  807. sprintf(device_status.deviceId,"0");
  808. rt_sprintf(device_status.tempture, "-15");
  809. rt_sprintf(device_status.humidity, "31");
  810. rt_sprintf(device_status.mqttcfg1, "8.136.98.49");
  811. device_status.x_init_dis = 500;
  812. device_status.y_init_dis = 6000;
  813. device_status.tape_used = 0;
  814. ef_set_env_blob("iSave", &device_status.iSave, sizeof(device_status.iSave)); //
  815. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  816. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  817. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  818. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  819. ef_set_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type)); //版本类型
  820. ef_set_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture)); //温度
  821. ef_set_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity)); //湿度
  822. ef_set_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1)); //风机开关
  823. ef_set_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1)); //采集时间
  824. ef_set_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2)); //采集时间
  825. ef_set_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2)); //采集时间
  826. ef_set_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3)); //采集时间
  827. ef_set_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3)); //采集时间
  828. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta)); //
  829. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  830. ef_set_env_blob("x_init_dis", &device_status.x_init_dis, sizeof(device_status.x_init_dis));
  831. ef_set_env_blob("y_init_dis", &device_status.y_init_dis, sizeof(device_status.y_init_dis));
  832. ef_set_and_save_env("collt_min_sh1", "0");
  833. ef_set_and_save_env("collt_min_eh1", "0");
  834. ef_set_and_save_env("collt_min_sh2", "0");
  835. ef_set_and_save_env("collt_min_eh2", "0");
  836. ef_set_and_save_env("collt_min_sh3", "0");
  837. ef_set_and_save_env("collt_min_eh3", "0");
  838. LOG_I("SOFTWARE VERSION:%s",device_status.driver);
  839. LOG_I("Default paramconf is:");
  840. LOG_I("开机次数:%d",device_status.rebootcnt);
  841. LOG_I("高温保护:%d",device_status.tph);
  842. LOG_I("低温保护:%d",device_status.tpl);
  843. LOG_I("数据上传间隔:%d",device_status.dattim);
  844. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  845. LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
  846. device_status.collecting_sh1,device_status.collt_min_sh1,
  847. device_status.collecting_eh1,device_status.collt_min_eh1,
  848. device_status.collecting_sh2,device_status.collt_min_sh2,
  849. device_status.collecting_eh2,device_status.collt_min_eh2,
  850. device_status.collecting_sh3,device_status.collt_min_sh3,
  851. device_status.collecting_eh3,device_status.collt_min_eh3);
  852. LOG_I("PowerType:%d",device_status.Power_Type);
  853. LOG_I("x_init_dis:%d",device_status.x_init_dis);
  854. LOG_I("y_init_dis:%d",device_status.y_init_dis);
  855. }//从easyflash读取参数配置
  856. else if (device_status.iSave==FLASH_FLAG) {
  857. ef_get_env_blob("dtype",&device_status.dtype,sizeof(device_status.dtype),NULL); //设备类型
  858. ef_get_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim),NULL); //数据上传间隔
  859. ef_get_env_blob("tph", &device_status.tph, sizeof(device_status.tph), NULL); //高温保护
  860. ef_get_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl), NULL); //低温保护
  861. ef_get_env_blob("dattim", &device_status.dattim, sizeof(device_status.dattim), NULL); //数据上传间隔
  862. ef_get_env_blob("imei",&device_status.imei,sizeof(device_status.imei),NULL); //IMEI
  863. ef_get_env_blob("iccid",&device_status.iccid,sizeof(device_status.iccid),NULL); //ICCID
  864. ef_get_env_blob("lat",&device_status.lat,sizeof(device_status.lat),NULL);
  865. ef_get_env_blob("lng",&device_status.lng,sizeof(device_status.lng),NULL);
  866. ef_get_env_blob("ds",&device_status.ds,sizeof(device_status.ds),NULL); //开关机状态
  867. ef_get_env_blob("env_t",&device_status.tempture,sizeof(device_status.tempture),NULL); //环境温度
  868. ef_get_env_blob("env_h",&device_status.humidity,sizeof(device_status.humidity),NULL); //环境湿度
  869. ef_get_env_blob("version_type",&device_status.version_type,sizeof(device_status.version_type),NULL); //版本类型
  870. ef_get_env_blob("driver_bl",&device_status.driver_bl,sizeof(device_status.driver_bl),NULL); //BL版本号 进BL的时候存储到FLASH
  871. ef_get_env_blob("collecting_sh1",&device_status.collecting_sh1,sizeof(device_status.collecting_sh1),NULL); //采集开始1
  872. ef_get_env_blob("collecting_eh1",&device_status.collecting_eh1,sizeof(device_status.collecting_eh1),NULL); //采集结束1
  873. ef_get_env_blob("collecting_sh2",&device_status.collecting_sh2,sizeof(device_status.collecting_sh2),NULL); //采集开始2
  874. ef_get_env_blob("collecting_eh2",&device_status.collecting_eh2,sizeof(device_status.collecting_eh2),NULL); //采集结束2
  875. ef_get_env_blob("collecting_sh3",&device_status.collecting_sh3,sizeof(device_status.collecting_sh3),NULL); //采集开始3
  876. ef_get_env_blob("collecting_eh3",&device_status.collecting_eh3,sizeof(device_status.collecting_eh3),NULL); //采集结束3
  877. ef_get_env_blob("run_sta",&device_status.run_sta,sizeof(device_status.run_sta),NULL); //运行状态
  878. ef_get_env_blob("Power_Type",&device_status.Power_Type,sizeof(device_status.Power_Type),NULL); //供电类型
  879. ef_get_env_blob("x_init_dis",&device_status.x_init_dis,sizeof(device_status.x_init_dis),NULL);
  880. ef_get_env_blob("y_init_dis",&device_status.y_init_dis,sizeof(device_status.y_init_dis),NULL);
  881. ef_get_env_blob("collt_min_sh1", &device_status.collt_min_sh1, sizeof(device_status.collt_min_sh1), NULL);
  882. ef_get_env_blob("collt_min_eh1", &device_status.collt_min_eh1, sizeof(device_status.collt_min_eh1), NULL);
  883. ef_get_env_blob("collt_min_sh2", &device_status.collt_min_sh2, sizeof(device_status.collt_min_sh2), NULL);
  884. ef_get_env_blob("collt_min_eh2", &device_status.collt_min_eh2, sizeof(device_status.collt_min_eh2), NULL);
  885. ef_get_env_blob("collt_min_sh3", &device_status.collt_min_sh3, sizeof(device_status.collt_min_sh3), NULL);
  886. ef_get_env_blob("collt_min_eh3", &device_status.collt_min_eh3, sizeof(device_status.collt_min_eh3), NULL);
  887. //光控计数
  888. device_status.work_done = 0;
  889. //工作完成标志位
  890. if(device_status.dattim==0)
  891. {
  892. device_status.dattim = 10;
  893. }
  894. /*设备类型,7孢子仪*/
  895. device_status.dtype = 7;
  896. device_status.version_type = 7;
  897. if (device_status.version_type==7) {
  898. rt_sprintf(device_status.driver, "%s",VERSION_BZY);
  899. //异常处理:20230331,修复第一段开始时间等于第二段结束时间的情况
  900. if(device_status.collecting_eh1==device_status.collecting_eh2&&device_status.collecting_sh2!=device_status.collecting_eh2)
  901. {
  902. device_status.collecting_eh2 = device_status.collecting_sh2 + 2;
  903. }
  904. }
  905. app_flashdb_init();
  906. LOG_I("SOFTWARE VERSION:%s-imei:%s--iccid:%s",device_status.driver,device_status.imei,device_status.iccid);
  907. LOG_I("read flash paramconfis:%d-%s-%s",device_status.version_type,device_status.driver_bl,device_status.driver_compile);
  908. LOG_I("开机次数:%d",device_status.rebootcnt);
  909. LOG_I("高温保护:%d",device_status.tph);
  910. LOG_I("低温保护:%d",device_status.tpl);
  911. LOG_I("数据上传间隔:%d",device_status.dattim);
  912. LOG_I("温湿度数据:%d-%d",atoi(device_status.tempture),atoi(device_status.humidity));
  913. LOG_I("孢子仪采集时间:[%d:%d-%d:%d,%d:%d-%d:%d,%d:%d-%d:%d]",
  914. device_status.collecting_sh1,device_status.collt_min_sh1,
  915. device_status.collecting_eh1,device_status.collt_min_eh1,
  916. device_status.collecting_sh2,device_status.collt_min_sh2,
  917. device_status.collecting_eh2,device_status.collt_min_eh2,
  918. device_status.collecting_sh3,device_status.collt_min_sh3,
  919. device_status.collecting_eh3,device_status.collt_min_eh3);
  920. LOG_I("PowerType:%d",device_status.Power_Type);
  921. LOG_I("x_init_dis:%d",device_status.x_init_dis);
  922. LOG_I("y_init_dis:%d",device_status.y_init_dis);
  923. }
  924. //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
  925. {
  926. device_status.update_flag = 0;
  927. //初始化联网状态
  928. rt_sprintf(device_status.net,"1");
  929. rt_sprintf(device_status.csq,"0");
  930. rt_sprintf(device_status.sim,"1");
  931. rt_sprintf(device_status.gps, "0");
  932. Hmi_Fresh();
  933. dtu_data_play();
  934. sht30_collect();
  935. slide_Fresh();
  936. ctr_photo(ON);
  937. #if SENSOR_EN==1
  938. mb_master_samlpe();
  939. #endif
  940. #if ROBOT_EN == 0
  941. uint16_t connect_num=0;
  942. //等待联网,
  943. while(atoi(device_status.net)==1)
  944. {
  945. connect_num++;
  946. LOG_I("联网中......%d",connect_num);
  947. //超时时间为100S
  948. if(connect_num==60)
  949. {
  950. 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);
  951. break;
  952. }
  953. rt_thread_delay(2000);
  954. Beep_Tip(100);
  955. }
  956. connect_num = 0;
  957. rt_thread_delay(2000);
  958. DTU_Write("%s",makeJson_Warn("turnon", "device_stm32"));
  959. rt_thread_delay(1000);
  960. UpLoad_Data();
  961. Workstatus_check();
  962. Work_Loop_Task();
  963. #endif
  964. }
  965. }
  966. void ResetParamDefault(void)
  967. {
  968. device_status.dtype = 7;
  969. device_status.ds = 1; //设备状态开机
  970. device_status.thsp = 300;
  971. device_status.dattim = 10;
  972. device_status.collecting_sh1 = 9;
  973. device_status.collecting_eh1 = 17;
  974. device_status.collecting_sh2 = 00;
  975. device_status.collecting_eh2 = 00;
  976. device_status.collecting_sh3 = 00;
  977. device_status.collecting_eh3 = 00;
  978. device_status.collt_min_sh1 = 0;
  979. device_status.collt_min_eh1 = 0;
  980. device_status.collt_min_sh2 = 0;
  981. device_status.collt_min_eh2 = 0;
  982. device_status.collt_min_sh3 = 0;
  983. device_status.collt_min_eh3 = 0;
  984. device_status.Power_Type = 0;
  985. device_status.tph = 70; //高温保护阈值
  986. device_status.tpl = 10; //低温保护阈值
  987. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  988. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  989. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  990. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  991. device_status.run_sta = 0;
  992. device_status.heatvalue = 25; //培养温度
  993. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  994. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  995. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  996. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  997. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  998. ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
  999. ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
  1000. ef_set_and_save_env("collt_min_sh1", "0");
  1001. ef_set_and_save_env("collt_min_eh1", "0");
  1002. ef_set_and_save_env("collt_min_sh2", "0");
  1003. ef_set_and_save_env("collt_min_eh2", "0");
  1004. ef_set_and_save_env("collt_min_sh3", "0");
  1005. ef_set_and_save_env("collt_min_eh3", "0");
  1006. ef_set_env_blob("jheat", &device_status.heatvalue, sizeof(device_status.heatvalue));
  1007. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1008. ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
  1009. rt_thread_delay(1000);
  1010. }
  1011. void ResetParamDefault_DC(void)
  1012. {
  1013. //将温度数据设置为-15摄氏度,以便上传的数据中包含温控数据
  1014. rt_sprintf(device_status.tempture,"%d",-15);
  1015. device_status.dattim = 10; //数据上传间隔
  1016. device_status.tph = 70; //高温保护阈值
  1017. device_status.tpl = 10; //低温保护阈值
  1018. device_status.ds = 1; //设备状态开机
  1019. device_status.collecting_sh1 = 9;
  1020. device_status.collecting_eh1 = 17;
  1021. device_status.collecting_sh2 = 00;
  1022. device_status.collecting_eh2 = 00;
  1023. device_status.collecting_sh3 = 00;
  1024. device_status.collecting_eh3 = 00;
  1025. device_status.collt_min_sh1 = 0;
  1026. device_status.collt_min_eh1 = 0;
  1027. device_status.collt_min_sh2 = 0;
  1028. device_status.collt_min_eh2 = 0;
  1029. device_status.collt_min_sh3 = 0;
  1030. device_status.collt_min_eh3 = 0;
  1031. device_status.Power_Type = 1;
  1032. ef_set_env_blob("dattim",&device_status.dattim,sizeof(device_status.dattim)); //上传时间
  1033. ef_set_env_blob("Power_Type", &device_status.Power_Type, sizeof(device_status.Power_Type));
  1034. ef_set_env_blob("ds", &device_status.ds, sizeof(device_status.ds));
  1035. ef_set_env_blob("tph", &device_status.tph, sizeof(device_status.tph));
  1036. ef_set_env_blob("tpl", &device_status.tpl, sizeof(device_status.tpl));
  1037. ef_set_env_blob("collecting_sh1", &device_status.collecting_sh1, sizeof(device_status.collecting_sh1)); //采集时间
  1038. ef_set_env_blob("collecting_eh1", &device_status.collecting_eh1, sizeof(device_status.collecting_eh1)); //采集时间
  1039. ef_set_env_blob("collecting_sh2", &device_status.collecting_sh2, sizeof(device_status.collecting_sh2)); //采集时间
  1040. ef_set_env_blob("collecting_eh2", &device_status.collecting_eh2, sizeof(device_status.collecting_eh2)); //采集时间
  1041. ef_set_env_blob("collecting_sh3", &device_status.collecting_sh3, sizeof(device_status.collecting_sh3)); //采集时间
  1042. ef_set_env_blob("collecting_eh3", &device_status.collecting_eh3, sizeof(device_status.collecting_eh3)); //采集时间
  1043. ef_set_and_save_env("collt_min_sh1", "0");
  1044. ef_set_and_save_env("collt_min_eh1", "0");
  1045. ef_set_and_save_env("collt_min_sh2", "0");
  1046. ef_set_and_save_env("collt_min_eh2", "0");
  1047. ef_set_and_save_env("collt_min_sh3", "0");
  1048. ef_set_and_save_env("collt_min_eh3", "0");
  1049. rt_thread_delay(1000);
  1050. }
  1051. //固件备份
  1052. void app_partion_backup(void)
  1053. {
  1054. rt_kprintf("固件恢复!!!\n");
  1055. const char *dst_part_name = "df_area";
  1056. const char *src_part_name = "app";
  1057. const struct fal_partition *dst_part;
  1058. const struct fal_partition *src_part;
  1059. dst_part = fal_partition_find(dst_part_name);
  1060. src_part = fal_partition_find(src_part_name);
  1061. if (dst_part == RT_NULL || src_part == RT_NULL)
  1062. {
  1063. if (dst_part == RT_NULL)
  1064. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1065. if (src_part == RT_NULL)
  1066. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1067. }
  1068. else
  1069. {
  1070. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1071. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1072. {
  1073. int clone_pos = 0;
  1074. int clone_len = 0, clone_tol_len;
  1075. rt_uint8_t *buf = rt_malloc(4096);
  1076. if (dst_part->len < src_part->len)
  1077. clone_tol_len = dst_part->len;
  1078. else
  1079. clone_tol_len = src_part->len;
  1080. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1081. {
  1082. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1083. if (clone_len < 0)
  1084. {
  1085. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1086. break;
  1087. }
  1088. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1089. {
  1090. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1091. break;
  1092. }
  1093. HMI_WRITE("#");
  1094. rt_kprintf("#");
  1095. clone_pos += clone_len;
  1096. }
  1097. if (clone_pos >= clone_tol_len)
  1098. {
  1099. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1100. }
  1101. else
  1102. {
  1103. rt_kprintf("Clone partition failed!");
  1104. }
  1105. if (buf)
  1106. rt_free(buf);
  1107. }
  1108. }
  1109. }
  1110. void Debug_log_ON(void)
  1111. {
  1112. rt_console_set_device("uart4");
  1113. finsh_set_device("uart4");
  1114. }
  1115. void Debug_log_OFF(void)
  1116. {
  1117. rt_console_set_device("uart1");
  1118. finsh_set_device("uart1");
  1119. rt_mq_init(&rx_mq, "rx_mq",
  1120. msg_pool, /* 存放消息的缓冲区 */
  1121. sizeof(struct rx_msg), /* 一条消息的最大长度 */
  1122. sizeof(msg_pool), /* 存放消息的缓冲区大小 */
  1123. RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
  1124. rt_device_set_rx_indicate(serial, uart_input);
  1125. hmi1 = rt_thread_create("Hmi_Fresh_thread",
  1126. Hmi_thread_entry,
  1127. RT_NULL,
  1128. HMI_THREAD_STACK_SIZE,
  1129. HMI_THREAD_PRIORITY,
  1130. HMI_THREAD_TIMESLICE);
  1131. if (hmi1 != RT_NULL)
  1132. {
  1133. rt_thread_startup(hmi1);
  1134. }
  1135. }
  1136. int delete_thread_ctr_motor()
  1137. {
  1138. if(ctr_motor!=RT_NULL)
  1139. {
  1140. rt_thread_delete(ctr_motor);
  1141. }
  1142. }
  1143. MSH_CMD_EXPORT(ResetParamDefault,ResetParamDefault);
  1144. MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
  1145. MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
  1146. //
  1147. //INIT_BOARD_EXPORT(HMI_uart4_init);