control.lua 29 KB

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