| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866 |
- --- 模块功能 孢子仪逻辑控制
- -- @module
- -- @author lcg
- -- @license MIT
- -- @copyright yf
- -- @release 2019.08.01
- require 'ril'
- require "misc"
- require "hmi"
- require "audio"
- module(..., package.seeall)
- --拍照完成标志位
- photodone = 0
- vbat = ""
- -- 工作标志位,采集标志位,定时时长,
- local worksta, collsta, worktim
- -- 限位开关、雨控、光控传感器变量
- local limit = {upl, upr, dwl, dwr, turn, rain, light}
- local fid = {
- read = 0x55, -- 功能号:读
- write = 0xAA, -- 功能号:写
- ---------------------------
- turn = 0x01, -- 转动转盘
- get_turn = 0x07, -- 获取的限位状态: turn和turn_init
- collect_fan = 0x02, -- 抽风机开关
- cool = 0x03, -- 散热开关
- heat = 0x04, -- 加热开关
- cold = 0x05, -- 制冷开关
- fsl = 0x06, -- 凡士林推杆电机开关
- wdgtim = 0x0a, -- 看门狗喂狗时间
- stm8_v = 0x0b, -- STM8版本
- temp = 0x0c, -- 换算温度值
- vbat = 0x0d, -- 电池电压值
-
- }
- ctrl_12v = pins.setup(pio.P0_29,0)
- local function i2c_open(id)
- if i2c.setup(id, i2c.SLOW) ~= i2c.SLOW then
- log.error('I2C.init is: ', 'fail')
- i2c.close(id)
- return
- end
- return i2c.SLOW
- end
- --- STM8S003的动作方法
- -- @param a,number,fid表的字段
- -- @param v,number,0 或 1,0是关闭,1是打开
- function action(a, v)
- i2c_open(2)
- i2c.send(2, 0x51, pack.pack(">bbb", fid.write, a, v))
- i2c.close(2)
- end
- function reboot_stm8()
- i2c_open(2)
- i2c.send(2, 0x51, pack.pack(">bb", fid.write, 0x88))
- i2c.close(2)
- end
- -- 获取turn限位的io状态
- function get_limit_turn()
- local buffer,idx = 0,1
- local turn,turn_init = 2,2
- i2c_open(2)
- i2c.send(2, 0x51, pack.pack(">bb", fid.read, fid.get_turn))
- buffer = i2c.recv(2, 0x51, 2)
- i2c.close(2)
- idx, turn = pack.unpack(buffer, 'b',idx)
- idx, turn_init = pack.unpack(buffer, 'b',idx)
- log.warn("++++++++++++++++++turn,turn_init:,",turn,turn_init)
- return turn,turn_init
- end
- turn_timeout = 0
- turn_reset_timeout = 0
- -- 转盘转动
- function turn(v,sec)
- turn_timeout = 0
- audio.play('/ldata/turn.mp3')
- v = v or 1
- sec = sec or 21-- 超时时长设为10s时,刚好转90度;避免刚好错过上一个限位,所以设置为两个90度的时间;
- log.warn("------ turn: ",v,sec)
- -- action(fid.turn, v)
- i2c_open(2)
- i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.turn, v, sec))
- i2c.close(2)
- -- sys.wait(5000)
- -- log.warn("++++++++++++++++++get_limit_turn():,",get_limit_turn())
- local turn,turn_init,cnt = 2,2,0
- while turn ~= 0 do
- turn,turn_init = get_limit_turn()
- sys.wait(1000)
- cnt = cnt + 1
- if cnt > sec then
- log.warn("----- turn timeout: ",sec)
- turn_timeout = 1
- break
- end
- end
- hmi.write("cangWei.rotateFlag.val=0")
- end
- -- -- 转盘复位
- -- function turn_init(v,sec)
- -- audio.play('/ldata/turn.mp3')
- -- v = v or 7
- -- sec = sec or 30-- 超时时长设为10s时,刚好转90度;避免刚好错过上一个限位,所以设置为两个90度的时间;
- -- log.warn("------ turn_init: ",v,sec)
- -- -- action(fid.turn, v)
- -- i2c_open(2)
- -- i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.turn, v, sec))
- -- i2c.close(2)
- -- -- sys.wait(5000)
- -- local turn,turn_init,cnt = 2,2,0
- -- while turn_init ~= 0 do
- -- turn,turn_init = get_limit_turn()
- -- sys.wait(1000)
- -- cnt = cnt + 1
- -- if cnt > sec then
- -- log.warn("----- turn_init timeout: ",sec)
- -- break
- -- end
- -- end
- -- hmi.write("cangWei.rotateFlag.val=0")
- -- end
- -- 转盘复位
- function turn_init(v,sec)
- turn_reset_timeout = 0
- v = v or 7
- sec = sec or 30-- 超时时长设为10s时,刚好转90度;避免刚好错过上一个限位,所以设置为两个90度的时间;
- log.warn("------ turn_init: ",v,sec)
- local turn,turn_init,cnt = 2,2,0
- turn,turn_init = get_limit_turn()
- if turn_init ~= 0 then
- audio.play('/ldata/turn.mp3')
- i2c_open(2)
- i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.turn, v, sec))
- i2c.close(2)
- while turn_init ~= 0 do
- turn,turn_init = get_limit_turn()
- sys.wait(1000)
- cnt = cnt + 1
- if cnt > sec then
- log.warn("----- turn_init timeout: ",sec)
- turn_reset_timeout = 1
- break
- end
- end
- hmi.write("cangWei.rotateFlag.val=0")
- else
- log.warn("----- current location is reset")
- hmi.write("cangWei.rotateFlag.val=0")
- return
- end
- hmi.write("cangWei.rotateFlag.val=0")
- end
- -- 采集风机
- function collect_fan(v)
- log.warn("------ collect_fan: ",v,type(v))
- hmi.hmista.collect_fan = v
- action(fid.collect_fan, v)
- end
- -- 散热风机
- function cool(v)
- log.warn("------ cool: ",v)
- hmi.hmista.cool_fan = v
- action(fid.cool, v)
- end
- -- 滴液
- function drop(t)
- audio.play('/ldata/drop.mp3')
- print("------ drop time: ",t)
- -- for i=1,t,1 do
- -- print("----------droping...",i)
- -- pins.setup(pio.P1_1,1)
- -- sys.wait(5000)
- -- pins.setup(pio.P1_1,0)
- -- sys.wait(5000)
- -- end
- t = t or 1
- log.warn("------ droping...",t)
- pins.setup(pio.P1_1,1)
- sys.wait(t*100)
- pins.setup(pio.P1_1,0)
- hmi.write("sysTest.switchDrop.val=0")
- hmi.write('sysTest.auto_flag.txt="drop0"')
- end
- -- 加热
- function heat(v)
- log.warn("------ heat: ",v)
- hmi.hmista.heat_sta = v
- action(fid.heat, v)
- end
- -- 制冷
- function cold(v)
- log.warn("------ cold: ",v)
- hmi.hmista.cold_sta = v
- action(fid.cold, v)
- end
- -- 涂抹凡士林
- function add_fsl(v,sec)
- -- log.warn("------ add_fsl :",v)
- -- action(fid.fsl, v)
- v = v or 1
- sec = sec or 5
- log.warn("------ add_fsl: ",v,sec)
- i2c_open(2)
- i2c.send(2, 0x51, pack.pack(">bbbb", fid.write, fid.fsl, v, sec))
- i2c.close(2)
- end
- sys.taskInit(function()
- -- local Rup, adcValue, voltValue, heatTemp, idx = 5100
- local iic_buffer
- -- hmi.hmista.tempStatus = 0
- local b20temp = 0
- adc.open(0)
- while true do
- local status, error = pcall(function ()
- -- adcValue, voltValue = adc.read(0)
- -- log.info("adc read:",adcValue,voltValue)
- -- if voltValue == 0xFFFF then
- -- log.warn("adc read error")
- -- adcValue = 5400
- -- end
- -- Rt = voltValue * 1000 / ((9000 - voltValue) * 1000 / Rup)
- -- log.info("control.heatTemp ADC value:", adcValue, voltValue, Rt)
- i2c_open(2)
- -- i2c.send(2, 0x51, pack.pack(">bbH", fid.read, fid.temp, Rt))
- i2c.send(2, 0x51, pack.pack(">bb", fid.read, fid.temp))
- -- heatTemp = i2c.recv(2, 0x51, 7)
- iic_buffer = i2c.recv(2, 0x51, 11)
-
- i2c.close(2)
- -- IIC读取STM8软件版本:
- idx, stm8_v = pack.unpack(iic_buffer, '>b')
- log.info("iic read stm8_v:",stm8_v)
- hmi.hmista.stm8_v = string.format('%d', stm8_v)
- -- IIC读取电池电压:
- idx, vbat = pack.unpack(iic_buffer, '>h',idx)
- local batsta = 0
- if(vbat < 21000) then
- batsta = 1;
- elseif(vbat > 25200) then
- batsta = 0;
- end
- -- hmi.hmista.vbat = string.format('%002d.%002d', vbat / 1000, vbat % 1000)
- hmi.hmista.vbat = string.format('"%d.%d"', vbat / 1000, vbat % 1000)
- hmi.hmista.bat_sta = batsta
- log.info("iic read vbat and bat_sta",hmi.hmista.vbat,hmi.hmista.bat_sta)
- -- hmi.write("workSta.vbat.txt=" .. string.format('"%002d.%002d"', vbat / 1000, vbat % 1000))
- -- hmi.write("workSta.vbat.txt=" .. hmi.hmista.vbat)
- -- hmi.write("workSta.bat_sta.val=" .. hmi.hmista.bat_sta)
- -- IIC读取B20温度--箱内温度:
- idx, b20temp = pack.unpack(iic_buffer, '>h',idx)
- -- log.info("iic read b20temp:",b20temp)
- log.info("iic read box_tmp:",b20temp)
- -- hmi.hmista.box_tem = string.format('"%002d.%002d"', b20temp/100, b20temp%100)
- hmi.hmista.box_tem = string.format('"%2d"', b20temp)
- if b20temp >= tonumber(hmi.hmista.envTem)/10 + 5 then
- cool(1)
- else
- cool(0)
- end
- -- IIC读取温度保温仓探头温度:
- idx, hmi.hmista.pre_temp = pack.unpack(iic_buffer, '>h',idx)
- log.info("iic read keep_temp:", hmi.hmista.pre_temp)
- -- sys.wait(1000)
- -- AM2320读取环境温湿度:
- hmi.hmista.envTem, hmi.hmista.envHum = AM2320.read(2, 0x5c)
- if not hmi.hmista.envTem then
- hmi.hmista.envTem, hmi.hmista.envHum = 250, 300
- end
- log.info("temperature and humidity:", hmi.hmista.envTem, hmi.hmista.envHum)
- end)
- if status ~= true and error ~= nil then
- log.warn("------ iic read stm8, pcall:",status)
- log.warn("------ iic read stm8, error:",error)
- else
- log.info("------- iic read stm8,pcall:",status)
- log.info("------- iic read stm8,error:",error)
- end
- log.info("-------",b20temp,tonumber(hmi.hmista.envTem)/10)
- sys.wait(5000)
- end
- end)
- function rtu_12v_reset()
- log.error("------ rtu_12v power will reset ------")
- -- action(fid.rtu_power, 1)
- end
- -- 看门狗
- function wdg()
- action(fid.wdgtim, 240)
- log.warn("------ wdg is running!")
- end
- -- 工作任务初始化
- local function workInit()
- end
- -- 停止所有stm8的动作:
- local function resetinit()
- collect_fan(0)
- cold(0)
- heat(0)
- cool(0)
- turn_init()
- end
- -- 开机自检\一键自检任务:
- function self_check()
- -- 检查转盘:转离;
- -- 检查抽风:通电5s
- -- 检查散热风扇:通电5s
- -- 检查步进电机:上下转动各5s,视限位开关情况;
- -- 检查
- end
- function lua_string_split(str, split_char)
- local sub_str_tab = {}
- while (true) do
- local pos = string.find(str, split_char)
- if (not pos) then
- local size_t = table.getn(sub_str_tab)
- table.insert(sub_str_tab, size_t + 1, str)
- break
- end
- local sub_str = string.sub(str, 1, pos - 1)
- local size_t = table.getn(sub_str_tab)
- table.insert(sub_str_tab, size_t + 1, sub_str)
- local t = string.len(str)
- str = string.sub(str, pos + 1, t)
- end
- return sub_str_tab
- end
- -- local str = "1,2,3,4,5,6,7,8,9"
- -- local ta = lua_string_split(str, ",")
- -- local size = table.getn(ta)
- -- for i = 1, size, 1 do
- -- print(ta[i])
- -- end
- -- 传入起止时间,计算时间戳的差值:
- function count_second(t1,t2)
- local tc1 = misc.getClock()
- tc1.hour = t1
- tc1.min = 0
- tc1.sec = 0
- local tc2 = misc.getClock()
- tc2.hour = t2
- tc2.min = 0
- tc2.sec = 0
- print(os.difftime(os.time(tc1), os.time(tc2)))
- end
- -- 传入不定长的无序的时间表:table;返回有序时间表;
- function sort_time_table(t)
- key = {}
- -- 遍历,拼凑key table
- for i in pairs(t) do
- table.insert(key,t[i][1]) --提取test1中的键值插入到key_test表中
- end
-
- -- 查看原始table
- print("-------origen table--------")
- for k,v in pairs(t) do
- print("key:" .. "k " .. k .. " v: ".. v[1])
- end
-
- -- print("-------key table--------")
-
- -- 查看key table
- -- for k,v in pairs(key) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- -- end
-
- -- print("-------key_sort table--------")
- -- 定义一个新table
- key_sort = {}
- -- key_sort = key
-
- for k,v in pairs(key) do
- table.insert(key_sort,v)
- end
-
- -- for k,v in pairs(key_sort) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- -- end
-
-
- -- print("===========================================")
- -- print("-------paixu key table--------")
- table.sort(key_sort)
- -- key = nil
-
- -- print("-------key table--------")
- -- 查看key table
- -- for k,v in pairs(key) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- -- end
-
- -- print("-------key_sort table--------")
-
- -- for k,v in pairs(key_sort) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- -- end
-
- print("***********************************")
-
- key_idx = {}
- for k,v in pairs(key_sort) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- for j,w in pairs(key) do
- -- print("j w :",j,w)
- if v == w then
- table.insert(key_idx,j)
- end
- end
-
- end
-
- -- print("-------key_index--------")
-
- -- for k,v in pairs(key_idx) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- -- end
-
- -- print("-------result--------")
- result = {}
- for k,v in pairs(key_idx) do
- -- print("key:" .. "k " .. k .. " v: ".. v)
- table.insert(result,t[v])
- print("result:",t[v][1],t[v][2])
- end
-
- -- print("-------check --------")
-
- -- for k,v in pairs(result) do
- -- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
- -- end
- return result
- end
- -- 是否进入培养模式
- develop_flag = 0
- -- 培养载玻片孢子
- function develop()
- log.warn("------start to develop hours:",hmi.hmiconf.cul_time,type(hmi.hmiconf.cul_time))
- -- 总培养时间:hmi.hmiconf.cul_time
- -- 已经培养的时间:分钟
- hmi.hmista.staytime = 0
- local ii = 0
- while develop_flag == 1 do
- ii = ii + 1
- log.warn("developing..........")
- log.info("..........hmi.hmista.set_temp:",hmi.hmiconf.set_temp)
- log.info("..........hmi.hmista.pre_temp:",hmi.hmista.pre_temp)
- if hmi.hmista.pre_temp < (hmi.hmiconf.set_temp - 10) then
- -- if hmi.hmista.pre_temp ~= (hmi.hmiconf.set_temp) then
- if hmi.hmista.cold_sta == 0 then
- heat(1)
- end
- else
- heat(0)
- end
- log.info("..........hmi.hmiconf.cold_sw:",hmi.hmiconf.cold_sw)
- if hmi.hmiconf.cold_sw == 1 then
- -- if hmi.hmista.pre_temp ~= (hmi.hmiconf.set_temp) then
- if hmi.hmista.pre_temp > (hmi.hmiconf.set_temp + 10) then
- if hmi.hmista.heat_sta == 0 then
- cold(1)
- end
- else
- cold(0)
- end
- end
- if ii % 60 == 0 then hmi.hmista.staytime = hmi.hmista.staytime + 1 end
- -- 1分钟检测判断一次;
- sys.wait(60*1000)
- end
- heat(0)
- cold(0)
- heat(0)
- cold(0)
- log.warn("develop ok!!!..........")
- end
- -- 收集时间段的转换
- -- hmi.hmiconf.coll_time为字符串;coll_time = ["7-9","10-11"],
- function conversion_time1()
- log.info("-----origin set collect time is:",hmi.hmiconf.coll_time)
- -- [ "8 - 10", "1 6 -2 0"," 6- 7", "12 -15","21 -2 1"]
- -- 第一步:先过滤空格和双引号;此步骤十分关键!
- coll_time = string.gsub(hmi.hmiconf.coll_time, '[ "]', '')
- coll_time = string.gsub(coll_time, '-', '~')
- -- [8-10,16-20,6-7,12-15,21-21]
- log.info("-----real set collect time is:",coll_time)
- -- table: 0x821f8d30
- -- 第二步:截去两头的中括号:
- coll_time = string.sub(coll_time,2,-2)
- -- 8-10,16-20,6-7,12-15,21-21
- -- 第三步:通过逗号分割字符串,填进table
- coll_time_table = lua_string_split(coll_time, ",")
- new_table = {}
- log.info("-----coll_time_table:",coll_time_table)
- -- 获取table的长度:
- local size = table.getn(coll_time_table)
- for i = 1, size, 1 do
- -- 第四步:获取table的长度,遍历table,找到每个元素,通过“-”切片,生成一对开始-结束时间
- print("set collect time part is:",coll_time_table[i])
- table_son = lua_string_split(coll_time_table[i], "~")
- for k,v in pairs(table_son) do
- table_son[k] = tonumber(table_son[k])
- end
- table.insert(new_table,table_son)
- -- print("22set collect time part is:",coll_time_table[i])
- -- local size = table.getn(tim)
- -- -- 每一个时间段是两个值:开始时间和结束时间
- -- print("every collect time size is:",size)
- -- for j = 1, size, 1 do
- -- print("time".. j .. "is:".. tim[j])
- -- end
- -- print("time" .. i .. "start is:".. tim[1])
- -- print("time" .. i .. "end is:".. tim[2])
- -- if tim[1] ~= tim[2] then
- -- if tim[2] > tim[1] then
- -- print("time" .. i .. "start")
- -- count_second(tim[2],tim[1])
- -- end
- -- table.sort(coll_time_table , function(a , b)
- -- return tostring(a[i][1]) > tostring(b[i][1])
- -- end)
- -- for key,value in pairs(coll_time_table) do
- -- print(key,value)
- -- end
- end
- -- for k,v in pairs(new_table) do
- -- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
- -- end
- return new_table
- end
- -- 收集时间段的转换;
- -- hmi.hmiconf.coll_time为table;coll_time = ["7-9","10-11"],
- function conversion_time()
- -- log.error("origin set collect time is:",hmi.hmiconf.coll_time)
- -- -- [ "8 - 10", "1 6 -2 0"," 6- 7", "12 -15","21 -2 1"]
- -- -- 第一步:先过滤空格和双引号;此步骤十分关键!
- -- coll_time = string.gsub(hmi.hmiconf.coll_time, '[ "]', '')
- -- coll_time = string.gsub(coll_time, '-', '~')
- -- -- [8-10,16-20,6-7,12-15,21-21]
- -- log.error("real set collect time is:",coll_time)
- -- -- table: 0x821f8d30
- -- -- 第二步:截去两头的中括号:
- -- coll_time = string.sub(coll_time,2,-2)
- -- -- 8-10,16-20,6-7,12-15,21-21
- -- -- 第三步:通过逗号分割字符串,填进table
- -- coll_time_table = lua_string_split(coll_time, ",")
- log.info("-----origin set collect time is:",hmi.hmiconf.coll_time)
- coll_time_table = hmi.hmiconf.coll_time
- new_table = {}
- log.info("-----coll_time_table:",coll_time_table)
- -- 获取table的长度:
- local size = table.getn(coll_time_table)
- for i = 1, size, 1 do
- -- 第四步:获取table的长度,遍历table,找到每个元素,通过“-”切片,生成一对开始-结束时间
- print("set collect time part is:",coll_time_table[i])
- print("type set collect time part is:",type(coll_time_table[i]))
- -- 过滤空格
- -- coll_time_table[i] = string.gsub(coll_time_table[i], ' ', '')
- table_son = lua_string_split(coll_time_table[i], "-")
- for k,v in pairs(table_son) do
- table_son[k] = tonumber(table_son[k])
- end
- table.insert(new_table,table_son)
- -- print("22set collect time part is:",coll_time_table[i])
- -- local size = table.getn(tim)
- -- -- 每一个时间段是两个值:开始时间和结束时间
- -- print("every collect time size is:",size)
- -- for j = 1, size, 1 do
- -- print("time".. j .. "is:".. tim[j])
- -- end
- -- print("time" .. i .. "start is:".. tim[1])
- -- print("time" .. i .. "end is:".. tim[2])
- -- if tim[1] ~= tim[2] then
- -- if tim[2] > tim[1] then
- -- print("time" .. i .. "start")
- -- count_second(tim[2],tim[1])
- -- end
- -- table.sort(coll_time_table , function(a , b)
- -- return tostring(a[i][1]) > tostring(b[i][1])
- -- end)
- -- for key,value in pairs(coll_time_table) do
- -- print(key,value)
- -- end
- end
- -- for k,v in pairs(new_table) do
- -- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
- -- end
- return new_table
- end
- coll_ok = 0
- -- 首次转仓的标志位:
- first_turn = 0
- sys.taskInit(function()
- -- reboot_stm8()
- -- sys.wait(10000)
- log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
- ctrl_12v(1)
- resetinit()
- sys.wait(30 * 1000)-- 等待第一次自动对时
-
- while true do
- -- 判断设备开关:hmi中默认为开;-- 设备开关:注意:1为开;0为关
- if hmi.hmiconf.on_off == 1 then
- log.info("device is on!")
- local a = {}
- -- a = sort_time_table(new_table)
- a = sort_time_table(conversion_time())
- t_num = table.getn(a)
- log.info("***collect time stage:",t_num)
- print(">>>>>>>>>>>>collect section>>>>>>>>>>>>>>>>")
- for k,v in pairs(a) do
- while true do
- print("key:" .. "k " .. k .. " v: " .. "start:" .. v[1] .. " end:".. v[2])
- local cnow = misc.getClock()
- local cstart = misc.getClock()
- cstart.hour = v[1]
- cstart.min = 0
- cstart.sec = 0
- local cend = misc.getClock()
- cend.hour = v[2]
- cend.min = 0
- cend.sec = 0
- print("now time:" .. os.date("%Y%m%d%H%M%S",os.time(cnow)))
- -- print("now time:" .. os.time(cnow))
- 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)))
- -- while coll_ok == 0 do
- -- 此处判断,一个一个时间段判断,判断当前时间是否大于开始时间:
- -- if true then -- 测试多段采集使用
- while os.difftime(os.time(cnow), os.time(cstart)) < 0 do
- cnow = misc.getClock()
- 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)))
- sys.wait(5000)
- end
- -- 如果当前时间大于当前的时间段的结束时间:则该段时间无效
- if os.difftime(os.time(cnow), os.time(cstart)) >= 0 and os.difftime(os.time(cnow), os.time(cend)) >= 0 then
- break
- end
- if os.difftime(os.time(cnow), os.time(cstart)) >= 0 and os.difftime(os.time(cnow), os.time(cend)) < 0 then
- -- if os.difftime(os.time(cnow), os.time(cstart)) >= 0 then
- 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)))
- log.warn("collect stage: " .. k .. "; collect seconds:" .. os.difftime(os.time(cend), os.time(cnow)))
-
- -- 注意:每次开机工作台不动,只有在每天开始第一段采集时,如果对准,则重新转动更换载玻片;如果没有对准,则对准;
- -- 此处不能用K==1来判断,因为,当前时间可能大于前几段时间的结束时间;第一段时间不一定有效;
- -- if k == 1 then turn(1) end
- -- if k >= t_num then coll_ok = 1 end
- audio.play('/ldata/collect.mp3')
- if first_turn == 0 then
- -- turn(1,5)
- -- 判断是否在复位位置;
- turn_init()
- turn()
- -- 涂抹凡士林油:
- add_fsl(dat,5)
- sys.wait(10000)
- -- 涂完凡士林,再次转动转盘到收集位置;
- turn()
- first_turn = 1
- end
- -- 只有开始和结束时刻不同的时间段,才有效;
- if os.difftime(os.time(cend), os.time(cnow)) > 0 then
- hmi.hmista.work_sta = 1-- 当前流程:收集
- sys.wait(1000)
- -- 打开抽风风扇
- collect_fan(1)
- sys.wait(1000 * os.difftime(os.time(cend), os.time(cnow)))
- -- 测试使用:每个时间段,延时10s做测试
- -- sys.wait(1000*10)
- collect_fan(0)
- else
- log.warn("this collect time is 0s,The wait time cannot be negative!")
- end
- else
- -- 其他时间,关闭收集风扇:
- 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)))
- sys.wait(2000)
- end
- -- 运行到此处,说明遍历完毕,没有适合的采集时间:
- break
- end
- end
- if hmi.hmista.work_sta == 1 then
- -- 采集完毕,转位,转培养:
- log.warn("===============collect ok,turn to drop!!!")
- -- print("stat to develop,hmi.hmiconf.cul_time:",hmi.hmiconf.cul_time)
- develop_flag = 1
- hmi.hmista.work_sta = 2-- 当前流程:培养
- sys.wait(2000)
- audio.play('/ldata/develop.mp3')
- sys.wait(3000)
- -- turn(1,5)
- turn(1)
- sys.wait(5000)
- log.warn("===============drop is starting")
- -- 滴液:
- drop(1)
- sys.wait(1000)
- sys.taskInit(develop)
- sys.wait(hmi.hmiconf.cul_time * 3600 * 1000)
- -- 培养30s
- -- sys.wait(10*1000)
- develop_flag = 0
- -- 培养完毕,转位,拍照:
- print("turn to camera!!!")
- -- turn(1,5)
- turn(1)
- print("start to take photo!!!!")
- hmi.hmista.work_sta = 3-- 当前流程:拍照
- sys.wait(2000)
- audio.play('/ldata/takephoto.mp3')
- -- 最低端拍照一张、最高端拍一张;
- -- haikang.autotakepic()
- -- 自动对焦:
- haikang.autotakepic2()
- -- print("this work finished,and will start new work task!!!")
- -- 延时20s等待mqtt client任务中,检测采集图像完成标志位置1状态;
- sys.wait(20000)
- hmi.hmista.work_sta = 0-- 当前流程:待机
- audio.play('/ldata/wait.mp3')
- ctrl_12v(0)
- sys.wait(3000)
- ctrl_12v(1)
- resetinit()
- log.error("this work finished,system will restart after 5s!!!")
- sys.restart("work finished")
- end
- else
- log.info("device is off!")
- end
- sys.wait(1000)
- end
- end)
- -- 喂狗:防止复位:
- -- 根据仓内温度和环境温度对比,控制散热风扇:
- -- sys.taskInit(function()
- -- sys.wait(5000)
- -- log.error("============== wdg() task is running")
- -- while true do
- -- wdg()
- -- log.error("============== to wdg()")
- -- sys.wait(5000)
- -- end
- -- end)
- -- 开机测试任务:
- -- sys.taskInit(function()
- -- sys.wait(5000)
- -- log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
- -- while true do
- -- log.info("================================")
- -- -- collect_fan(1)
- -- -- sys.wait(2000)
- -- -- collect_fan(0)
- -- -- sys.wait(2000)
- -- -- cool(0)
- -- -- heat(0)
- -- turn(2)
- -- sys.wait(10000)
- -- cool(0)
- -- end
- -- end)
- -- sys.taskInit(function()
- -- sys.wait(5000)
- -- log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
- -- while true do
- -- log.info("================================")
- -- hmi.write("sysSet.keep.val=" .. 1)
- -- hmi.write("sysSet.keeptemp.val=" .. 1)
- -- hmi.write("sysSet.datt.val=" .. 2)
- -- hmi.write("sysSet.datatime.val=" .. 2)
- -- sys.wait(2000)
- -- end
- -- end)
|