control.lua 29 KB

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