control.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  1. #include <control.h>
  2. /*
  3. * Copyright (c) 2006-2020, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-05-30 Administrator the first version
  10. */
  11. #include "beep.h"
  12. #include "hmi.h"
  13. #include "easyflash.h"
  14. #include "stdio.h"
  15. #include "sound.h"
  16. #include "dtu_uart2.h"
  17. #include "sht30_collect.h"
  18. #include <user_mb_app.h>
  19. #define DBG_ENABLE
  20. #define DBG_COLOR
  21. #define DBG_TAG "control"
  22. #define DBG_LEVEL DBG_LOG
  23. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  24. //工作检测
  25. #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
  26. #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
  27. #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
  28. //工作流程
  29. #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
  30. #define WORK_LOOP_THREAD_STACK_SIZE 2048 //栈大小
  31. #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
  32. #define PWM_EN 1 //0关闭,1-打开
  33. //设备状态表结构体
  34. Device_Status_typedef device_status;
  35. rt_thread_t tid_workloop = RT_NULL;
  36. rt_thread_t tid_wkstc = RT_NULL;
  37. rt_thread_t tid_ctr = RT_NULL;
  38. rt_thread_t tid_heat = RT_NULL;
  39. rt_thread_t tid_485_play = RT_NULL;
  40. rt_event_t event_485_play;
  41. extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
  42. /***************************************************************************/
  43. int thread_delete_workloop(void)
  44. {
  45. rt_err_t result;
  46. if(tid_workloop!=RT_NULL)
  47. {
  48. result = rt_thread_delete(tid_workloop);
  49. }
  50. return result;
  51. }
  52. int thread_restart_workloop(void)
  53. {
  54. rt_err_t result;
  55. result = Work_Loop_Task();
  56. return result;
  57. }
  58. /**************************1.0定制2000W*************************************/
  59. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  60. {
  61. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  62. if(status_ws==ON)
  63. {
  64. rt_pin_write(FAN_P,ON);
  65. }
  66. else {
  67. rt_pin_write(FAN_P, OFF);
  68. }
  69. return 0;
  70. }
  71. uint8_t ctr_Fan_IN(uint8_t status_ws)
  72. {
  73. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  74. if(status_ws==ON)
  75. {
  76. rt_pin_write(FAN_I,ON);
  77. }
  78. else {
  79. rt_pin_write(FAN_I, OFF);
  80. }
  81. return 0;
  82. }
  83. /****************************5.0***********************************/
  84. void ctr_turnv5(void)
  85. {
  86. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  87. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  88. // 转出
  89. rt_pin_write(CLEAR_R,ON);
  90. rt_pin_write(CLEAR_L,OFF);
  91. rt_thread_mdelay(300);
  92. //停止
  93. rt_pin_write(CLEAR_R,OFF);
  94. rt_pin_write(CLEAR_L,OFF);
  95. rt_thread_mdelay(300);
  96. //转入
  97. rt_pin_write(CLEAR_R,OFF);
  98. rt_pin_write(CLEAR_L,ON);
  99. rt_thread_mdelay(300);
  100. //停止
  101. rt_pin_write(CLEAR_R,OFF);
  102. rt_pin_write(CLEAR_L,OFF);
  103. rt_thread_mdelay(300);
  104. }
  105. /**************************机器人********************************/
  106. void ctr_Motor_Robot(uint8_t status_ws)
  107. {
  108. unsigned int close_timeout = 0;
  109. unsigned int open_timeout = 0;
  110. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  111. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  112. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  113. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  114. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  115. rt_pin_write(Motor_Power,OFF);
  116. rt_pin_write(Motor_Direction_Ctr,ON);
  117. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  118. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  119. if(status_ws == ON)
  120. {
  121. //检测按键状态
  122. while(rt_pin_read(Motor_Direction_Button)==1)
  123. {
  124. if(rt_pin_read(LIMIT_CLOSE)==1)
  125. {
  126. rt_pin_write(Motor_Direction_Ctr,ON);
  127. close_timeout++;
  128. //打开电机驱动器电源,默认关闭
  129. rt_pin_write(Motor_Power,ON);
  130. if(close_timeout==2)
  131. {
  132. rt_pin_write(CLEAR_R,ON);
  133. rt_pin_write(CLEAR_L,OFF);
  134. rt_pin_write(Motor_Power,OFF);
  135. rt_thread_delay(20);
  136. rt_pin_write(Motor_Power,ON);
  137. }
  138. rt_thread_delay(20);
  139. if(close_timeout>500)
  140. {
  141. close_timeout = 600;
  142. rt_pin_write(Motor_Power,OFF);
  143. rt_pin_write(CLEAR_R,OFF);
  144. rt_pin_write(CLEAR_L,OFF);
  145. }
  146. }
  147. else
  148. {
  149. close_timeout = 0;
  150. rt_pin_write(Motor_Power,OFF); //关闭驱动器电源
  151. rt_pin_write(CLEAR_R,OFF);
  152. rt_pin_write(CLEAR_L,OFF);
  153. }
  154. rt_thread_delay(20);
  155. }
  156. rt_pin_write(Motor_Power,OFF);
  157. rt_thread_delay(1000*2);
  158. while(rt_pin_read(Motor_Direction_Button)==0)
  159. {
  160. if(rt_pin_read(LIMIT_OPEN)==1)
  161. {
  162. open_timeout ++;
  163. //打开电机驱动器电源,默认关闭
  164. if(open_timeout==2){
  165. rt_pin_write(CLEAR_R,OFF);
  166. rt_pin_write(CLEAR_L,ON);
  167. }
  168. rt_pin_write(Motor_Power,ON);
  169. rt_pin_write(Motor_Direction_Ctr,OFF);
  170. if(open_timeout>500)
  171. {
  172. open_timeout = 600;
  173. rt_pin_write(Motor_Power,OFF);
  174. rt_pin_write(CLEAR_R,OFF);
  175. rt_pin_write(CLEAR_L,OFF);
  176. }
  177. rt_thread_delay(20);
  178. }
  179. if(rt_pin_read(LIMIT_OPEN)==0)
  180. {
  181. open_timeout = 0;
  182. rt_pin_write(Motor_Power,OFF);
  183. rt_pin_write(CLEAR_R,OFF);
  184. rt_pin_write(CLEAR_L,OFF);
  185. }
  186. rt_thread_delay(20);
  187. close_timeout = 0;
  188. }
  189. open_timeout = 0;
  190. close_timeout = 0;
  191. }
  192. else {
  193. rt_pin_write(Motor_Power,OFF);
  194. }
  195. }
  196. /*************************吸虫塔*********************************/
  197. //吸虫电机
  198. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  199. {
  200. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  201. if(status_ws==ON)
  202. {
  203. rt_pin_write(MOTOR1_CTR_PIN,ON);
  204. }
  205. else {
  206. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  207. }
  208. return 0;
  209. }
  210. uint8_t ctr_Camera_Power(uint8_t status_ws)
  211. {
  212. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  213. if(status_ws==ON)
  214. {
  215. rt_pin_write(MOTOR1_CTR_PIN,ON);
  216. }
  217. else {
  218. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  219. }
  220. return 0;
  221. }
  222. //吸附电机
  223. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  224. {
  225. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  226. if(status_ws==ON)
  227. {
  228. rt_pin_write(MOTOR2_CTR_PIN,ON);
  229. }
  230. else {
  231. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  232. }
  233. return 0;
  234. }
  235. //清扫-卷带电机
  236. void ctr_JD_Clear(uint8_t shake_sw)
  237. {
  238. rt_uint16_t turn_timeout = 0;
  239. rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
  240. ctr_cangmen_up(OFF); //清扫传送带的电机
  241. if(shake_sw==ON)
  242. {
  243. ctr_Heat(ON); //清洁风机
  244. Motor_shake(ON, 0);
  245. ctr_cangmen_up(OPEN); //打开清扫
  246. }
  247. else {
  248. Motor_shake(OFF, 0);
  249. ctr_cangmen_up(OPEN); //打开清扫
  250. }
  251. while(rt_pin_read(MOTOR_C_LIMIT)==0)
  252. {
  253. Motor_Juandai(ON);
  254. rt_thread_delay(50);
  255. turn_timeout++;
  256. if(turn_timeout==1000) break;
  257. }
  258. turn_timeout = 0;
  259. while(rt_pin_read(MOTOR_C_LIMIT)==1)
  260. {
  261. Motor_Juandai(ON);
  262. turn_timeout++;
  263. rt_thread_delay(50);
  264. if(turn_timeout==1000) break;
  265. }
  266. Motor_shake(OFF, 1);
  267. Motor_Juandai(OFF);
  268. ctr_cangmen_up(OFF); //关闭清扫
  269. ctr_Heat(OFF);
  270. }
  271. //工作任务流程
  272. void WorkTask_XCT(void)
  273. {
  274. while(device_status.work_status==1&&device_status.ds==1)
  275. {
  276. ctr_Motor_xifu(ON);
  277. //打开相机供电
  278. ctr_Camera_Power(ON);
  279. //ctr_Heat(ON); //清洁风机
  280. uint32_t photo_fre = 0;
  281. photo_fre = device_status.dattim * 60;
  282. while(photo_fre!=0)
  283. {
  284. photo_fre --;
  285. rt_thread_delay(1000);
  286. LOG_I("收集中,等待开始拍照:%d",photo_fre);
  287. }
  288. //卷带
  289. ctr_JD_Clear(OFF);
  290. //拍照
  291. HK_TakePhoto();
  292. //关闭吸附风机
  293. ctr_Motor_xifu(OFF);
  294. //关闭相机供电
  295. //ctr_Heat(OFF);
  296. ctr_Camera_Power(OFF);
  297. //卷带
  298. ctr_JD_Clear(ON);
  299. // rt_thread_delay(1000);
  300. // ctr_JD_Clear(ON);
  301. //震动
  302. Motor_shake(ON, device_status.shake_sec);
  303. }
  304. //关闭风机
  305. ctr_Motor_xichong(OFF);
  306. ctr_Motor_xifu(OFF);
  307. LOG_I("不在工作时间,未工作!!!");
  308. }
  309. /**********************************************************************/
  310. /*光控*/
  311. uint8_t In_Light_status(void)
  312. {
  313. rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
  314. return rt_pin_read(LIGHT_PIN);
  315. }
  316. /*雨控*/
  317. uint8_t In_Rain_status(void)
  318. {
  319. rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
  320. return rt_pin_read(RAIN_PIN);
  321. }
  322. /*加热*/
  323. uint8_t ctr_Heat(uint8_t status_heat)
  324. {
  325. device_status.heat_status = status_heat;
  326. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  327. rt_pin_write(Heat_Pin,status_heat);
  328. return status_heat;
  329. }
  330. /*制冷*/
  331. uint8_t ctr_Cold(uint8_t status_cold)
  332. {
  333. device_status.cold_status = status_cold;
  334. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  335. rt_pin_write(Cold_Pin,status_cold);
  336. return status_cold;
  337. }
  338. /*相机供电*/
  339. void ctr_photo(uint8_t status_photo)
  340. {
  341. rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
  342. rt_pin_write(PHOTO,status_photo);
  343. }
  344. /*************************************************
  345. Function: ctr_Cool_Fan
  346. Description: 散热风机的开关
  347. Calls:
  348. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  349. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  350. Input: ON-开,OFF-关
  351. Output: NULL
  352. Return: ON-开,OFF-关
  353. Others: NULL
  354. *************************************************/
  355. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  356. {
  357. device_status.cool_fan_status = status_cool;
  358. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  359. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  360. if(status_cool==OFF)
  361. {
  362. rt_pin_write(Cool_Fan_L,OFF);
  363. rt_pin_write(Cool_Fan_R,OFF);
  364. }
  365. if(status_cool==ON)
  366. {
  367. rt_pin_write(Cool_Fan_L,ON);
  368. rt_pin_write(Cool_Fan_R,OFF);
  369. }
  370. return device_status.cool_fan_status;
  371. }
  372. /*************************************************
  373. Function: ctr_Collect_Fan
  374. Description: 采集风机的开关
  375. Calls:
  376. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  377. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  378. Input: ON-开,OFF-关
  379. Output: NULL
  380. Return: ON-开,OFF-关
  381. Others: NULL
  382. *************************************************/
  383. uint8_t ctr_Collect_Fan(uint8_t status_collect)
  384. {
  385. device_status.collect_fan_status = status_collect;
  386. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  387. if(status_collect==OFF)
  388. {
  389. rt_pin_write(Collect_Fan,OFF);
  390. }
  391. if(status_collect==ON)
  392. {
  393. rt_pin_write(Collect_Fan,ON);
  394. }
  395. return device_status.collect_fan_status;
  396. }
  397. /*************************************************
  398. Function: ctr_develop
  399. Description: 培养载玻片孢子
  400. Calls:
  401. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  402. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  403. Input: v=0(复位) v=90转90度
  404. Output: NULL
  405. Return: NULL
  406. Others: NULL
  407. *************************************************/
  408. /*上仓门*/
  409. uint8_t ctr_cangmen_up(uint8_t status_up)
  410. {
  411. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  412. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  413. if(status_up==OFF)
  414. {
  415. rt_pin_write(Collect_fan_R,ON);
  416. rt_pin_write(Collect_fan_L,ON);
  417. device_status.ctr_up_motor = 0;
  418. rt_thread_delay(1000);
  419. }
  420. if(status_up==OPEN)
  421. {
  422. device_status.up_status = 1;
  423. rt_pin_write(Collect_fan_R,OFF);
  424. rt_pin_write(Collect_fan_L,OFF);
  425. rt_pin_write(Collect_fan_R,ON);
  426. rt_pin_write(Collect_fan_L,OFF);
  427. }
  428. if(status_up==CLOSE)
  429. {
  430. device_status.up_status = 0;
  431. rt_pin_write(Collect_fan_R,OFF);
  432. rt_pin_write(Collect_fan_L,OFF);
  433. rt_pin_write(Collect_fan_R,OFF);
  434. rt_pin_write(Collect_fan_L,ON);
  435. }
  436. return device_status.up_status;
  437. }
  438. //下仓门
  439. uint8_t ctr_cangmen_down(uint8_t status_down)
  440. {
  441. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  442. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  443. if(status_down==OFF)
  444. {
  445. rt_pin_write(C_DOWN_R,ON);
  446. rt_pin_write(C_DOWN_L,ON);
  447. device_status.ctr_down_motor = 0;
  448. rt_thread_delay(1000);
  449. }
  450. if(status_down==OPEN)
  451. {
  452. device_status.down_status = 1;
  453. rt_pin_write(C_DOWN_R,OFF);
  454. rt_pin_write(C_DOWN_L,OFF);
  455. rt_pin_write(C_DOWN_R,ON);
  456. rt_pin_write(C_DOWN_L,OFF);
  457. }
  458. if(status_down==CLOSE)
  459. {
  460. device_status.down_status = 0;
  461. rt_pin_write(C_DOWN_R,OFF);
  462. rt_pin_write(C_DOWN_L,OFF);
  463. rt_pin_write(C_DOWN_R,OFF);
  464. rt_pin_write(C_DOWN_L,ON);
  465. }
  466. return device_status.down_status;
  467. }
  468. //镇流器
  469. uint8_t ctr_lamp(uint8_t status_lamp,uint8_t lamp_num)
  470. {
  471. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  472. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  473. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  474. if(status_lamp==ON)
  475. {
  476. switch(lamp_num){
  477. case 1:
  478. rt_pin_write(LAMP_SW,ON);
  479. device_status.lamp_status = status_lamp;
  480. break;
  481. case 2:
  482. rt_pin_write(LAMP_1_SW,ON);
  483. device_status.lamp_status = status_lamp;
  484. break;
  485. case 3:
  486. rt_pin_write(LAMP_2_SW,ON);
  487. device_status.lamp_status = status_lamp;
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. else {
  494. switch(lamp_num){
  495. case 1:
  496. rt_pin_write(LAMP_SW,OFF);
  497. device_status.lamp_status = status_lamp;
  498. break;
  499. case 2:
  500. rt_pin_write(LAMP_1_SW,OFF);
  501. device_status.lamp1_status = status_lamp;
  502. break;
  503. case 3:
  504. rt_pin_write(LAMP_2_SW,OFF);
  505. device_status.lamp2_status = status_lamp;
  506. break;
  507. default:
  508. break;
  509. }
  510. }
  511. HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
  512. return device_status.lamp_status;
  513. }
  514. //震动电机
  515. uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
  516. {
  517. static rt_uint16_t shake_i = 0;
  518. shake_i = atoi(device_status.power);
  519. LOG_I("震动电机待机电流:%d",shake_i);
  520. if(device_status.dtype==7&&shake_sec!=0)
  521. DTU_Write("drop");
  522. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  523. #if 0
  524. rt_pin_write(SHAKE_SW,ON);
  525. return 0;
  526. #endif
  527. if(status_shake==ON&&device_status.shake_ws==1)
  528. {
  529. #if 0
  530. for(int8_t s = 0;s++;s<2)
  531. {
  532. rt_pin_write(SHAKE_SW,ON);
  533. rt_thread_delay(100);
  534. rt_pin_write(SHAKE_SW,OFF);
  535. rt_thread_delay(1000);
  536. }
  537. #endif
  538. rt_pin_write(SHAKE_SW,ON);
  539. if(device_status.dtype==3)
  540. {
  541. while(shake_sec--)
  542. {
  543. rt_thread_delay(100);
  544. if(shake_sec==20&&device_status.clear_status==0&&device_status.work_status==1&&atoi(device_status.power)-shake_i<100&&shake_i!=0&&shake_i<5000)
  545. {
  546. char * shake_warn = makeJson_Warn("电流不足", "震动电机");
  547. DTU_Write("%s",shake_warn);
  548. rt_free(shake_warn);
  549. }
  550. LOG_D("震动电机:%d",atoi(device_status.power));
  551. }
  552. }
  553. if(device_status.dtype==7) rt_thread_delay(shake_sec*1000);
  554. rt_pin_write(SHAKE_SW,OFF);
  555. HMI_WRITE("cangWei.switchShake.val=%d",0);
  556. }
  557. else
  558. {
  559. rt_pin_write(SHAKE_SW,OFF);
  560. HMI_WRITE("cangWei.switchShake.val=%d",0);
  561. }
  562. if(device_status.dtype==7)
  563. HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
  564. return status_shake;
  565. }
  566. //补光
  567. uint8_t ctr_led(uint8_t status_led)
  568. {
  569. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  570. if(device_status.version_type==6)
  571. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  572. if(status_led==ON)
  573. {
  574. rt_pin_write(LED_SW,ON);
  575. if(device_status.version_type==6)
  576. rt_pin_write(LED_P,ON);
  577. HMI_WRITE("sysTest.switchFill.val=%d",ON);
  578. }
  579. else if(status_led==OFF){
  580. rt_pin_write(LED_SW,OFF);
  581. if(device_status.version_type==6)
  582. rt_pin_write(LED_P,OFF);
  583. HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
  584. HMI_WRITE("sysTest.switchFill.val=%d",OFF);
  585. }
  586. return status_led;
  587. }
  588. /*************************************************
  589. Function: ctr_turn
  590. Description: 转盘驱动
  591. Calls:
  592. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  593. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  594. Input: v=0(复位) v=90转90度
  595. Output: NULL
  596. Return: NULL
  597. Others: NULL
  598. *************************************************/
  599. void ctr_turn(uint8_t v,uint8_t sta)
  600. {
  601. DTU_Write("turn.mp3");
  602. LOG_I("================v===============:%d",v);
  603. rt_uint16_t turn_cnt = 0;
  604. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  605. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  606. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  607. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  608. //if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  609. while(rt_pin_read(LIMIT_0)==0||rt_pin_read(LIMIT_90)==0)
  610. {
  611. rt_pin_write(TURN_ZP_R, ON);
  612. rt_pin_write(TURN_ZP_L, OFF);
  613. //Motor_shake(ON,0);
  614. turn_cnt ++;
  615. rt_thread_delay(100);
  616. if(turn_cnt==1000)
  617. {
  618. turn_cnt = 0;
  619. rt_pin_write(TURN_ZP_R, OFF);
  620. rt_pin_write(TURN_ZP_L, OFF);
  621. //Motor_shake(OFF,0);
  622. char *turn_warn =makeJson_Warn("未检测到限位", "转盘");
  623. DTU_Write("%s",turn_warn);
  624. rt_free(turn_warn);
  625. break;
  626. }
  627. }
  628. turn_cnt = 0;
  629. while(v==0&&rt_pin_read(LIMIT_0)==1)
  630. {
  631. rt_pin_write(TURN_ZP_R, ON);
  632. rt_pin_write(TURN_ZP_L, OFF);
  633. Motor_shake(ON,0);
  634. rt_thread_delay(200); //10 to 100
  635. turn_cnt ++;
  636. if(turn_cnt==300) //6000 to 600
  637. {
  638. turn_cnt = 0;
  639. // rt_pin_write(TURN_ZP_R, OFF);
  640. // rt_pin_write(TURN_ZP_L, OFF);
  641. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  642. // DTU_Write("%s",turn_warn);
  643. // rt_free(turn_warn);
  644. break;
  645. }
  646. }
  647. while(v==0&&rt_pin_read(LIMIT_0)==1)
  648. {
  649. rt_pin_write(TURN_ZP_R, ON);
  650. rt_pin_write(TURN_ZP_L, OFF);
  651. //Motor_shake(ON,0);
  652. rt_thread_delay(50); //10 to 50
  653. turn_cnt ++;
  654. if(turn_cnt==10) //6000 to 1200
  655. {
  656. turn_cnt = 0;
  657. rt_pin_write(TURN_ZP_R, OFF);
  658. rt_pin_write(TURN_ZP_L, OFF);
  659. //Motor_shake(OFF,0);
  660. char *turn_warn =makeJson_Warn("超时", "转盘复位");
  661. DTU_Write("%s",turn_warn);
  662. rt_free(turn_warn);
  663. break;
  664. }
  665. }
  666. turn_cnt = 0;
  667. while(v==90&&rt_pin_read(LIMIT_90)==1)
  668. {
  669. rt_pin_write(TURN_ZP_R, ON);
  670. rt_pin_write(TURN_ZP_L, OFF);
  671. //Motor_shake(ON,0);
  672. rt_thread_delay(200);
  673. turn_cnt ++;
  674. if(turn_cnt==150)
  675. {
  676. turn_cnt = 0;
  677. // rt_pin_write(TURN_ZP_R, OFF);
  678. // rt_pin_write(TURN_ZP_L, OFF);
  679. // char *turn_warn =makeJson_Warn("超时", "转盘");
  680. // DTU_Write(turn_warn);
  681. // rt_free(turn_warn);
  682. break;
  683. }
  684. }
  685. while(v==90&&rt_pin_read(LIMIT_90)==1)
  686. {
  687. // rt_pin_write(TURN_ZP_R, ON);
  688. // rt_pin_write(TURN_ZP_L, OFF);
  689. Motor_shake(ON,0);
  690. rt_thread_delay(50);
  691. turn_cnt ++;
  692. if(turn_cnt==10)
  693. {
  694. turn_cnt = 0;
  695. rt_pin_write(TURN_ZP_R, OFF);
  696. rt_pin_write(TURN_ZP_L, OFF);
  697. //Motor_shake(OFF,0);
  698. char *turn_warn =makeJson_Warn("超时", "转盘");
  699. DTU_Write(turn_warn);
  700. rt_free(turn_warn);
  701. break;
  702. }
  703. }
  704. if(sta!=0)
  705. {
  706. //保存位置
  707. device_status.run_sta = sta;
  708. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  709. }
  710. turn_cnt = 0;
  711. rt_pin_write(TURN_ZP_R, OFF);
  712. rt_pin_write(TURN_ZP_L, OFF);
  713. Motor_shake(OFF,0);
  714. HMI_WRITE("cangWei.rotateFlag.val=0");
  715. }
  716. /*
  717. *
  718. * */
  719. void ctr_Motor_Driver_Power(uint8_t status_ws)
  720. {
  721. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  722. rt_pin_write(SHAKE_SW, status_ws);
  723. }
  724. /*************************************************
  725. Function: ctr_Motor_Driver
  726. Description: 步进电机驱动
  727. Calls:
  728. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  729. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  730. Input: 50000000
  731. Output: NULL
  732. Return: shake_sec
  733. Others: NULL
  734. *************************************************/
  735. void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
  736. {
  737. beep_init();
  738. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  739. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  740. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  741. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  742. if(EN==0)
  743. {
  744. rt_pin_write(Motor_Power,0);
  745. }
  746. else{
  747. rt_pin_write(Motor_EN, EN);
  748. }
  749. rt_pin_write(Motor_DIR, DIR);
  750. beep_set(pwm, pwm/2); //200kzh - 50%
  751. while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
  752. {
  753. if(time_out==0)
  754. {
  755. break;
  756. }
  757. rt_thread_delay(50);
  758. time_out --;
  759. }
  760. while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
  761. {
  762. if(time_out==0)
  763. {
  764. break;
  765. }
  766. rt_thread_delay(50);
  767. time_out --;
  768. }
  769. //rt_pin_write(Motor_EN, OFF);
  770. //if(device_status.autotakepic==0)
  771. //ctr_lamp(OFF, 1);
  772. beep_set(0, 0); //200kzh - 50%
  773. rt_thread_delay(50);
  774. }
  775. /*************************************************
  776. Function: ctr_Petroleum_jelly
  777. Description: 涂凡士林
  778. Calls:
  779. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  780. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  781. Input: NULL
  782. Output: NULL
  783. Return: NULL
  784. Others: NULL
  785. *************************************************/
  786. void ctr_Petroleum_jelly(rt_uint32_t delay)
  787. {
  788. beep_init();
  789. pwm_on(2,50000);
  790. rt_thread_delay(delay*1000);
  791. pwm_off(2,0);
  792. rt_thread_delay(1000);
  793. pwm_on(3, 50000);
  794. rt_thread_delay(delay*1000);
  795. pwm_off(3,0);
  796. device_status.fsl_sw = 0;
  797. }
  798. //1.0转仓
  799. void turnzp(void)
  800. {
  801. static uint16_t turn_timeout = 0;
  802. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  803. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  804. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  805. // rt_pin_write(CLEAR_R,OFF);
  806. // rt_pin_write(CLEAR_L,OFF);
  807. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  808. {
  809. rt_thread_delay(20);
  810. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  811. {
  812. // rt_pin_write(CLEAR_R,ON);
  813. // rt_pin_write(CLEAR_L,OFF);
  814. Motor_Juandai(ON);
  815. rt_thread_delay(50);
  816. turn_timeout++;
  817. if(turn_timeout==600)
  818. {
  819. LOG_E("检测不到限位,电机强制停止");
  820. char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
  821. DTU_Write("%s",ctr_zp_limit_warn);
  822. rt_free(ctr_zp_limit_warn);
  823. break;
  824. }
  825. }
  826. break;
  827. }
  828. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  829. {
  830. rt_thread_delay(20);
  831. while(rt_pin_read(CLEAR_R_LIMIT)==1)
  832. {
  833. // rt_pin_write(CLEAR_R,ON);
  834. // rt_pin_write(CLEAR_L,OFF);
  835. Motor_Juandai(ON);
  836. rt_thread_delay(50);
  837. turn_timeout++;
  838. if(turn_timeout==600)
  839. {
  840. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  841. char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
  842. DTU_Write("%s",ctr_zp_limit_warn);
  843. rt_free(ctr_zp_limit_warn);
  844. break;
  845. }
  846. }
  847. break;
  848. }
  849. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  850. {
  851. rt_thread_delay(20);
  852. if(rt_pin_read(CLEAR_R_LIMIT)==0)
  853. {
  854. // rt_pin_write(CLEAR_R,OFF);
  855. // rt_pin_write(CLEAR_L,OFF);
  856. Motor_Juandai(OFF);
  857. break;
  858. }
  859. else {
  860. break;
  861. }
  862. }
  863. Motor_Juandai(OFF);
  864. // rt_pin_write(CLEAR_R,OFF);
  865. // rt_pin_write(CLEAR_L,OFF);
  866. turn_timeout = 0;
  867. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  868. HMI_WRITE("sysTest.switchClear.val=%d",0);
  869. rt_thread_delay(1000);
  870. }
  871. //清扫-常闭型限位开关
  872. //新结构使用推杆电机做清扫
  873. uint8_t ctr_clear(uint16_t status_clear)
  874. {
  875. static uint16_t clear_num = 0;
  876. #if PWM_EN == 0
  877. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  878. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  879. #endif
  880. rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
  881. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  882. /*************************************************************外接清扫
  883. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  884. rt_pin_write(POWER_CLEAR,ON);
  885. rt_thread_delay(1000*100);
  886. rt_pin_write(POWER_CLEAR,OFF);
  887. ************************************************************/
  888. //停止
  889. #if PWM_EN == 0
  890. rt_pin_write(CLEAR_R,OFF);
  891. rt_pin_write(CLEAR_L,OFF);
  892. #else
  893. Motor_Qingsao_R_PWM(OFF);
  894. Motor_Qingsao_L_PWM(OFF);
  895. #endif
  896. if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
  897. rt_thread_delay(100);
  898. //如果清扫位置在中间
  899. while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
  900. {
  901. #if PWM_EN == 0
  902. rt_pin_write(CLEAR_R,ON);
  903. rt_pin_write(CLEAR_L,OFF);
  904. #else
  905. Motor_Qingsao_R_PWM(ON);
  906. Motor_Qingsao_L_PWM(OFF);
  907. #endif
  908. rt_thread_delay(50);
  909. clear_num++;
  910. //已到达检测位置
  911. if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  912. {
  913. #if PWM_EN == 0
  914. rt_pin_write(CLEAR_R,ON);
  915. rt_pin_write(CLEAR_L,ON);
  916. #else
  917. Motor_Qingsao_R_PWM(OFF);
  918. Motor_Qingsao_L_PWM(OFF);
  919. #endif
  920. rt_thread_delay(2000);
  921. break;
  922. }
  923. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  924. {
  925. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  926. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  927. #if PWM_EN == 0
  928. rt_pin_write(CLEAR_R,ON);
  929. rt_pin_write(CLEAR_L,ON);
  930. #else
  931. Motor_Qingsao_R_PWM(OFF);
  932. Motor_Qingsao_L_PWM(OFF);
  933. #endif
  934. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  935. if(Read_Real_Voltage()/100<12)
  936. {
  937. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  938. HMI_WRITE("sysTest.switchClear.val=%d",0);
  939. device_status.clear_status = 0;
  940. return 0;
  941. }
  942. break;
  943. }
  944. }
  945. clear_num = 0;
  946. //如果清扫位置在左侧
  947. while(rt_pin_read(CLEAR_R_LIMIT)==0)
  948. {
  949. #if PWM_EN == 0
  950. rt_pin_write(CLEAR_R,ON);
  951. rt_pin_write(CLEAR_L,OFF);
  952. #else
  953. Motor_Qingsao_R_PWM(ON);
  954. Motor_Qingsao_L_PWM(OFF);
  955. #endif
  956. rt_thread_delay(50);
  957. clear_num++;
  958. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  959. {
  960. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  961. //DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  962. #if PWM_EN == 0
  963. rt_pin_write(CLEAR_R,ON);
  964. rt_pin_write(CLEAR_L,ON);
  965. #else
  966. Motor_Qingsao_R_PWM(OFF);
  967. Motor_Qingsao_L_PWM(OFF);
  968. #endif
  969. rt_free(makeJson_Warn("Clear Motor ERR","device"));
  970. if(Read_Real_Voltage()/100<12)
  971. {
  972. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  973. HMI_WRITE("sysTest.switchClear.val=%d",0);
  974. device_status.clear_status = 0;
  975. return 0;
  976. }
  977. break;
  978. }
  979. }
  980. clear_num = 0;
  981. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
  982. {
  983. #if PWM_EN == 0
  984. rt_pin_write(CLEAR_R,ON);
  985. rt_pin_write(CLEAR_L,ON);
  986. #else
  987. Motor_Qingsao_R_PWM(OFF);
  988. Motor_Qingsao_L_PWM(OFF);
  989. #endif
  990. if(Read_Real_Voltage()/100<12)
  991. {
  992. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  993. HMI_WRITE("sysTest.switchClear.val=%d",0);
  994. device_status.clear_status = 0;
  995. return 0;
  996. }
  997. rt_thread_delay(2000);
  998. break;
  999. }
  1000. while(rt_pin_read(CLEAR_L_LIMIT)==0)
  1001. {
  1002. #if PWM_EN == 0
  1003. rt_pin_write(CLEAR_R,OFF);
  1004. rt_pin_write(CLEAR_L,ON);
  1005. #else
  1006. Motor_Qingsao_R_PWM(OFF);
  1007. Motor_Qingsao_L_PWM(ON);
  1008. #endif
  1009. rt_thread_delay(50);
  1010. clear_num++;
  1011. if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
  1012. {
  1013. rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
  1014. char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
  1015. //DTU_Write("%s",Clear_ERR);
  1016. #if PWM_EN == 0
  1017. rt_pin_write(CLEAR_R,ON);
  1018. rt_pin_write(CLEAR_L,ON);
  1019. #else
  1020. Motor_Qingsao_R_PWM(OFF);
  1021. Motor_Qingsao_L_PWM(OFF);
  1022. #endif
  1023. rt_free(Clear_ERR);
  1024. if(Read_Real_Voltage()/100<12)
  1025. {
  1026. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1027. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1028. device_status.clear_status = 0;
  1029. return 0;
  1030. }
  1031. break;
  1032. }
  1033. }
  1034. while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
  1035. {
  1036. #if PWM_EN == 0
  1037. rt_pin_write(CLEAR_R,ON);
  1038. rt_pin_write(CLEAR_L,ON);
  1039. #else
  1040. Motor_Qingsao_R_PWM(OFF);
  1041. Motor_Qingsao_L_PWM(OFF);
  1042. #endif
  1043. rt_thread_delay(2000);
  1044. break;
  1045. }
  1046. while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
  1047. {
  1048. #if PWM_EN == 0
  1049. rt_pin_write(CLEAR_R,ON);
  1050. rt_pin_write(CLEAR_L,ON);
  1051. #else
  1052. Motor_Qingsao_R_PWM(OFF);
  1053. Motor_Qingsao_L_PWM(OFF);
  1054. #endif
  1055. if(device_status.version_type==41)
  1056. {
  1057. rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
  1058. char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
  1059. //DTU_Write("%s",Clear_ERR);
  1060. rt_free(Clear_ERR);
  1061. }
  1062. return 0;
  1063. }
  1064. clear_num = 0;
  1065. rt_thread_delay(2000);
  1066. #if PWM_EN == 0
  1067. rt_pin_write(CLEAR_R,ON);
  1068. rt_pin_write(CLEAR_L,ON);
  1069. #else
  1070. Motor_Qingsao_R_PWM(OFF);
  1071. Motor_Qingsao_L_PWM(OFF);
  1072. #endif
  1073. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1074. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1075. device_status.clear_status = 0;
  1076. return 1;
  1077. }
  1078. //路由器供电
  1079. void ctr_router(uint8_t status_router)
  1080. {
  1081. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1082. if(device_status.version_type==6)
  1083. rt_pin_mode(ROUTER_P,PIN_MODE_OUTPUT);
  1084. //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1085. if(status_router==ON)
  1086. {
  1087. rt_pin_write(ROUTER,ON);
  1088. //
  1089. #if LAMP_EN == 0
  1090. if(device_status.version_type==6)
  1091. rt_pin_write(ROUTER_P,ON);
  1092. #endif
  1093. }
  1094. else {
  1095. rt_pin_write(ROUTER,OFF);
  1096. //
  1097. #if LAMP_EN == 0
  1098. if(device_status.version_type==6)
  1099. rt_pin_write(ROUTER_P,OFF);
  1100. #endif
  1101. }
  1102. }
  1103. //清扫-推杆电机
  1104. void ctr_TG_Clear(uint16_t clear_timeout)
  1105. {
  1106. #if 0
  1107. int current_clear[200];
  1108. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1109. LOG_I("清扫电机待机:%d",Electric_current);
  1110. #endif
  1111. static uint16_t clear_count = 0;
  1112. clear_count = clear_timeout;
  1113. #if PWM_EN == 0
  1114. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1115. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1116. #endif
  1117. while(clear_count!=0)
  1118. {
  1119. #if PWM_EN == 0
  1120. rt_pin_write(CLEAR_R,ON);
  1121. rt_pin_write(CLEAR_L,OFF);
  1122. #else
  1123. Motor_Qingsao_R_PWM(ON);
  1124. Motor_Qingsao_L_PWM(OFF);
  1125. #endif
  1126. clear_count--;
  1127. rt_thread_delay(100);
  1128. }
  1129. #if PWM_EN == 0
  1130. rt_pin_write(CLEAR_R,ON);
  1131. rt_pin_write(CLEAR_L,ON);
  1132. #else
  1133. Motor_Qingsao_R_PWM(OFF);
  1134. Motor_Qingsao_L_PWM(OFF);
  1135. #endif
  1136. rt_thread_delay(3000);
  1137. clear_count = 0;
  1138. while(clear_timeout!=clear_count)
  1139. {
  1140. #if PWM_EN == 0
  1141. rt_pin_write(CLEAR_R,OFF);
  1142. rt_pin_write(CLEAR_L,ON);
  1143. #else
  1144. Motor_Qingsao_R_PWM(OFF);
  1145. Motor_Qingsao_L_PWM(ON);
  1146. #endif
  1147. // if(clear_count<200)
  1148. // current_clear[clear_count] = atoi(device_status.power);
  1149. clear_count++;
  1150. rt_thread_delay(100);
  1151. }
  1152. #if PWM_EN == 0
  1153. rt_pin_write(CLEAR_R,ON);
  1154. rt_pin_write(CLEAR_L,ON);
  1155. #else
  1156. Motor_Qingsao_R_PWM(OFF);
  1157. Motor_Qingsao_L_PWM(OFF);
  1158. #endif
  1159. #if 0
  1160. bubblesort(current_clear,200);
  1161. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1162. {
  1163. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1164. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1165. DTU_Write("%s",ctr_clear_warn);
  1166. rt_free(ctr_clear_warn);
  1167. }
  1168. #endif
  1169. rt_thread_delay(2000);
  1170. device_status.clear_status = 0;
  1171. HMI_WRITE("cangWei.rotateFlag.val=%d",0);
  1172. HMI_WRITE("sysTest.switchClear.val=%d",0);
  1173. }
  1174. //DTU,RTU供电
  1175. void ctr_RTU_DTU(uint8_t status_rtu)
  1176. {
  1177. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1178. if(status_rtu==ON)
  1179. {
  1180. rt_pin_write(RTU_DTU,ON);
  1181. }
  1182. else
  1183. {
  1184. LOG_W("RTU电源复位!!!");
  1185. rt_pin_write(RTU_DTU,OFF);
  1186. }
  1187. }
  1188. //海康拍照
  1189. void Hk_Take_io(void)
  1190. {
  1191. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1192. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1193. rt_pin_write(HK_TAKE, 1);
  1194. rt_pin_write(HK_IO, 0);
  1195. if(device_status.dtype==3)
  1196. Beep_Tip(1000);
  1197. if(device_status.dtype==7)
  1198. rt_thread_mdelay(1000);
  1199. rt_pin_write(HK_TAKE, 0);
  1200. rt_pin_write(HK_IO, 1);
  1201. }
  1202. void calorstat(uint16_t heat_time)
  1203. {
  1204. //uint8_t heat_up_cnt = 0;
  1205. uint16_t n = 0;
  1206. uint16_t delay_heat = 60;
  1207. FlagStatus heat_flg = RESET;
  1208. device_status.heat_time_min = 0;
  1209. device_status.run_sta = device_status.heattem; //初始化加热实时温度
  1210. ctr_Heat(ON);
  1211. // UpLoad_Data(); //开始加热时的第一条数据
  1212. //恒温模式
  1213. for(n=0;n < heat_time;n++)
  1214. {
  1215. if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
  1216. {
  1217. heat_flg = SET;
  1218. ctr_Heat(OFF);
  1219. while(delay_heat!=0)
  1220. {
  1221. delay_heat --;
  1222. rt_kprintf("正在恒温加热!!!:%d",delay_heat);
  1223. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"",delay_heat);
  1224. rt_thread_delay(1000);
  1225. if(device_status.heattem>device_status.run_sta + 1){
  1226. device_status.run_sta = device_status.heattem;
  1227. }
  1228. }
  1229. delay_heat = 60;
  1230. //跳出FOR循环
  1231. n = n + 60;
  1232. }
  1233. if(device_status.heattem>device_status.run_sta + 1){
  1234. device_status.run_sta = device_status.heattem;
  1235. }
  1236. if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
  1237. {
  1238. //device_status.run_sta = device_status.heattem;
  1239. ctr_Heat(ON);
  1240. }
  1241. else if (device_status.heattem>=device_status.heatvalue&&heat_flg==SET)
  1242. {
  1243. // if(heat_up_cnt==0)
  1244. // {
  1245. // UpLoad_Data(); //加热到设定温度的数据
  1246. // heat_up_cnt = 1;
  1247. // }
  1248. //device_status.run_sta = device_status.heattem;
  1249. ctr_Heat(OFF);
  1250. }
  1251. if((n % 60)==0)
  1252. {
  1253. device_status.heat_time_min = device_status.heat_time_min + 1;
  1254. // if(device_status.heat_time_min % 5 == 0)
  1255. // {
  1256. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1257. // }
  1258. }
  1259. //如果触发异常退出加热
  1260. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1261. {
  1262. rt_thread_delay(1000);
  1263. if(device_status.bat_status == 1)
  1264. {
  1265. rt_kprintf("============================触发异常退出加热==========================");
  1266. ctr_lamp(OFF,1);
  1267. ctr_Heat(OFF);
  1268. break;
  1269. }
  1270. }
  1271. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1272. {
  1273. ctr_Heat(OFF);
  1274. rt_kprintf("异常退出!!!\n");
  1275. break;
  1276. }
  1277. rt_thread_delay(1000);
  1278. rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
  1279. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"",n,heat_time);
  1280. }
  1281. ctr_Heat(OFF);
  1282. //UpLoad_Data(); //加热结束的数据
  1283. }
  1284. //初始化所有控制驱动,关闭
  1285. void HardWare_Init(uint8_t boot)
  1286. {
  1287. Beep_Tip(200);
  1288. //关闭上仓门
  1289. ctr_cangmen_up(OFF);
  1290. if(device_status.dtype==3){
  1291. //关闭下仓门电机
  1292. ctr_cangmen_down(OFF);
  1293. }
  1294. //关闭镇流器
  1295. if(device_status.autotakepic==0)
  1296. ctr_lamp(OFF,1);
  1297. //关闭震动电机
  1298. ctr_shake(OFF,0);
  1299. //关闭加热
  1300. ctr_Heat(OFF);
  1301. //关闭补光灯
  1302. ctr_led(OFF);
  1303. //关闭12V供电(海康相机,RTU)
  1304. ctr_photo(OFF);
  1305. //关闭清扫电机驱动
  1306. ctr_clear(OFF);
  1307. }
  1308. //拍照
  1309. void HK_TakePhoto(void)
  1310. {
  1311. //打开补光灯
  1312. DTU_Write("takephoto.mp3");
  1313. rt_thread_delay(10000);
  1314. char *takephotojson = makeJson_takephoto();
  1315. DTU_Write("%s",takephotojson);
  1316. rt_free(takephotojson);
  1317. //吸虫塔
  1318. if(device_status.version_type!=6)
  1319. {
  1320. ctr_led(ON);
  1321. if(device_status.version_type==48)
  1322. {
  1323. rt_thread_delay(1000*60);
  1324. }
  1325. else {
  1326. rt_thread_delay(1000*15);
  1327. }
  1328. }
  1329. //触发海康相机拍照
  1330. Hk_Take_io();
  1331. if(device_status.version_type!=6)
  1332. {
  1333. rt_thread_delay(1000*45);
  1334. }
  1335. else {
  1336. ctr_led(ON);
  1337. rt_thread_delay(1000*19);
  1338. }
  1339. ctr_led(OFF);
  1340. }
  1341. //光控测试任务
  1342. void SunTest(void)
  1343. {
  1344. rt_uint16_t ui_timeout_l = 0;
  1345. //未检测到光照,一秒扫描等待,超时30S
  1346. while(Read_Light_Voltage()<500)
  1347. {
  1348. ui_timeout_l ++;
  1349. rt_thread_delay(1000);
  1350. if(ui_timeout_l==30) break;
  1351. }
  1352. rt_thread_delay(3000);
  1353. if(Read_Light_Voltage()<500)
  1354. {
  1355. device_status.sun_status = 0;//光控状态
  1356. }
  1357. if(Read_Light_Voltage()>500)
  1358. {
  1359. rt_thread_delay(1000);
  1360. }
  1361. rt_thread_delay(3000);
  1362. if(Read_Light_Voltage()>500)
  1363. {
  1364. device_status.sun_status = 1;
  1365. ctr_lamp(OFF,1);
  1366. }
  1367. HMI_WRITE("sysTest.switchSun.val=%d",0);
  1368. device_status.sun_test = 0;
  1369. device_status.work_status=0;
  1370. }
  1371. /*上电初始化*/
  1372. void ResetInit(void)
  1373. {
  1374. if(device_status.dtype==7)
  1375. {
  1376. ctr_Motor_Driver(0, 0, 0, 0);
  1377. //关闭电磁阀
  1378. ctr_shake(OFF, 0);
  1379. //关闭加热
  1380. ctr_Heat(OFF);
  1381. //关闭制冷
  1382. ctr_Cold(OFF);
  1383. //关闭补光灯
  1384. ctr_led(OFF);
  1385. //转盘复位
  1386. if(device_status.run_sta==0)
  1387. ctr_turn(0, 0);
  1388. }
  1389. if(device_status.version_type!=6&&device_status.version_type!=7)
  1390. {
  1391. device_status.work_done = 1;
  1392. //1.打开路由器供电
  1393. if(device_status.power_status==1)
  1394. {
  1395. ctr_router(ON);
  1396. ctr_RTU_DTU(ON);
  1397. //2.打开相机供电
  1398. ctr_photo(ON);
  1399. }
  1400. //3.关闭镇流器
  1401. ctr_lamp(OFF,1);
  1402. //4.关闭震动电机
  1403. ctr_shake(OFF, 0);
  1404. //5.关闭加热
  1405. ctr_Heat(OFF);
  1406. //6.关闭补光灯
  1407. ctr_led(OFF);
  1408. if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
  1409. {
  1410. LOG_W("欠压!");
  1411. return;
  1412. }
  1413. //7.关闭上仓门
  1414. device_status.ctr_up_motor = 2;
  1415. // while(device_status.ctr_up_motor!=0)
  1416. // {
  1417. // LOG_I("关闭上仓门...");
  1418. // rt_thread_delay(100);
  1419. // }
  1420. //8.关闭下仓门
  1421. device_status.ctr_down_motor = 2;
  1422. while(device_status.ctr_down_motor!=0)
  1423. {
  1424. LOG_I("关闭下仓门....");
  1425. rt_thread_delay(100);
  1426. }
  1427. //9.清扫
  1428. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1429. {
  1430. LOG_W("异常退出!!!\n");
  1431. return;
  1432. }
  1433. #if 0//LAMP_EN == 0
  1434. ctr_Fan_PUT(OFF);
  1435. ctr_Fan_IN(OFF);
  1436. #endif
  1437. if(device_status.version_type==4||device_status.version_type==41)
  1438. {
  1439. device_status.clear_status=1;
  1440. while(device_status.clear_status==1){
  1441. rt_thread_delay(1000);
  1442. }
  1443. }
  1444. }
  1445. else {
  1446. if(device_status.power_status==1)
  1447. {
  1448. //1.打开路由器供电
  1449. ctr_router(ON);
  1450. ctr_RTU_DTU(ON);
  1451. //2.打开相机供电
  1452. ctr_photo(ON);
  1453. }
  1454. //5.关闭加热
  1455. //6.关闭补光灯
  1456. ctr_led(OFF);
  1457. }
  1458. }
  1459. /*三色光源*/
  1460. void Led_Check(uint8_t tp)
  1461. {
  1462. if(tp==3)
  1463. {
  1464. if(device_status.light_over_tim>2*3600)
  1465. {
  1466. device_status.work_status = 1;
  1467. device_status.lamp_status = 1;
  1468. device_status.lamp1_status = 0;
  1469. device_status.lamp2_status = 0;
  1470. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1471. }
  1472. else if (device_status.light_over_tim>3600&&device_status.light_over_tim<7200) {
  1473. device_status.work_status = 1;
  1474. device_status.lamp_status = 0;
  1475. device_status.lamp1_status = 1;
  1476. device_status.lamp2_status = 0;
  1477. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1478. }
  1479. else if (device_status.light_over_tim<1*3600) {
  1480. device_status.work_status = 1;
  1481. device_status.lamp_status = 0;
  1482. device_status.lamp1_status = 0;
  1483. device_status.lamp2_status = 1;
  1484. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1485. }
  1486. }
  1487. else if (tp==6) {
  1488. if(device_status.light_over_tim>4*3600)
  1489. {
  1490. device_status.work_status = 1;
  1491. device_status.lamp_status = 1;
  1492. device_status.lamp1_status = 0;
  1493. device_status.lamp2_status = 0;
  1494. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1495. }
  1496. else if (device_status.light_over_tim<14400&&device_status.light_over_tim>7200) {
  1497. device_status.work_status = 1;
  1498. device_status.lamp_status = 0;
  1499. device_status.lamp1_status = 1;
  1500. device_status.lamp2_status = 0;
  1501. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1502. }
  1503. else if (device_status.light_over_tim<7200) {
  1504. device_status.work_status = 1;
  1505. device_status.lamp_status = 0;
  1506. device_status.lamp1_status = 0;
  1507. device_status.lamp2_status = 1;
  1508. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1509. }
  1510. }
  1511. else if (tp==9) {
  1512. if(device_status.light_over_tim>6*3600)
  1513. {
  1514. device_status.work_status = 1;
  1515. device_status.lamp_status = 1;
  1516. device_status.lamp1_status = 0;
  1517. device_status.lamp2_status = 0;
  1518. LOG_I("光控定时,计时中:光源1计数:%d",device_status.light_over_tim);
  1519. }
  1520. else if (device_status.light_over_tim<6*3600&&device_status.light_over_tim>3*3600) {
  1521. device_status.work_status = 1;
  1522. device_status.lamp_status = 0;
  1523. device_status.lamp1_status = 1;
  1524. device_status.lamp2_status = 0;
  1525. LOG_I("光控定时,计时中:光源2计数:%d",device_status.light_over_tim);
  1526. }
  1527. else if (device_status.light_over_tim<3*3600) {
  1528. device_status.work_status = 1;
  1529. device_status.lamp_status = 0;
  1530. device_status.lamp1_status = 0;
  1531. device_status.lamp2_status = 1;
  1532. LOG_I("光控定时,计时中:光源3计数:%d",device_status.light_over_tim);
  1533. }
  1534. }
  1535. }
  1536. //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1537. void HardWare_Check(uint8_t boot)
  1538. {
  1539. if(boot==0&&device_status.version_type!=6)
  1540. {
  1541. //停止所有动作 -完成
  1542. HardWare_Init(boot);
  1543. }
  1544. else {
  1545. //停止所有动作
  1546. //读取记录待机功耗(实测大概70mA)
  1547. //(需减去屏幕的功耗大概120mA)
  1548. //动作上仓门,读取功率值保存
  1549. //动作下仓门,读取功率值保存
  1550. //动作清扫,读取功率值保存
  1551. //打开镇流器,读取功率值保存
  1552. //打开海康相机供电,读取功率值保存
  1553. //打开补光灯,读取功率值保存
  1554. //打开路由器/RTU供电,读取功率值保存
  1555. //打开加热,读取功率值保存
  1556. }
  1557. }
  1558. //工作状态检测
  1559. void WorkCheck(void)
  1560. {
  1561. static rt_uint32_t light_timeout = 3600;
  1562. //雨控检测,温控检测,电池电压检测
  1563. while(device_status.ds==1&&device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
  1564. {
  1565. //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1566. if(device_status.timeSwitch==1)
  1567. {
  1568. LOG_I("***********时控*****************");
  1569. if(device_status.startBar==device_status.stopBar&&device_status.startBar==0){
  1570. device_status.work_status = 1;
  1571. LOG_I("24 hours work...");
  1572. #if LAMP_EN == 1
  1573. device_status.lamp_status = 1;
  1574. device_status.lamp1_status = 0;
  1575. device_status.lamp2_status = 0;
  1576. if(device_status.sun_status==1){
  1577. ctr_lamp(OFF, 1);
  1578. ctr_lamp(OFF, 2);
  1579. ctr_lamp(OFF, 3);
  1580. }
  1581. #endif
  1582. }
  1583. //如果开始和结束是同一天
  1584. if(device_status.startBar<device_status.stopBar)
  1585. {
  1586. LOG_I("开始和结束是同一天");
  1587. time_t now;
  1588. time(&now);
  1589. static struct tm nowtime; //现在
  1590. static struct tm starttime; //开始
  1591. static struct tm endtime; //结束
  1592. //获取当前时间戳
  1593. nowtime =*localtime(&now);
  1594. nowtime.tm_hour = nowtime.tm_hour-8;
  1595. //获取定时开始的时间戳
  1596. starttime = *localtime(&now);
  1597. starttime.tm_hour = device_status.startBar-8;
  1598. starttime.tm_min = 0;
  1599. starttime.tm_sec = 0;
  1600. //获取定时结束的时间戳
  1601. endtime = *localtime(&now);
  1602. endtime.tm_hour = device_status.stopBar-8;
  1603. endtime.tm_min = 0;
  1604. endtime.tm_sec = 0;
  1605. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1606. //如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1607. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
  1608. {
  1609. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1610. device_status.work_status = 1;
  1611. }
  1612. //如果当前时间小于开始时间
  1613. else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
  1614. {
  1615. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1616. device_status.work_status = 0;
  1617. }
  1618. //当前时间大于结束时间
  1619. else {
  1620. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d",mktime(&nowtime),mktime(&starttime));
  1621. device_status.work_status = 0;
  1622. }
  1623. #if LAMP_EN == 1
  1624. //如果当前时间超过20点,未到21点
  1625. if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+3600)<0))
  1626. {
  1627. LOG_I("3led:20~21");
  1628. device_status.lamp_status = 1;
  1629. device_status.lamp1_status = 0;
  1630. device_status.lamp2_status = 0;
  1631. }
  1632. else if ((difftime(mktime(&nowtime), mktime(&starttime)+3600) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+7200)<0)) {
  1633. LOG_I("3led:21~22");
  1634. device_status.lamp1_status = 1;
  1635. device_status.lamp_status = 0;
  1636. device_status.lamp2_status = 0;
  1637. }
  1638. else if ((difftime(mktime(&nowtime), mktime(&starttime)+7200) >=0) && (difftime(mktime(&nowtime),mktime(&starttime)+10800)<0)) {
  1639. LOG_I("3led:22~23");
  1640. device_status.lamp2_status = 1;
  1641. device_status.lamp_status = 0;
  1642. device_status.lamp1_status =0;
  1643. }
  1644. #endif
  1645. }
  1646. //如果结束时间是第二天
  1647. else if (device_status.startBar>device_status.stopBar) {
  1648. time_t now;
  1649. time(&now);
  1650. static struct tm nowtime; //现在
  1651. static struct tm starttime; //开始
  1652. static struct tm endtime; //结束
  1653. //获取当前时间戳
  1654. nowtime =*localtime(&now);
  1655. nowtime.tm_hour = nowtime.tm_hour-8;
  1656. //获取定时开始的时间戳
  1657. starttime = *localtime(&now);
  1658. starttime.tm_hour = device_status.startBar-8;
  1659. starttime.tm_min = 0;
  1660. starttime.tm_sec = 0;
  1661. //获取定时结束的时间戳
  1662. endtime = *localtime(&now);
  1663. endtime.tm_hour = device_status.stopBar-8;
  1664. endtime.tm_min = 0;
  1665. endtime.tm_sec = 0;
  1666. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
  1667. //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1668. if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
  1669. {
  1670. //获取定时结束的时间戳
  1671. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1672. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1673. endtime.tm_min = 0;
  1674. endtime.tm_sec = 0;
  1675. device_status.work_status = 1;
  1676. LOG_I("当前前时间大于开始时间,且没有开始工作");
  1677. }
  1678. //当前时间小于开始时间
  1679. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  1680. {
  1681. //获取定时结束的时间戳
  1682. endtime = *localtime(&now); //将获取到的时间转换为时间戳
  1683. endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
  1684. endtime.tm_min = 0;
  1685. endtime.tm_sec = 0;
  1686. //如果当前时间戳小于结束时间戳
  1687. if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
  1688. {
  1689. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  1690. device_status.work_status = 1;
  1691. }
  1692. else
  1693. {
  1694. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  1695. device_status.work_status = 0; //不在工作时间,待机
  1696. }
  1697. }
  1698. }
  1699. }
  1700. else {
  1701. LOG_I("光控定时模式!");
  1702. /*
  1703. time_t now;
  1704. time(&now);
  1705. static struct tm nowstamp;
  1706. nowstamp =*localtime(&now);
  1707. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  1708. static rt_uint32_t nowstamp_num = 0;
  1709. nowstamp_num = mktime(&nowstamp);
  1710. rt_kprintf("now stamp:%d\n",nowstamp_num);
  1711. */
  1712. if(device_status.sun_status==0&&device_status.light_over_tim!=0)
  1713. {
  1714. device_status.light_over_tim--;
  1715. light_timeout = 3600;
  1716. #if LAMP_EN == 1
  1717. Led_Check(device_status.sunBar);
  1718. #else
  1719. device_status.work_status = 1;;
  1720. LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
  1721. #endif
  1722. }
  1723. else
  1724. {
  1725. light_timeout --;
  1726. //没有在测试并且没有在自检
  1727. if(device_status.test_flag==0&&device_status.selfcheck_ws==0)
  1728. {
  1729. ctr_lamp(OFF,1);
  1730. #if LAMP_EN == 1
  1731. ctr_lamp(OFF,2);
  1732. ctr_lamp(OFF,3);
  1733. #endif
  1734. }
  1735. device_status.work_status = 0;
  1736. if(light_timeout==0) device_status.light_over_tim = 0; //持续1小时白天,清除光控计时,20250519
  1737. if(device_status.light_over_tim==0&&device_status.sun_status==1) device_status.light_over_tim = device_status.sunBar * 3600;
  1738. LOG_I("光控中,白天:%d!!!",device_status.sun_status);
  1739. }
  1740. }
  1741. rt_thread_delay(1000);
  1742. }
  1743. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d",device_status.rain_status,device_status.temp_status,device_status.bat_status);
  1744. device_status.work_status = 0;
  1745. rt_thread_delay(1000);
  1746. }
  1747. //工作任务流程s
  1748. void WorkTask(void)
  1749. {
  1750. static int clt_time = 0; //收集时间差秒数
  1751. static int heat_time = 0;
  1752. LOG_I("工作状态:%d",device_status.work_status);
  1753. LOG_I("开关机状态:%d",device_status.ds);
  1754. while(device_status.work_status==1&&device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1&&device_status.clear_status==0)
  1755. {
  1756. device_status.work_done = 0;
  1757. if(clt_time==0)
  1758. {
  1759. #if 0
  1760. if(device_status.version_type==41&&atoi(device_status.power)<350)
  1761. {
  1762. LOG_W("相机电流不足!");
  1763. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  1764. DTU_Write("%s",ctr_hk_p_warn);
  1765. rt_free(ctr_hk_p_warn);
  1766. }
  1767. #endif
  1768. #if 0//LAMP_EN == 0
  1769. ctr_Fan_PUT(ON);
  1770. #endif
  1771. DTU_Write("workstart.mp3");
  1772. }
  1773. //1.打开灯管
  1774. #if LAMP_EN == 1
  1775. if(device_status.lamp_status==1&&device_status.sun_status==0) //光控状态不亮灯
  1776. {
  1777. ctr_lamp(ON,1);
  1778. ctr_lamp(OFF,2);
  1779. ctr_lamp(OFF,3);
  1780. }
  1781. else if (device_status.lamp1_status==1&&device_status.sun_status==0) {
  1782. ctr_lamp(OFF,1);
  1783. ctr_lamp(ON,2);
  1784. ctr_lamp(OFF,3);
  1785. }
  1786. else if (device_status.lamp2_status==1&&device_status.sun_status==0) {
  1787. ctr_lamp(OFF,1);
  1788. ctr_lamp(OFF,2);
  1789. ctr_lamp(ON,3);
  1790. }
  1791. #else
  1792. ctr_lamp(ON,1);
  1793. #endif
  1794. LOG_I("打开灯管!!!");
  1795. #if 0
  1796. rt_thread_delay(2000);
  1797. if(atoi(device_status.power)<450)
  1798. {
  1799. LOG_I("Current is:%d",atoi(device_status.power));
  1800. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  1801. {
  1802. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  1803. DTU_Write("%s",lamp_err_warn);
  1804. rt_free(lamp_err_warn);
  1805. }
  1806. }
  1807. #endif
  1808. if(device_status.version_type!=5)
  1809. {
  1810. rt_thread_delay(1000); //待灯管启动再往下执行,否则会触发电机过流
  1811. //2.打开上仓门
  1812. LOG_I("打开上仓门!!!");
  1813. device_status.ctr_up_motor = 1;
  1814. //3.收集
  1815. clt_time = device_status.clt_time * 60;
  1816. while(clt_time!=1)
  1817. {
  1818. #if 0//LAMP_EN == 0
  1819. ctr_Fan_IN(ON);
  1820. #endif
  1821. clt_time --;
  1822. rt_thread_delay(1000);
  1823. #if 0//LAMP_EN == 0
  1824. if(clt_time%30==0)
  1825. ctr_Fan_PUT(OFF);
  1826. #endif
  1827. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
  1828. //如果触发异常
  1829. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1830. {
  1831. ctr_lamp(OFF,1);
  1832. #if LAMP_EN == 1
  1833. ctr_lamp(OFF,2);
  1834. ctr_lamp(OFF,3);
  1835. #endif
  1836. #if 0//LAMP_EN == 0
  1837. ctr_Fan_IN(OFF);
  1838. #endif
  1839. rt_kprintf("异常退出!!!\n");
  1840. break;
  1841. }
  1842. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1843. {
  1844. ctr_lamp(OFF,1);
  1845. #if LAMP_EN == 1
  1846. ctr_lamp(OFF,2);
  1847. ctr_lamp(OFF,3);
  1848. #endif
  1849. #if 0//LAMP_EN == 0
  1850. ctr_Fan_IN(OFF);
  1851. #endif
  1852. rt_kprintf("异常退出!!!\n");
  1853. break;
  1854. }
  1855. }
  1856. clt_time = 1;
  1857. if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
  1858. {
  1859. ctr_lamp(OFF,1);
  1860. #if LAMP_EN == 1
  1861. ctr_lamp(OFF,2);
  1862. ctr_lamp(OFF,3);
  1863. #endif
  1864. #if 0//LAMP_EN == 0
  1865. ctr_Fan_IN(OFF);
  1866. #endif
  1867. rt_kprintf("异常退出!!!\n");
  1868. break;
  1869. }
  1870. if(device_status.timeSwitch==0&&device_status.sun_status==1)
  1871. {
  1872. ctr_lamp(OFF,1);
  1873. #if LAMP_EN == 1
  1874. ctr_lamp(OFF,2);
  1875. ctr_lamp(OFF,3);
  1876. #endif
  1877. #if 0//LAMP_EN == 0
  1878. ctr_Fan_IN(OFF);
  1879. #endif
  1880. rt_kprintf("异常退出!!!\n");
  1881. break;
  1882. }
  1883. #if 0//LAMP_EN == 0
  1884. ctr_Fan_IN(OFF);
  1885. #endif
  1886. //4.关闭上仓门
  1887. device_status.ctr_up_motor = 2;
  1888. rt_kprintf("关闭上仓门!!!\n");
  1889. while(device_status.ctr_up_motor!=0)
  1890. {
  1891. rt_thread_delay(100);
  1892. LOG_I("等待上仓门关闭!!!");
  1893. }
  1894. //5.加热
  1895. if(device_status.heat_sw==0)
  1896. {
  1897. calorstat(device_status.heattime*60);
  1898. }
  1899. else {
  1900. heat_time = device_status.heattime * 60;
  1901. while(heat_time!=0)
  1902. {
  1903. heat_time--;
  1904. LOG_I("正在加热状态!!!");
  1905. rt_thread_delay(1000);
  1906. }
  1907. }
  1908. //6.打开下仓门
  1909. if(device_status.rain_status==0)
  1910. {
  1911. device_status.ctr_down_motor =1;
  1912. }
  1913. else {
  1914. device_status.ctr_down_motor = 2;
  1915. device_status.ctr_up_motor = 2;
  1916. }
  1917. //待下仓门完全打开
  1918. while(device_status.ctr_down_motor!=0)
  1919. {
  1920. rt_thread_delay(100);
  1921. rt_kprintf("等待下仓门打开!!!\n");
  1922. }
  1923. rt_thread_delay(3000);
  1924. //关闭下仓门
  1925. device_status.ctr_down_motor = 2;
  1926. while(device_status.ctr_down_motor!=0)
  1927. {
  1928. rt_thread_delay(100);
  1929. rt_kprintf("等待下仓门关闭!!!\n");
  1930. }
  1931. //2022-04-12add:
  1932. LOG_I("打开上仓门!!!");
  1933. device_status.ctr_up_motor = 1;
  1934. if(device_status.version_type==4||device_status.version_type==41)
  1935. {
  1936. //震动
  1937. ctr_shake(ON, device_status.shake_sec);
  1938. //补光&&拍照
  1939. LOG_I("拍照!!!");
  1940. ctr_led(ON);
  1941. HK_TakePhoto();
  1942. //清扫
  1943. device_status.clear_status=1;
  1944. //清扫失效则使用震动
  1945. //ctr_shake(ON, 100);
  1946. }
  1947. }
  1948. else {
  1949. //打开高压
  1950. ctr_Heat(ON);
  1951. //打开补光
  1952. ctr_led(ON);
  1953. LOG_I("打开补光!!");
  1954. rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
  1955. //2.翻转清虫
  1956. while(rt_pin_read(CLEAR_L_LIMIT)==1) rt_thread_delay(20);
  1957. rt_thread_delay(500);
  1958. Hk_Take_io(); //触发海康相机拍照
  1959. rt_thread_delay(500);
  1960. ctr_turnv5(); //摆动清虫
  1961. LOG_I("摆动清虫!");
  1962. }
  1963. }
  1964. if(device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status==1 || device_status.ds==0)
  1965. {
  1966. LOG_I("-------------------not work--------------------");
  1967. device_status.work_status = 0;
  1968. }
  1969. //工作结束
  1970. if(device_status.work_done==0&&device_status.work_status==0)
  1971. {
  1972. DTU_Write("workdone");
  1973. if (device_status.version_type==1) {
  1974. turnzp(); //2024-03-21 :开机不再转仓
  1975. }
  1976. ResetInit();
  1977. clt_time = 0;
  1978. }
  1979. rt_kprintf("***not work-device status:%d\n",device_status.ds);
  1980. }
  1981. void guanshan_on(void *args)
  1982. {
  1983. LOG_W("guanshan_on!");
  1984. rt_event_send(event_485_play, 0x01);
  1985. }
  1986. static void pin_guangshan_bind(void)
  1987. {
  1988. /* PC14引脚为输入模式 */
  1989. rt_pin_mode(GUANG_SHAN, PIN_MODE_INPUT);
  1990. /* 绑定中断,下降沿模式,回调函数名为guanshan_on */
  1991. rt_pin_attach_irq(GUANG_SHAN, PIN_IRQ_MODE_RISING, guanshan_on, RT_NULL);
  1992. /* 使能中断 */
  1993. rt_pin_irq_enable(GUANG_SHAN, PIN_IRQ_ENABLE);
  1994. }
  1995. static void pin_guangshan_unbind(void)
  1996. {
  1997. rt_pin_detach_irq(GUANG_SHAN);
  1998. }
  1999. #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
  2000. void slidelock()
  2001. {
  2002. ctr_Motor_Driver_Power(ON); //电机驱动供电
  2003. rt_thread_delay(10);
  2004. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  2005. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  2006. }
  2007. void motor_zero()
  2008. {
  2009. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  2010. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  2011. rt_pin_mode(CTR_RO_LIMIT, PIN_MODE_OUTPUT); //CCW PB15 配置为输出模式
  2012. rt_pin_mode(CTR_RC_LIMIT, PIN_MODE_OUTPUT); //CW PC6 配置为输出模式
  2013. rt_thread_delay(300);
  2014. rt_thread_delay(300);
  2015. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT); //GET_PIN(B,8)
  2016. rt_pin_write(CTR_RO_LIMIT, OFF); //CCW back配置
  2017. uint32_t j;
  2018. for(j=0;j<3200;j++) //多配置1600个脉冲,保证肯定能归零;
  2019. {
  2020. rt_pin_write(CTR_RC_LIMIT, OFF);
  2021. rt_thread_delay(PULES);
  2022. rt_pin_write(CTR_RC_LIMIT, ON);
  2023. rt_thread_delay(PULES);
  2024. if( rt_pin_read(Motor_Direction_Button)==0)//判断得到限位信号
  2025. {
  2026. rt_thread_delay(5);
  2027. if( rt_pin_read(Motor_Direction_Button)==0)
  2028. {
  2029. slidelock();
  2030. break;
  2031. }
  2032. }
  2033. }
  2034. rt_thread_delay(300);
  2035. }
  2036. uint16_t ctr_xmove(uint32_t xdis,uint16_t pulse)
  2037. {
  2038. //rt_kprintf("run here1");
  2039. uint32_t j;
  2040. uint32_t xtemp = 0;
  2041. xtemp = xdis; //两个脉冲移动0.72度,轴方向上移动2um
  2042. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
  2043. rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
  2044. rt_pin_mode(CTR_RO_LIMIT, PIN_MODE_OUTPUT); //CCW PB15 配置为输出模式
  2045. rt_pin_mode(CTR_RC_LIMIT, PIN_MODE_OUTPUT); //CW PC6 配置为输出模式
  2046. //rt_kprintf("run here2");
  2047. rt_pin_write(CTR_RO_LIMIT, OFF); //CCW 置高
  2048. rt_thread_delay(300);
  2049. for(j=0;j<xtemp;j++)
  2050. {
  2051. //rt_kprintf("run here4");
  2052. if((j <= 0.1*xtemp))
  2053. {
  2054. //rt_kprintf("run here5");
  2055. //rt_kprintf("xtemp is %d",xtemp );
  2056. rt_pin_write(CTR_RC_LIMIT, OFF);
  2057. //rt_kprintf("run here10");
  2058. //rt_kprintf("pulse is %d",pulse);
  2059. //rt_thread_delay(1); //前10%以线性加速运动
  2060. rt_hw_us_delay(pulse*(0.1*xtemp - j + 20)/20); //前10%以线性加速运动
  2061. rt_pin_write(CTR_RC_LIMIT, ON);
  2062. //rt_kprintf("run here11");
  2063. //rt_thread_delay(1);
  2064. rt_hw_us_delay(pulse*(0.1*xtemp - j + 20)/20);
  2065. //rt_kprintf("run here9");
  2066. }
  2067. if((j >0.1*xtemp)&&(j <= 0.9*xtemp))
  2068. {
  2069. //rt_kprintf("run here6");
  2070. rt_pin_write(CTR_RC_LIMIT, OFF);
  2071. rt_hw_us_delay(pulse);
  2072. rt_pin_write(CTR_RC_LIMIT, ON);
  2073. rt_hw_us_delay(pulse);
  2074. }
  2075. if((j > 0.9*xtemp))
  2076. {
  2077. //rt_kprintf("run here7");
  2078. rt_pin_write(CTR_RC_LIMIT, OFF);
  2079. rt_hw_us_delay(pulse*(j-0.9*xtemp + 20)/20); //后10%以线性减速运动
  2080. rt_pin_write(CTR_RC_LIMIT, ON);
  2081. rt_hw_us_delay(pulse*(j-0.9*xtemp + 20)/20);
  2082. }
  2083. //rt_kprintf("run here8");
  2084. }
  2085. slidelock();
  2086. return xdis;
  2087. }
  2088. /*
  2089. * 灯管
  2090. *
  2091. * */
  2092. void WorkTask_CB5(void)
  2093. {
  2094. static int work_loop = 0;
  2095. while(device_status.work_status==1&&device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1)
  2096. {
  2097. work_loop++;
  2098. device_status.work_done = 0;
  2099. //发送串口消息,通知yolo是被及相机采集模块启动,定期将模块的运行状态展示到屏幕上
  2100. if(work_loop%60==0||work_loop==1)
  2101. {
  2102. DTU_Write("{\"cmd\":\"workstart\"}");
  2103. //推杆电机1打开,导流板到落虫通道
  2104. device_status.ctr_up_motor = 1;
  2105. while(device_status.ctr_up_motor!=0)
  2106. {
  2107. rt_thread_delay(2000);
  2108. LOG_I("等待上仓门打开....");
  2109. }
  2110. //推杆电机2关闭,益虫仓门关闭
  2111. device_status.ctr_down_motor = 2;
  2112. while(device_status.ctr_down_motor!=0)
  2113. {
  2114. rt_thread_delay(2000);
  2115. LOG_I("等待益虫仓门打开....");
  2116. }
  2117. //打开86步进电机驱动器供电
  2118. ctr_Motor_Driver_Power(ON);
  2119. //风机启动
  2120. //打开灯管
  2121. ctr_lamp(ON,1);
  2122. //打开补光灯
  2123. ctr_led(ON);
  2124. //光栅启动
  2125. ctr_photo(ON);
  2126. //
  2127. pin_guangshan_bind();
  2128. pin_motor_86_bind();
  2129. }
  2130. rt_thread_delay(1000);
  2131. }
  2132. device_status.work_status = 0;
  2133. //工作结束
  2134. if(device_status.work_done==0&&device_status.work_status==0)
  2135. {
  2136. pin_guangshan_unbind();
  2137. pin_motor_86_unbind();
  2138. //打开推杆电机,益虫放生
  2139. device_status.ctr_down_motor = 1;
  2140. DTU_Write("{\"cmd\":\"workdone\"}");
  2141. ResetInit();
  2142. }
  2143. work_loop = 0;
  2144. while(device_status.work_status==0)
  2145. {
  2146. work_loop++;
  2147. LOG_I("WorkTask_CB5 sleeping......%d",work_loop);
  2148. if(work_loop>120)
  2149. {
  2150. work_loop = 0;
  2151. DTU_Write("{\"cmd\":\"workdone\"}");
  2152. }
  2153. rt_thread_delay(5000);
  2154. }
  2155. }
  2156. /* 入口函数-工作状态检测 */
  2157. static void Work_status_thread_entry(void *parameter)
  2158. {
  2159. static uint16_t sun_status_num = 120;
  2160. //如果是光控定时,进行光控监测的消抖
  2161. if(device_status.timeSwitch==0&&device_status.sun_status==0)
  2162. {
  2163. rt_kprintf("光控定时!!!\n");
  2164. while(sun_status_num!=0)
  2165. {
  2166. sun_status_num--;
  2167. rt_thread_delay(1000);
  2168. rt_kprintf("开机光控检测:%d\n",sun_status_num);
  2169. }
  2170. device_status.light_over_tim = device_status.sunBar * 3600;
  2171. device_status.test_flag = 0;
  2172. }
  2173. while(1)
  2174. {
  2175. WorkCheck();
  2176. }
  2177. }
  2178. /*入口函数-工作流程*/
  2179. static void Work_Loop_thread_entry(void *parameter)
  2180. {
  2181. rt_thread_delay(1000);
  2182. ResetInit();
  2183. while(1)
  2184. {
  2185. if(device_status.version_type!=5&&device_status.dtype==3)
  2186. {
  2187. WorkTask();
  2188. }
  2189. if(device_status.version_type==5)
  2190. {
  2191. WorkTask_CB5();
  2192. }
  2193. rt_thread_delay(1000);
  2194. }
  2195. }
  2196. /*入口函数清扫*/
  2197. static void Control_Clear_thread_entry(void *parameter)
  2198. {
  2199. while(1)
  2200. {
  2201. if(device_status.update_flag==1) break;
  2202. if(device_status.clear_status==1)
  2203. {
  2204. if(device_status.version_type==41){
  2205. ctr_TG_Clear(300);
  2206. }else {
  2207. ctr_TG_Clear(200);
  2208. }
  2209. }
  2210. rt_thread_delay(100);
  2211. }
  2212. }
  2213. /*入口函数加热*/
  2214. static void Control_Heat_thread_entry(void *parameter)
  2215. {
  2216. static int total_seconds = 200;
  2217. static int current_second = 0;
  2218. static int process = 0;
  2219. while(1)
  2220. {
  2221. if(device_status.update_flag==1) break;
  2222. if(device_status.work_status==1&&device_status.heat_sw==1)
  2223. {
  2224. ctr_lamp(ON, 1);
  2225. rt_thread_delay(10000);
  2226. calorstat(device_status.heattime*60);
  2227. }
  2228. else
  2229. {
  2230. if(device_status.selfcheck_ws==2)
  2231. {
  2232. while(current_second <= total_seconds)
  2233. {
  2234. process = (current_second * 100) / total_seconds;
  2235. device_status.process = process / 100.0;
  2236. LOG_I("process:%d",process);
  2237. HMI_WRITE("selfcheck.s106.val=%d",process);
  2238. rt_thread_delay(1000);
  2239. current_second++;
  2240. if(device_status.selfcheck_ws==0)
  2241. {
  2242. HMI_WRITE("selfcheck.s106.val=100");
  2243. break;
  2244. }
  2245. }
  2246. HMI_WRITE("selfcheck.s106.val=100");
  2247. }else {
  2248. process = 0;
  2249. current_second = 0;
  2250. }
  2251. }
  2252. rt_thread_delay(1000);
  2253. }
  2254. }
  2255. static void thread_485_play_entry(void *param)
  2256. {
  2257. static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
  2258. rt_uint32_t e;
  2259. // 创建事件控制块
  2260. event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
  2261. if (event_485_play == RT_NULL)
  2262. {
  2263. LOG_E("Event create failed!");
  2264. return;
  2265. }
  2266. while(1)
  2267. {
  2268. if(device_status.update_flag==1) break;
  2269. //等待事件
  2270. if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
  2271. {
  2272. LOG_I("Interrupt occurred and processed.");
  2273. _485_play(sound105, 4);
  2274. //发送串口消息,通知JETSON计算卡进行拍照
  2275. DTU_Write("{\"guangshan\":\"on\"}");
  2276. }
  2277. }
  2278. }
  2279. /*清扫线程*/
  2280. int Control_clear(void)
  2281. {
  2282. /* 创建线程*/
  2283. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  2284. Control_Clear_thread_entry, RT_NULL,
  2285. 2048, //2048
  2286. 24, //23
  2287. WORK_CHECK_THREAD_TIMESLICE); //5
  2288. if(tid_ctr!=RT_NULL)
  2289. {
  2290. rt_thread_startup(tid_ctr);
  2291. }
  2292. return 0;
  2293. }
  2294. /*恒温加热线程*/
  2295. int Control_heat(void)
  2296. {
  2297. /* 创建线程*/
  2298. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  2299. Control_Heat_thread_entry, RT_NULL,
  2300. 1024, //2048
  2301. 25, //23
  2302. WORK_CHECK_THREAD_TIMESLICE); //5
  2303. if(tid_heat!=RT_NULL)
  2304. {
  2305. rt_thread_startup(tid_heat);
  2306. }
  2307. return 0;
  2308. }
  2309. int thread_485_play(void)
  2310. {
  2311. tid_485_play = rt_thread_create("thread_485play",
  2312. thread_485_play_entry, RT_NULL,
  2313. 1024, //1024
  2314. 26, //23
  2315. 5); //5
  2316. if(tid_485_play!=RT_NULL)
  2317. {
  2318. rt_thread_startup(tid_485_play);
  2319. }
  2320. else {
  2321. LOG_E("thread_485_play start failed!");
  2322. }
  2323. return 0;
  2324. }
  2325. /* 创建线程 -工作状态检测*/
  2326. int Workstatus_check(void)
  2327. {
  2328. /* 创建线程*/
  2329. tid_wkstc = rt_thread_create("work_status_check_thread",
  2330. Work_status_thread_entry, RT_NULL,
  2331. WORK_CHECK_THREAD_STACK_SIZE, //1024
  2332. WORK_CHECK_THREAD_PRIORITY, //23
  2333. WORK_CHECK_THREAD_TIMESLICE); //5
  2334. if(tid_wkstc!=RT_NULL)
  2335. {
  2336. rt_thread_startup(tid_wkstc);
  2337. }
  2338. return 0;
  2339. }
  2340. /*工作流程线程*/
  2341. int Work_Loop_Task(void)
  2342. {
  2343. rt_err_t result_loop = 0;
  2344. /* 创建线程*/
  2345. tid_workloop = rt_thread_create("work_loop_thread",
  2346. Work_Loop_thread_entry, RT_NULL,
  2347. WORK_LOOP_THREAD_STACK_SIZE, //2048
  2348. WORK_LOOP_THREAD_PRIORITY, //12
  2349. WORK_LOOP_THREAD_TIMESLICE); //5
  2350. if(tid_workloop!=RT_NULL)
  2351. {
  2352. result_loop = rt_thread_startup(tid_workloop);
  2353. }
  2354. return result_loop;
  2355. }
  2356. MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
  2357. MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
  2358. MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
  2359. MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
  2360. MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
  2361. //INIT_APP_EXPORT(Control_heat);