control.c 108 KB

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