| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- #include <control.h>
- /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2020-05-30 Administrator the first version
- */
- #include "beep.h"
- #include "hmi.h"
- #include "easyflash.h"
- #include "stdio.h"
- #include "sound.h"
- #define DBG_TAG "control.c"
- #define DBG_LVL LOG_LVL_INFO
- #include "dtu_uart2.h"
- #include "sht30_collect.h"
- //工作检测
- #define WORK_CHECK_THREAD_PRIORITY 23 //线程优先级
- #define WORK_CHECK_THREAD_STACK_SIZE 1024 //栈大小
- #define WORK_CHECK_THREAD_TIMESLICE 5 //时间片
- //工作流程
- #define WORK_LOOP_THREAD_PRIORITY 12 //线程优先级
- #define WORK_LOOP_THREAD_STACK_SIZE 1024 //栈大小
- #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
- //设备状态表结构体
- Device_Status_typedef device_status;
- rt_thread_t tid_workloop = RT_NULL;
- rt_thread_t tid_wkstc = RT_NULL;
- rt_thread_t tid_heatloop = RT_NULL;
- /*光控*/
- uint8_t In_Light_status(void)
- {
- rt_pin_mode(LIGHT_PIN,PIN_MODE_INPUT);
- return rt_pin_read(LIGHT_PIN);
- }
- /*雨控*/
- uint8_t In_Rain_status(void)
- {
- rt_pin_mode(RAIN_PIN,PIN_MODE_INPUT);
- return rt_pin_read(RAIN_PIN);
- }
- /*加热*/
- uint8_t ctr_Heat(uint8_t status_heat)
- {
- device_status.heat_status = status_heat;
- rt_pin_mode(Heat_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Heat_Pin,status_heat);
- return status_heat;
- }
- /*相机供电*/
- void ctr_photo(uint8_t status_photo)
- {
- rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
- rt_pin_write(PHOTO,status_photo);
- }
- /*采集风机/上仓门*/
- uint8_t ctr_cangmen_up(uint8_t status_up)
- {
- rt_pin_mode(PC5_PWM, PIN_MODE_OUTPUT);
- rt_pin_mode(PC4_PWM, PIN_MODE_OUTPUT);
- if(status_up==OFF)
- {
- Motor_UP_PWM_R(OFF);
- Motor_UP_PWM_L(OFF);
- device_status.ctr_up_motor = 0;
- }
- if(status_up==OPEN)
- {
- device_status.up_status = 1;
- Motor_UP_PWM_R(ON);
- Motor_UP_PWM_L(OFF);
- }
- if(status_up==CLOSE)
- {
- device_status.up_status = 0;
- Motor_UP_PWM_R(OFF);
- Motor_UP_PWM_L(ON);
- }
- return device_status.up_status;
- }
- //下仓门
- uint8_t ctr_cangmen_down(uint8_t status_down)
- {
- if(status_down==OFF)
- {
- Motor_Down_PWM_L(OFF);
- Motor_Down_PWM_R(OFF);
- device_status.ctr_down_motor = 0;
- }
- if(status_down==OPEN)
- {
- device_status.down_status = 1;
- Motor_Down_PWM_L(OFF);
- Motor_Down_PWM_R(ON);
- }
- if(status_down==CLOSE)
- {
- device_status.down_status = 0;
- Motor_Down_PWM_R(OFF);
- Motor_Down_PWM_L(ON);
- }
- return device_status.down_status;
- }
- //镇流器
- uint8_t ctr_lamp(uint8_t status_lamp)
- {
- rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
- if(status_lamp==ON)
- {
- rt_pin_write(LAMP_SW,ON);
- device_status.lamp_status = status_lamp;
- }
- else {
- rt_pin_write(LAMP_SW, OFF);
- device_status.lamp_status = status_lamp;
- }
- return device_status.lamp_status;
- }
- //震动电机
- uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
- {
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- if(status_shake==ON&&device_status.shake_ws==1)
- {
- rt_pin_write(SHAKE_SW,ON);
- rt_thread_delay(shake_sec*100);
- rt_pin_write(SHAKE_SW,OFF);
- HMI_WRITE("cangWei.switchShake.val=%d",0);
- }
- else
- {
- rt_pin_write(SHAKE_SW,OFF);
- HMI_WRITE("cangWei.switchShake.val=%d",0);
- }
- return status_shake;
- }
- //补光
- uint8_t ctr_led(uint8_t status_led)
- {
- rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
- if(status_led==ON)
- {
- rt_pin_write(LED_SW,ON);
- HMI_WRITE("sysTest.switchFill.val=%d",ON);
- }
- else if(status_led==OFF){
- rt_pin_write(LED_SW,OFF);
- HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
- HMI_WRITE("sysTest.switchFill.val=%d",OFF);
- }
- return status_led;
- }
- //转仓
- void turnzp(void)
- {
- static uint16_t turn_timeout = 0;
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(10);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(10);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- turn_timeout = 0;
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- rt_thread_delay(1000);
- }
- //雨控电机
- void ctr_rain_Motor(uint8_t r_m_s)
- {
- rt_uint16_t check_rlt = 0;
- rt_pin_mode(CTR_R_LOW, PIN_MODE_OUTPUT);
- //雨控限位
- rt_pin_mode(CTR_RO_LIMIT,PIN_MODE_INPUT);
- rt_pin_mode(CTR_RC_LIMIT,PIN_MODE_INPUT);
- rt_pin_write(CTR_R_LOW,OFF);
- if(r_m_s==OPEN)
- {
- device_status.clear_status = 1; /*落虫*/
- if(rt_pin_read(CTR_RO_LIMIT)==0)
- rt_thread_delay(100);
- if(rt_pin_read(CTR_RO_LIMIT)==0) return;
- Rain_Motor_PWM_R(OFF);
- Rain_Motor_PWM_L(ON);
- while(rt_pin_read(CTR_RO_LIMIT)!=0)
- {
- check_rlt++;
- rt_thread_delay(20);
- if(check_rlt==2000) //超时10s
- {
- break;
- }
- }
- Rain_Motor_PWM_R(OFF);
- Rain_Motor_PWM_L(OFF);
- check_rlt = 0;
- }
- else if (r_m_s==CLOSE) {
- device_status.clear_status = 0; /*排水*/
- if(rt_pin_read(CTR_RC_LIMIT)==0)
- rt_thread_delay(100);
- if(rt_pin_read(CTR_RC_LIMIT)==0) return;
- Rain_Motor_PWM_R(ON);
- Rain_Motor_PWM_L(OFF);
- while(rt_pin_read(CTR_RC_LIMIT)!=0)
- {
- check_rlt++;
- rt_thread_delay(20);
- if(check_rlt==2000) //超时10s
- {
- break;
- }
- }
- check_rlt = 0;
- Rain_Motor_PWM_R(OFF);
- Rain_Motor_PWM_L(OFF);
- }
- else {
- Rain_Motor_PWM_R(OFF);
- Rain_Motor_PWM_L(OFF);
- }
- }
- //清扫-常闭型限位开关
- //新结构使用推杆电机做清扫
- uint8_t ctr_clear(uint16_t status_clear)
- {
- static uint16_t clear_num = 0;
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_R_LIMIT,PIN_MODE_INPUT);
- rt_pin_mode(CLEAR_L_LIMIT,PIN_MODE_INPUT);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- if(status_clear==OFF||Read_Real_Voltage()/100<12) return 0;
- rt_thread_delay(100);
- //如果清扫位置在中间
- while(rt_pin_read(CLEAR_R_LIMIT)==0&&rt_pin_read(CLEAR_L_LIMIT)==0)
- {
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(50);
- clear_num++;
- //已到达检测位置
- if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(2000);
- break;
- }
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_free(makeJson_Warn("Clear Motor ERR","device"));
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- clear_num = 0;
- //如果清扫位置在左侧
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_pin_write(CLEAR_L,ON);
- rt_pin_write(CLEAR_R,OFF);
- rt_thread_delay(50);
- clear_num++;
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- DTU_Write("%s",makeJson_Warn("Clear Motor ERR","device"));
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_free(makeJson_Warn("Clear Motor ERR","device"));
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- clear_num = 0;
- while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1||(Read_Real_Voltage()/100)<12)
- {
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==0)
- {
- rt_pin_write(CLEAR_L,OFF);
- rt_pin_write(CLEAR_R,ON);
- rt_thread_delay(50);
- clear_num++;
- if(clear_num>=status_clear||atoi(device_status.power)>2000||(Read_Real_Voltage()/100)<12)
- {
- rt_kprintf("Clear Motor ERR:%d",atoi(device_status.power));
- char * Clear_ERR = makeJson_Warn("Clear Motor ERR","device");
- DTU_Write("%s",Clear_ERR);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_free(Clear_ERR);
- if(Read_Real_Voltage()/100<12)
- {
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 0;
- }
- break;
- }
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_kprintf("Clear Motor Limit Not Access:%d",atoi(device_status.power));
- char * Clear_ERR = makeJson_Warn("Clear Motor Limit Not Access","device");
- DTU_Write("%s",Clear_ERR);
- rt_free(Clear_ERR);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- return 0;
- }
- clear_num = 0;
- rt_thread_delay(1000);
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- device_status.clear_status = 0;
- return 1;
- }
- //路由器供电
- void ctr_router(uint8_t status_router)
- {
- rt_pin_mode(ROUTER, PIN_MODE_OUTPUT);
- if(status_router==ON)
- {
- rt_pin_write(ROUTER,ON);
- }
- else {
- rt_pin_write(ROUTER,OFF);
- }
- }
- //DTU_RTU供电
- void ctr_DTU_Power(uint8_t DTU_Power)
- {
- rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
- if(DTU_Power==ON)
- {
- rt_pin_write(ROUTER,ON);
- }
- else {
- rt_pin_write(ROUTER,OFF);
- }
- }
- //清扫-推杆电机
- void ctr_TG_Clear(uint16_t clear_timeout)
- {
- static uint16_t clear_count = 0;
- clear_count = clear_timeout;
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- while(clear_count!=0)
- {
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- clear_count--;
- rt_thread_delay(100);
- }
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(2000);
- while(clear_timeout!=clear_count)
- {
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- clear_count++;
- rt_thread_delay(100);
- }
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(1000);
- HMI_WRITE("cangWei.rotateFlag.val=%d",0);
- HMI_WRITE("sysTest.switchClear.val=%d",0);
- }
- //DTU,RTU供电
- void ctr_RTU_DTU(uint8_t status_rtu)
- {
- rt_pin_mode(RTU_DTU, PIN_MODE_OUTPUT);
- if(status_rtu==ON)
- {
- rt_pin_write(RTU_DTU,ON);
- }
- else
- {
- rt_pin_write(RTU_DTU,OFF);
- }
- }
- //海康拍照
- void Hk_Take_io(void)
- {
- rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
- rt_pin_write(HK_TAKE, 1);
- Beep_Tip(1000);
- rt_pin_write(HK_TAKE, 0);
- }
- void calorstat(uint16_t heat_time)
- {
- uint8_t heat_up_cnt = 0;
- uint16_t n = 0;
- uint16_t delay_heat = 60;
- FlagStatus heat_flg = RESET;
- device_status.heat_time_min = 0;
- ctr_Heat(ON);
- UpLoad_Data(); //开始加热时的第一条数据
- //恒温模式
- for(n=0;n < heat_time;n++)
- {
- if(device_status.heattem>device_status.heatvalue&&heat_flg==RESET)
- {
- heat_flg = SET;
- ctr_Heat(OFF);
- while(delay_heat!=0)
- {
- delay_heat --;
- rt_kprintf("正在恒温加热!!!:%d",delay_heat);
- rt_thread_delay(1000);
- }
- delay_heat = 60;
- //跳出FOR循环
- n = n + 60;
- }
- if(device_status.heattem < (device_status.heatvalue - 5)&&heat_flg == SET)
- {
- heat_up_cnt = 0;
- ctr_Heat(ON);
- }
- else if (device_status.heattem>device_status.heatvalue&&heat_flg==SET)
- {
- if(heat_up_cnt==0)
- {
- UpLoad_Data(); //加热到设定温度的数据
- heat_up_cnt = 1;
- }
- ctr_Heat(OFF);
- }
- if((n % 60)==0)
- {
- device_status.heat_time_min = device_status.heat_time_min + 1;
- if(device_status.heat_time_min % 2 == 0)
- {
- UpLoad_Data(); //从加热开始每两分钟上传的数据
- }
- }
- //如果触发异常退出加热
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- rt_kprintf("============================触发异常退出加热==========================");
- ctr_lamp(OFF);
- ctr_Heat(OFF);
- break;
- }
- rt_thread_delay(1000);
- rt_kprintf("=======正在加热======:%d-%d\n",n,heat_time);
- }
- ctr_Heat(OFF);
- UpLoad_Data(); //加热结束的数据
- }
- //初始化所有控制驱动,关闭
- void HardWare_Init(uint8_t boot)
- {
- Beep_Tip(200);
- //雨控电机归位
- ctr_rain_Motor(OFF);
- //关闭上仓门
- ctr_cangmen_up(OFF);
- //关闭下仓门电机
- ctr_cangmen_down(OFF);
- //关闭镇流器
- ctr_lamp(OFF);
- //关闭震动电机
- ctr_shake(OFF,0);
- //关闭清扫电机
- //关闭加热
- ctr_Heat(OFF);
- //关闭补光灯
- ctr_led(OFF);
- //关闭12V供电(海康相机,RTU)
- ctr_photo(OFF);
- }
- //拍照
- void HK_TakePhoto(void)
- {
- //打开补光灯
- ctr_led(ON);
- DTU_Write("takephoto.mp3");
- //延时
- rt_thread_delay(1000*10);
- rt_kprintf("\n测试拍照!\n");
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- //触发海康相机拍照
- Hk_Take_io();
- rt_thread_delay(1000*45);
- ctr_led(OFF);
- }
- //光控测试任务
- void SunTest(void)
- {
- while(Read_Light_Voltage()<500)
- {
- rt_thread_delay(1000);
- }
- rt_thread_delay(3000);
- if(Read_Light_Voltage()<500)
- {
- device_status.sun_status = 0;//光控状态
- }
- if(Read_Light_Voltage()>500)
- {
- rt_thread_delay(1000);
- }
- rt_thread_delay(3000);
- if(Read_Light_Voltage()>500)
- {
- device_status.sun_status = 1;
- ctr_lamp(OFF);
- }
- HMI_WRITE("sysTest.switchSun.val=%d",0);
- device_status.sun_test = 0;
- }
- /*上电初始化*/
- void ResetInit(void)
- {
- device_status.work_done = 1;
- //1.打开路由器供电
- ctr_router(ON);
- ctr_RTU_DTU(ON);
- //2.打开相机供电
- ctr_photo(ON);
- //3.关闭镇流器
- ctr_lamp(OFF);
- //4.关闭震动电机
- ctr_shake(OFF, 0);
- //5.关闭加热
- ctr_Heat(OFF);
- //6.关闭补光灯
- ctr_led(OFF);
- if(device_status.bat_status==1||atoi(device_status.fuse_voltage)<1)
- {
- rt_kprintf("欠压!!!\n");
- return;
- }
- //切换排水
- ctr_rain_Motor(CLOSE);
- //7.关闭上仓门
- device_status.ctr_up_motor = 2;
- while(device_status.ctr_up_motor!=0)
- {
- rt_kprintf("关闭上仓门...\n");
- rt_thread_delay(100);
- }
- //8.关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- rt_kprintf("关闭下仓门....\n");
- rt_thread_delay(100);
- }
- //9.清扫
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- rt_kprintf("异常退出!!!\n");
- return;
- }
- if(device_status.version_type==4)
- {
- if(ctr_clear(650)==0)
- ctr_TG_Clear(250);
- }
- else if (device_status.version_type==1) {
- turnzp();
- }
- rt_thread_delay(1000);
- }
- //出厂前自检 0-开机自检 1-屏幕或指令下发手动自检
- void HardWare_Check(uint8_t boot)
- {
- if(boot==0)
- {
- //停止所有动作 -完成
- HardWare_Init(boot);
- }
- else {
- //停止所有动作
- //读取记录待机功耗(实测大概70mA)
- //(需减去屏幕的功耗大概120mA)
- //动作上仓门,读取功率值保存
- //动作下仓门,读取功率值保存
- //动作清扫,读取功率值保存
- //打开镇流器,读取功率值保存
- //打开海康相机供电,读取功率值保存
- //打开补光灯,读取功率值保存
- //打开路由器/RTU供电,读取功率值保存
- //打开加热,读取功率值保存
- }
- }
- //工作状态检测
- void WorkCheck(void)
- {
- //雨控检测,温控检测,电池电压检测
- while(device_status.rain_status==0&&device_status.temp_status==0&&device_status.bat_status==0)
- {
- //工作模式检测(RTC时间正常,1-时控/0-光控)(RTC时间异常强行使用光控)
- if(device_status.timeSwitch==1)
- {
- rt_kprintf("***************时控*****************\n");
- //如果开始和结束是同一天
- if(device_status.startBar<device_status.stopBar)
- {
- rt_kprintf("开始和结束是同一天");
- time_t now;
- time(&now);
- static struct tm nowtime; //现在
- static struct tm starttime; //开始
- static struct tm endtime; //结束
- //获取当前时间戳
- nowtime =*localtime(&now);
- nowtime.tm_hour = nowtime.tm_hour-8;
- //获取定时开始的时间戳
- starttime = *localtime(&now);
- starttime.tm_hour = device_status.startBar-8;
- starttime.tm_min = 0;
- starttime.tm_sec = 0;
- //获取定时结束的时间戳
- endtime = *localtime(&now);
- endtime.tm_hour = device_status.stopBar-8;
- endtime.tm_min = 0;
- endtime.tm_sec = 0;
- rt_kprintf("***工作结束在当天:现在时间:%d-开始时间:%d-结束时间:%d***\n",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
- //如果当前时间大于开始时间且小于结束时间,且没有开始工作
- if((difftime(mktime(&nowtime), mktime(&starttime)) >=0) && (difftime(mktime(&nowtime),mktime(&endtime))<0))
- {
- if(device_status.sun_status==0)
- {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中!!!\n");
- device_status.work_status = 1;
- }
- else {
- device_status.work_status = 0;
- rt_kprintf("当前时间大于开始时间,小于结束时间,光控中!!!\n");
- }
- }
- //如果当前时间小于开始时间
- else if(difftime(mktime(&nowtime),mktime(&starttime))<0)
- {
- rt_kprintf("当前时间小于开始时间,不工作!!!-%d-%d\n",mktime(&nowtime),mktime(&starttime));
- device_status.work_status = 0;
- }
- //当前时间大于结束时间
- else {
- rt_kprintf("当前时间大于结束时间,不工作!!!-%d-%d\n",mktime(&nowtime),mktime(&starttime));
- device_status.work_status = 0;
- }
- }
- //如果结束时间是第二天
- else if (device_status.startBar>device_status.stopBar) {
- time_t now;
- time(&now);
- static struct tm nowtime; //现在
- static struct tm starttime; //开始
- static struct tm endtime; //结束
- //获取当前时间戳
- nowtime =*localtime(&now);
- nowtime.tm_hour = nowtime.tm_hour-8;
- //获取定时开始的时间戳
- starttime = *localtime(&now);
- starttime.tm_hour = device_status.startBar-8;
- starttime.tm_min = 0;
- starttime.tm_sec = 0;
- //获取定时结束的时间戳
- endtime = *localtime(&now);
- endtime.tm_hour = device_status.stopBar-8;
- endtime.tm_min = 0;
- endtime.tm_sec = 0;
- rt_kprintf("工作结束在第二天:现在时间:%d-开始时间:%d-结束时间:%d!!!\n",mktime(&nowtime),mktime(&starttime),mktime(&endtime));
- //如果当前时间大于开始时间,且没有开始工作 ,注意时间戳日期加1
- if((difftime(mktime(&nowtime), mktime(&starttime)) > 0 ))
- {
- //获取定时结束的时间戳
- endtime = *localtime(&now); //将获取到的时间转换为时间戳
- endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
- endtime.tm_min = 0;
- endtime.tm_sec = 0;
- if(device_status.sun_status==0)
- {
- device_status.work_status = 1;
- rt_kprintf("当前前时间大于开始时间,且没有开始工作\n");
- }
- else {
- device_status.work_status = 0;
- rt_kprintf("当前前时间大于开始时间,且没有开始工作,光控!!!\n");
- }
- }
- //当前时间小于开始时间
- else if ((difftime(mktime(&nowtime), mktime(&starttime)) < 0))
- {
- //获取定时结束的时间戳
- endtime = *localtime(&now); //将获取到的时间转换为时间戳
- endtime.tm_hour = device_status.stopBar-8; //这里需减8用于时区转换
- endtime.tm_min = 0;
- endtime.tm_sec = 0;
- //如果当前时间戳小于结束时间戳
- if(difftime(mktime(&nowtime),mktime(&endtime)) < 0)
- {
- rt_kprintf("当前时间大于开始时间,且没有开始工作,当前时间小于结束时间的情况:计算工作时间:从当前时间工作第二天的结束时间\n");
- if(device_status.sun_status==0)
- {
- device_status.work_status = 1;
- }
- else {
- device_status.work_status = 0;
- }
- }
- else
- {
- rt_kprintf("当前时间小于开始时间,且没有开始工作,不是重启引起的,即还没到开始时间!!!\n");
- device_status.work_status = 0; //不在工作时间,待机
- }
- }
- }
- }
- else {
- rt_kprintf("光控定时模式!!!\n");
- time_t now;
- time(&now);
- static struct tm nowstamp;
- nowstamp =*localtime(&now);
- nowstamp.tm_hour = nowstamp.tm_hour - 8;
- static rt_uint32_t nowstamp_num = 0;
- nowstamp_num = mktime(&nowstamp);
- rt_kprintf("now stamp:%d\n",nowstamp_num);
- if(device_status.sun_status==0)
- {
- //检测到光控,计算定时
- if(device_status.light_over_tim > nowstamp_num)
- {
- device_status.work_status = 1;
- rt_kprintf("光控定时,计时中:%d----%d\n",device_status.light_over_tim,nowstamp_num);
- }
- else
- {
- device_status.work_status = 0; //置位工作状态
- rt_kprintf("光控定时任务已完成!!!\n");
- }
- }
- else
- {
- device_status.work_status = 0;
- device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
- rt_kprintf("光控中,白天:%d!!!\n",nowstamp_num);
- }
- }
- rt_thread_delay(1000);
- }
- rt_kprintf("雨控状态,温控状态,电压状态:\n%d\n%d\n%d\n",device_status.rain_status,device_status.temp_status,device_status.bat_status);
- device_status.work_status = 0;
- rt_thread_delay(1000);
- }
- //工作任务流程s
- void WorkTast(void)
- {
- ctr_router(ON); //打开路由器供电
- static int clt_time = 0; //收集时间差秒数
- rt_kprintf("工作状态:%d\n",device_status.work_status);
- rt_kprintf("开关机状态:%d\n",device_status.ds);
- while(device_status.work_status==1&&device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1)
- {
- device_status.work_done = 0;
- if(clt_time==0) DTU_Write("workstart.mp3");
- //1.打开灯管
- ctr_lamp(ON);
- rt_kprintf("打开灯管!!!\n");
- rt_thread_delay(5000);
- //切换通道为落虫
- ctr_rain_Motor(OPEN);
- //2.打开上仓门
- rt_kprintf("打开上仓门!!!\n");
- device_status.ctr_up_motor = 1;
- //3.收集
- clt_time = device_status.clt_time * 60;
- while(clt_time!=1)
- {
- clt_time --;
- rt_thread_delay(1000);
- rt_kprintf("正在收集.....剩余时间:%d分-%d秒\n",device_status.clt_time,clt_time);
- //如果触发异常
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF);
- rt_kprintf("异常退出!!!\n");
- break;
- }
- }
- clt_time = 1;
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1)
- {
- ctr_lamp(OFF);
- rt_kprintf("异常退出!!!\n");
- break;
- }
- //4.关闭上仓门
- device_status.ctr_up_motor = 2;
- rt_kprintf("关闭上仓门!!!\n");
- // while(device_status.ctr_up_motor!=0)
- // {
- // rt_thread_delay(100);
- // rt_kprintf("等待上仓门关闭!!!\n");
- // }
- //5.加热
- rt_kprintf("加热!!!\n");
- //calorstat(device_status.heattime*60);
- rt_thread_delay(device_status.heattime*60*1000);
- //6.打开下仓门
- if(device_status.rain_status==0)
- {
- device_status.ctr_down_motor =1;
- }
- //待下仓门完全打开
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门打开!!!\n");
- }
- rt_thread_delay(1000);
- //关闭下仓门
- device_status.ctr_down_motor = 2;
- while(device_status.ctr_down_motor!=0)
- {
- rt_thread_delay(100);
- rt_kprintf("等待下仓门关闭!!!\n");
- }
- if(device_status.version_type==4)
- {
- //震动
- ctr_shake(ON, 1);
- //补光&&拍照
- rt_kprintf("拍照!!!\n");
- HK_TakePhoto();
- //清扫
- if(ctr_clear(1200)==0)
- ctr_TG_Clear(250);
- }
- }
- if(device_status.bat_status == 1 || device_status.temp_status == 1|| device_status.rain_status==1||device_status.ds==0)
- {
- rt_kprintf("-------------------not work--------------------\n");
- device_status.work_status = 0;
- }
- //工作结束
- if(device_status.work_done==0&&device_status.work_status==0)
- {
- DTU_Write("workdone");
- ResetInit();
- }
- rt_kprintf("***not work-device status:%d\n",device_status.ds);
- }
- /* 入口函数-工作状态检测 */
- static void Work_status_thread_entry(void *parameter)
- {
- static uint16_t sun_status_num = 300;
- //如果是光控定时,进行光控监测的消抖
- if(device_status.timeSwitch==0&&device_status.sun_status==0)
- {
- rt_kprintf("光控定时!!!\n");
- while(sun_status_num!=0)
- {
- sun_status_num--;
- rt_thread_delay(1000);
- rt_kprintf("开机光控检测:%d\n",sun_status_num);
- }
- time_t now;
- time(&now);
- static struct tm nowstamp;
- nowstamp =*localtime(&now);
- nowstamp.tm_hour = nowstamp.tm_hour - 8;
- static rt_uint32_t nowstamp_num = 0;
- nowstamp_num = mktime(&nowstamp);
- device_status.light_over_tim = device_status.sunBar * 3600 + nowstamp_num;
- }
- while(1)
- {
- WorkCheck();
- rt_thread_delay(1);
- }
- }
- /*入口函数-工作流程*/
- static void Work_Loop_thread_entry(void *parameter)
- {
- rt_thread_delay(1000);
- ResetInit();
- while(1)
- {
- WorkTast();
- rt_thread_delay(1000);
- }
- }
- static void Heat_Loop_thread_entry(void *parameter)
- {
- while(1)
- {
- if(device_status.work_status==1)
- {
- rt_kprintf(">>>>>>>>>>>>>>>>>>>>>>>恒温加热开始!!!\n");
- calorstat(device_status.heattime*60);
- }
- else {
- ;
- }
- rt_thread_delay(1000);
- }
- }
- /* 创建线程 -是否置位工作状态*/
- int Workstatus_check(void)
- {
- /* 创建线程*/
- tid_wkstc = rt_thread_create("work_status_check_thread",
- Work_status_thread_entry, RT_NULL,
- WORK_CHECK_THREAD_STACK_SIZE, //2048
- WORK_CHECK_THREAD_PRIORITY, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_wkstc!=RT_NULL)
- {
- rt_thread_startup(tid_wkstc);
- }
- return 0;
- }
- /*工作流程线程*/
- int Work_Loop_Task(void)
- {
- /* 创建线程*/
- tid_workloop = rt_thread_create("work_loop_thread",
- Work_Loop_thread_entry, RT_NULL,
- WORK_LOOP_THREAD_STACK_SIZE, //2048
- WORK_LOOP_THREAD_PRIORITY, //12
- WORK_LOOP_THREAD_TIMESLICE); //5
- if(tid_workloop!=RT_NULL)
- {
- rt_thread_startup(tid_workloop);
- }
- return 0;
- }
- /*恒温加热线程*/
- int Heat_Loop_Task(void)
- {
- /* 创建线程*/
- tid_heatloop = rt_thread_create("heat_loop_thread",
- Heat_Loop_thread_entry, RT_NULL,
- WORK_LOOP_THREAD_STACK_SIZE, //2048
- 25, //12
- WORK_LOOP_THREAD_TIMESLICE); //5
- if(tid_heatloop!=RT_NULL)
- {
- rt_thread_startup(tid_heatloop);
- }
- return 0;
- }
- MSH_CMD_EXPORT(HardWare_Check,HardWare_Check<ON|OFF>);
- MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
- MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
- MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
|