control.lua 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. --- 模块功能 孢子仪逻辑控制
  2. -- @module
  3. -- @author lcg
  4. -- @license MIT
  5. -- @copyright yf
  6. -- @release 2019.08.01
  7. require 'ril'
  8. require "misc"
  9. require "hmi"
  10. require "audio"
  11. module(..., package.seeall)
  12. --拍照完成标志位
  13. photodone = 0
  14. vbat = ""
  15. -- 工作标志位,采集标志位,定时时长,
  16. local worksta, collsta, worktim
  17. -- 限位开关、雨控、光控传感器变量
  18. local limit = {upl, upr, dwl, dwr, turn, rain, light}
  19. local fid = {
  20. read = 0x55, -- 功能号:读
  21. write = 0xAA, -- 功能号:写
  22. ---------------------------
  23. turn = 0x01, -- 转动转盘
  24. get_turn = 0x07, -- 获取的限位状态: turn和turn_init
  25. collect_fan = 0x02, -- 抽风机开关
  26. cool = 0x03, -- 散热开关
  27. heat = 0x04, -- 加热开关
  28. cold = 0x05, -- 制冷开关
  29. fsl = 0x06, -- 凡士林推杆电机开关
  30. wdgtim = 0x0a, -- 看门狗喂狗时间
  31. stm8_v = 0x0b, -- STM8版本
  32. temp = 0x0c, -- 换算温度值
  33. vbat = 0x0d, -- 电池电压值
  34. }
  35. Sanre_Fan = pins.setup(pio.P0_29,0)
  36. ctrl_12v = pins.setup(pio.P0_29,0)
  37. local function i2c_open(id)
  38. if i2c.setup(id, i2c.SLOW) ~= i2c.SLOW then
  39. log.error('I2C.init is: ', 'fail')
  40. i2c.close(id)
  41. return
  42. end
  43. return i2c.SLOW
  44. end
  45. --- STM8S003的动作方法
  46. -- @param a,number,fid表的字段
  47. -- @param v,number,0 或 1,0是关闭,1是打开
  48. function action(a, v)
  49. i2c_open(2)
  50. i2c.send(2, 0x51, pack.pack(">bbb", fid.write, a, v))
  51. i2c.close(2)
  52. end
  53. function reboot_stm8()
  54. i2c_open(2)
  55. i2c.send(2, 0x51, pack.pack(">bb", fid.write, 0x88))
  56. i2c.close(2)
  57. end
  58. -- 获取turn限位的io状态
  59. function get_limit_turn()
  60. local buffer,idx = 0,1
  61. local turn,turn_init = 2,2
  62. i2c_open(2)
  63. i2c.send(2, 0x51, pack.pack(">bb", fid.read, fid.get_turn))
  64. buffer = i2c.recv(2, 0x51, 2)
  65. i2c.close(2)
  66. idx, turn = pack.unpack(buffer, 'b',idx)
  67. idx, turn_init = pack.unpack(buffer, 'b',idx)
  68. log.warn("++++++++++++++++++turn,turn_init:,",turn,turn_init)
  69. return turn,turn_init
  70. end
  71. turn_timeout = 0
  72. turn_reset_timeout = 0
  73. -- 转盘转动
  74. function turn(v,sec)
  75. turn_timeout = 0
  76. audio.play(0,"FILE","/ldata/turn.mp3", hmi.hmiconf.vol)
  77. v = v or 1
  78. sec = sec or 21-- 超时时长设为10s时,刚好转90度;避免刚好错过上一个限位,所以设置为两个90度的时间;
  79. log.warn("------ turn: ",v,sec)
  80. -- action(fid.turn, v)
  81. i2c_open(2)
  82. i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.turn, v, sec))
  83. i2c.close(2)
  84. -- sys.wait(5000)
  85. -- log.warn("++++++++++++++++++get_limit_turn():,",get_limit_turn())
  86. local turn,turn_init,cnt = 2,2,0
  87. while turn ~= 0 do
  88. turn,turn_init = get_limit_turn()
  89. sys.wait(1000)
  90. cnt = cnt + 1
  91. if cnt > sec then
  92. log.warn("----- turn timeout: ",sec)
  93. turn_timeout = 1
  94. break
  95. end
  96. end
  97. hmi.write("cangWei.rotateFlag.val=0")
  98. end
  99. -- -- 转盘复位
  100. -- function turn_init(v,sec)
  101. -- audio.play('/ldata/turn.mp3')
  102. -- v = v or 7
  103. -- sec = sec or 30-- 超时时长设为10s时,刚好转90度;避免刚好错过上一个限位,所以设置为两个90度的时间;
  104. -- log.warn("------ turn_init: ",v,sec)
  105. -- -- action(fid.turn, v)
  106. -- i2c_open(2)
  107. -- i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.turn, v, sec))
  108. -- i2c.close(2)
  109. -- -- sys.wait(5000)
  110. -- local turn,turn_init,cnt = 2,2,0
  111. -- while turn_init ~= 0 do
  112. -- turn,turn_init = get_limit_turn()
  113. -- sys.wait(1000)
  114. -- cnt = cnt + 1
  115. -- if cnt > sec then
  116. -- log.warn("----- turn_init timeout: ",sec)
  117. -- break
  118. -- end
  119. -- end
  120. -- hmi.write("cangWei.rotateFlag.val=0")
  121. -- end
  122. -- 转盘复位
  123. function turn_init(v,sec)
  124. turn_reset_timeout = 0
  125. v = v or 7
  126. sec = sec or 30-- 超时时长设为10s时,刚好转90度;避免刚好错过上一个限位,所以设置为两个90度的时间;
  127. log.warn("------ turn_init: ",v,sec)
  128. local turn,turn_init,cnt = 2,2,0
  129. turn,turn_init = get_limit_turn()
  130. if turn_init ~= 0 then
  131. audio.play(0,"FILE","/ldata/turn.mp3", hmi.hmiconf.vol)
  132. i2c_open(2)
  133. i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.turn, v, sec))
  134. i2c.close(2)
  135. while turn_init ~= 0 do
  136. turn,turn_init = get_limit_turn()
  137. sys.wait(1000)
  138. cnt = cnt + 1
  139. if cnt > sec then
  140. log.warn("----- turn_init timeout: ",sec)
  141. turn_reset_timeout = 1
  142. break
  143. end
  144. end
  145. hmi.write("cangWei.rotateFlag.val=0")
  146. else
  147. log.warn("----- current location is reset")
  148. hmi.write("cangWei.rotateFlag.val=0")
  149. return
  150. end
  151. hmi.write("cangWei.rotateFlag.val=0")
  152. end
  153. -- 采集风机
  154. function collect_fan(v)
  155. log.warn("------ collect_fan: ",v,type(v))
  156. hmi.hmista.collect_fan = v
  157. action(fid.collect_fan, v)
  158. end
  159. -- 散热风机
  160. function cool(v)
  161. if VERSION:find('V21') then
  162. Sanre_Fan(v)
  163. end
  164. log.warn("------ cool: ",v)
  165. hmi.hmista.cool_fan = v
  166. action(fid.cool, v)
  167. end
  168. -- 滴液
  169. function drop(t)
  170. audio.play(0,"FILE","/ldata/drop.mp3", hmi.hmiconf.vol)
  171. print("------ drop time: ",t)
  172. -- for i=1,t,1 do
  173. -- print("----------droping...",i)
  174. -- pins.setup(pio.P1_1,1)
  175. -- sys.wait(5000)
  176. -- pins.setup(pio.P1_1,0)
  177. -- sys.wait(5000)
  178. -- end
  179. t = t or 1
  180. log.warn("------ droping...",t)
  181. pins.setup(pio.P1_1,1)
  182. sys.wait(t*100)
  183. pins.setup(pio.P1_1,0)
  184. hmi.write("sysTest.switchDrop.val=0")
  185. hmi.write('sysTest.auto_flag.txt="drop0"')
  186. end
  187. -- 加热
  188. function heat(v)
  189. log.warn("------ heat: ",v)
  190. hmi.hmista.heat_sta = v
  191. action(fid.heat, v)
  192. end
  193. -- 制冷
  194. function cold(v)
  195. log.warn("------ cold: ",v)
  196. hmi.hmista.cold_sta = v
  197. action(fid.cold, v)
  198. end
  199. -- 涂抹凡士林
  200. function add_fsl(v,sec)
  201. -- log.warn("------ add_fsl :",v)
  202. -- action(fid.fsl, v)
  203. v = v or 1
  204. sec = sec or 5
  205. log.warn("------ add_fsl: ",v,sec)
  206. i2c_open(2)
  207. i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.fsl, v, sec))
  208. i2c.close(2)
  209. end
  210. sys.taskInit(function()
  211. -- local Rup, adcValue, voltValue, heatTemp, idx = 5100
  212. local iic_buffer
  213. -- hmi.hmista.tempStatus = 0
  214. local b20temp = 0
  215. adc.open(0)
  216. while true do
  217. local status, error = pcall(function ()
  218. -- adcValue, voltValue = adc.read(0)
  219. -- log.info("adc read:",adcValue,voltValue)
  220. -- if voltValue == 0xFFFF then
  221. -- log.warn("adc read error")
  222. -- adcValue = 5400
  223. -- end
  224. -- Rt = voltValue * 1000 / ((9000 - voltValue) * 1000 / Rup)
  225. -- log.info("control.heatTemp ADC value:", adcValue, voltValue, Rt)
  226. i2c_open(2)
  227. -- i2c.send(2, 0x51, pack.pack(">bbH", fid.read, fid.temp, Rt))
  228. i2c.send(2, 0x51, pack.pack(">bb", fid.read, fid.temp))
  229. -- heatTemp = i2c.recv(2, 0x51, 7)
  230. iic_buffer = i2c.recv(2, 0x51, 11)
  231. i2c.close(2)
  232. -- IIC读取STM8软件版本:
  233. idx, stm8_v = pack.unpack(iic_buffer, '>b')
  234. log.info("iic read stm8_v:",stm8_v)
  235. hmi.hmista.stm8_v = string.format('%d', stm8_v)
  236. -- IIC读取电池电压:
  237. idx, vbat = pack.unpack(iic_buffer, '>h',idx)
  238. local batsta = 0
  239. if(vbat < 21000) then
  240. batsta = 1;
  241. elseif(vbat > 25200) then
  242. batsta = 0;
  243. end
  244. -- hmi.hmista.vbat = string.format('%002d.%002d', vbat / 1000, vbat % 1000)
  245. hmi.hmista.vbat = string.format('"%d.%d"', vbat / 1000, vbat % 1000)
  246. hmi.hmista.bat_sta = batsta
  247. log.info("iic read vbat and bat_sta",hmi.hmista.vbat,hmi.hmista.bat_sta)
  248. -- hmi.write("workSta.vbat.txt=" .. string.format('"%002d.%002d"', vbat / 1000, vbat % 1000))
  249. -- hmi.write("workSta.vbat.txt=" .. hmi.hmista.vbat)
  250. -- hmi.write("workSta.bat_sta.val=" .. hmi.hmista.bat_sta)
  251. -- IIC读取B20温度--箱内温度:
  252. idx, b20temp = pack.unpack(iic_buffer, '>h',idx)
  253. -- log.info("iic read b20temp:",b20temp)
  254. log.info("iic read box_tmp:",b20temp)
  255. -- hmi.hmista.box_tem = string.format('"%002d.%002d"', b20temp/100, b20temp%100)
  256. hmi.hmista.box_tem = string.format('"%2d"', b20temp)
  257. if b20temp >= tonumber(hmi.hmista.envTem)/10 + 5 then
  258. cool(1)
  259. else
  260. cool(0)
  261. end
  262. -- IIC读取温度保温仓探头温度:
  263. idx, hmi.hmista.pre_temp = pack.unpack(iic_buffer, '>h',idx)
  264. log.info("iic read keep_temp:", hmi.hmista.pre_temp)
  265. -- sys.wait(1000)
  266. -- AM2320读取环境温湿度:
  267. hmi.hmista.envTem, hmi.hmista.envHum = AM2320.sht30read(2)
  268. if not hmi.hmista.envTem then
  269. hmi.hmista.envTem, hmi.hmista.envHum = AM2320.read(2, 0x5c)
  270. if not hmi.hmista.envTem then
  271. hmi.hmista.envTem, hmi.hmista.envHum = 250, 300
  272. end
  273. end
  274. log.info("temperature and humidity:", hmi.hmista.envTem, hmi.hmista.envHum)
  275. end)
  276. if status ~= true and error ~= nil then
  277. log.warn("------ iic read stm8, pcall:",status)
  278. log.warn("------ iic read stm8, error:",error)
  279. else
  280. log.info("------- iic read stm8,pcall:",status)
  281. log.info("------- iic read stm8,error:",error)
  282. end
  283. log.info("-------",b20temp,tonumber(hmi.hmista.envTem)/10)
  284. sys.wait(5000)
  285. end
  286. end)
  287. function rtu_12v_reset()
  288. log.error("------ rtu_12v power will reset ------")
  289. -- action(fid.rtu_power, 1)
  290. end
  291. -- 看门狗
  292. function wdg()
  293. action(fid.wdgtim, 240)
  294. log.warn("------ wdg is running!")
  295. end
  296. -- 工作任务初始化
  297. local function workInit()
  298. end
  299. -- 停止所有stm8的动作:
  300. local function resetinit()
  301. collect_fan(0)
  302. cold(0)
  303. heat(0)
  304. cool(0)
  305. turn_init()
  306. end
  307. -- 开机自检\一键自检任务:
  308. function self_check()
  309. -- 检查转盘:转离;
  310. -- 检查抽风:通电5s
  311. -- 检查散热风扇:通电5s
  312. -- 检查步进电机:上下转动各5s,视限位开关情况;
  313. -- 检查
  314. end
  315. function lua_string_split(str, split_char)
  316. local sub_str_tab = {}
  317. while (true) do
  318. local pos = string.find(str, split_char)
  319. if (not pos) then
  320. local size_t = table.getn(sub_str_tab)
  321. table.insert(sub_str_tab, size_t + 1, str)
  322. break
  323. end
  324. local sub_str = string.sub(str, 1, pos - 1)
  325. local size_t = table.getn(sub_str_tab)
  326. table.insert(sub_str_tab, size_t + 1, sub_str)
  327. local t = string.len(str)
  328. str = string.sub(str, pos + 1, t)
  329. end
  330. return sub_str_tab
  331. end
  332. -- local str = "1,2,3,4,5,6,7,8,9"
  333. -- local ta = lua_string_split(str, ",")
  334. -- local size = table.getn(ta)
  335. -- for i = 1, size, 1 do
  336. -- print(ta[i])
  337. -- end
  338. -- 传入起止时间,计算时间戳的差值:
  339. function count_second(t1,t2)
  340. local tc1 = misc.getClock()
  341. tc1.hour = t1
  342. tc1.min = 0
  343. tc1.sec = 0
  344. local tc2 = misc.getClock()
  345. tc2.hour = t2
  346. tc2.min = 0
  347. tc2.sec = 0
  348. print(os.difftime(os.time(tc1), os.time(tc2)))
  349. end
  350. -- 传入不定长的无序的时间表:table;返回有序时间表;
  351. function sort_time_table(t)
  352. key = {}
  353. -- 遍历,拼凑key table
  354. for i in pairs(t) do
  355. table.insert(key,t[i][1]) --提取test1中的键值插入到key_test表中
  356. end
  357. -- 查看原始table
  358. print("-------origen table--------")
  359. for k,v in pairs(t) do
  360. print("key:" .. "k " .. k .. " v: ".. v[1])
  361. end
  362. -- print("-------key table--------")
  363. -- 查看key table
  364. -- for k,v in pairs(key) do
  365. -- print("key:" .. "k " .. k .. " v: ".. v)
  366. -- end
  367. -- print("-------key_sort table--------")
  368. -- 定义一个新table
  369. key_sort = {}
  370. -- key_sort = key
  371. for k,v in pairs(key) do
  372. table.insert(key_sort,v)
  373. end
  374. -- for k,v in pairs(key_sort) do
  375. -- print("key:" .. "k " .. k .. " v: ".. v)
  376. -- end
  377. -- print("===========================================")
  378. -- print("-------paixu key table--------")
  379. table.sort(key_sort)
  380. -- key = nil
  381. -- print("-------key table--------")
  382. -- 查看key table
  383. -- for k,v in pairs(key) do
  384. -- print("key:" .. "k " .. k .. " v: ".. v)
  385. -- end
  386. -- print("-------key_sort table--------")
  387. -- for k,v in pairs(key_sort) do
  388. -- print("key:" .. "k " .. k .. " v: ".. v)
  389. -- end
  390. print("***********************************")
  391. key_idx = {}
  392. for k,v in pairs(key_sort) do
  393. -- print("key:" .. "k " .. k .. " v: ".. v)
  394. for j,w in pairs(key) do
  395. -- print("j w :",j,w)
  396. if v == w then
  397. table.insert(key_idx,j)
  398. end
  399. end
  400. end
  401. -- print("-------key_index--------")
  402. -- for k,v in pairs(key_idx) do
  403. -- print("key:" .. "k " .. k .. " v: ".. v)
  404. -- end
  405. -- print("-------result--------")
  406. result = {}
  407. for k,v in pairs(key_idx) do
  408. -- print("key:" .. "k " .. k .. " v: ".. v)
  409. table.insert(result,t[v])
  410. print("result:",t[v][1],t[v][2])
  411. end
  412. -- print("-------check --------")
  413. -- for k,v in pairs(result) do
  414. -- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
  415. -- end
  416. return result
  417. end
  418. -- 是否进入培养模式
  419. develop_flag = 0
  420. -- 培养载玻片孢子
  421. function develop()
  422. log.warn("------start to develop hours:",hmi.hmiconf.cul_time,type(hmi.hmiconf.cul_time))
  423. -- 总培养时间:hmi.hmiconf.cul_time
  424. -- 已经培养的时间:分钟
  425. hmi.hmista.staytime = 0
  426. local ii = 0
  427. while develop_flag == 1 do
  428. ii = ii + 1
  429. log.warn("developing..........")
  430. log.info("..........hmi.hmista.set_temp:",hmi.hmiconf.set_temp)
  431. log.info("..........hmi.hmista.pre_temp:",hmi.hmista.pre_temp)
  432. if hmi.hmista.pre_temp < (hmi.hmiconf.set_temp - 10) then
  433. -- if hmi.hmista.pre_temp ~= (hmi.hmiconf.set_temp) then
  434. if hmi.hmista.cold_sta == 0 then
  435. heat(1)
  436. end
  437. else
  438. heat(0)
  439. end
  440. log.info("..........hmi.hmiconf.cold_sw:",hmi.hmiconf.cold_sw)
  441. if hmi.hmiconf.cold_sw == 1 then
  442. -- if hmi.hmista.pre_temp ~= (hmi.hmiconf.set_temp) then
  443. if hmi.hmista.pre_temp > (hmi.hmiconf.set_temp + 10) then
  444. if hmi.hmista.heat_sta == 0 then
  445. cold(1)
  446. end
  447. else
  448. cold(0)
  449. end
  450. end
  451. if ii % 60 == 0 then hmi.hmista.staytime = hmi.hmista.staytime + 1 end
  452. -- 1分钟检测判断一次;
  453. sys.wait(60*1000)
  454. end
  455. heat(0)
  456. cold(0)
  457. heat(0)
  458. cold(0)
  459. log.warn("develop ok!!!..........")
  460. end
  461. -- 收集时间段的转换
  462. -- hmi.hmiconf.coll_time为字符串;coll_time = ["7-9","10-11"],
  463. function conversion_time1()
  464. log.info("-----origin set collect time is:",hmi.hmiconf.coll_time)
  465. -- [ "8 - 10", "1 6 -2 0"," 6- 7", "12 -15","21 -2 1"]
  466. -- 第一步:先过滤空格和双引号;此步骤十分关键!
  467. coll_time = string.gsub(hmi.hmiconf.coll_time, '[ "]', '')
  468. coll_time = string.gsub(coll_time, '-', '~')
  469. -- [8-10,16-20,6-7,12-15,21-21]
  470. log.info("-----real set collect time is:",coll_time)
  471. -- table: 0x821f8d30
  472. -- 第二步:截去两头的中括号:
  473. coll_time = string.sub(coll_time,2,-2)
  474. -- 8-10,16-20,6-7,12-15,21-21
  475. -- 第三步:通过逗号分割字符串,填进table
  476. coll_time_table = lua_string_split(coll_time, ",")
  477. new_table = {}
  478. log.info("-----coll_time_table:",coll_time_table)
  479. -- 获取table的长度:
  480. local size = table.getn(coll_time_table)
  481. for i = 1, size, 1 do
  482. -- 第四步:获取table的长度,遍历table,找到每个元素,通过“-”切片,生成一对开始-结束时间
  483. print("set collect time part is:",coll_time_table[i])
  484. table_son = lua_string_split(coll_time_table[i], "~")
  485. for k,v in pairs(table_son) do
  486. table_son[k] = tonumber(table_son[k])
  487. end
  488. table.insert(new_table,table_son)
  489. -- print("22set collect time part is:",coll_time_table[i])
  490. -- local size = table.getn(tim)
  491. -- -- 每一个时间段是两个值:开始时间和结束时间
  492. -- print("every collect time size is:",size)
  493. -- for j = 1, size, 1 do
  494. -- print("time".. j .. "is:".. tim[j])
  495. -- end
  496. -- print("time" .. i .. "start is:".. tim[1])
  497. -- print("time" .. i .. "end is:".. tim[2])
  498. -- if tim[1] ~= tim[2] then
  499. -- if tim[2] > tim[1] then
  500. -- print("time" .. i .. "start")
  501. -- count_second(tim[2],tim[1])
  502. -- end
  503. -- table.sort(coll_time_table , function(a , b)
  504. -- return tostring(a[i][1]) > tostring(b[i][1])
  505. -- end)
  506. -- for key,value in pairs(coll_time_table) do
  507. -- print(key,value)
  508. -- end
  509. end
  510. -- for k,v in pairs(new_table) do
  511. -- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
  512. -- end
  513. return new_table
  514. end
  515. -- 收集时间段的转换;
  516. -- hmi.hmiconf.coll_time为table;coll_time = ["7-9","10-11"],
  517. function conversion_time()
  518. -- log.error("origin set collect time is:",hmi.hmiconf.coll_time)
  519. -- -- [ "8 - 10", "1 6 -2 0"," 6- 7", "12 -15","21 -2 1"]
  520. -- -- 第一步:先过滤空格和双引号;此步骤十分关键!
  521. -- coll_time = string.gsub(hmi.hmiconf.coll_time, '[ "]', '')
  522. -- coll_time = string.gsub(coll_time, '-', '~')
  523. -- -- [8-10,16-20,6-7,12-15,21-21]
  524. -- log.error("real set collect time is:",coll_time)
  525. -- -- table: 0x821f8d30
  526. -- -- 第二步:截去两头的中括号:
  527. -- coll_time = string.sub(coll_time,2,-2)
  528. -- -- 8-10,16-20,6-7,12-15,21-21
  529. -- -- 第三步:通过逗号分割字符串,填进table
  530. -- coll_time_table = lua_string_split(coll_time, ",")
  531. log.info("-----origin set collect time is:",hmi.hmiconf.coll_time)
  532. coll_time_table = hmi.hmiconf.coll_time
  533. new_table = {}
  534. log.info("-----coll_time_table:",coll_time_table)
  535. -- 获取table的长度:
  536. local size = table.getn(coll_time_table)
  537. for i = 1, size, 1 do
  538. -- 第四步:获取table的长度,遍历table,找到每个元素,通过“-”切片,生成一对开始-结束时间
  539. print("set collect time part is:",coll_time_table[i])
  540. print("type set collect time part is:",type(coll_time_table[i]))
  541. -- 过滤空格
  542. -- coll_time_table[i] = string.gsub(coll_time_table[i], ' ', '')
  543. table_son = lua_string_split(coll_time_table[i], "-")
  544. for k,v in pairs(table_son) do
  545. table_son[k] = tonumber(table_son[k])
  546. end
  547. table.insert(new_table,table_son)
  548. -- print("22set collect time part is:",coll_time_table[i])
  549. -- local size = table.getn(tim)
  550. -- -- 每一个时间段是两个值:开始时间和结束时间
  551. -- print("every collect time size is:",size)
  552. -- for j = 1, size, 1 do
  553. -- print("time".. j .. "is:".. tim[j])
  554. -- end
  555. -- print("time" .. i .. "start is:".. tim[1])
  556. -- print("time" .. i .. "end is:".. tim[2])
  557. -- if tim[1] ~= tim[2] then
  558. -- if tim[2] > tim[1] then
  559. -- print("time" .. i .. "start")
  560. -- count_second(tim[2],tim[1])
  561. -- end
  562. -- table.sort(coll_time_table , function(a , b)
  563. -- return tostring(a[i][1]) > tostring(b[i][1])
  564. -- end)
  565. -- for key,value in pairs(coll_time_table) do
  566. -- print(key,value)
  567. -- end
  568. end
  569. -- for k,v in pairs(new_table) do
  570. -- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
  571. -- end
  572. return new_table
  573. end
  574. coll_ok = 0
  575. -- 首次转仓的标志位:
  576. first_turn = 0
  577. sys.taskInit(function()
  578. -- reboot_stm8()
  579. -- sys.wait(10000)
  580. log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
  581. if not VERSION:find('V21') then
  582. ctrl_12v(1)
  583. end
  584. resetinit()
  585. sys.wait(30 * 1000)-- 等待第一次自动对时
  586. while true do
  587. -- 判断设备开关:hmi中默认为开;-- 设备开关:注意:1为开;0为关
  588. if hmi.hmiconf.on_off == 1 then
  589. log.info("device is on!")
  590. local a = {}
  591. -- a = sort_time_table(new_table)
  592. a = sort_time_table(conversion_time())
  593. t_num = table.getn(a)
  594. log.info("***collect time stage:",t_num)
  595. print(">>>>>>>>>>>>collect section>>>>>>>>>>>>>>>>")
  596. for k,v in pairs(a) do
  597. while true do
  598. print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
  599. local cnow = misc.getClock()
  600. local cstart = misc.getClock()
  601. cstart.hour = v[1]
  602. cstart.min = 0
  603. cstart.sec = 0
  604. local cend = misc.getClock()
  605. cend.hour = v[2]
  606. cend.min = 0
  607. cend.sec = 0
  608. print("now time:" .. os.date("%Y%m%d%H%M%S",os.time(cnow)))
  609. -- print("now time:" .. os.time(cnow))
  610. print("collect_time" .. k .. " : " .. os.date("%Y%m%d%H%M%S", os.time(cstart)) .. "~" .. os.date("%Y%m%d%H%M%S", os.time(cend)))
  611. -- while coll_ok == 0 do
  612. -- 此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
  613. -- if true then -- 测试多段采集使用
  614. while os.difftime(os.time(cnow), os.time(cstart)) < 0 do
  615. cnow = misc.getClock()
  616. log.warn("collect time is not arrivd!!! >>>" .. "now time:" .. os.date("%Y%m%d%H%M%S",os.time(cnow)) .. "; next collect start time:" .. os.date("%Y%m%d%H%M%S", os.time(cstart)))
  617. sys.wait(5000)
  618. end
  619. -- 如果当前时间大于当前的时间段的结束时间:则该段时间无效
  620. if os.difftime(os.time(cnow), os.time(cstart)) >= 0 and os.difftime(os.time(cnow), os.time(cend)) >= 0 then
  621. break
  622. end
  623. if os.difftime(os.time(cnow), os.time(cstart)) >= 0 and os.difftime(os.time(cnow), os.time(cend)) < 0 then
  624. -- if os.difftime(os.time(cnow), os.time(cstart)) >= 0 then
  625. log.warn("start to collect!!! >>>" .. "start now time:" .. os.date("%Y%m%d%H%M%S",os.time(cnow)) .. "; collect end time:" .. os.date("%Y%m%d%H%M%S", os.time(cend)))
  626. log.warn("collect stage: " .. k .. "; collect seconds:" .. os.difftime(os.time(cend), os.time(cnow)))
  627. -- 注意:每次开机工作台不动,只有在每天开始第一段采集时,如果对准,则重新转动更换载玻片;如果没有对准,则对准;
  628. -- 此处不能用K==1来判断,因为,当前时间可能大于前几段时间的结束时间;第一段时间不一定有效;
  629. -- if k == 1 then turn(1) end
  630. -- if k >= t_num then coll_ok = 1 end
  631. audio.play(0,"FILE","/ldata/collect.mp3", hmi.hmiconf.vol)
  632. if first_turn == 0 then
  633. -- turn(1,5)
  634. -- 判断是否在复位位置;
  635. turn_init()
  636. turn()
  637. -- 涂抹凡士林油:
  638. add_fsl(dat,5)
  639. sys.wait(10000)
  640. -- 涂完凡士林,再次转动转盘到收集位置;
  641. turn()
  642. first_turn = 1
  643. end
  644. -- 只有开始和结束时刻不同的时间段,才有效;
  645. if os.difftime(os.time(cend), os.time(cnow)) > 0 then
  646. hmi.hmista.work_sta = 1-- 当前流程:收集
  647. sys.wait(1000)
  648. -- 打开抽风风扇
  649. collect_fan(1)
  650. sys.wait(1000 * os.difftime(os.time(cend), os.time(cnow)))
  651. -- 测试使用:每个时间段,延时10s做测试
  652. -- sys.wait(1000*10)
  653. collect_fan(0)
  654. else
  655. log.warn("this collect time is 0s,The wait time cannot be negative!")
  656. end
  657. else
  658. -- 其他时间,关闭收集风扇:
  659. log.warn("next collect time is not arrivd!!! >>>" .. "start now time:" .. os.date("%Y%m%d%H%M%S",os.time(cnow)) .. "; net collect start time:" .. os.date("%Y%m%d%H%M%S", os.time(cstart)))
  660. sys.wait(2000)
  661. end
  662. -- 运行到此处,说明遍历完毕,没有适合的采集时间:
  663. break
  664. end
  665. end
  666. if hmi.hmista.work_sta == 1 then
  667. -- 采集完毕,转位,转培养:
  668. log.warn("===============collect ok,turn to drop!!!")
  669. -- print("stat to develop,hmi.hmiconf.cul_time:",hmi.hmiconf.cul_time)
  670. develop_flag = 1
  671. hmi.hmista.work_sta = 2-- 当前流程:培养
  672. sys.wait(2000)
  673. audio.play(0,"FILE","/ldata/develop.mp3", hmi.hmiconf.vol)
  674. sys.wait(3000)
  675. -- turn(1,5)
  676. turn(1)
  677. sys.wait(5000)
  678. log.warn("===============drop is starting")
  679. -- 滴液:
  680. drop(1)
  681. sys.wait(1000)
  682. sys.taskInit(develop)
  683. sys.wait(hmi.hmiconf.cul_time * 3600 * 1000)
  684. -- 培养30s
  685. -- sys.wait(10*1000)
  686. develop_flag = 0
  687. -- 培养完毕,转位,拍照:
  688. print("turn to camera!!!")
  689. -- turn(1,5)
  690. turn(1)
  691. print("start to take photo!!!!")
  692. hmi.hmista.work_sta = 3-- 当前流程:拍照
  693. sys.wait(2000)
  694. audio.play(0,"FILE","/ldata/takephoto.mp3", hmi.hmiconf.vol)
  695. -- 最低端拍照一张、最高端拍一张;
  696. -- haikang.autotakepic()
  697. -- 自动对焦:
  698. haikang.autotakepic2()
  699. -- print("this work finished,and will start new work task!!!")
  700. -- 延时20s等待mqtt client任务中,检测采集图像完成标志位置1状态;
  701. sys.wait(20000)
  702. hmi.hmista.work_sta = 0-- 当前流程:待机
  703. audio.play(0,"FILE","/ldata/wait.mp3", hmi.hmiconf.vol)
  704. if not VERSION:find('V21') then
  705. ctrl_12v(0)
  706. end
  707. sys.wait(3000)
  708. if not VERSION:find('V21') then
  709. ctrl_12v(1)
  710. end
  711. resetinit()
  712. log.error("this work finished,system will restart after 5s!!!")
  713. sys.restart("work finished")
  714. end
  715. else
  716. log.info("device is off!")
  717. end
  718. sys.wait(1000)
  719. end
  720. end)
  721. -- 喂狗:防止复位:
  722. -- 根据仓内温度和环境温度对比,控制散热风扇:
  723. -- sys.taskInit(function()
  724. -- sys.wait(5000)
  725. -- log.error("============== wdg() task is running")
  726. -- while true do
  727. -- wdg()
  728. -- log.error("============== to wdg()")
  729. -- sys.wait(5000)
  730. -- end
  731. -- end)
  732. -- 开机测试任务:
  733. -- sys.taskInit(function()
  734. -- sys.wait(5000)
  735. -- log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
  736. -- while true do
  737. -- log.info("================================")
  738. -- -- collect_fan(1)
  739. -- -- sys.wait(2000)
  740. -- -- collect_fan(0)
  741. -- -- sys.wait(2000)
  742. -- -- cool(0)
  743. -- -- heat(0)
  744. -- turn(2)
  745. -- sys.wait(10000)
  746. -- cool(0)
  747. -- end
  748. -- end)
  749. -- sys.taskInit(function()
  750. -- sys.wait(5000)
  751. -- log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
  752. -- while true do
  753. -- log.info("================================")
  754. -- hmi.write("sysSet.keep.val=" .. 1)
  755. -- hmi.write("sysSet.keeptemp.val=" .. 1)
  756. -- hmi.write("sysSet.datt.val=" .. 2)
  757. -- hmi.write("sysSet.datatime.val=" .. 2)
  758. -- sys.wait(2000)
  759. -- end
  760. -- end)