control.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655
  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 "test.h"
  18. #include "sht30_collect.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. #define LOG_INR 10 // 默认的log打印间隔(作用域在本文件)
  34. extern Res_def JD_Check_Test_Sta(JD_Test_Type type);
  35. extern void Report_Limit_Error(le_def type);
  36. extern RTC_DateTypeDef GetDate;
  37. extern RTC_TimeTypeDef GetTime;
  38. volatile works_def ws_now = ws_standby;
  39. // 设备状态表结构体
  40. Device_Status_typedef device_status;
  41. rt_thread_t tid_workloop = RT_NULL;
  42. rt_thread_t tid_wkstc = RT_NULL;
  43. rt_thread_t tid_ctr = RT_NULL;
  44. rt_thread_t tid_heat = RT_NULL;
  45. /**************************1.0定制2000W*************************************/
  46. //rt_pin_mode
  47. /**
  48. * @brief 控制风扇输出
  49. *
  50. * @param status_ws 开关量
  51. * @return uint8_t -
  52. */
  53. uint8_t ctr_Fan_PUT(uint8_t status_ws)
  54. {
  55. rt_pin_mode(FAN_P, PIN_MODE_OUTPUT);
  56. if (status_ws == ON)
  57. {
  58. rt_pin_write(FAN_P, ON);
  59. }
  60. else
  61. {
  62. rt_pin_write(FAN_P, OFF);
  63. }
  64. return 0;
  65. }
  66. /**
  67. * @brief 控制风扇输出
  68. *
  69. * @param status_ws 开关量
  70. * @return uint8_t -
  71. */
  72. uint8_t ctr_Fan_IN(uint8_t status_ws)
  73. {
  74. rt_pin_mode(FAN_I, PIN_MODE_OUTPUT);
  75. if (status_ws == ON)
  76. {
  77. rt_pin_write(FAN_I, ON);
  78. }
  79. else
  80. {
  81. rt_pin_write(FAN_I, OFF);
  82. }
  83. return 0;
  84. }
  85. /****************************定制色诱*************************************/
  86. /**/
  87. #if Color_EN == 1
  88. /**
  89. * @brief 检测色诱按键是否有下降沿出现
  90. *
  91. * @param key_pin 引脚号
  92. * @return uint8_t 无意义
  93. */
  94. uint8_t Read_Color_Key(rt_base_t key_pin)
  95. {
  96. rt_uint32_t sta_s = 0;
  97. rt_pin_mode(key_pin, PIN_MODE_INPUT);
  98. while (rt_pin_read(key_pin) == 1)
  99. {
  100. sta_s++;
  101. /*持续的*/
  102. rt_thread_delay(50);
  103. }
  104. while (rt_pin_read(key_pin) == 0)
  105. {
  106. rt_thread_delay(50);
  107. if (sta_s > 0)
  108. break;
  109. }
  110. LOG_W("检测到IO0翻转!!!");
  111. return 1;
  112. }
  113. /*读限位开关状态*/
  114. /**
  115. * @brief 读取限位开关的状态
  116. *
  117. * @param pin_limit 引脚号
  118. * @return uint8_t 读取结果
  119. */
  120. uint8_t Read_Color_Limit(rt_base_t pin_limit)
  121. {
  122. uint8_t l_sta = 0;
  123. rt_pin_mode(pin_limit, PIN_MODE_INPUT);
  124. l_sta = rt_pin_read(pin_limit);
  125. rt_thread_delay(20);
  126. l_sta = rt_pin_read(pin_limit);
  127. return l_sta;
  128. }
  129. /*色诱板*/
  130. /**
  131. * @brief 色诱板的控制逻辑
  132. *
  133. * @param forward 下一步进行的动作
  134. * @param time_board 计数?
  135. * @return int 操作完成的动作
  136. */
  137. int ctr_Color_board(uint8_t forward, uint16_t time_board)
  138. {
  139. if (forward == CLOSE) // 归位
  140. {
  141. if (device_status.cul_time > 0)
  142. device_status.cul_time--;
  143. ctr_cangmen_down(OFF);
  144. ctr_cangmen_up(OFF);
  145. rt_thread_delay(1000);
  146. // if(device_status.cul_time>7) device_status.cul_time=7;
  147. // if(device_status.cul_time!=7)
  148. // {
  149. // uint8_t ct = device_status.cul_time;
  150. // while(1)
  151. // {
  152. // Beep_Tip(100);
  153. // rt_thread_delay(100);
  154. // ct--;
  155. // if(ct==0) break;
  156. // }
  157. // //return device_status.cul_time;
  158. // }
  159. if (Read_Color_Limit(LIMIT_RESET) == 0)
  160. {
  161. device_status.cul_time = 0;
  162. LOG_W("已在初始位置!");
  163. Beep_Tip(1000);
  164. return 0;
  165. // 黄板已经在复位状态,直接返回0;
  166. }
  167. ctr_cangmen_up(CLOSE);
  168. rt_thread_delay(20000);
  169. ctr_cangmen_up(OFF);
  170. // if(device_status.cul_time==7&&Read_Color_Limit(LIMIT_BOARD)==0)
  171. // {
  172. // LOG_W("第七天!!!");
  173. // ctr_cangmen_down(CLOSE);
  174. // rt_thread_delay(500);
  175. // }
  176. LOG_W("先打开运行到限位的右侧!");
  177. while (Read_Color_Limit(LIMIT_BOARD) == 0)
  178. {
  179. LOG_W("归位:到达限位中间");
  180. ctr_cangmen_down(CLOSE);
  181. if (Read_Color_Limit(LIMIT_RESET) == 0)
  182. {
  183. ctr_cangmen_down(OFF);
  184. break;
  185. }
  186. }
  187. // 先打开运行到限位的右侧
  188. while (Read_Color_Limit(LIMIT_BOARD) == 1)
  189. {
  190. LOG_W("归位:运行!");
  191. ctr_cangmen_down(CLOSE);
  192. if (Read_Color_Limit(LIMIT_RESET) == 0)
  193. {
  194. ctr_cangmen_down(OFF);
  195. break;
  196. }
  197. }
  198. while (Read_Color_Limit(LIMIT_BOARD) == 0) // 到达限位中间 继续运行
  199. {
  200. LOG_W("归位:到达限位中间");
  201. ctr_cangmen_down(CLOSE);
  202. if (Read_Color_Limit(LIMIT_RESET) == 0)
  203. {
  204. ctr_cangmen_down(OFF);
  205. break;
  206. }
  207. }
  208. if (Read_Color_Limit(LIMIT_RESET) == 0)
  209. return 0;
  210. LOG_W("到达限位左侧!");
  211. // 停止电机
  212. ctr_cangmen_down(OFF);
  213. rt_thread_delay(2000);
  214. // 将推杆电机归位
  215. LOG_D("*推杆电机归位*");
  216. /*推杆电机伸出*/
  217. ctr_cangmen_up(OPEN);
  218. rt_thread_delay(20000);
  219. ctr_cangmen_up(OFF);
  220. return 2;
  221. }
  222. else if (forward == OPEN)
  223. {
  224. ctr_cangmen_down(OFF);
  225. ctr_cangmen_up(OFF);
  226. rt_thread_delay(1000);
  227. if (device_status.cul_time == 7)
  228. {
  229. Beep_Tip(5000);
  230. return 7;
  231. }
  232. LOG_W("打开推杆电机!");
  233. // 2023-04-18:add
  234. // if(Read_Color_Limit(LIMIT_BOARD)==0)
  235. {
  236. LOG_D("*推杆电机归位*");
  237. /*推杆电机归位*/
  238. ctr_cangmen_up(CLOSE);
  239. rt_thread_delay(20000);
  240. ctr_cangmen_up(OFF);
  241. }
  242. if (device_status.cul_time < 7)
  243. device_status.cul_time++;
  244. while (Read_Color_Limit(LIMIT_BOARD) == 0)
  245. {
  246. ctr_cangmen_down(forward);
  247. rt_thread_delay(50);
  248. }
  249. while (Read_Color_Limit(LIMIT_BOARD) == 1)
  250. {
  251. time_board--;
  252. if (time_board == 0)
  253. break;
  254. ctr_cangmen_down(forward);
  255. rt_thread_delay(50);
  256. }
  257. ctr_cangmen_down(OFF);
  258. rt_thread_delay(1000);
  259. ctr_cangmen_up(OPEN);
  260. rt_thread_delay(20000);
  261. ctr_cangmen_up(OFF);
  262. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  263. return OPEN;
  264. }
  265. else if (forward == RESET)
  266. {
  267. ctr_cangmen_up(OFF);
  268. ctr_cangmen_down(OFF);
  269. if (Read_Color_Limit(LIMIT_RESET) == 0)
  270. {
  271. device_status.cul_time = 0;
  272. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  273. LOG_W("已在初始位置!");
  274. Beep_Tip(1000);
  275. ctr_cangmen_up(CLOSE);
  276. rt_thread_delay(20000);
  277. ctr_cangmen_up(OFF);
  278. device_status.cul_time = 0;
  279. return 0;
  280. // 黄板已经在复位状态,直接返回0;
  281. }
  282. // if(Read_Color_Limit(LIMIT_BOARD)==0)
  283. {
  284. LOG_D("*推杆电机归位*");
  285. /*推杆电机归位*/
  286. ctr_cangmen_up(CLOSE);
  287. rt_thread_delay(20000);
  288. ctr_cangmen_up(OFF);
  289. }
  290. while (Read_Color_Limit(LIMIT_RESET) == 1)
  291. {
  292. time_board--;
  293. if (time_board == 0)
  294. break;
  295. ctr_cangmen_down(CLOSE);
  296. rt_thread_delay(20);
  297. }
  298. device_status.cul_time = 0;
  299. ctr_cangmen_down(OFF);
  300. }
  301. // device_status.cul_time = 0;
  302. // ef_set_env_blob("cul_time",&device_status.cul_time,sizeof(device_status.cul_time)); //采集时间
  303. return RESET; // 复位返回0
  304. }
  305. #endif
  306. /****************************5.0***********************************/
  307. void ctr_turnv5(void)
  308. {
  309. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  310. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  311. // 转出
  312. rt_pin_write(CLEAR_R, ON);
  313. rt_pin_write(CLEAR_L, OFF);
  314. rt_thread_mdelay(300);
  315. // 停止
  316. rt_pin_write(CLEAR_R, OFF);
  317. rt_pin_write(CLEAR_L, OFF);
  318. rt_thread_mdelay(300);
  319. // 转入
  320. rt_pin_write(CLEAR_R, OFF);
  321. rt_pin_write(CLEAR_L, ON);
  322. rt_thread_mdelay(300);
  323. // 停止
  324. rt_pin_write(CLEAR_R, OFF);
  325. rt_pin_write(CLEAR_L, OFF);
  326. rt_thread_mdelay(300);
  327. }
  328. /**************************机器人********************************/
  329. void ctr_Motor_Robot(uint8_t status_ws)
  330. {
  331. unsigned int close_timeout = 0;
  332. unsigned int open_timeout = 0;
  333. rt_pin_mode(LIMIT_OPEN, PIN_MODE_INPUT);
  334. rt_pin_mode(LIMIT_CLOSE, PIN_MODE_INPUT);
  335. rt_pin_mode(Motor_Power, PIN_MODE_OUTPUT);
  336. rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT);
  337. rt_pin_mode(Motor_Direction_Ctr, PIN_MODE_OUTPUT);
  338. rt_pin_write(Motor_Power, OFF);
  339. rt_pin_write(Motor_Direction_Ctr, ON);
  340. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  341. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  342. if (status_ws == ON)
  343. {
  344. // 检测按键状态
  345. while (rt_pin_read(Motor_Direction_Button) == 1)
  346. {
  347. if (rt_pin_read(LIMIT_CLOSE) == 1)
  348. {
  349. rt_pin_write(Motor_Direction_Ctr, ON);
  350. close_timeout++;
  351. // 打开电机驱动器电源,默认关闭
  352. rt_pin_write(Motor_Power, ON);
  353. if (close_timeout == 2)
  354. {
  355. rt_pin_write(CLEAR_R, ON);
  356. rt_pin_write(CLEAR_L, OFF);
  357. rt_pin_write(Motor_Power, OFF);
  358. rt_thread_delay(20);
  359. rt_pin_write(Motor_Power, ON);
  360. }
  361. rt_thread_delay(20);
  362. if (close_timeout > 500)
  363. {
  364. close_timeout = 600;
  365. rt_pin_write(Motor_Power, OFF);
  366. rt_pin_write(CLEAR_R, OFF);
  367. rt_pin_write(CLEAR_L, OFF);
  368. }
  369. }
  370. else
  371. {
  372. close_timeout = 0;
  373. rt_pin_write(Motor_Power, OFF); // 关闭驱动器电源
  374. rt_pin_write(CLEAR_R, OFF);
  375. rt_pin_write(CLEAR_L, OFF);
  376. }
  377. rt_thread_delay(20);
  378. }
  379. rt_pin_write(Motor_Power, OFF);
  380. rt_thread_delay(1000 * 2);
  381. while (rt_pin_read(Motor_Direction_Button) == 0)
  382. {
  383. if (rt_pin_read(LIMIT_OPEN) == 1)
  384. {
  385. open_timeout++;
  386. // 打开电机驱动器电源,默认关闭
  387. if (open_timeout == 2)
  388. {
  389. rt_pin_write(CLEAR_R, OFF);
  390. rt_pin_write(CLEAR_L, ON);
  391. }
  392. rt_pin_write(Motor_Power, ON);
  393. rt_pin_write(Motor_Direction_Ctr, OFF);
  394. if (open_timeout > 500)
  395. {
  396. open_timeout = 600;
  397. rt_pin_write(Motor_Power, OFF);
  398. rt_pin_write(CLEAR_R, OFF);
  399. rt_pin_write(CLEAR_L, OFF);
  400. }
  401. rt_thread_delay(20);
  402. }
  403. if (rt_pin_read(LIMIT_OPEN) == 0)
  404. {
  405. open_timeout = 0;
  406. rt_pin_write(Motor_Power, OFF);
  407. rt_pin_write(CLEAR_R, OFF);
  408. rt_pin_write(CLEAR_L, OFF);
  409. }
  410. rt_thread_delay(20);
  411. close_timeout = 0;
  412. }
  413. open_timeout = 0;
  414. close_timeout = 0;
  415. }
  416. else
  417. {
  418. rt_pin_write(Motor_Power, OFF);
  419. }
  420. }
  421. /*************************吸虫塔*********************************/
  422. // 吸虫电机
  423. uint8_t ctr_Motor_xichong(uint8_t status_ws)
  424. {
  425. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  426. if (status_ws == ON)
  427. {
  428. rt_pin_write(MOTOR1_CTR_PIN, ON);
  429. }
  430. else
  431. {
  432. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  433. }
  434. return 0;
  435. }
  436. uint8_t ctr_Camera_Power(uint8_t status_ws)
  437. {
  438. rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
  439. if (status_ws == ON)
  440. {
  441. rt_pin_write(MOTOR1_CTR_PIN, ON);
  442. }
  443. else
  444. {
  445. rt_pin_write(MOTOR1_CTR_PIN, OFF);
  446. }
  447. return 0;
  448. }
  449. // 吸附电机
  450. uint8_t ctr_Motor_xifu(uint8_t status_ws)
  451. {
  452. rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
  453. if (status_ws == ON)
  454. {
  455. rt_pin_write(MOTOR2_CTR_PIN, ON);
  456. }
  457. else
  458. {
  459. rt_pin_write(MOTOR2_CTR_PIN, OFF);
  460. }
  461. return 0;
  462. }
  463. // 清扫-卷带电机
  464. void ctr_JD_Clear(uint8_t shake_sw)
  465. {
  466. rt_uint16_t turn_timeout = 0;
  467. rt_pin_mode(MOTOR_C_LIMIT, PIN_MODE_INPUT);
  468. ctr_cangmen_up(OFF); // 清扫传送带的电机
  469. if (shake_sw == ON)
  470. {
  471. ctr_Heat(ON); // 清洁风机
  472. Motor_shake(ON, 0);
  473. ctr_cangmen_up(OPEN); // 打开清扫
  474. }
  475. else
  476. {
  477. Motor_shake(OFF, 0);
  478. ctr_cangmen_up(OPEN); // 打开清扫
  479. }
  480. while (rt_pin_read(MOTOR_C_LIMIT) == 0)
  481. {
  482. Motor_Juandai(ON);
  483. rt_thread_delay(50);
  484. turn_timeout++;
  485. if (turn_timeout == 1000)
  486. break;
  487. }
  488. turn_timeout = 0;
  489. while (rt_pin_read(MOTOR_C_LIMIT) == 1)
  490. {
  491. Motor_Juandai(ON);
  492. turn_timeout++;
  493. rt_thread_delay(50);
  494. if (turn_timeout == 1000)
  495. break;
  496. }
  497. Motor_shake(OFF, 1);
  498. Motor_Juandai(OFF);
  499. ctr_cangmen_up(OFF); // 关闭清扫
  500. ctr_Heat(OFF);
  501. }
  502. // 工作任务流程
  503. void WorkTask_XCTm(void)
  504. {
  505. while (device_status.work_status == 1 && device_status.ds == 1)
  506. {
  507. ctr_Motor_xifu(ON);
  508. // 打开相机供电
  509. ctr_Camera_Power(ON);
  510. // ctr_Heat(ON); //清洁风机
  511. uint32_t photo_fre = 0;
  512. photo_fre = device_status.dattim * 60;
  513. while (photo_fre != 0)
  514. {
  515. photo_fre--;
  516. rt_thread_delay(1000);
  517. LOG_I("收集中,等待开始拍照:%d", photo_fre);
  518. }
  519. // 卷带
  520. ctr_JD_Clear(OFF);
  521. // 拍照
  522. HK_TakePhoto();
  523. // 关闭吸附风机
  524. ctr_Motor_xifu(OFF);
  525. // 关闭相机供电
  526. // ctr_Heat(OFF);
  527. ctr_Camera_Power(OFF);
  528. // 卷带
  529. ctr_JD_Clear(ON);
  530. // rt_thread_delay(1000);
  531. // ctr_JD_Clear(ON);
  532. // 震动
  533. Motor_shake(ON, device_status.shake_sec);
  534. }
  535. // 关闭风机
  536. ctr_Motor_xichong(OFF);
  537. ctr_Motor_xifu(OFF);
  538. LOG_I("不在工作时间,未工作!!!");
  539. }
  540. /**********************************************************************/
  541. /*光控*/
  542. uint8_t In_Light_status(void)
  543. {
  544. rt_pin_mode(LIGHT_PIN, PIN_MODE_INPUT);
  545. return rt_pin_read(LIGHT_PIN);
  546. }
  547. /*雨控*/
  548. uint8_t In_Rain_status(void)
  549. {
  550. rt_pin_mode(RAIN_PIN, PIN_MODE_INPUT);
  551. return rt_pin_read(RAIN_PIN);
  552. }
  553. /*加热*/
  554. uint8_t ctr_Heat(uint8_t status_heat)
  555. {
  556. device_status.heat_status = status_heat;
  557. rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
  558. rt_pin_write(Heat_Pin, status_heat);
  559. return status_heat;
  560. }
  561. /*制冷*/
  562. uint8_t ctr_Cold(uint8_t status_cold)
  563. {
  564. device_status.cold_status = status_cold;
  565. rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
  566. rt_pin_write(Cold_Pin, status_cold);
  567. return status_cold;
  568. }
  569. /*相机供电*/
  570. void ctr_photo(uint8_t status_photo)
  571. {
  572. rt_pin_mode(PHOTO, PIN_MODE_OUTPUT);
  573. rt_pin_write(PHOTO, status_photo);
  574. }
  575. /*************************************************
  576. Function: ctr_Cool_Fan
  577. Description: 散热风机的开关
  578. Calls:
  579. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  580. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  581. Input: ON-开,OFF-关
  582. Output: NULL
  583. Return: ON-开,OFF-关
  584. Others: NULL
  585. *************************************************/
  586. uint8_t ctr_Cool_Fan(uint8_t status_cool)
  587. {
  588. device_status.cool_fan_status = status_cool;
  589. rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
  590. rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
  591. if (status_cool == OFF)
  592. {
  593. rt_pin_write(Cool_Fan_L, OFF);
  594. rt_pin_write(Cool_Fan_R, OFF);
  595. }
  596. if (status_cool == ON)
  597. {
  598. rt_pin_write(Cool_Fan_L, ON);
  599. rt_pin_write(Cool_Fan_R, OFF);
  600. }
  601. return device_status.cool_fan_status;
  602. }
  603. #include "Work_XCT.h"
  604. extern void CollFanSetPWM(uint8_t percent);
  605. extern SYSCFG syscfg;
  606. /*************************************************
  607. Function: ctr_Collect_Fan
  608. Description: 采集风机的开关
  609. Calls:
  610. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  611. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  612. Input: ON-开,OFF-关
  613. Output: NULL
  614. Return: ON-开,OFF-关
  615. Others: NULL
  616. *************************************************/
  617. uint8_t ctr_Collect_Fan(uint8_t status_collect , uint8_t pwm)
  618. {
  619. device_status.collect_fan_status = status_collect;
  620. rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
  621. if (status_collect == OFF)
  622. {
  623. CollFanSetPWM(0);
  624. rt_pin_write(Collect_Fan, OFF);
  625. }
  626. if (status_collect == ON)
  627. {
  628. CollFanSetPWM(pwm);
  629. rt_pin_write(Collect_Fan, ON);
  630. }
  631. return device_status.collect_fan_status;
  632. }
  633. /*************************************************
  634. Function: ctr_develop
  635. Description: 培养载玻片孢子
  636. Calls:
  637. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  638. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  639. Input: v=0(复位) v=90转90度
  640. Output: NULL
  641. Return: NULL
  642. Others: NULL
  643. *************************************************/
  644. void ctr_develop(void)
  645. {
  646. rt_kprintf("===============开始培养=================:%d\n", device_status.cul_time);
  647. // 已经培养的时间-分钟
  648. device_status.stay_time = 0;
  649. rt_uint32_t ii = 0;
  650. while (device_status.develop_flag == 1)
  651. {
  652. float stayedtime = 0.01;
  653. char stayed[5];
  654. stayedtime = (float)device_status.stay_time / 60.0;
  655. sprintf(stayed, "%0.02f", stayedtime);
  656. HMI_WRITE("workSta.staytime.txt=\"%s\"", stayed);
  657. ii++;
  658. LOG_I("developing...................................%d\n", ii);
  659. LOG_I("============================设定的培养温度:%d\n", device_status.heatvalue);
  660. LOG_I("============================培养仓实时温度:%d\n", device_status.pre_temp);
  661. if (device_status.pre_temp < (device_status.heatvalue - 5))
  662. {
  663. // 未制冷
  664. if (device_status.cold_status == 0)
  665. {
  666. ctr_Heat(ON);
  667. }
  668. }
  669. else
  670. {
  671. ctr_Heat(OFF);
  672. }
  673. LOG_I("============================制冷开关:%d\n", device_status.cold_sw);
  674. if (device_status.cold_sw == 1)
  675. {
  676. if (device_status.pre_temp > (device_status.heatvalue - 10))
  677. {
  678. if (device_status.heat_status == 0)
  679. {
  680. ctr_Cold(ON);
  681. }
  682. }
  683. else
  684. {
  685. ctr_Cold(OFF);
  686. }
  687. }
  688. if (ii % 60 == 0)
  689. {
  690. device_status.stay_time++;
  691. if (device_status.stay_time / 60 == device_status.cul_time)
  692. device_status.develop_flag = 0;
  693. LOG_I("developed...................................%d分钟\n", device_status.stay_time);
  694. }
  695. if (device_status.box_temp > 35)
  696. {
  697. ctr_Cool_Fan(ON);
  698. }
  699. else if (device_status.box_temp < 30)
  700. {
  701. ctr_Cool_Fan(OFF);
  702. }
  703. rt_thread_delay(1000);
  704. }
  705. ctr_Cold(OFF);
  706. ctr_Heat(OFF);
  707. LOG_I("=========!!!培养完成!!!==========");
  708. }
  709. /*上仓门*/
  710. /**
  711. * @brief 控制仓门升起
  712. *
  713. * @param status_down 指令
  714. * @return uint8_t 执行后的状态
  715. */
  716. uint8_t ctr_cangmen_up(uint8_t status_up)
  717. {
  718. rt_pin_mode(Collect_fan_R, PIN_MODE_OUTPUT);
  719. rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
  720. if (status_up == OFF)
  721. {
  722. rt_pin_write(Collect_fan_R, ON);
  723. rt_pin_write(Collect_fan_L, ON);
  724. device_status.ctr_up_motor = 0;
  725. rt_thread_delay(1000);
  726. }
  727. if (status_up == OPEN)
  728. {
  729. device_status.up_status = 1;
  730. rt_pin_write(Collect_fan_R, OFF);
  731. rt_pin_write(Collect_fan_L, OFF);
  732. rt_pin_write(Collect_fan_R, ON);
  733. rt_pin_write(Collect_fan_L, OFF);
  734. }
  735. if (status_up == CLOSE)
  736. {
  737. device_status.up_status = 0;
  738. rt_pin_write(Collect_fan_R, OFF);
  739. rt_pin_write(Collect_fan_L, OFF);
  740. rt_pin_write(Collect_fan_R, OFF);
  741. rt_pin_write(Collect_fan_L, ON);
  742. }
  743. return device_status.up_status;
  744. }
  745. // 下仓门
  746. /**
  747. * @brief 控制仓门落下
  748. *
  749. * @param status_down 指令
  750. * @return uint8_t 执行后的状态
  751. */
  752. uint8_t ctr_cangmen_down(uint8_t status_down)
  753. {
  754. rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
  755. rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
  756. if (status_down == OFF)
  757. {
  758. rt_pin_write(C_DOWN_R, ON);
  759. rt_pin_write(C_DOWN_L, ON);
  760. device_status.ctr_down_motor = 0;
  761. rt_thread_delay(1000);
  762. }
  763. if (status_down == OPEN)
  764. {
  765. device_status.down_status = 1;
  766. rt_pin_write(C_DOWN_R, OFF);
  767. rt_pin_write(C_DOWN_L, OFF);
  768. rt_pin_write(C_DOWN_R, ON);
  769. rt_pin_write(C_DOWN_L, OFF);
  770. }
  771. if (status_down == CLOSE)
  772. {
  773. device_status.down_status = 0;
  774. rt_pin_write(C_DOWN_R, OFF);
  775. rt_pin_write(C_DOWN_L, OFF);
  776. rt_pin_write(C_DOWN_R, OFF);
  777. rt_pin_write(C_DOWN_L, ON);
  778. }
  779. return device_status.down_status;
  780. }
  781. // 镇流器
  782. /**
  783. * @brief 控制镇流器动作
  784. *
  785. * @param status_lamp 指令
  786. * @param lamp_num 镇流器编号
  787. * @return uint8_t 更新后的镇流器状态
  788. */
  789. uint8_t ctr_lamp(uint8_t status_lamp, uint8_t lamp_num)
  790. {
  791. rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
  792. rt_pin_mode(LAMP_1_SW, PIN_MODE_OUTPUT);
  793. rt_pin_mode(LAMP_2_SW, PIN_MODE_OUTPUT);
  794. if (status_lamp == ON)
  795. {
  796. switch (lamp_num)
  797. {
  798. case 1:
  799. rt_pin_write(LAMP_SW, ON);
  800. rt_pin_write(LAMP_1_SW, ON);
  801. device_status.lamp_status = status_lamp;
  802. break;
  803. case 2:
  804. rt_pin_write(LAMP_1_SW, ON);
  805. device_status.lamp_status = status_lamp;
  806. break;
  807. case 3:
  808. rt_pin_write(LAMP_2_SW, ON);
  809. device_status.lamp_status = status_lamp;
  810. break;
  811. default:
  812. break;
  813. }
  814. }
  815. else
  816. {
  817. switch (lamp_num)
  818. {
  819. case 1:
  820. rt_pin_write(LAMP_SW, OFF);
  821. device_status.lamp_status = status_lamp;
  822. break;
  823. case 2:
  824. rt_pin_write(LAMP_1_SW, OFF);
  825. device_status.lamp1_status = status_lamp;
  826. break;
  827. case 3:
  828. rt_pin_write(LAMP_2_SW, OFF);
  829. device_status.lamp2_status = status_lamp;
  830. break;
  831. default:
  832. break;
  833. }
  834. }
  835. HMI_WRITE("sysTest.switchLamp.val=%d", device_status.lamp_status);
  836. return device_status.lamp_status;
  837. }
  838. // 震动电机
  839. /**
  840. * @brief 控制震动电机动作
  841. *
  842. * @param status_shake 指令
  843. * @param shake_sec 震动时间(秒)
  844. * @return uint8_t 执行后的状态
  845. */
  846. uint8_t ctr_shake(uint8_t status_shake, uint16_t shake_sec)
  847. {
  848. static rt_uint16_t shake_i = 0;
  849. if (device_status.dtype == dt_BZY && device_status.version_type == vt_XCT)
  850. {
  851. return 0;
  852. }
  853. shake_i = atoi(device_status.power);
  854. LOG_I("震动电机待机电流:%d", shake_i);
  855. if (device_status.dtype == 7 && shake_sec != 0)
  856. DTU_Write("drop");
  857. rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
  858. if (status_shake == ON && device_status.shake_ws == 1)
  859. {
  860. rt_pin_write(SHAKE_SW, ON);
  861. if (device_status.dtype == 3)
  862. {
  863. while (shake_sec--)
  864. {
  865. rt_thread_delay(100);
  866. 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)
  867. {
  868. char *shake_warn = makeJson_Warn("电流不足", "震动电机");
  869. DTU_Write("%s", shake_warn);
  870. rt_free(shake_warn);
  871. }
  872. LOG_D("震动电机:%d", atoi(device_status.power));
  873. }
  874. }
  875. if (device_status.dtype == 7)
  876. rt_thread_delay(shake_sec * 1000);
  877. rt_pin_write(SHAKE_SW, OFF);
  878. HMI_WRITE("cangWei.switchShake.val=%d", 0);
  879. }
  880. else
  881. {
  882. rt_pin_write(SHAKE_SW, OFF);
  883. HMI_WRITE("cangWei.switchShake.val=%d", 0);
  884. }
  885. if (device_status.dtype == 7)
  886. HMI_WRITE("sysTest.switchDrop.val=%d", OFF);
  887. return status_shake;
  888. }
  889. // 补光
  890. extern JD_Test_Sta jts;
  891. /**
  892. * @brief 控制补光灯动作
  893. *
  894. * @param status_led 指令
  895. * @return uint8_t 执行后的状态
  896. */
  897. uint8_t ctr_led(uint8_t status_led)
  898. {
  899. //rt_kprintf("ctr_led %d \n\n", status_led);
  900. rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
  901. if (device_status.version_type == 6)
  902. rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
  903. if (status_led == ON)
  904. {
  905. rt_pin_write(LED_SW, ON);
  906. if (device_status.version_type == 6)
  907. rt_pin_write(LED_P, ON);
  908. HMI_WRITE("sysTest.switchFill.val=%d", ON);
  909. }
  910. else if (status_led == OFF)
  911. {
  912. rt_pin_write(LED_SW, OFF);
  913. if (device_status.version_type == 6)
  914. rt_pin_write(LED_P, OFF);
  915. if (jts.switchled_test == ON)
  916. {
  917. jts.switchled_test = OFF;
  918. Recode_e_code(e_TestLed, 2, "外部工作导致补光灯关闭");
  919. }
  920. }
  921. return status_led;
  922. }
  923. /*************************************************
  924. Function: ctr_turn
  925. Description: 转盘驱动
  926. Calls:
  927. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  928. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  929. Input: v=0(复位) v=90转90度
  930. Output: NULL
  931. Return: NULL
  932. Others: NULL
  933. *************************************************/
  934. void ctr_turn(uint8_t v, uint8_t sta)
  935. {
  936. if (device_status.dtype == dt_BZY && device_status.version_type == vt_XCT)
  937. {
  938. return;
  939. }
  940. DTU_Write("turn.mp3");
  941. LOG_I("================v===============:%d", v);
  942. rt_uint16_t turn_cnt = 0;
  943. rt_pin_mode(TURN_ZP_R, PIN_MODE_OUTPUT);
  944. rt_pin_mode(TURN_ZP_L, PIN_MODE_OUTPUT);
  945. rt_pin_mode(LIMIT_0, PIN_MODE_INPUT);
  946. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  947. // if(v==0&&rt_pin_read(LIMIT_0)==0) return; //如果在初始位置,则不再往下执行
  948. while (rt_pin_read(LIMIT_0) == 0 || rt_pin_read(LIMIT_90) == 0)
  949. {
  950. rt_pin_write(TURN_ZP_R, ON);
  951. rt_pin_write(TURN_ZP_L, OFF);
  952. // Motor_shake(ON,0);
  953. turn_cnt++;
  954. rt_thread_delay(100);
  955. if (turn_cnt == 1000)
  956. {
  957. turn_cnt = 0;
  958. rt_pin_write(TURN_ZP_R, OFF);
  959. rt_pin_write(TURN_ZP_L, OFF);
  960. // Motor_shake(OFF,0);
  961. char *turn_warn = makeJson_Warn("未检测到限位", "转盘");
  962. DTU_Write("%s", turn_warn);
  963. rt_free(turn_warn);
  964. break;
  965. }
  966. }
  967. turn_cnt = 0;
  968. while (v == 0 && rt_pin_read(LIMIT_0) == 1)
  969. {
  970. rt_pin_write(TURN_ZP_R, ON);
  971. rt_pin_write(TURN_ZP_L, OFF);
  972. Motor_shake(ON, 0);
  973. rt_thread_delay(200); // 10 to 100
  974. turn_cnt++;
  975. if (turn_cnt == 300) // 6000 to 600
  976. {
  977. turn_cnt = 0;
  978. // rt_pin_write(TURN_ZP_R, OFF);
  979. // rt_pin_write(TURN_ZP_L, OFF);
  980. // char *turn_warn =makeJson_Warn("超时", "转盘复位");
  981. // DTU_Write("%s",turn_warn);
  982. // rt_free(turn_warn);
  983. break;
  984. }
  985. }
  986. while (v == 0 && rt_pin_read(LIMIT_0) == 1)
  987. {
  988. rt_pin_write(TURN_ZP_R, ON);
  989. rt_pin_write(TURN_ZP_L, OFF);
  990. // Motor_shake(ON,0);
  991. rt_thread_delay(50); // 10 to 50
  992. turn_cnt++;
  993. if (turn_cnt == 10) // 6000 to 1200
  994. {
  995. turn_cnt = 0;
  996. rt_pin_write(TURN_ZP_R, OFF);
  997. rt_pin_write(TURN_ZP_L, OFF);
  998. // Motor_shake(OFF,0);
  999. char *turn_warn = makeJson_Warn("超时", "转盘复位");
  1000. DTU_Write("%s", turn_warn);
  1001. rt_free(turn_warn);
  1002. break;
  1003. }
  1004. }
  1005. turn_cnt = 0;
  1006. while (v == 90 && rt_pin_read(LIMIT_90) == 1)
  1007. {
  1008. rt_pin_write(TURN_ZP_R, ON);
  1009. rt_pin_write(TURN_ZP_L, OFF);
  1010. // Motor_shake(ON,0);
  1011. rt_thread_delay(200);
  1012. turn_cnt++;
  1013. if (turn_cnt == 150)
  1014. {
  1015. turn_cnt = 0;
  1016. // rt_pin_write(TURN_ZP_R, OFF);
  1017. // rt_pin_write(TURN_ZP_L, OFF);
  1018. // char *turn_warn =makeJson_Warn("超时", "转盘");
  1019. // DTU_Write(turn_warn);
  1020. // rt_free(turn_warn);
  1021. break;
  1022. }
  1023. }
  1024. while (v == 90 && rt_pin_read(LIMIT_90) == 1)
  1025. {
  1026. // rt_pin_write(TURN_ZP_R, ON);
  1027. // rt_pin_write(TURN_ZP_L, OFF);
  1028. Motor_shake(ON, 0);
  1029. rt_thread_delay(50);
  1030. turn_cnt++;
  1031. if (turn_cnt == 10)
  1032. {
  1033. turn_cnt = 0;
  1034. rt_pin_write(TURN_ZP_R, OFF);
  1035. rt_pin_write(TURN_ZP_L, OFF);
  1036. // Motor_shake(OFF,0);
  1037. char *turn_warn = makeJson_Warn("超时", "转盘");
  1038. DTU_Write(turn_warn);
  1039. rt_free(turn_warn);
  1040. break;
  1041. }
  1042. }
  1043. if (sta != 0)
  1044. {
  1045. // 保存位置
  1046. device_status.run_sta = sta;
  1047. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  1048. }
  1049. turn_cnt = 0;
  1050. rt_pin_write(TURN_ZP_R, OFF);
  1051. rt_pin_write(TURN_ZP_L, OFF);
  1052. Motor_shake(OFF, 0);
  1053. HMI_WRITE("cangWei.rotateFlag.val=0");
  1054. }
  1055. /*************************************************
  1056. Function: ctr_Motor_Driver
  1057. Description: 步进电机驱动
  1058. Calls:
  1059. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1060. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1061. Input: 50000000
  1062. Output: NULL
  1063. Return: shake_sec
  1064. Others: NULL
  1065. *************************************************/
  1066. extern JD_Test_Sta jts;
  1067. Res_def ctr_Motor_Driver(uint8_t EN, uint8_t DIR, rt_uint32_t pwm, rt_uint32_t time_out)
  1068. {
  1069. Res_def result = res_OK;
  1070. beep_init();
  1071. rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
  1072. rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
  1073. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  1074. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  1075. JD_Test_Type EN_Type = tt_typemax;
  1076. if (jts.autotest == ON)
  1077. {
  1078. EN_Type = tt_auto;
  1079. HMI_WRITE_Force("");
  1080. if (DIR == 0)
  1081. {
  1082. HMI_WRITE("sysTest.result.txt=\"一键测试 > 步进上行中\"");
  1083. }
  1084. }
  1085. if (EN == 0)
  1086. {
  1087. rt_pin_write(Motor_Power, 0);
  1088. }
  1089. else
  1090. {
  1091. rt_pin_write(Motor_EN, EN);
  1092. }
  1093. rt_pin_write(Motor_DIR, DIR);
  1094. beep_set(pwm, pwm / 2); // 200kzh - 50%
  1095. while (DIR == 1 && rt_pin_read(LIMIT_DOWN) == 1)
  1096. {
  1097. if (time_out == 0)
  1098. {
  1099. result = res_Timeout;
  1100. switch (EN_Type)
  1101. {
  1102. case tt_auto:
  1103. Recode_e_code(e_StepDown_Fail, 1, "一键测试:步进下行超时"); // 一键测试
  1104. break;
  1105. default:
  1106. if (pwm != 0)
  1107. {
  1108. Recode_e_code(e_StepDown_Fail, 0, "工作中:步进下行超时"); // 流程中
  1109. }
  1110. break;
  1111. }
  1112. break;
  1113. }
  1114. rt_thread_delay(50);
  1115. time_out--;
  1116. }
  1117. while (DIR == 0 && rt_pin_read(LIMIT_UP) == 1)
  1118. {
  1119. if (time_out == 0)
  1120. {
  1121. result = res_Timeout;
  1122. switch (EN_Type)
  1123. {
  1124. case tt_auto:
  1125. Recode_e_code(e_StepUp_Fail, 1, "一键测试:步进上行超时"); // 一键测试
  1126. break;
  1127. default:
  1128. if (pwm != 0)
  1129. {
  1130. Recode_e_code(e_StepUp_Fail, 0, "工作中:步进上行超时"); // 流程中
  1131. }
  1132. break;
  1133. }
  1134. break;
  1135. }
  1136. rt_thread_delay(50);
  1137. time_out--;
  1138. if (EN_Type == tt_auto && JD_Check_Test_Sta(tt_auto) != res_OK)
  1139. {
  1140. HMI_WRITE_Force("");
  1141. HMI_WRITE("sysTest.result.txt=\"一键测试 > 步进上行退出\"");
  1142. break;
  1143. }
  1144. }
  1145. rt_pin_write(Motor_EN, OFF);
  1146. if (device_status.autotakepic == 0)
  1147. {
  1148. ctr_lamp(OFF, 1);
  1149. }
  1150. beep_set(0, 0); // 200kzh - 50%
  1151. return result;
  1152. }
  1153. /*************************************************
  1154. Function: ctr_Petroleum_jelly
  1155. Description: 涂凡士林
  1156. Calls:
  1157. Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
  1158. Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
  1159. Input: NULL
  1160. Output: NULL
  1161. Return: NULL
  1162. Others: NULL
  1163. *************************************************/
  1164. void ctr_Petroleum_jelly(rt_uint32_t delay)
  1165. {
  1166. beep_init();
  1167. pwm_on(2, 50000);
  1168. rt_thread_delay(delay * 1000);
  1169. pwm_off(2, 0);
  1170. rt_thread_delay(1000);
  1171. pwm_on(3, 50000);
  1172. rt_thread_delay(delay * 1000);
  1173. pwm_off(3, 0);
  1174. device_status.fsl_sw = 0;
  1175. }
  1176. // 1.0转仓
  1177. void turnzp(void)
  1178. {
  1179. static uint16_t turn_timeout = 0;
  1180. // rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1181. // rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1182. rt_pin_mode(CLEAR_R_LIMIT, PIN_MODE_INPUT);
  1183. // rt_pin_write(CLEAR_R,OFF);
  1184. // rt_pin_write(CLEAR_L,OFF);
  1185. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1186. {
  1187. rt_thread_delay(20);
  1188. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1189. {
  1190. // rt_pin_write(CLEAR_R,ON);
  1191. // rt_pin_write(CLEAR_L,OFF);
  1192. Motor_Juandai(ON);
  1193. rt_thread_delay(50);
  1194. turn_timeout++;
  1195. if (turn_timeout == 600)
  1196. {
  1197. LOG_E("检测不到限位,电机强制停止");
  1198. char *ctr_zp_limit_warn = makeJson_Warn("电流不足/限位超时", "转盘电机");
  1199. DTU_Write("%s", ctr_zp_limit_warn);
  1200. rt_free(ctr_zp_limit_warn);
  1201. break;
  1202. }
  1203. }
  1204. break;
  1205. }
  1206. while (rt_pin_read(CLEAR_R_LIMIT) == 1)
  1207. {
  1208. rt_thread_delay(20);
  1209. while (rt_pin_read(CLEAR_R_LIMIT) == 1)
  1210. {
  1211. // rt_pin_write(CLEAR_R,ON);
  1212. // rt_pin_write(CLEAR_L,OFF);
  1213. Motor_Juandai(ON);
  1214. rt_thread_delay(50);
  1215. turn_timeout++;
  1216. if (turn_timeout == 600)
  1217. {
  1218. LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
  1219. char *ctr_zp_limit_warn = makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障", "转盘电机");
  1220. DTU_Write("%s", ctr_zp_limit_warn);
  1221. rt_free(ctr_zp_limit_warn);
  1222. break;
  1223. }
  1224. }
  1225. break;
  1226. }
  1227. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1228. {
  1229. rt_thread_delay(20);
  1230. if (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1231. {
  1232. // rt_pin_write(CLEAR_R,OFF);
  1233. // rt_pin_write(CLEAR_L,OFF);
  1234. Motor_Juandai(OFF);
  1235. break;
  1236. }
  1237. else
  1238. {
  1239. break;
  1240. }
  1241. }
  1242. Motor_Juandai(OFF);
  1243. // rt_pin_write(CLEAR_R,OFF);
  1244. // rt_pin_write(CLEAR_L,OFF);
  1245. turn_timeout = 0;
  1246. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1247. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1248. rt_thread_delay(1000);
  1249. }
  1250. // 清扫-常闭型限位开关
  1251. // 新结构使用推杆电机做清扫
  1252. uint8_t ctr_clear(uint16_t status_clear)
  1253. {
  1254. static uint16_t clear_num = 0;
  1255. #if PWM_EN == 0
  1256. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1257. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1258. #endif
  1259. rt_pin_mode(CLEAR_R_LIMIT, PIN_MODE_INPUT);
  1260. rt_pin_mode(CLEAR_L_LIMIT, PIN_MODE_INPUT);
  1261. /*************************************************************外接清扫
  1262. rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1263. rt_pin_write(POWER_CLEAR,ON);
  1264. rt_thread_delay(1000*100);
  1265. rt_pin_write(POWER_CLEAR,OFF);
  1266. ************************************************************/
  1267. // 停止
  1268. #if PWM_EN == 0
  1269. rt_pin_write(CLEAR_R, OFF);
  1270. rt_pin_write(CLEAR_L, OFF);
  1271. #else
  1272. Motor_Qingsao_R_PWM(OFF);
  1273. Motor_Qingsao_L_PWM(OFF);
  1274. #endif
  1275. if (status_clear == OFF || Read_Real_Voltage() / 100 < 12)
  1276. return 0;
  1277. rt_thread_delay(100);
  1278. // 如果清扫位置在中间
  1279. while (rt_pin_read(CLEAR_R_LIMIT) == 0 && rt_pin_read(CLEAR_L_LIMIT) == 0)
  1280. {
  1281. #if PWM_EN == 0
  1282. rt_pin_write(CLEAR_R, ON);
  1283. rt_pin_write(CLEAR_L, OFF);
  1284. #else
  1285. Motor_Qingsao_R_PWM(ON);
  1286. Motor_Qingsao_L_PWM(OFF);
  1287. #endif
  1288. rt_thread_delay(50);
  1289. clear_num++;
  1290. // 已到达检测位置
  1291. if (rt_pin_read(CLEAR_L_LIMIT) == 1 || rt_pin_read(CLEAR_R_LIMIT) == 1)
  1292. {
  1293. #if PWM_EN == 0
  1294. rt_pin_write(CLEAR_R, ON);
  1295. rt_pin_write(CLEAR_L, ON);
  1296. #else
  1297. Motor_Qingsao_R_PWM(OFF);
  1298. Motor_Qingsao_L_PWM(OFF);
  1299. #endif
  1300. rt_thread_delay(2000);
  1301. break;
  1302. }
  1303. if (clear_num >= status_clear || atoi(device_status.power) > 2000 || (Read_Real_Voltage() / 100) < 12)
  1304. {
  1305. rt_kprintf("Clear Motor ERR:%d", atoi(device_status.power));
  1306. // DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1307. #if PWM_EN == 0
  1308. rt_pin_write(CLEAR_R, ON);
  1309. rt_pin_write(CLEAR_L, ON);
  1310. #else
  1311. Motor_Qingsao_R_PWM(OFF);
  1312. Motor_Qingsao_L_PWM(OFF);
  1313. #endif
  1314. rt_free(makeJson_Warn("Clear Motor ERR", "device"));
  1315. if (Read_Real_Voltage() / 100 < 12)
  1316. {
  1317. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1318. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1319. device_status.clear_status = 0;
  1320. return 0;
  1321. }
  1322. break;
  1323. }
  1324. }
  1325. clear_num = 0;
  1326. // 如果清扫位置在左侧
  1327. while (rt_pin_read(CLEAR_R_LIMIT) == 0)
  1328. {
  1329. #if PWM_EN == 0
  1330. rt_pin_write(CLEAR_R, ON);
  1331. rt_pin_write(CLEAR_L, OFF);
  1332. #else
  1333. Motor_Qingsao_R_PWM(ON);
  1334. Motor_Qingsao_L_PWM(OFF);
  1335. #endif
  1336. rt_thread_delay(50);
  1337. clear_num++;
  1338. if (clear_num >= status_clear || atoi(device_status.power) > 2000 || (Read_Real_Voltage() / 100) < 12)
  1339. {
  1340. rt_kprintf("Clear Motor ERR:%d", atoi(device_status.power));
  1341. // DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
  1342. #if PWM_EN == 0
  1343. rt_pin_write(CLEAR_R, ON);
  1344. rt_pin_write(CLEAR_L, ON);
  1345. #else
  1346. Motor_Qingsao_R_PWM(OFF);
  1347. Motor_Qingsao_L_PWM(OFF);
  1348. #endif
  1349. rt_free(makeJson_Warn("Clear Motor ERR", "device"));
  1350. if (Read_Real_Voltage() / 100 < 12)
  1351. {
  1352. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1353. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1354. device_status.clear_status = 0;
  1355. return 0;
  1356. }
  1357. break;
  1358. }
  1359. }
  1360. clear_num = 0;
  1361. while (rt_pin_read(CLEAR_L_LIMIT) == 1 || rt_pin_read(CLEAR_R_LIMIT) == 1 || (Read_Real_Voltage() / 100) < 12)
  1362. {
  1363. #if PWM_EN == 0
  1364. rt_pin_write(CLEAR_R, ON);
  1365. rt_pin_write(CLEAR_L, ON);
  1366. #else
  1367. Motor_Qingsao_R_PWM(OFF);
  1368. Motor_Qingsao_L_PWM(OFF);
  1369. #endif
  1370. if (Read_Real_Voltage() / 100 < 12)
  1371. {
  1372. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1373. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1374. device_status.clear_status = 0;
  1375. return 0;
  1376. }
  1377. rt_thread_delay(2000);
  1378. break;
  1379. }
  1380. while (rt_pin_read(CLEAR_L_LIMIT) == 0)
  1381. {
  1382. #if PWM_EN == 0
  1383. rt_pin_write(CLEAR_R, OFF);
  1384. rt_pin_write(CLEAR_L, ON);
  1385. #else
  1386. Motor_Qingsao_R_PWM(OFF);
  1387. Motor_Qingsao_L_PWM(ON);
  1388. #endif
  1389. rt_thread_delay(50);
  1390. clear_num++;
  1391. if (clear_num >= status_clear || atoi(device_status.power) > 2000 || (Read_Real_Voltage() / 100) < 12)
  1392. {
  1393. rt_kprintf("Clear Motor ERR:%d", atoi(device_status.power));
  1394. char *Clear_ERR = makeJson_Warn("Clear Motor ERR", "device");
  1395. // DTU_Write("%s",Clear_ERR);
  1396. #if PWM_EN == 0
  1397. rt_pin_write(CLEAR_R, ON);
  1398. rt_pin_write(CLEAR_L, ON);
  1399. #else
  1400. Motor_Qingsao_R_PWM(OFF);
  1401. Motor_Qingsao_L_PWM(OFF);
  1402. #endif
  1403. rt_free(Clear_ERR);
  1404. if (Read_Real_Voltage() / 100 < 12)
  1405. {
  1406. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1407. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1408. device_status.clear_status = 0;
  1409. return 0;
  1410. }
  1411. break;
  1412. }
  1413. }
  1414. while (rt_pin_read(CLEAR_L_LIMIT) == 1 || rt_pin_read(CLEAR_R_LIMIT) == 1)
  1415. {
  1416. #if PWM_EN == 0
  1417. rt_pin_write(CLEAR_R, ON);
  1418. rt_pin_write(CLEAR_L, ON);
  1419. #else
  1420. Motor_Qingsao_R_PWM(OFF);
  1421. Motor_Qingsao_L_PWM(OFF);
  1422. #endif
  1423. rt_thread_delay(2000);
  1424. break;
  1425. }
  1426. while (rt_pin_read(CLEAR_L_LIMIT) == 1 && rt_pin_read(CLEAR_R_LIMIT) == 1)
  1427. {
  1428. #if PWM_EN == 0
  1429. rt_pin_write(CLEAR_R, ON);
  1430. rt_pin_write(CLEAR_L, ON);
  1431. #else
  1432. Motor_Qingsao_R_PWM(OFF);
  1433. Motor_Qingsao_L_PWM(OFF);
  1434. #endif
  1435. if (device_status.version_type == 41)
  1436. {
  1437. rt_kprintf("Clear Motor Limit Not Access:%d", atoi(device_status.power));
  1438. char *Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access", "device");
  1439. // DTU_Write("%s",Clear_ERR);
  1440. rt_free(Clear_ERR);
  1441. }
  1442. return 0;
  1443. }
  1444. clear_num = 0;
  1445. rt_thread_delay(2000);
  1446. #if PWM_EN == 0
  1447. rt_pin_write(CLEAR_R, ON);
  1448. rt_pin_write(CLEAR_L, ON);
  1449. #else
  1450. Motor_Qingsao_R_PWM(OFF);
  1451. Motor_Qingsao_L_PWM(OFF);
  1452. #endif
  1453. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1454. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1455. device_status.clear_status = 0;
  1456. return 1;
  1457. }
  1458. // 路由器供电
  1459. void ctr_router(uint8_t status_router)
  1460. {
  1461. rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
  1462. if (device_status.version_type == 6)
  1463. rt_pin_mode(ROUTER_P, PIN_MODE_OUTPUT);
  1464. // rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
  1465. if (status_router == ON)
  1466. {
  1467. rt_pin_write(ROUTER, ON);
  1468. //
  1469. #if LAMP_EN == 0
  1470. if (device_status.version_type == 6)
  1471. rt_pin_write(ROUTER_P, ON);
  1472. #endif
  1473. }
  1474. else
  1475. {
  1476. rt_pin_write(ROUTER, OFF);
  1477. //
  1478. #if LAMP_EN == 0
  1479. if (device_status.version_type == 6)
  1480. rt_pin_write(ROUTER_P, OFF);
  1481. #endif
  1482. }
  1483. }
  1484. // 清扫-推杆电机
  1485. void ctr_TG_Clear(uint16_t clear_timeout)
  1486. {
  1487. #if 0
  1488. int current_clear[200];
  1489. int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
  1490. LOG_I("清扫电机待机:%d",Electric_current);
  1491. #endif
  1492. static uint16_t clear_count = 0;
  1493. clear_count = clear_timeout;
  1494. #if PWM_EN == 0
  1495. rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
  1496. rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
  1497. #endif
  1498. while (clear_count != 0)
  1499. {
  1500. #if PWM_EN == 0
  1501. rt_pin_write(CLEAR_R, ON);
  1502. rt_pin_write(CLEAR_L, OFF);
  1503. #else
  1504. Motor_Qingsao_R_PWM(ON);
  1505. Motor_Qingsao_L_PWM(OFF);
  1506. #endif
  1507. clear_count--;
  1508. rt_thread_delay(100);
  1509. }
  1510. #if PWM_EN == 0
  1511. rt_pin_write(CLEAR_R, ON);
  1512. rt_pin_write(CLEAR_L, ON);
  1513. #else
  1514. Motor_Qingsao_R_PWM(OFF);
  1515. Motor_Qingsao_L_PWM(OFF);
  1516. #endif
  1517. rt_thread_delay(3000);
  1518. clear_count = 0;
  1519. while (clear_timeout != clear_count)
  1520. {
  1521. #if PWM_EN == 0
  1522. rt_pin_write(CLEAR_R, OFF);
  1523. rt_pin_write(CLEAR_L, ON);
  1524. #else
  1525. Motor_Qingsao_R_PWM(OFF);
  1526. Motor_Qingsao_L_PWM(ON);
  1527. #endif
  1528. // if(clear_count<200)
  1529. // current_clear[clear_count] = atoi(device_status.power);
  1530. clear_count++;
  1531. rt_thread_delay(100);
  1532. }
  1533. #if PWM_EN == 0
  1534. rt_pin_write(CLEAR_R, ON);
  1535. rt_pin_write(CLEAR_L, ON);
  1536. #else
  1537. Motor_Qingsao_R_PWM(OFF);
  1538. Motor_Qingsao_L_PWM(OFF);
  1539. #endif
  1540. #if 0
  1541. bubblesort(current_clear,200);
  1542. if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
  1543. {
  1544. LOG_E("清扫电机电流不足:%d!",current_clear[199]);
  1545. char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
  1546. DTU_Write("%s",ctr_clear_warn);
  1547. rt_free(ctr_clear_warn);
  1548. }
  1549. #endif
  1550. rt_thread_delay(2000);
  1551. device_status.clear_status = 0;
  1552. HMI_WRITE("cangWei.rotateFlag.val=%d", 0);
  1553. HMI_WRITE("sysTest.switchClear.val=%d", 0);
  1554. }
  1555. extern void CTL_CAM1(void);
  1556. extern void CTL_CAM0(void);
  1557. // DTU,RTU供电
  1558. void ctr_RTU_DTU(uint8_t status_rtu)
  1559. {
  1560. rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
  1561. if (status_rtu == ON)
  1562. {
  1563. rt_pin_write(RTU_DTU, ON);
  1564. CTL_CAM1();
  1565. }
  1566. else if(syscfg.Demo_mode != 1 && ws_now != ws_KeyTig && (syscfg.not_poweroff == 0)) // 这两种情况下 不允许断电
  1567. {
  1568. LOG_W("RTU电源复位!!! %d" ,syscfg.Demo_mode);
  1569. rt_pin_write(RTU_DTU, OFF);
  1570. CTL_CAM0();
  1571. syscfg.rtu_sta = 0;
  1572. sprintf(device_status.net , "1"); // 断电的时候 网络状态置为断开
  1573. }
  1574. else
  1575. {
  1576. LOG_W("RTU电源复位被打断 ws_now %d , Demo_mode %d\n" , ws_now ,syscfg.Demo_mode);
  1577. }
  1578. }
  1579. // 海康拍照
  1580. void Hk_Take_io(void)
  1581. {
  1582. rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
  1583. rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
  1584. rt_pin_write(HK_TAKE, 1);
  1585. rt_pin_write(HK_IO, 0);
  1586. if (device_status.dtype == 3)
  1587. Beep_Tip(1000);
  1588. if (device_status.dtype == 7)
  1589. rt_thread_mdelay(1000);
  1590. rt_pin_write(HK_TAKE, 0);
  1591. rt_pin_write(HK_IO, 1);
  1592. }
  1593. /**
  1594. * @brief 控制执行加热动作
  1595. *
  1596. * @param heat_time 加热时间(秒)
  1597. */
  1598. void calorstat(uint16_t heat_time)
  1599. {
  1600. // uint8_t heat_up_cnt = 0;
  1601. uint16_t n = 0;
  1602. uint16_t delay_heat = 60;
  1603. FlagStatus heat_flg = RESET;
  1604. device_status.heat_time_min = 0;
  1605. device_status.run_sta = device_status.heattem; // 初始化加热实时温度
  1606. ctr_Heat(ON);
  1607. // UpLoad_Data(); //开始加热时的第一条数据
  1608. // 恒温模式
  1609. for (n = 0; n < heat_time; n++)
  1610. {
  1611. if (device_status.heattem > device_status.heatvalue && heat_flg == RESET)
  1612. {
  1613. heat_flg = SET;
  1614. ctr_Heat(OFF);
  1615. while (delay_heat != 0)
  1616. {
  1617. delay_heat--;
  1618. rt_kprintf("正在恒温加热!!!:%d", delay_heat);
  1619. HMI_WRITE("workSta.g0.txt=\"正在恒温加热!!!:%d\"", delay_heat);
  1620. rt_thread_delay(1000);
  1621. if (device_status.heattem > device_status.run_sta + 1)
  1622. {
  1623. device_status.run_sta = device_status.heattem;
  1624. }
  1625. }
  1626. delay_heat = 60;
  1627. // 跳出FOR循环
  1628. n = n + 60;
  1629. }
  1630. if (device_status.heattem < (device_status.heatvalue - 5) && heat_flg == SET)
  1631. {
  1632. device_status.run_sta = device_status.heattem;
  1633. ctr_Heat(ON);
  1634. }
  1635. else if (device_status.heattem >= device_status.heatvalue && heat_flg == SET)
  1636. {
  1637. // if(heat_up_cnt==0)
  1638. // {
  1639. // UpLoad_Data(); //加热到设定温度的数据
  1640. // heat_up_cnt = 1;
  1641. // }
  1642. device_status.run_sta = device_status.heattem;
  1643. ctr_Heat(OFF);
  1644. }
  1645. if ((n % 60) == 0)
  1646. {
  1647. device_status.heat_time_min = device_status.heat_time_min + 1;
  1648. // if(device_status.heat_time_min % 5 == 0)
  1649. // {
  1650. // UpLoad_Data(); //从加热开始每5分钟上传的数据
  1651. // }
  1652. }
  1653. // 如果触发异常退出加热
  1654. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1)
  1655. {
  1656. rt_thread_delay(1000);
  1657. if (device_status.bat_status == 1)
  1658. {
  1659. rt_kprintf("============================触发异常退出加热==========================");
  1660. ctr_lamp(OFF, 1);
  1661. ctr_Heat(OFF);
  1662. break;
  1663. }
  1664. }
  1665. // if (device_status.timeSwitch == 0 && device_status.sun_status == 1)
  1666. // {
  1667. // ctr_Heat(OFF);
  1668. // rt_kprintf("异常退出!!!\n");
  1669. // break;
  1670. // }
  1671. rt_thread_delay(1000);
  1672. rt_kprintf("=======正在加热======:%d-%d\n", n, heat_time);
  1673. HMI_WRITE("workSta.g0.txt=\"正在加热!!!:%d-%d\"", n, heat_time);
  1674. }
  1675. ctr_Heat(OFF);
  1676. // UpLoad_Data(); //加热结束的数据
  1677. }
  1678. // 初始化所有控制驱动,关闭
  1679. void HardWare_Init(uint8_t boot)
  1680. {
  1681. Beep_Tip(200);
  1682. // 关闭镇流器
  1683. if (device_status.autotakepic == 0)
  1684. {
  1685. ctr_lamp(OFF, 1);
  1686. }
  1687. // 关闭震动电机
  1688. ctr_shake(OFF, 0);
  1689. // 关闭加热
  1690. ctr_Heat(OFF);
  1691. // 关闭补光灯
  1692. ctr_led(OFF);
  1693. // 关闭12V供电(海康相机,RTU)
  1694. ctr_photo(OFF);
  1695. }
  1696. // 拍照
  1697. void HK_TakePhoto(void)
  1698. {
  1699. // 打开补光灯
  1700. DTU_Write("takephoto.mp3");
  1701. rt_thread_delay(1000);
  1702. char *takephotojson = makeJson_takephoto();
  1703. DTU_Write("%s", takephotojson);
  1704. rt_free(takephotojson);
  1705. // 吸虫塔
  1706. if (device_status.version_type != 6)
  1707. {
  1708. ctr_led(ON);
  1709. if (device_status.version_type == 48)
  1710. {
  1711. rt_thread_delay(1000 * 60);
  1712. }
  1713. else
  1714. {
  1715. rt_thread_delay(1000 * 15);
  1716. }
  1717. }
  1718. // 触发海康相机拍照
  1719. Hk_Take_io();
  1720. if (device_status.version_type != 6)
  1721. {
  1722. rt_thread_delay(1000 * 45);
  1723. }
  1724. else
  1725. {
  1726. ctr_led(ON);
  1727. rt_thread_delay(1000 * 19);
  1728. }
  1729. ctr_led(OFF);
  1730. }
  1731. // 光控测试任务
  1732. void SunTest(void)
  1733. {
  1734. rt_uint16_t ui_timeout_l = 0;
  1735. // 未检测到光照,一秒扫描等待,超时30S
  1736. while (Read_Light_Voltage() < 500)
  1737. {
  1738. ui_timeout_l++;
  1739. rt_thread_delay(1000);
  1740. if (ui_timeout_l == 30)
  1741. break;
  1742. }
  1743. rt_thread_delay(3000);
  1744. // if (Read_Light_Voltage() < 500)
  1745. // {
  1746. // device_status.sun_status = 0; // 光控状态
  1747. // }
  1748. // if (Read_Light_Voltage() > 500)
  1749. // {
  1750. // rt_thread_delay(1000);
  1751. // }
  1752. // rt_thread_delay(3000);
  1753. // if (Read_Light_Voltage() > 500)
  1754. // {
  1755. // device_status.sun_status = 1;
  1756. // ctr_lamp(OFF, 1);
  1757. // }
  1758. HMI_WRITE("sysTest.switchSun.val=%d", 0);
  1759. device_status.sun_test = 0;
  1760. device_status.work_status = 0;
  1761. }
  1762. /*上电初始化*/
  1763. void ResetInit(void)
  1764. {
  1765. // 关闭补光灯
  1766. ctr_led(OFF);
  1767. // 关诱捕灯
  1768. ctr_lamp(OFF, 1);
  1769. // 关风机
  1770. ctr_Collect_Fan(OFF , syscfg.fan_speed);
  1771. if (device_status.bat_status == 0)
  1772. {
  1773. LOG_W("欠压! %s" , device_status.voltage);
  1774. return;
  1775. }
  1776. // 1.打开路由器供电
  1777. ctr_router(ON);
  1778. ctr_RTU_DTU(ON);
  1779. // 2.打开相机供电
  1780. ctr_photo(ON);
  1781. rt_kprintf("RTU POWER ON");
  1782. }
  1783. /*三色光源*/
  1784. void Led_Check(uint8_t tp)
  1785. {
  1786. if (tp == 3)
  1787. {
  1788. if (device_status.light_over_tim > 2 * 3600)
  1789. {
  1790. device_status.work_status = 1;
  1791. device_status.lamp_status = 1;
  1792. device_status.lamp1_status = 0;
  1793. device_status.lamp2_status = 0;
  1794. LOG_I("光控定时,计时中:光源1计数:%d", device_status.light_over_tim);
  1795. }
  1796. else if (device_status.light_over_tim > 3600 && device_status.light_over_tim < 7200)
  1797. {
  1798. device_status.work_status = 1;
  1799. device_status.lamp_status = 0;
  1800. device_status.lamp1_status = 1;
  1801. device_status.lamp2_status = 0;
  1802. LOG_I("光控定时,计时中:光源2计数:%d", device_status.light_over_tim);
  1803. }
  1804. else if (device_status.light_over_tim < 1 * 3600)
  1805. {
  1806. device_status.work_status = 1;
  1807. device_status.lamp_status = 0;
  1808. device_status.lamp1_status = 0;
  1809. device_status.lamp2_status = 1;
  1810. LOG_I("光控定时,计时中:光源3计数:%d", device_status.light_over_tim);
  1811. }
  1812. }
  1813. else if (tp == 6)
  1814. {
  1815. if (device_status.light_over_tim > 4 * 3600)
  1816. {
  1817. device_status.work_status = 1;
  1818. device_status.lamp_status = 1;
  1819. device_status.lamp1_status = 0;
  1820. device_status.lamp2_status = 0;
  1821. LOG_I("光控定时,计时中:光源1计数:%d", device_status.light_over_tim);
  1822. }
  1823. else if (device_status.light_over_tim < 14400 && device_status.light_over_tim > 7200)
  1824. {
  1825. device_status.work_status = 1;
  1826. device_status.lamp_status = 0;
  1827. device_status.lamp1_status = 1;
  1828. device_status.lamp2_status = 0;
  1829. LOG_I("光控定时,计时中:光源2计数:%d", device_status.light_over_tim);
  1830. }
  1831. else if (device_status.light_over_tim < 7200)
  1832. {
  1833. device_status.work_status = 1;
  1834. device_status.lamp_status = 0;
  1835. device_status.lamp1_status = 0;
  1836. device_status.lamp2_status = 1;
  1837. LOG_I("光控定时,计时中:光源3计数:%d", device_status.light_over_tim);
  1838. }
  1839. }
  1840. else if (tp == 9)
  1841. {
  1842. if (device_status.light_over_tim > 6 * 3600)
  1843. {
  1844. device_status.work_status = 1;
  1845. device_status.lamp_status = 1;
  1846. device_status.lamp1_status = 0;
  1847. device_status.lamp2_status = 0;
  1848. LOG_I("光控定时,计时中:光源1计数:%d", device_status.light_over_tim);
  1849. }
  1850. else if (device_status.light_over_tim < 6 * 3600 && device_status.light_over_tim > 3 * 3600)
  1851. {
  1852. device_status.work_status = 1;
  1853. device_status.lamp_status = 0;
  1854. device_status.lamp1_status = 1;
  1855. device_status.lamp2_status = 0;
  1856. LOG_I("光控定时,计时中:光源2计数:%d", device_status.light_over_tim);
  1857. }
  1858. else if (device_status.light_over_tim < 3 * 3600)
  1859. {
  1860. device_status.work_status = 1;
  1861. device_status.lamp_status = 0;
  1862. device_status.lamp1_status = 0;
  1863. device_status.lamp2_status = 1;
  1864. LOG_I("光控定时,计时中:光源3计数:%d", device_status.light_over_tim);
  1865. }
  1866. }
  1867. }
  1868. // 出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
  1869. void HardWare_Check(uint8_t boot)
  1870. {
  1871. if (boot == 0 && device_status.version_type != 6)
  1872. {
  1873. // 停止所有动作 -完成
  1874. HardWare_Init(boot);
  1875. }
  1876. else
  1877. {
  1878. // 停止所有动作
  1879. // 读取记录待机功耗(实测大概70mA)
  1880. //(需减去屏幕的功耗大概120mA)
  1881. // 动作上仓门,读取功率值保存
  1882. // 动作下仓门,读取功率值保存
  1883. // 动作清扫,读取功率值保存
  1884. // 打开镇流器,读取功率值保存
  1885. // 打开海康相机供电,读取功率值保存
  1886. // 打开补光灯,读取功率值保存
  1887. // 打开路由器/RTU供电,读取功率值保存
  1888. // 打开加热,读取功率值保存
  1889. }
  1890. }
  1891. // 工作状态检测
  1892. void WorkCheck(void)
  1893. {
  1894. uint16_t log_en_inr = LOG_INR; // 打印log的速度 数值越小 打印越频繁
  1895. static int16_t log_en_cnt = -1;
  1896. // 雨控检测,温控检测,电池电压检测
  1897. while (device_status.ds == 1 && device_status.rain_status == 0 && device_status.temp_status == 0 && device_status.bat_status == 0)
  1898. {
  1899. // 工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
  1900. if (device_status.timeSwitch == 1)
  1901. {
  1902. LOG_I("***********时控*****************");
  1903. // 如果开始和结束是同一天
  1904. if (device_status.startBar < device_status.stopBar)
  1905. {
  1906. LOG_I("开始和结束是同一天");
  1907. time_t now;
  1908. time(&now);
  1909. static struct tm nowtime; // 现在
  1910. static struct tm starttime; // 开始
  1911. static struct tm endtime; // 结束
  1912. // 获取当前时间戳
  1913. nowtime = *localtime(&now);
  1914. nowtime.tm_hour = nowtime.tm_hour - 8;
  1915. // 获取定时开始的时间戳
  1916. starttime = *localtime(&now);
  1917. starttime.tm_hour = device_status.startBar - 8;
  1918. starttime.tm_min = 0;
  1919. starttime.tm_sec = 0;
  1920. // 获取定时结束的时间戳
  1921. endtime = *localtime(&now);
  1922. endtime.tm_hour = device_status.stopBar - 8;
  1923. endtime.tm_min = 0;
  1924. endtime.tm_sec = 0;
  1925. LOG_I("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***", mktime(&nowtime), mktime(&starttime), mktime(&endtime));
  1926. // 如果当前时间大于开始时间且小于结束时间,且没有开始工作
  1927. if ((difftime(mktime(&nowtime), mktime(&starttime)) >= 0) && (difftime(mktime(&nowtime), mktime(&endtime)) < 0))
  1928. {
  1929. LOG_I("当前时间大于开始时间,小于结束时间,工作中!!!");
  1930. device_status.work_status = 1;
  1931. }
  1932. // 如果当前时间小于开始时间
  1933. else if (difftime(mktime(&nowtime), mktime(&starttime)) < 0)
  1934. {
  1935. LOG_I("当前时间小于开始时间,不工作!!!-%d-%d", mktime(&nowtime), mktime(&starttime));
  1936. device_status.work_status = 0;
  1937. }
  1938. // 当前时间大于结束时间
  1939. else
  1940. {
  1941. LOG_I("当前时间大于结束时间,不工作!!!-%d-%d", mktime(&nowtime), mktime(&starttime));
  1942. device_status.work_status = 0;
  1943. }
  1944. #if LAMP_EN == 1
  1945. // 如果当前时间超过20点,未到21点
  1946. if ((difftime(mktime(&nowtime), mktime(&starttime)) >= 0) && (difftime(mktime(&nowtime), mktime(&starttime) + 3600) < 0))
  1947. {
  1948. LOG_I("3led:20~21");
  1949. device_status.lamp_status = 1;
  1950. device_status.lamp1_status = 0;
  1951. device_status.lamp2_status = 0;
  1952. }
  1953. else if ((difftime(mktime(&nowtime), mktime(&starttime) + 3600) >= 0) && (difftime(mktime(&nowtime), mktime(&starttime) + 7200) < 0))
  1954. {
  1955. LOG_I("3led:21~22");
  1956. device_status.lamp1_status = 1;
  1957. device_status.lamp_status = 0;
  1958. device_status.lamp2_status = 0;
  1959. }
  1960. else if ((difftime(mktime(&nowtime), mktime(&starttime) + 7200) >= 0) && (difftime(mktime(&nowtime), mktime(&starttime) + 10800) < 0))
  1961. {
  1962. LOG_I("3led:22~23");
  1963. device_status.lamp2_status = 1;
  1964. device_status.lamp_status = 0;
  1965. device_status.lamp1_status = 0;
  1966. }
  1967. #endif
  1968. }
  1969. // 如果结束时间是第二天
  1970. else if (device_status.startBar > device_status.stopBar)
  1971. {
  1972. time_t now;
  1973. time(&now);
  1974. static struct tm nowtime; // 现在
  1975. static struct tm starttime; // 开始
  1976. static struct tm endtime; // 结束
  1977. // 获取当前时间戳
  1978. nowtime = *localtime(&now);
  1979. nowtime.tm_hour = nowtime.tm_hour - 8;
  1980. // 获取定时开始的时间戳
  1981. starttime = *localtime(&now);
  1982. starttime.tm_hour = device_status.startBar - 8;
  1983. starttime.tm_min = 0;
  1984. starttime.tm_sec = 0;
  1985. // 获取定时结束的时间戳
  1986. endtime = *localtime(&now);
  1987. endtime.tm_hour = device_status.stopBar - 8;
  1988. endtime.tm_min = 0;
  1989. endtime.tm_sec = 0;
  1990. LOG_I("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!", mktime(&nowtime), mktime(&starttime), mktime(&endtime));
  1991. // 如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
  1992. if ((difftime(mktime(&nowtime), mktime(&starttime)) > 0))
  1993. {
  1994. // 获取定时结束的时间戳
  1995. endtime = *localtime(&now); // 将获取到的时间转换为时间戳
  1996. endtime.tm_hour = device_status.stopBar - 8; // 这里需减8用于时区转换
  1997. endtime.tm_min = 0;
  1998. endtime.tm_sec = 0;
  1999. device_status.work_status = 1;
  2000. LOG_I("当前前时间大于开始时间,且没有开始工作");
  2001. }
  2002. // 当前时间小于开始时间
  2003. else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
  2004. {
  2005. // 获取定时结束的时间戳
  2006. endtime = *localtime(&now); // 将获取到的时间转换为时间戳
  2007. endtime.tm_hour = device_status.stopBar - 8; // 这里需减8用于时区转换
  2008. endtime.tm_min = 0;
  2009. endtime.tm_sec = 0;
  2010. // 如果当前时间戳小于结束时间戳
  2011. if (difftime(mktime(&nowtime), mktime(&endtime)) < 0)
  2012. {
  2013. LOG_I("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间");
  2014. device_status.work_status = 1;
  2015. }
  2016. else
  2017. {
  2018. LOG_I("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间");
  2019. device_status.work_status = 0; // 不在工作时间,待机
  2020. }
  2021. }
  2022. }
  2023. }
  2024. else
  2025. {
  2026. LOG_I("光控定时模式!");
  2027. /*
  2028. time_t now;
  2029. time(&now);
  2030. static struct tm nowstamp;
  2031. nowstamp =*localtime(&now);
  2032. nowstamp.tm_hour = nowstamp.tm_hour - 8;
  2033. static rt_uint32_t nowstamp_num = 0;
  2034. nowstamp_num = mktime(&nowstamp);
  2035. rt_kprintf("now stamp:%d\n",nowstamp_num);
  2036. */
  2037. // if (device_status.sun_status == 0 && device_status.light_over_tim != 0)
  2038. // {
  2039. //
  2040. // device_status.light_over_tim--;
  2041. //
  2042. //#if LAMP_EN == 1
  2043. // Led_Check(device_status.sunBar);
  2044. //#else
  2045. // device_status.work_status = 1;
  2046. // ;
  2047. // LOG_I("光控定时,计时中>>>>>>>>>:%d", device_status.light_over_tim);
  2048. //#endif
  2049. // }
  2050. // else
  2051. // {
  2052. // if (device_status.test_flag == 0)
  2053. // {
  2054. // ctr_lamp(OFF, 1);
  2055. //#if LAMP_EN == 1
  2056. // ctr_lamp(OFF, 2);
  2057. // ctr_lamp(OFF, 3);
  2058. //#endif
  2059. // }
  2060. // device_status.work_status = 0;
  2061. // if (device_status.light_over_tim == 0 && device_status.sun_status == 1)
  2062. // device_status.light_over_tim = device_status.sunBar * 3600;
  2063. // LOG_I("光控中,白天:%d!!!", device_status.sun_status);
  2064. // }
  2065. }
  2066. rt_thread_delay(1000);
  2067. }
  2068. if (chk_log_time)
  2069. {
  2070. LOG_I("雨控状态,温控状态,电压状态:%d-%d-%d", device_status.rain_status, device_status.temp_status, device_status.bat_status);
  2071. }
  2072. device_status.work_status = 0;
  2073. rt_thread_delay(1000);
  2074. }
  2075. /**
  2076. * @brief 孢子仪工作线程
  2077. *
  2078. */
  2079. void WorkTask_BZY(void)
  2080. {
  2081. if (device_status.ds == 1)
  2082. {
  2083. // 时间段判断
  2084. // 先判断第一个时间段
  2085. time_t now;
  2086. time(&now);
  2087. static struct tm now_stamp; // 现在
  2088. static struct tm cul_sh1_stamp; // 开始1
  2089. static struct tm cul_eh1_stamp; // 结束1
  2090. static struct tm cul_sh2_stamp; // 开始2
  2091. static struct tm cul_eh2_stamp; // 结束2
  2092. static struct tm cul_sh3_stamp; // 开始3
  2093. static struct tm cul_eh3_stamp; // 结束3
  2094. now_stamp = *localtime(&now);
  2095. // 获取定时开始的时间戳
  2096. cul_sh1_stamp = *localtime(&now);
  2097. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2098. cul_sh1_stamp.tm_min = 0;
  2099. cul_sh1_stamp.tm_sec = 0;
  2100. cul_sh2_stamp = *localtime(&now);
  2101. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2102. cul_sh2_stamp.tm_min = 0;
  2103. cul_sh2_stamp.tm_sec = 0;
  2104. cul_sh3_stamp = *localtime(&now);
  2105. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  2106. cul_sh3_stamp.tm_min = 0;
  2107. cul_sh3_stamp.tm_sec = 0;
  2108. // 获取定时结束的时间戳
  2109. cul_eh1_stamp = *localtime(&now);
  2110. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2111. cul_eh1_stamp.tm_min = 0;
  2112. cul_eh1_stamp.tm_sec = 0;
  2113. cul_eh2_stamp = *localtime(&now);
  2114. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2115. cul_eh2_stamp.tm_min = 0;
  2116. cul_eh2_stamp.tm_sec = 0;
  2117. cul_eh3_stamp = *localtime(&now);
  2118. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2119. cul_eh3_stamp.tm_min = 0;
  2120. cul_eh3_stamp.tm_sec = 0;
  2121. LOG_I("采集时间1-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_sh1_stamp.tm_year + 1900, cul_sh1_stamp.tm_mon + 1, cul_sh1_stamp.tm_mday, cul_sh1_stamp.tm_hour, cul_sh1_stamp.tm_min, cul_sh1_stamp.tm_sec);
  2122. LOG_I("采集时间2-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_sh2_stamp.tm_year + 1900, cul_sh2_stamp.tm_mon + 1, cul_sh2_stamp.tm_mday, cul_sh2_stamp.tm_hour, cul_sh2_stamp.tm_min, cul_sh2_stamp.tm_sec);
  2123. LOG_I("采集时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_sh3_stamp.tm_year + 1900, cul_sh3_stamp.tm_mon + 1, cul_sh3_stamp.tm_mday, cul_sh3_stamp.tm_hour, cul_sh3_stamp.tm_min, cul_sh3_stamp.tm_sec);
  2124. LOG_I("结束时间1-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_eh1_stamp.tm_year + 1900, cul_eh1_stamp.tm_mon + 1, cul_eh1_stamp.tm_mday, cul_eh1_stamp.tm_hour, cul_eh1_stamp.tm_min, cul_eh1_stamp.tm_sec);
  2125. LOG_I("结束时间2-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_eh2_stamp.tm_year + 1900, cul_eh2_stamp.tm_mon + 1, cul_eh2_stamp.tm_mday, cul_eh2_stamp.tm_hour, cul_eh2_stamp.tm_min, cul_eh2_stamp.tm_sec);
  2126. LOG_I("结束时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_eh3_stamp.tm_year + 1900, cul_eh3_stamp.tm_mon + 1, cul_eh3_stamp.tm_mday, cul_eh3_stamp.tm_hour, cul_eh3_stamp.tm_min, cul_eh3_stamp.tm_sec);
  2127. // 此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2128. // 当前时间戳小于第一段开始时间戳
  2129. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2130. {
  2131. time_t now;
  2132. time(&now);
  2133. cul_sh1_stamp = *localtime(&now);
  2134. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2135. cul_sh1_stamp.tm_min = 0;
  2136. cul_sh1_stamp.tm_sec = 0;
  2137. now_stamp = *localtime(&now);
  2138. LOG_I("======收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_sh1_stamp.tm_year + 1900, cul_sh1_stamp.tm_mon + 1, cul_sh1_stamp.tm_mday, cul_sh1_stamp.tm_hour, cul_sh1_stamp.tm_min, cul_sh1_stamp.tm_sec);
  2139. LOG_I("======未到收集时间1:%04d年%02d月%02d日%02d时%02d分%02d秒.", now_stamp.tm_year + 1900, now_stamp.tm_mon + 1, now_stamp.tm_mday, now_stamp.tm_hour, now_stamp.tm_min, now_stamp.tm_sec);
  2140. LOG_D("run_sta:%d", device_status.run_sta);
  2141. }
  2142. // 当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2143. if (difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0 && difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2144. {
  2145. time_t now;
  2146. time(&now);
  2147. cul_sh2_stamp = *localtime(&now);
  2148. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2149. cul_sh2_stamp.tm_min = 0;
  2150. cul_sh2_stamp.tm_sec = 0;
  2151. now_stamp = *localtime(&now);
  2152. LOG_I("==============收集时间2:%04d年%02d月%02d日%02d时%02d分%02d秒.\n", cul_sh2_stamp.tm_year + 1900, cul_sh2_stamp.tm_mon + 1, cul_sh2_stamp.tm_mday, cul_sh2_stamp.tm_hour, cul_sh2_stamp.tm_min, cul_sh2_stamp.tm_sec);
  2153. LOG_I("==============未到收集时间2:%04d年%02d月%02d日%02d时%02d分%02d秒.\n", now_stamp.tm_year + 1900, now_stamp.tm_mon + 1, now_stamp.tm_mday, now_stamp.tm_hour, now_stamp.tm_min, now_stamp.tm_sec);
  2154. LOG_D("run_sta:%d", device_status.run_sta);
  2155. }
  2156. // 如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2157. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2158. {
  2159. // 转盘到涂凡士林位置
  2160. if (device_status.run_sta == 0)
  2161. {
  2162. LOG_I("start to collect!!!");
  2163. DTU_Write("collect.mp3");
  2164. ctr_turn(90, 4);
  2165. rt_thread_delay(2000);
  2166. // 涂凡士林
  2167. ctr_Petroleum_jelly(10);
  2168. rt_thread_delay(5000);
  2169. }
  2170. /*转盘到采集位置*/
  2171. if (device_status.run_sta == 4)
  2172. ctr_turn(90, 1);
  2173. // 只有开始和结束时刻不同的时间段,才有效
  2174. if ((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0) || (difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0))
  2175. {
  2176. if ((difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0) && difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) > 0)
  2177. {
  2178. device_status.work_status = 1;
  2179. ctr_Collect_Fan(ON , syscfg.fan_speed);
  2180. // 当前时间戳小于第一段结束时间戳
  2181. while (difftime(mktime(&cul_eh1_stamp), mktime(&now_stamp)) > 0)
  2182. {
  2183. time(&now);
  2184. now_stamp = *localtime(&now);
  2185. cul_eh1_stamp = *localtime(&now);
  2186. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2187. cul_eh1_stamp.tm_min = 0;
  2188. cul_eh1_stamp.tm_sec = 0;
  2189. if (device_status.run_sta != 1)
  2190. break;
  2191. LOG_I("1===========采集风机启动,正在收集.....%d-%d", mktime(&now_stamp), mktime(&cul_eh1_stamp));
  2192. rt_thread_mdelay(10000);
  2193. }
  2194. }
  2195. else
  2196. {
  2197. device_status.work_status = 0;
  2198. ctr_Collect_Fan(OFF , syscfg.fan_speed);
  2199. LOG_I("未到收集1时间!");
  2200. }
  2201. if (difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0 && difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) > 0)
  2202. {
  2203. device_status.work_status = 1;
  2204. ctr_Collect_Fan(ON , syscfg.fan_speed);
  2205. while (difftime(mktime(&cul_eh2_stamp), mktime(&now_stamp)) > 0)
  2206. {
  2207. time(&now);
  2208. now_stamp = *localtime(&now);
  2209. cul_eh2_stamp = *localtime(&now);
  2210. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2211. cul_eh2_stamp.tm_min = 0;
  2212. cul_eh2_stamp.tm_sec = 0;
  2213. if (device_status.run_sta != 1)
  2214. break;
  2215. LOG_I("2===========采集风机启动,正在收集.....%d-%d", mktime(&now_stamp), mktime(&cul_eh2_stamp));
  2216. rt_thread_mdelay(10000);
  2217. }
  2218. }
  2219. else
  2220. {
  2221. device_status.work_status = 0;
  2222. ctr_Collect_Fan(OFF , syscfg.fan_speed);
  2223. LOG_I("未到收集2时间!");
  2224. }
  2225. }
  2226. else
  2227. {
  2228. LOG_I("this collect time is 0s,The wait time cannot be negative!");
  2229. }
  2230. }
  2231. if (device_status.run_sta == 2)
  2232. {
  2233. ctr_Collect_Fan(OFF , syscfg.fan_speed);
  2234. device_status.develop_flag = 1;
  2235. device_status.work_status = 2;
  2236. ctr_develop(); // 培养
  2237. ctr_turn(90, 3);
  2238. }
  2239. if (device_status.run_sta == 3)
  2240. {
  2241. ctr_Collect_Fan(OFF , syscfg.fan_speed);
  2242. device_status.work_status = 3;
  2243. device_status.autotakepic = 1; // 对焦拍照
  2244. }
  2245. // 在培养之前再次确认时间戳
  2246. time(&now);
  2247. now_stamp = *localtime(&now);
  2248. cul_eh2_stamp = *localtime(&now);
  2249. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2250. cul_eh2_stamp.tm_min = 0;
  2251. cul_eh2_stamp.tm_sec = 0;
  2252. // 当前时间戳大于收集结束时间戳并且run_sta==1
  2253. if (device_status.run_sta == 1 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) > 0)
  2254. {
  2255. ctr_Collect_Fan(OFF , syscfg.fan_speed);
  2256. LOG_I("===============collect ok,turn to drop!!!");
  2257. device_status.develop_flag = 1;
  2258. device_status.work_status = 2; // 现在是培养
  2259. DTU_Write("develop.mp3");
  2260. if (device_status.run_sta == 1)
  2261. ctr_turn(90, 2); /*转盘到培养位置*/
  2262. rt_thread_delay(1000);
  2263. ctr_shake(ON, device_status.shake_sec);
  2264. rt_thread_delay(1000);
  2265. ctr_develop(); // 培养
  2266. if (device_status.run_sta == 2)
  2267. ctr_turn(90, 3); /*转盘到拍照位置*/
  2268. rt_thread_delay(1000);
  2269. device_status.work_status = 3; // 现在是拍照
  2270. device_status.autotakepic = 1; // 对焦拍照
  2271. while (device_status.autotakepic == 0)
  2272. {
  2273. rt_thread_mdelay(1000);
  2274. LOG_I("正在对焦拍照............B");
  2275. }
  2276. device_status.work_status = 0; // 待机
  2277. DTU_Write("wait.mp3");
  2278. LOG_I("=====================work finished==================");
  2279. }
  2280. rt_thread_delay(1000);
  2281. }
  2282. device_status.work_status = 0; // 待机
  2283. LOG_W("==================!!!设备已待机!!!===================");
  2284. }
  2285. /********************* 胶带结构的孢子仪主工作流程 ************************/
  2286. #define test_min 0
  2287. /**
  2288. * @brief 检查是否到工作时间段
  2289. *
  2290. * @return uint8_t 0-未到 / 已到:时间段编号(1-2-3)
  2291. */
  2292. static uint8_t Confirm_time_period(void)
  2293. {
  2294. static uint8_t once_print = 0;
  2295. uint8_t log_en_inr = LOG_INR * 3; // 打印log的速度 数值越小 打印越频繁
  2296. static int16_t log_en_cnt = -1;
  2297. uint8_t time_period = 0;
  2298. // 时间段判断
  2299. // 先判断第一个时间段
  2300. time_t now;
  2301. time(&now);
  2302. static struct tm now_stamp; // 现在
  2303. static struct tm cul_sh1_stamp; // 开始1
  2304. static struct tm cul_eh1_stamp; // 结束1
  2305. static struct tm cul_sh2_stamp; // 开始2
  2306. static struct tm cul_eh2_stamp; // 结束2
  2307. static struct tm cul_sh3_stamp; // 开始3
  2308. static struct tm cul_eh3_stamp; // 结束3
  2309. now_stamp = *localtime(&now);
  2310. // 获取定时开始的时间戳
  2311. cul_sh1_stamp = *localtime(&now);
  2312. cul_sh1_stamp.tm_hour = device_status.collecting_sh1;
  2313. cul_sh1_stamp.tm_min = 0 + test_min;
  2314. cul_sh1_stamp.tm_sec = 0;
  2315. cul_sh2_stamp = *localtime(&now);
  2316. cul_sh2_stamp.tm_hour = device_status.collecting_sh2;
  2317. cul_sh2_stamp.tm_min = 0 + test_min;
  2318. cul_sh2_stamp.tm_sec = 0;
  2319. cul_sh3_stamp = *localtime(&now);
  2320. cul_sh3_stamp.tm_hour = device_status.collecting_sh3;
  2321. cul_sh3_stamp.tm_min = 0 + test_min;
  2322. cul_sh3_stamp.tm_sec = 0;
  2323. // 获取定时结束的时间戳
  2324. cul_eh1_stamp = *localtime(&now);
  2325. cul_eh1_stamp.tm_hour = device_status.collecting_eh1;
  2326. cul_eh1_stamp.tm_min = 0 + test_min;
  2327. cul_eh1_stamp.tm_sec = 0;
  2328. cul_eh2_stamp = *localtime(&now);
  2329. cul_eh2_stamp.tm_hour = device_status.collecting_eh2;
  2330. cul_eh2_stamp.tm_min = 0 + test_min;
  2331. cul_eh2_stamp.tm_sec = 0;
  2332. cul_eh3_stamp = *localtime(&now);
  2333. cul_eh3_stamp.tm_hour = device_status.collecting_eh3;
  2334. cul_eh3_stamp.tm_min = 0 + test_min;
  2335. cul_eh3_stamp.tm_sec = 0;
  2336. if (once_print == 0 || chk_log_time)
  2337. {
  2338. rt_kprintf("\n\n");
  2339. LOG_I("采集时间1-%04d-%02d-%02d %02d:%02d:%02d", cul_sh1_stamp.tm_year + 1900, cul_sh1_stamp.tm_mon + 1, cul_sh1_stamp.tm_mday, cul_sh1_stamp.tm_hour, cul_sh1_stamp.tm_min, cul_sh1_stamp.tm_sec);
  2340. LOG_I("结束时间1-%04d-%02d-%02d %02d:%02d:%02d\n", cul_eh1_stamp.tm_year + 1900, cul_eh1_stamp.tm_mon + 1, cul_eh1_stamp.tm_mday, cul_eh1_stamp.tm_hour, cul_eh1_stamp.tm_min, cul_eh1_stamp.tm_sec);
  2341. LOG_I("采集时间2-%04d-%02d-%02d %02d:%02d:%02d", cul_sh2_stamp.tm_year + 1900, cul_sh2_stamp.tm_mon + 1, cul_sh2_stamp.tm_mday, cul_sh2_stamp.tm_hour, cul_sh2_stamp.tm_min, cul_sh2_stamp.tm_sec);
  2342. LOG_I("结束时间2-%04d-%02d-%02d %02d:%02d:%02d", cul_eh2_stamp.tm_year + 1900, cul_eh2_stamp.tm_mon + 1, cul_eh2_stamp.tm_mday, cul_eh2_stamp.tm_hour, cul_eh2_stamp.tm_min, cul_eh2_stamp.tm_sec);
  2343. // LOG_I("采集时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_sh3_stamp.tm_year + 1900, cul_sh3_stamp.tm_mon + 1, cul_sh3_stamp.tm_mday, cul_sh3_stamp.tm_hour, cul_sh3_stamp.tm_min, cul_sh3_stamp.tm_sec);
  2344. // LOG_I("结束时间3-%04d年%02d月%02d日%02d时%02d分%02d秒.", cul_eh3_stamp.tm_year + 1900, cul_eh3_stamp.tm_mon + 1, cul_eh3_stamp.tm_mday, cul_eh3_stamp.tm_hour, cul_eh3_stamp.tm_min, cul_eh3_stamp.tm_sec);
  2345. rt_kprintf("\n\n");
  2346. once_print = 1;
  2347. }
  2348. // 此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  2349. // 当前时间戳小于第一段开始时间戳
  2350. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) < 0)
  2351. {
  2352. if (chk_log_time)
  2353. {
  2354. uint32_t time_cache = cul_sh1_stamp.tm_hour * 3600 + cul_sh1_stamp.tm_min * 60 + cul_sh1_stamp.tm_sec;
  2355. uint32_t time_now = now_stamp.tm_hour * 3600 + now_stamp.tm_min * 60 + now_stamp.tm_sec;
  2356. uint32_t time_remainder = time_cache - time_now;
  2357. uint8_t hour = time_remainder / 3600;
  2358. uint8_t min = (time_remainder % 3600) / 60;
  2359. uint8_t sec = time_remainder % 60;
  2360. LOG_I(" ====== 未到收集时间 A :%04d-%02d-%02d %02d:%02d:%02d 剩余:%02d:%02d:%02d ====== run_sta %d",
  2361. cul_sh1_stamp.tm_year + 1900,
  2362. cul_sh1_stamp.tm_mon + 1,
  2363. cul_sh1_stamp.tm_mday,
  2364. cul_sh1_stamp.tm_hour,
  2365. cul_sh1_stamp.tm_min,
  2366. cul_sh1_stamp.tm_sec,
  2367. hour,
  2368. min,
  2369. sec,
  2370. device_status.run_sta);
  2371. }
  2372. time_period = 0;
  2373. }
  2374. // 当前时间戳小于第二段结束 时间戳并且大于第1段结束时间戳
  2375. if (difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) < 0 && difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) > 0)
  2376. {
  2377. if (chk_log_time)
  2378. {
  2379. uint32_t time_cache = cul_sh2_stamp.tm_hour * 3600 + cul_sh2_stamp.tm_min * 60 + cul_sh2_stamp.tm_sec;
  2380. uint32_t time_now = now_stamp.tm_hour * 3600 + now_stamp.tm_min * 60 + now_stamp.tm_sec;
  2381. uint32_t time_remainder = time_cache - time_now;
  2382. uint8_t hour = time_remainder / 3600;
  2383. uint8_t min = (time_remainder % 3600) / 60;
  2384. uint8_t sec = time_remainder % 60;
  2385. LOG_I(" ====== 未到收集时间 B :%04d-%02d-%02d %02d:%02d:%02d 剩余:%02d:%02d:%02d ====== run_sta %d",
  2386. cul_sh2_stamp.tm_year + 1900,
  2387. cul_sh2_stamp.tm_mon + 1,
  2388. cul_sh2_stamp.tm_mday,
  2389. cul_sh2_stamp.tm_hour,
  2390. cul_sh2_stamp.tm_min,
  2391. cul_sh2_stamp.tm_sec,
  2392. hour,
  2393. min,
  2394. sec,
  2395. device_status.run_sta);
  2396. }
  2397. time_period = 0;
  2398. }
  2399. // 如果当前 时间戳大于开始时间戳并且小于 结束时间戳
  2400. if (difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)) < 0)
  2401. {
  2402. time_period = 1;
  2403. }
  2404. else if (difftime(mktime(&now_stamp), mktime(&cul_sh2_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh2_stamp)) < 0)
  2405. {
  2406. time_period = 2;
  2407. }
  2408. else if (difftime(mktime(&now_stamp), mktime(&cul_sh3_stamp)) >= 0 && difftime(mktime(&now_stamp), mktime(&cul_eh3_stamp)) < 0)
  2409. {
  2410. time_period = 3;
  2411. }
  2412. // if (chk_log_time)
  2413. // rt_kprintf("time_period > %d %X %X\n", time_period, difftime(mktime(&now_stamp), mktime(&cul_sh1_stamp)), difftime(mktime(&now_stamp), mktime(&cul_eh1_stamp)));
  2414. return time_period;
  2415. }
  2416. /**
  2417. * @brief
  2418. *
  2419. * @param mot 电机选择
  2420. * @param limit1 限位1选择
  2421. * @param limit2 限位2选择(不需要的话填0)
  2422. * @param mct 电机操作选择
  2423. * @return Res_def 0-成功
  2424. */
  2425. Res_def Mtor_Control(Motor_type mot, Limit_type limit1, Limit_type limit2, Motor_Ctl_type mct, uint16_t timeout)
  2426. {
  2427. uint8_t res = res_OK;
  2428. uint16_t Motor_L, Motor_R, Lim1, Lim2 = LIMIT_0;
  2429. uint16_t wait_out = 0;
  2430. uint8_t Check_EN = 0;
  2431. if (JD_Check_Test_Sta(tt_auto) == res_OK)
  2432. {
  2433. Check_EN = 2;
  2434. }
  2435. switch (mot) // 电机选型
  2436. {
  2437. case mot_ZP:
  2438. Motor_L = TURN_ZP_L;
  2439. Motor_R = TURN_ZP_R;
  2440. break;
  2441. case mot_CM:
  2442. Motor_L = TURN_CM_L;
  2443. Motor_R = TURN_CM_R;
  2444. break;
  2445. default:
  2446. LOG_W("illegal Motor_type %d", mot);
  2447. return res_Verifi;
  2448. break;
  2449. }
  2450. switch (limit1) // 限位选型1
  2451. {
  2452. case lt_Angle_0:
  2453. Lim1 = LIMIT_0;
  2454. break;
  2455. case lt_Angle_90:
  2456. Lim1 = LIMIT_90;
  2457. break;
  2458. default:
  2459. LOG_W("illegal limit1_type %d", limit1);
  2460. return res_Verifi;
  2461. break;
  2462. }
  2463. if (limit2 != 0)
  2464. {
  2465. switch (limit2) // 限位选型2
  2466. {
  2467. case lt_Angle_0:
  2468. Lim2 = LIMIT_0;
  2469. rt_pin_mode(Lim2, PIN_MODE_INPUT);
  2470. break;
  2471. case lt_Angle_90:
  2472. Lim2 = LIMIT_90;
  2473. rt_pin_mode(Lim2, PIN_MODE_INPUT);
  2474. break;
  2475. default:
  2476. LOG_W("illegal limit2_type %d", limit2);
  2477. return res_Verifi;
  2478. break;
  2479. }
  2480. }
  2481. rt_pin_mode(Motor_L, PIN_MODE_OUTPUT);
  2482. rt_pin_mode(Motor_R, PIN_MODE_OUTPUT);
  2483. rt_pin_mode(Lim1, PIN_MODE_INPUT);
  2484. // 走出限位
  2485. while ((rt_pin_read(Lim1) == 1) && (wait_out * 1000 < timeout))
  2486. {
  2487. switch (mct)
  2488. {
  2489. case mct_Foreward:
  2490. rt_pin_write(Motor_R, ON);
  2491. rt_pin_write(Motor_L, OFF);
  2492. break;
  2493. case mct_Reversal:
  2494. rt_pin_write(Motor_R, OFF);
  2495. rt_pin_write(Motor_L, ON);
  2496. break;
  2497. case mct_Stop:
  2498. rt_pin_write(Motor_R, OFF);
  2499. rt_pin_write(Motor_L, OFF);
  2500. break;
  2501. default:
  2502. LOG_W("illegal Motor_Ctl_type %d", mct);
  2503. return res_Verifi;
  2504. break;
  2505. }
  2506. wait_out++;
  2507. rt_thread_mdelay(1000);
  2508. rt_kprintf("wait exit limit %d/%d \n", wait_out, timeout / 1000);
  2509. }
  2510. // 参数校验完成 开始操作
  2511. switch (mct)
  2512. {
  2513. case mct_Foreward:
  2514. rt_pin_write(Motor_R, ON);
  2515. rt_pin_write(Motor_L, OFF);
  2516. break;
  2517. case mct_Reversal:
  2518. rt_pin_write(Motor_R, OFF);
  2519. rt_pin_write(Motor_L, ON);
  2520. break;
  2521. case mct_Stop:
  2522. rt_pin_write(Motor_R, OFF);
  2523. rt_pin_write(Motor_L, OFF);
  2524. break;
  2525. default:
  2526. LOG_W("illegal Motor_Ctl_type %d", mct);
  2527. return res_Verifi;
  2528. break;
  2529. }
  2530. if (mct == mct_Stop)
  2531. {
  2532. rt_thread_mdelay(10);
  2533. return res;
  2534. }
  2535. res = res_Timeout;
  2536. while ((wait_out * 100 < timeout) && (limit2 == 0))
  2537. {
  2538. switch (mct)
  2539. {
  2540. case mct_Foreward:
  2541. rt_pin_write(Motor_R, ON);
  2542. rt_pin_write(Motor_L, OFF);
  2543. break;
  2544. case mct_Reversal:
  2545. rt_pin_write(Motor_R, OFF);
  2546. rt_pin_write(Motor_L, ON);
  2547. break;
  2548. case mct_Stop:
  2549. rt_pin_write(Motor_R, OFF);
  2550. rt_pin_write(Motor_L, OFF);
  2551. break;
  2552. default:
  2553. LOG_W("illegal Motor_Ctl_type %d", mct);
  2554. return res_Verifi;
  2555. break;
  2556. }
  2557. if (rt_pin_read(Lim1) == 1)
  2558. {
  2559. // LOG_W("lim1 TIG");
  2560. res = res_OK;
  2561. break;
  2562. }
  2563. wait_out++;
  2564. rt_thread_mdelay(100);
  2565. if ((Check_EN == 2) && (JD_Check_Test_Sta(tt_auto) != res_OK))
  2566. {
  2567. rt_kprintf("tt_tape Break 2\n");
  2568. break;
  2569. }
  2570. }
  2571. while ((wait_out * 100 < timeout) && (limit2 != 0))
  2572. {
  2573. switch (mct)
  2574. {
  2575. case mct_Foreward:
  2576. rt_pin_write(Motor_R, ON);
  2577. rt_pin_write(Motor_L, OFF);
  2578. break;
  2579. case mct_Reversal:
  2580. rt_pin_write(Motor_R, OFF);
  2581. rt_pin_write(Motor_L, ON);
  2582. break;
  2583. case mct_Stop:
  2584. rt_pin_write(Motor_R, OFF);
  2585. rt_pin_write(Motor_L, OFF);
  2586. break;
  2587. default:
  2588. LOG_W("illegal Motor_Ctl_type %d", mct);
  2589. return res_Verifi;
  2590. break;
  2591. }
  2592. if (rt_pin_read(Lim1) == 0 || rt_pin_read(Lim2) == 0)
  2593. {
  2594. // LOG_W("lim1 or 2 TIG");
  2595. res = res_OK;
  2596. break;
  2597. }
  2598. wait_out++;
  2599. rt_thread_mdelay(100);
  2600. if ((Check_EN == 2) && (JD_Check_Test_Sta(tt_auto) != res_OK))
  2601. {
  2602. rt_kprintf("tt_tape Break 2\n");
  2603. break;
  2604. }
  2605. }
  2606. rt_pin_write(TURN_ZP_R, OFF);
  2607. rt_pin_write(TURN_ZP_L, OFF);
  2608. if (wait_out * 100 >= timeout && Check_EN == 0)
  2609. {
  2610. Report_Limit_Error(le_limit_tape);
  2611. rt_kprintf("胶带限位失效啦\n");
  2612. }
  2613. return res;
  2614. }
  2615. /**
  2616. * @brief 控制转盘电机
  2617. *
  2618. * @param mct 控制操作
  2619. * @return Res_def 执行结果
  2620. */
  2621. Res_def Mtor_Control_ZP(Motor_Ctl_type mct)
  2622. {
  2623. ctr_Collect_Fan(device_status.collect_fan_status , 100);
  2624. uint8_t res = Mtor_Control(mot_ZP, lt_Angle_0, 0, mct, 15000);
  2625. rt_kprintf("Mtor_Control finsh %d - %d\n", mct, res);
  2626. ctr_Collect_Fan(device_status.collect_fan_status , syscfg.fan_speed);
  2627. return res;
  2628. }
  2629. Res_def Mtor_Control_CM(Motor_Ctl_type mct)
  2630. {
  2631. LOG_I("Mtor_Control_CM Start\n");
  2632. uint8_t res = Mtor_Control(mot_CM, lt_Angle_0, 0, mct, 20000);
  2633. LOG_I("Mtor_Control Finsh %d - %d\n", mct, res);
  2634. return res;
  2635. }
  2636. /**
  2637. * @brief 获取当前时间
  2638. *
  2639. * @param now_stamp 存放时间戳的指针
  2640. */
  2641. void Get_now_time(struct tm *now_stamp)
  2642. {
  2643. time_t now;
  2644. time(&now);
  2645. *now_stamp = *localtime(&now);
  2646. }
  2647. /**
  2648. * @brief 检查是否处于工作时间段
  2649. *
  2650. * @param coll_ch 时间段通道
  2651. * @return Temporal_list 检查结果
  2652. */
  2653. Temporal_list Check_Work_time_period(uint8_t coll_ch)
  2654. {
  2655. static struct tm now_stamp = {0}; // 现在
  2656. static struct tm cul_s_stamp = {0}; // 开始
  2657. static struct tm cul_e_stamp = {0}; // 结束
  2658. Get_now_time(&now_stamp);
  2659. Get_now_time(&cul_s_stamp);
  2660. Get_now_time(&cul_e_stamp);
  2661. switch (coll_ch)
  2662. {
  2663. case 1:
  2664. cul_s_stamp.tm_hour = device_status.collecting_sh1;
  2665. cul_s_stamp.tm_min = 0 + test_min;
  2666. cul_s_stamp.tm_sec = 0;
  2667. cul_e_stamp.tm_hour = device_status.collecting_eh1;
  2668. cul_e_stamp.tm_min = 0 + test_min;
  2669. cul_e_stamp.tm_sec = 0;
  2670. break;
  2671. case 2:
  2672. cul_s_stamp.tm_hour = device_status.collecting_sh2;
  2673. cul_s_stamp.tm_min = 0 + test_min;
  2674. cul_s_stamp.tm_sec = 0;
  2675. cul_e_stamp.tm_hour = device_status.collecting_eh2;
  2676. cul_e_stamp.tm_min = 0 + test_min;
  2677. cul_e_stamp.tm_sec = 0;
  2678. // rt_kprintf("cul_s_stamp %d cul_e_stamp %d\n", cul_s_stamp.tm_hour, cul_e_stamp.tm_hour);
  2679. if (cul_s_stamp.tm_hour == cul_e_stamp.tm_hour) // 检查第二时间段是否有效
  2680. {
  2681. return tl_Error;
  2682. }
  2683. break;
  2684. default:
  2685. return tl_Verifi;
  2686. break;
  2687. }
  2688. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) >= 0 && // 大于开始时间
  2689. difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) < 0) // 小于结束时间
  2690. {
  2691. return tl_Center;
  2692. }
  2693. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) < 0) // 小于开始时间
  2694. {
  2695. return tl_Front;
  2696. }
  2697. if (difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) >= 0) // 大于结束时间
  2698. {
  2699. // rt_kprintf("now %02d:%02d:%02d start %02d:%02d:%02d end %02d:%02d:%02d\n" ,
  2700. // now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec,
  2701. // cul_s_stamp.tm_hour,cul_s_stamp.tm_min,cul_s_stamp.tm_sec,
  2702. // cul_e_stamp.tm_hour,cul_e_stamp.tm_min,cul_e_stamp.tm_sec
  2703. // );
  2704. return tl_Back;
  2705. }
  2706. return tl_Verifi;
  2707. }
  2708. /**
  2709. * @brief 检查是否处于工作时间段 跨0点模式
  2710. *
  2711. * @param coll_ch 时间段通道
  2712. * @return Temporal_list 检查结果
  2713. */
  2714. Temporal_list Check_Work_time_period_0oclock(void)
  2715. {
  2716. static struct tm now_stamp = {0}; // 现在
  2717. static struct tm cul_s_stamp = {0}; // 开始
  2718. static struct tm cul_e_stamp = {0}; // 结束
  2719. Get_now_time(&now_stamp);
  2720. Get_now_time(&cul_s_stamp);
  2721. Get_now_time(&cul_e_stamp);
  2722. /** STEP1:get cheange value
  2723. *
  2724. */
  2725. uint8_t cv_start = 0, cv_end = 0;
  2726. cv_start = device_status.collecting_sh1 + 24;
  2727. cv_end = device_status.collecting_eh1 + 48;
  2728. /** STEP2: make zero
  2729. *
  2730. */
  2731. cv_end -= cv_start;
  2732. /** SETP3: get Dvalue
  2733. *
  2734. */
  2735. if (now_stamp.tm_hour >= device_status.collecting_eh1 && now_stamp.tm_hour < device_status.collecting_sh1) // not yet
  2736. {
  2737. // rt_kprintf("未到定时区间\n");
  2738. return tl_Front;
  2739. }
  2740. else if (now_stamp.tm_hour >= device_status.collecting_sh1 && now_stamp.tm_hour < 24) // Phase 1
  2741. {
  2742. // rt_kprintf("阶段1\n");
  2743. now_stamp.tm_hour += 24;
  2744. now_stamp.tm_hour -= cv_start;
  2745. }
  2746. else
  2747. {
  2748. // rt_kprintf("阶段2\n");
  2749. now_stamp.tm_hour += 48;
  2750. now_stamp.tm_hour -= cv_start;
  2751. }
  2752. // rt_kprintf("当前时间为 %d 设定时间为 0 ~ %d\n" ,now_stamp.tm_hour ,cv_end);
  2753. cul_s_stamp.tm_hour = 0;
  2754. cul_s_stamp.tm_min = 0 + test_min;
  2755. cul_s_stamp.tm_sec = 0;
  2756. cul_e_stamp.tm_hour = cv_end;
  2757. cul_e_stamp.tm_min = 0 + test_min;
  2758. cul_e_stamp.tm_sec = 0;
  2759. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) >= 0 && // 大于开始时间
  2760. difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) < 0) // 小于结束时间
  2761. {
  2762. return tl_Center;
  2763. }
  2764. if (difftime(mktime(&now_stamp), mktime(&cul_s_stamp)) < 0) // 小于开始时间
  2765. {
  2766. return tl_Front;
  2767. }
  2768. if (difftime(mktime(&now_stamp), mktime(&cul_e_stamp)) >= 0) // 大于结束时间
  2769. {
  2770. // rt_kprintf("now %02d:%02d:%02d start %02d:%02d:%02d end %02d:%02d:%02d\n" ,
  2771. // now_stamp.tm_hour,now_stamp.tm_min,now_stamp.tm_sec,
  2772. // cul_s_stamp.tm_hour,cul_s_stamp.tm_min,cul_s_stamp.tm_sec,
  2773. // cul_e_stamp.tm_hour,cul_e_stamp.tm_min,cul_e_stamp.tm_sec
  2774. // );
  2775. return tl_Back;
  2776. }
  2777. return tl_Verifi;
  2778. }
  2779. /**
  2780. * @brief 写工作运行状态
  2781. *
  2782. * @param sta 要写入的值
  2783. */
  2784. static void Set_run_sta(run_sta_list sta)
  2785. {
  2786. device_status.run_sta = sta;
  2787. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  2788. uint8_t check = 0;
  2789. ef_get_env_blob("run_sta", &check, sizeof(device_status.run_sta), NULL);
  2790. if (check != sta)
  2791. {
  2792. ef_set_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta));
  2793. }
  2794. rt_kprintf("Set_run_sta > %d \n", check);
  2795. }
  2796. void Set_runsta_reset(void)
  2797. {
  2798. Recode_e_code(e_set_rs_RESET, 1, "工作流程被复位");
  2799. Set_run_sta(rs_RESET);
  2800. }
  2801. MSH_CMD_EXPORT(Set_runsta_reset, Set_runsta_reset);
  2802. /**
  2803. * @brief 读工作运行状态
  2804. *
  2805. * @return run_sta_list 读出的值
  2806. */
  2807. static run_sta_list Get_run_sta(void)
  2808. {
  2809. ef_get_env_blob("run_sta", &device_status.run_sta, sizeof(device_status.run_sta), NULL);
  2810. return device_status.run_sta;
  2811. }
  2812. /**
  2813. * @brief 获取剩余工作时间
  2814. *
  2815. * @param coll_ch 工作时间段通道号
  2816. * @return uint8_t 百分比的值
  2817. */
  2818. static int8_t Get_remainder_time(uint8_t coll_ch)
  2819. {
  2820. static struct tm now_stamp = {0}; // 现在
  2821. static struct tm cul_s_stamp = {0}; // 开始
  2822. static struct tm cul_e_stamp = {0}; // 结束
  2823. uint32_t dividend = 0, divisor = 0, quotient = 0;
  2824. int8_t result = 0;
  2825. Get_now_time(&now_stamp);
  2826. Get_now_time(&cul_s_stamp);
  2827. Get_now_time(&cul_e_stamp);
  2828. switch (coll_ch)
  2829. {
  2830. case 1:
  2831. cul_s_stamp.tm_hour = device_status.collecting_sh1;
  2832. cul_s_stamp.tm_min = 0 + test_min;
  2833. cul_s_stamp.tm_sec = 0;
  2834. cul_e_stamp.tm_hour = device_status.collecting_eh1;
  2835. cul_e_stamp.tm_min = 0 + test_min;
  2836. cul_e_stamp.tm_sec = 0;
  2837. break;
  2838. case 2:
  2839. cul_s_stamp.tm_hour = device_status.collecting_sh2;
  2840. cul_s_stamp.tm_min = 0 + test_min;
  2841. cul_s_stamp.tm_sec = 0;
  2842. cul_e_stamp.tm_hour = device_status.collecting_eh2;
  2843. cul_e_stamp.tm_min = 0 + test_min;
  2844. cul_e_stamp.tm_sec = 0;
  2845. if (cul_s_stamp.tm_hour == cul_e_stamp.tm_hour) // 检查第二时间段是否有效
  2846. {
  2847. return -1;
  2848. }
  2849. break;
  2850. default:
  2851. return -1;
  2852. break;
  2853. }
  2854. dividend = (cul_e_stamp.tm_hour * 3600 + cul_e_stamp.tm_min * 60 + cul_e_stamp.tm_sec) - (cul_s_stamp.tm_hour * 3600 + cul_s_stamp.tm_min * 60 + cul_s_stamp.tm_sec);
  2855. divisor = (cul_e_stamp.tm_hour * 3600 + cul_e_stamp.tm_min * 60 + cul_e_stamp.tm_sec) - (now_stamp.tm_hour * 3600 + now_stamp.tm_min * 60 + now_stamp.tm_sec);
  2856. divisor *= 100;
  2857. quotient = divisor / dividend;
  2858. result = abs(100 - quotient);
  2859. return result;
  2860. }
  2861. /*********************↓ 胶带结构的孢子仪主工作流程 ↓************************/
  2862. /**
  2863. * @brief 检查工作事件
  2864. *
  2865. * @return Temporal_list
  2866. */
  2867. Temporal_list Check_WorkDay(void)
  2868. {
  2869. work_day_def work_day = {0};
  2870. if (ef_get_env_blob("work_day", &work_day, sizeof(work_day), NULL) == 0)
  2871. {
  2872. return res_Verifi;
  2873. }
  2874. else if (work_day.year == 0 && work_day.month == 0 && work_day.day == 0)
  2875. {
  2876. // rt_kprintf("work_day %d.%d.%d \n", work_day.year, work_day.month, work_day.day);
  2877. return tl_Error;
  2878. }
  2879. static struct tm now_stamp = {0}; // 现在
  2880. Get_now_time(&now_stamp);
  2881. now_stamp.tm_mon += 1;
  2882. // 时间已过期
  2883. if (work_day.year > now_stamp.tm_year)
  2884. {
  2885. return tl_Back;
  2886. }
  2887. else if (work_day.month > now_stamp.tm_mon)
  2888. {
  2889. return tl_Back;
  2890. }
  2891. else if (work_day.day > now_stamp.tm_mday)
  2892. {
  2893. return tl_Back;
  2894. }
  2895. if (work_day.year == now_stamp.tm_year &&
  2896. work_day.month == now_stamp.tm_mon &&
  2897. work_day.day == now_stamp.tm_mday)
  2898. {
  2899. return tl_Center;
  2900. }
  2901. if (work_day.year < now_stamp.tm_year)
  2902. {
  2903. return tl_Front;
  2904. }
  2905. else if (work_day.month < now_stamp.tm_mon)
  2906. {
  2907. return tl_Front;
  2908. }
  2909. else if (work_day.day < now_stamp.tm_mday)
  2910. {
  2911. return tl_Front;
  2912. }
  2913. return res_Verifi;
  2914. }
  2915. void Recode_WorkDay(void)
  2916. {
  2917. work_day_def work_day = {0};
  2918. static struct tm now_stamp = {0}; // 现在
  2919. Get_now_time(&now_stamp);
  2920. work_day.year = now_stamp.tm_year;
  2921. work_day.month = now_stamp.tm_mon + 1;
  2922. work_day.day = now_stamp.tm_mday;
  2923. ef_set_env_blob("work_day", &work_day, sizeof(work_day));
  2924. LOG_E("Recode_WorkDay > %d.%02d.%02d\n", work_day.year % 100, work_day.month, work_day.day);
  2925. char buf[100] = {0};
  2926. sprintf(buf, "今日(%d)工作结束", now_stamp.tm_mday);
  2927. Recode_e_code(e_ReWorkDay, now_stamp.tm_mday, buf); // 工作结束
  2928. }
  2929. MSH_CMD_EXPORT(Recode_WorkDay, Recode_WorkDay);
  2930. void Clear_WorkDay(void)
  2931. {
  2932. work_day_def work_day = {0};
  2933. rt_kprintf("WorkDay SET %d \n", ef_set_env_blob("work_day", &work_day, sizeof(work_day)));
  2934. }
  2935. uint8_t ws_stop_waitA = 0;
  2936. uint8_t ws_stop_waitB = 0;
  2937. /*********************↑ 胶带结构的孢子仪主工作流程 ↑************************/
  2938. /**
  2939. * @brief 孢子仪工作线程V2
  2940. *
  2941. */
  2942. void WorkTask_BZYV2(void)
  2943. {
  2944. // 打开采集风机
  2945. ctr_Collect_Fan(ON , syscfg.fan_speed);
  2946. // 打开胶带步进电机
  2947. // 等待采集结束
  2948. // 拍照
  2949. // 打开相机供电
  2950. // 打开补光灯
  2951. // 滑台归位
  2952. // 滑台启动
  2953. }
  2954. // 工作任务流程s
  2955. void WorkTask(void)
  2956. {
  2957. static int clt_time = 0; // 收集时间差秒数
  2958. static int heat_time = 0;
  2959. uint16_t log_en_inr = LOG_INR; // 打印log的速度 数值越小 打印越频繁
  2960. static int16_t log_en_cnt = -1;
  2961. if (chk_log_time)
  2962. {
  2963. LOG_I("工作状态:%d", device_status.work_status);
  2964. LOG_I("开关机状态:%d", device_status.ds);
  2965. }
  2966. 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)
  2967. {
  2968. device_status.work_done = 0;
  2969. if (clt_time == 0)
  2970. {
  2971. #if 0
  2972. if(device_status.version_type==41&&atoi(device_status.power)<350)
  2973. {
  2974. LOG_W("相机电流不足!");
  2975. char *ctr_hk_p_warn = makeJson_Warn("电流不足", "相机");
  2976. DTU_Write("%s",ctr_hk_p_warn);
  2977. rt_free(ctr_hk_p_warn);
  2978. }
  2979. #endif
  2980. #if LAMP_EN == 0
  2981. ctr_Fan_PUT(ON);
  2982. #endif
  2983. DTU_Write("workstart.mp3");
  2984. }
  2985. // 1.打开灯管
  2986. #if LAMP_EN == 1
  2987. if (device_status.lamp_status == 1)
  2988. {
  2989. ctr_lamp(ON, 1);
  2990. ctr_lamp(OFF, 2);
  2991. ctr_lamp(OFF, 3);
  2992. }
  2993. else if (device_status.lamp1_status == 1)
  2994. {
  2995. ctr_lamp(OFF, 1);
  2996. ctr_lamp(ON, 2);
  2997. ctr_lamp(OFF, 3);
  2998. }
  2999. else if (device_status.lamp2_status == 1)
  3000. {
  3001. ctr_lamp(OFF, 1);
  3002. ctr_lamp(OFF, 2);
  3003. ctr_lamp(ON, 3);
  3004. }
  3005. #else
  3006. ctr_lamp(ON, 1);
  3007. #endif
  3008. LOG_I("打开灯管!!!");
  3009. #if 0
  3010. rt_thread_delay(2000);
  3011. if(atoi(device_status.power)<450)
  3012. {
  3013. LOG_I("Current is:%d",atoi(device_status.power));
  3014. if(atoi(device_status.power)>50&&atoi(device_status.power)<5000) //过滤电流检测芯片损坏导致电流异常
  3015. {
  3016. char* lamp_err_warn = makeJson_Warn("电流不足", "灯管");
  3017. DTU_Write("%s",lamp_err_warn);
  3018. rt_free(lamp_err_warn);
  3019. }
  3020. }
  3021. #endif
  3022. if (device_status.version_type != 5)
  3023. {
  3024. if (clt_time == 0)
  3025. {
  3026. rt_thread_delay(5000); // 待灯管启动再往下执行,否则会触发电机过流
  3027. // 2.打开上仓门
  3028. rt_kprintf("打开上仓门!!!\n");
  3029. device_status.ctr_up_motor = 1;
  3030. }
  3031. // 3.收集
  3032. clt_time = device_status.clt_time * 60;
  3033. while (clt_time != 1)
  3034. {
  3035. #if LAMP_EN == 0
  3036. ctr_Fan_IN(ON);
  3037. #endif
  3038. clt_time--;
  3039. rt_thread_delay(1000);
  3040. #if LAMP_EN == 0
  3041. if (clt_time % 30 == 0)
  3042. ctr_Fan_PUT(OFF);
  3043. #endif
  3044. rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n", device_status.clt_time, clt_time);
  3045. HMI_WRITE("workSta.g0.txt=\"正在收集.....剩余时间:%d分-%d秒\"", device_status.clt_time, clt_time);
  3046. // 如果触发异常
  3047. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1)
  3048. {
  3049. ctr_lamp(OFF, 1);
  3050. #if LAMP_EN == 1
  3051. ctr_lamp(OFF, 2);
  3052. ctr_lamp(OFF, 3);
  3053. #endif
  3054. #if LAMP_EN == 0
  3055. ctr_Fan_IN(OFF);
  3056. #endif
  3057. rt_kprintf("异常退出!!!\n");
  3058. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3059. break;
  3060. }
  3061. // if (device_status.timeSwitch == 0 && device_status.sun_status == 1)
  3062. // {
  3063. // ctr_lamp(OFF, 1);
  3064. //#if LAMP_EN == 1
  3065. // ctr_lamp(OFF, 2);
  3066. // ctr_lamp(OFF, 3);
  3067. //#endif
  3068. //#if LAMP_EN == 0
  3069. // ctr_Fan_IN(OFF);
  3070. //#endif
  3071. // rt_kprintf("异常退出!!!\n");
  3072. // HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3073. // break;
  3074. // }
  3075. }
  3076. clt_time = 1;
  3077. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1)
  3078. {
  3079. ctr_lamp(OFF, 1);
  3080. #if LAMP_EN == 1
  3081. ctr_lamp(OFF, 2);
  3082. ctr_lamp(OFF, 3);
  3083. #endif
  3084. #if LAMP_EN == 0
  3085. ctr_Fan_IN(OFF);
  3086. #endif
  3087. rt_kprintf("异常退出!!!\n");
  3088. HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3089. break;
  3090. }
  3091. // if (device_status.timeSwitch == 0 && device_status.sun_status == 1)
  3092. // {
  3093. // ctr_lamp(OFF, 1);
  3094. //#if LAMP_EN == 1
  3095. // ctr_lamp(OFF, 2);
  3096. // ctr_lamp(OFF, 3);
  3097. //#endif
  3098. //#if LAMP_EN == 0
  3099. // ctr_Fan_IN(OFF);
  3100. //#endif
  3101. // rt_kprintf("异常退出!!!\n");
  3102. // HMI_WRITE("workSta.g0.txt=\"异常退出\"");
  3103. // break;
  3104. // }
  3105. #if LAMP_EN == 0
  3106. ctr_Fan_IN(OFF);
  3107. #endif
  3108. // 4.关闭上仓门
  3109. device_status.ctr_up_motor = 2;
  3110. rt_kprintf("关闭上仓门!!!\n");
  3111. HMI_WRITE("workSta.g0.txt=\"关闭上仓门!\"");
  3112. while (device_status.ctr_up_motor != 0)
  3113. {
  3114. rt_thread_delay(100);
  3115. LOG_I("等待上仓门关闭!!!");
  3116. }
  3117. // 5.加热
  3118. if (device_status.heat_sw == 0)
  3119. {
  3120. calorstat(device_status.heattime * 60);
  3121. }
  3122. else
  3123. {
  3124. heat_time = device_status.heattime * 60;
  3125. while (heat_time != 0)
  3126. {
  3127. heat_time--;
  3128. LOG_I("正在加热状态!!!");
  3129. rt_thread_delay(1000);
  3130. }
  3131. }
  3132. // 6.打开下仓门
  3133. if (device_status.rain_status == 0)
  3134. {
  3135. device_status.ctr_down_motor = 1;
  3136. }
  3137. else
  3138. {
  3139. device_status.ctr_down_motor = 2;
  3140. device_status.ctr_up_motor = 2;
  3141. }
  3142. // 待下仓门完全打开
  3143. while (device_status.ctr_down_motor != 0)
  3144. {
  3145. rt_thread_delay(100);
  3146. rt_kprintf("等待下仓门打开!!!\n");
  3147. HMI_WRITE("workSta.g0.txt=\"等待下仓门打开!\"");
  3148. }
  3149. rt_thread_delay(3000);
  3150. // 关闭下仓门
  3151. device_status.ctr_down_motor = 2;
  3152. while (device_status.ctr_down_motor != 0)
  3153. {
  3154. rt_thread_delay(100);
  3155. rt_kprintf("等待下仓门关闭!!!\n");
  3156. HMI_WRITE("workSta.g0.txt=\"等待下仓门关闭!\"");
  3157. }
  3158. // 2022-04-12add:
  3159. LOG_I("打开上仓门!!!");
  3160. HMI_WRITE("workSta.g0.txt=\"打开上仓门!\"");
  3161. device_status.ctr_up_motor = 1;
  3162. }
  3163. else
  3164. {
  3165. // 打开高压
  3166. ctr_Heat(ON);
  3167. // 打开补光
  3168. ctr_led(ON);
  3169. LOG_I("打开补光!!");
  3170. rt_pin_mode(CLEAR_L_LIMIT, PIN_MODE_INPUT);
  3171. // 2.翻转清虫
  3172. while (rt_pin_read(CLEAR_L_LIMIT) == 1)
  3173. rt_thread_delay(20);
  3174. rt_thread_delay(500);
  3175. Hk_Take_io(); // 触发海康相机拍照
  3176. rt_thread_delay(500);
  3177. ctr_turnv5(); // 摆动清虫
  3178. LOG_I("摆动清虫!");
  3179. }
  3180. }
  3181. if (device_status.bat_status == 1 || device_status.temp_status == 1 || device_status.rain_status == 1 || device_status.ds == 0)
  3182. {
  3183. if (chk_log_time)
  3184. {
  3185. rt_kprintf("-------------------not work--------------------\n");
  3186. }
  3187. device_status.work_status = 0;
  3188. }
  3189. // 工作结束
  3190. if (device_status.work_done == 0 && device_status.work_status == 0)
  3191. {
  3192. DTU_Write("workdone");
  3193. HMI_WRITE("workSta.g0.txt=\"工作任务已完成!\"");
  3194. ResetInit();
  3195. }
  3196. if (chk_log_time)
  3197. {
  3198. rt_kprintf(" ***not work-device status:%d\n", device_status.ds);
  3199. }
  3200. }
  3201. /* 入口函数-工作状态检测 */
  3202. /**
  3203. * @brief 工作状态检测线程服务函数
  3204. *
  3205. * @param parameter -
  3206. */
  3207. static void Work_status_thread_entry(void *parameter)
  3208. {
  3209. // static uint16_t sun_status_num = 120;
  3210. // // 如果是光控定时,进行光控监测的消抖
  3211. // if (device_status.timeSwitch == 0 && device_status.sun_status == 0)
  3212. // {
  3213. // rt_kprintf("光控定时!!!\n");
  3214. // while (sun_status_num != 0)
  3215. // {
  3216. // sun_status_num--;
  3217. // rt_thread_delay(1000);
  3218. // rt_kprintf("开机光控检测:%d\n", sun_status_num);
  3219. // }
  3220. // device_status.light_over_tim = device_status.sunBar * 3600;
  3221. // device_status.test_flag = 0;
  3222. // }
  3223. // while (1)
  3224. // {
  3225. // WorkCheck();
  3226. // }
  3227. }
  3228. /*入口函数-工作流程*/
  3229. /**
  3230. * @brief 工作流程线程服务函数
  3231. *
  3232. * @param parameter -
  3233. */
  3234. static void Work_Loop_thread_entry(void *parameter)
  3235. {
  3236. // uint16_t log_en_inr = LOG_INR; // 打印log的速度 数值越小 打印越频繁
  3237. // static int16_t log_en_cnt = -1;
  3238. rt_thread_delay(1000);
  3239. ResetInit();
  3240. // rt_kprintf(" dtype = %d vtype = %d \n\n", device_status.dtype, device_status.version_type);
  3241. // rt_kprintf("Version : %s %s\n", VERSION_BZY , device_status.driver_compile);
  3242. UpLoad_Data();
  3243. while (1)
  3244. {
  3245. rt_thread_mdelay(1000);
  3246. WorkTask_XCT();
  3247. }
  3248. }
  3249. /*入口函数清扫*/
  3250. /**
  3251. * @brief 控制清扫线程服务函数
  3252. *
  3253. * @param parameter -
  3254. */
  3255. static void Control_Clear_thread_entry(void *parameter)
  3256. {
  3257. while (1)
  3258. {
  3259. #if ROBOT_EN == 1
  3260. ctr_Motor_Robot(ON);
  3261. rt_thread_delay(100);
  3262. #else
  3263. #if Color_EN == 1
  3264. /*复位*/
  3265. if (Read_Color_Key(KEY_RESET) == 1)
  3266. {
  3267. ctr_Color_board(RESET, 1200);
  3268. rt_thread_delay(5000);
  3269. }
  3270. #else
  3271. if (device_status.clear_status == 1)
  3272. {
  3273. if (ctr_clear(1200) == 0)
  3274. ctr_TG_Clear(400);
  3275. }
  3276. rt_thread_delay(100);
  3277. #endif
  3278. #endif
  3279. }
  3280. }
  3281. /*入口函数加热*/
  3282. /**
  3283. * @brief 控制加热线程服务函数
  3284. *
  3285. * @param parameter -
  3286. */
  3287. static void Control_Heat_thread_entry(void *parameter)
  3288. {
  3289. uint16_t var_clear = 0;
  3290. #if ROBOT_EN == 1
  3291. static uint8_t key_on = 0;
  3292. #else
  3293. #if Color_EN == 0
  3294. while (device_status.work_status == 0)
  3295. {
  3296. rt_thread_delay(1000);
  3297. }
  3298. #endif
  3299. #endif
  3300. while (1)
  3301. {
  3302. #if ROBOT_EN == 1
  3303. // 磁悬浮供电
  3304. // 监测磁悬浮限位位置`
  3305. LOG_D("Read 磁悬浮:%d", device_status.heattem);
  3306. // 等待10S
  3307. if (device_status.heattem < 30 && Read_Real_Voltage() / 100 > 12) // 磁悬浮处于关闭状态
  3308. {
  3309. key_on = 0;
  3310. // 发送触摸按钮信号
  3311. rt_pin_mode(EMS_Dir, PIN_MODE_OUTPUT);
  3312. rt_pin_write(EMS_Dir, 0);
  3313. rt_thread_delay(50);
  3314. rt_pin_write(EMS_Dir, 1);
  3315. }
  3316. else if (device_status.heattem < 30 && Read_Real_Voltage() / 100 < 12)
  3317. {
  3318. ctr_router(OFF);
  3319. }
  3320. if (device_status.heattem > 40)
  3321. {
  3322. key_on = 1;
  3323. rt_thread_delay(1000);
  3324. LOG_D("Read 磁悬浮已打开:%d", device_status.heattem);
  3325. }
  3326. // 磁悬浮供电
  3327. // 监测磁悬浮限位位置
  3328. // 根据位置确定是否 按下按钮+
  3329. if (Read_Real_Voltage() / 100 < 12 && key_on == 1)
  3330. {
  3331. key_on = 0;
  3332. rt_pin_mode(EMS_Dir, PIN_MODE_OUTPUT);
  3333. rt_pin_write(EMS_Dir, 0);
  3334. rt_thread_delay(50);
  3335. rt_pin_write(EMS_Dir, 1);
  3336. }
  3337. #else
  3338. #if Color_EN == 1
  3339. /*黄板归位*/
  3340. if (Read_Color_Key(KEY_BACK) == 1)
  3341. {
  3342. LOG_W("黄板归位!");
  3343. ctr_Color_board(CLOSE, 300);
  3344. rt_thread_delay(5000);
  3345. }
  3346. #else
  3347. if (device_status.work_status == 1 && device_status.heat_sw == 1)
  3348. {
  3349. ctr_lamp(ON, 1);
  3350. rt_thread_delay(10000);
  3351. calorstat(device_status.heattime * 60);
  3352. }
  3353. else
  3354. {
  3355. LOG_I("Not calorstat!");
  3356. }
  3357. if (device_status.work_status == 1 && device_status.version_type == 5)
  3358. {
  3359. var_clear++;
  3360. if (var_clear == 300)
  3361. {
  3362. var_clear = 0;
  3363. ctr_cangmen_down(ON);
  3364. rt_thread_mdelay(5000);
  3365. ctr_cangmen_down(OFF);
  3366. rt_thread_mdelay(500);
  3367. ctr_cangmen_down(ON);
  3368. rt_thread_mdelay(5000);
  3369. ctr_cangmen_down(OFF);
  3370. }
  3371. }
  3372. // //吸虫塔补光灯测试
  3373. // while(device_status.vol!=200)
  3374. // {
  3375. // if(var_led==0) ctr_led(ON);
  3376. // var_led++;
  3377. // if(var_led>10)
  3378. // {
  3379. // ctr_led(OFF);
  3380. // var_ledoff++;
  3381. // if(var_ledoff>10)
  3382. // {
  3383. // var_led = 0;
  3384. // var_ledoff = 0;
  3385. // ctr_led(ON);
  3386. // }
  3387. // }
  3388. // rt_thread_delay(1000);
  3389. // }
  3390. rt_thread_delay(1000);
  3391. #endif
  3392. #endif
  3393. }
  3394. }
  3395. /*清扫线程*/
  3396. /**
  3397. * @brief 创建用于控制清扫操作的线程
  3398. *
  3399. * @return int -
  3400. */
  3401. int Control_clear(void)
  3402. {
  3403. /* 创建线程*/
  3404. tid_ctr = rt_thread_create("Control_Clear_thread_entry",
  3405. Control_Clear_thread_entry, RT_NULL,
  3406. 2048, // 2048
  3407. 24, // 23
  3408. WORK_CHECK_THREAD_TIMESLICE); // 5
  3409. if (tid_ctr != RT_NULL)
  3410. {
  3411. rt_thread_startup(tid_ctr);
  3412. }
  3413. return 0;
  3414. }
  3415. /*恒温加热线程*/
  3416. /**
  3417. * @brief 创建用于恒温加热操作的线程
  3418. *
  3419. * @return int -
  3420. */
  3421. int Control_heat(void)
  3422. {
  3423. /* 创建线程*/
  3424. tid_heat = rt_thread_create("Control_Heat_thread_entry",
  3425. Control_Heat_thread_entry, RT_NULL,
  3426. 1024, // 2048
  3427. 25, // 23
  3428. WORK_CHECK_THREAD_TIMESLICE); // 5
  3429. if (tid_heat != RT_NULL)
  3430. {
  3431. rt_thread_startup(tid_heat);
  3432. }
  3433. return 0;
  3434. }
  3435. /* 创建线程 -工作状态检测*/
  3436. int Workstatus_check(void)
  3437. {
  3438. // /* 创建线程*/
  3439. // tid_wkstc = rt_thread_create("work_status_check_thread",
  3440. // Work_status_thread_entry, RT_NULL,
  3441. // WORK_CHECK_THREAD_STACK_SIZE, // 1024
  3442. // WORK_CHECK_THREAD_PRIORITY, // 23
  3443. // WORK_CHECK_THREAD_TIMESLICE); // 5
  3444. // if (tid_wkstc != RT_NULL)
  3445. // {
  3446. // rt_thread_startup(tid_wkstc);
  3447. // }
  3448. return 0;
  3449. }
  3450. /*工作流程线程*/
  3451. int Work_Loop_Task(void)
  3452. {
  3453. /* 创建线程*/
  3454. tid_workloop = rt_thread_create("work_loop_thread",
  3455. Work_Loop_thread_entry, RT_NULL,
  3456. WORK_LOOP_THREAD_STACK_SIZE, // 2048
  3457. WORK_LOOP_THREAD_PRIORITY, // 12
  3458. WORK_LOOP_THREAD_TIMESLICE); // 5
  3459. #if Color_EN == 0
  3460. if (tid_workloop != RT_NULL)
  3461. {
  3462. rt_thread_startup(tid_workloop);
  3463. }
  3464. #endif
  3465. return 0;
  3466. }
  3467. MSH_CMD_EXPORT(HardWare_Check, HardWare_Check<ON | OFF>);
  3468. MSH_CMD_EXPORT(ctr_lamp, ctr_lamp<ON | OFF>);
  3469. MSH_CMD_EXPORT(ctr_clear, ctr_clear<ON | OFF>);
  3470. MSH_CMD_EXPORT(ctr_led, sample : ctr_led<ON | OFF>);