control.c 115 KB

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