| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155 |
- #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;
- /*光控*/
- 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(Collect_fan_R, PIN_MODE_OUTPUT);
- rt_pin_mode(Collect_fan_L, PIN_MODE_OUTPUT);
- if(status_up==OFF)
- {
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,ON);
- device_status.ctr_up_motor = 0;
- rt_thread_delay(1000);
- }
- if(status_up==OPEN)
- {
- device_status.up_status = 1;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- rt_pin_write(Collect_fan_R,ON);
- rt_pin_write(Collect_fan_L,OFF);
- }
- if(status_up==CLOSE)
- {
- device_status.up_status = 0;
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,OFF);
- rt_pin_write(Collect_fan_R,OFF);
- rt_pin_write(Collect_fan_L,ON);
- }
- return device_status.up_status;
- }
- //下仓门
- uint8_t ctr_cangmen_down(uint8_t status_down)
- {
- rt_pin_mode(C_DOWN_R, PIN_MODE_OUTPUT);
- rt_pin_mode(C_DOWN_L, PIN_MODE_OUTPUT);
- if(status_down==OFF)
- {
- rt_pin_write(C_DOWN_R,ON);
- rt_pin_write(C_DOWN_L,ON);
- device_status.ctr_down_motor = 0;
- rt_thread_delay(1000);
- }
- if(status_down==OPEN)
- {
- device_status.down_status = 1;
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,OFF);
- rt_pin_write(C_DOWN_R,ON);
- rt_pin_write(C_DOWN_L,OFF);
- }
- if(status_down==CLOSE)
- {
- device_status.down_status = 0;
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_L,OFF);
- rt_pin_write(C_DOWN_R,OFF);
- rt_pin_write(C_DOWN_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;
- }
- //LED
- void ctr_LED(uint8_t Sta_BLUE,uint8_t Sta_YELLOW,uint8_t Sta_PURPLE,uint8_t Sta_GREEN)
- {
- rt_pin_mode(LED_YELLOW, PIN_MODE_OUTPUT);
- rt_pin_mode(LED_BLUE, PIN_MODE_OUTPUT);
- rt_pin_mode(LED_PURPLE, PIN_MODE_OUTPUT);
- rt_pin_mode(LED_GREEN, PIN_MODE_OUTPUT);
- rt_pin_write(LED_YELLOW,Sta_YELLOW);
- rt_pin_write(LED_BLUE,Sta_BLUE);
- rt_pin_write(LED_PURPLE,Sta_PURPLE);
- rt_pin_write(LED_GREEN,Sta_GREEN);
- }
- //震动电机
- 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_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_thread_delay(50);
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_thread_delay(20);
- if(rt_pin_read(CLEAR_R_LIMIT)==0)
- {
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- break;
- }
- else {
- 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);
- }
- //清扫-常闭型限位开关
- //新结构使用推杆电机做清扫
- 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_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- rt_pin_write(POWER_CLEAR,ON);
- rt_thread_delay(1000*100);
- rt_pin_write(POWER_CLEAR,OFF);
- ************************************************************/
- //停止
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- return 0;
- }
- clear_num = 0;
- rt_thread_delay(3000);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- 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);
- if(device_status.version_type==6)
- {
- rt_pin_write(POWER_CLEAR,ON);
- }
- //rt_pin_write(POWER_CLEAR,ON);
- }
- else {
- rt_pin_write(ROUTER,OFF);
- if(device_status.version_type==6)
- rt_pin_write(POWER_CLEAR,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,ON);
- rt_pin_write(CLEAR_L,ON);
- rt_thread_delay(3000);
- 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,ON);
- rt_pin_write(CLEAR_L,ON);
- rt_thread_delay(3000);
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- 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_kprintf("RTU电源复位!!!\n");
- 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_cangmen_up(OFF);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"上仓门电机已停止!!!\"");
- rt_thread_delay(1000);
- }
- //关闭下仓门电机
- ctr_cangmen_down(OFF);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"下仓门电机已停止!!!\"");
- rt_thread_delay(1000);
- }
- //关闭镇流器
- //ctr_lamp(OFF);
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_OFF, GREEN_OFF);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"镇流器已关闭!!!\"");
- rt_thread_delay(1000);
- }
- //关闭震动电机
- ctr_shake(OFF,0);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"震动电机已关闭!!!\"");
- rt_thread_delay(1000);
- }
- //关闭清扫电机
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"清扫电机已关闭!!!\"");
- rt_thread_delay(1000);
- }
- //关闭加热
- ctr_Heat(OFF);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"加热带已关闭!!!\"");
- rt_thread_delay(1000);
- }
- //关闭补光灯
- ctr_led(OFF);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"补光灯已关闭!!!\"");
- rt_thread_delay(1000);
- }
- //关闭12V供电(海康相机,RTU)
- ctr_photo(OFF);
- if(boot==1)
- {
- HMI_WRITE("hardcheck.checktip.txt=\"12V供电已关闭!!!\"");
- rt_thread_delay(1000);
- }
- }
- //拍照
- 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;
- device_status.work_status=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;
- }
- //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(350);
- }
- 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))
- {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中!!!\n");
- device_status.work_status = 1;
- }
- //如果当前时间小于开始时间
- 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;
- device_status.work_status = 1;
- 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");
- device_status.work_status = 1;
- }
- 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);
- }
- //工作状态检测-LED
- void WorkCheck_LED(void)
- {
- while(device_status.temp_status==0&&device_status.bat_status==0&&device_status.rain_status==0&&device_status.ds==1)
- {
- time_t now;
- time(&now);
- static struct tm nowtime; //现在
- static struct tm start_time; //阶段1:17:30-18:30
- static struct tm end_time_1; //阶段1:17:30-18:30
- static struct tm end_time_2; //阶段2:18:30-19:30
- static struct tm end_time_3; //阶段3:19:30-20:30
- static struct tm end_time_4; //阶段3:20:30-22:30
- static struct tm end_time_5; //阶段3:22:30-01:30
- static struct tm end_time_6; //阶段3:01:30-05:30
- static struct tm end_time_7; //阶段3:05:30-06:30
- //获取当前时间戳
- nowtime =*localtime(&now);
- nowtime.tm_hour = nowtime.tm_hour-8;
- //获取阶段1的开始时间戳
- start_time = *localtime(&now);
- start_time.tm_hour = 17-8;
- start_time.tm_min = 30;
- start_time.tm_sec = 0;
- //获取阶段1的结束时间戳
- end_time_1 = *localtime(&now);
- end_time_1.tm_hour = 18-8;
- end_time_1.tm_min = 30;
- end_time_1.tm_sec = 0;
- //获取阶段2的结束时间戳
- end_time_2 = *localtime(&now);
- end_time_2.tm_hour = 19-8;
- end_time_2.tm_min = 30;
- end_time_2.tm_sec = 0;
- //获取阶段3的结束时间
- end_time_3 = *localtime(&now);
- end_time_3.tm_hour = 20-8;
- end_time_3.tm_min = 30;
- end_time_3.tm_sec = 0;
- //获取阶段4的结束时间戳
- end_time_4 = *localtime(&now);
- end_time_4.tm_hour = 22 - 8;
- end_time_4.tm_min = 30;
- end_time_4.tm_sec = 0;
- //获取阶段5的结束时间戳
- end_time_5 = *localtime(&now);
- end_time_5.tm_hour = 1 - 8;
- end_time_5.tm_min = 30;
- end_time_5.tm_sec = 0;
- //获取阶段6的结束时间戳
- end_time_6 = *localtime(&now);
- end_time_6.tm_hour = 5 - 8;
- end_time_6.tm_min = 30;
- end_time_6.tm_sec = 0;
- //获取阶段6的结束时间戳
- end_time_7 = *localtime(&now);
- end_time_7.tm_hour = 6 - 8;
- end_time_7.tm_min = 30;
- end_time_7.tm_sec = 0;
- rt_kprintf("阶段1:现在时间:%d-开始时间1:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&start_time),mktime(&end_time_1));
- rt_kprintf("阶段2:现在时间:%d-开始时间2:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&end_time_1),mktime(&end_time_2));
- rt_kprintf("阶段3:现在时间:%d-开始时间3:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&end_time_2),mktime(&end_time_3));
- rt_kprintf("阶段4:现在时间:%d-开始时间4:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&end_time_3),mktime(&end_time_4));
- rt_kprintf("阶段5:现在时间:%d-开始时间5:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&end_time_4),mktime(&end_time_5));
- rt_kprintf("阶段6:现在时间:%d-开始时间6:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&end_time_5),mktime(&end_time_6));
- rt_kprintf("阶段7:现在时间:%d-开始时间7:%d-结束时间1:%d***\n",mktime(&nowtime),mktime(&end_time_6),mktime(&end_time_7));
- //如果当前时间大于开始时间且小于结束时间,且没有开始工作
- if((difftime(mktime(&nowtime), mktime(&start_time)) >=0) && (difftime(mktime(&nowtime),mktime(&end_time_1))<0))
- {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,亮紫灯!!!\n");
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_ON, GREEN_OFF);
- device_status.work_status = 1;
- }
- else if ((difftime(mktime(&nowtime), mktime(&end_time_1)) >=0) && (difftime(mktime(&nowtime),mktime(&end_time_2))<0)) {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,黄绿光灯打开,蓝紫光灯关!!!\n");
- ctr_LED(BLUE_OFF, YELLOW_ON, PURPLE_OFF, GREEN_ON);
- device_status.work_status = 1;
- }
- else if ((difftime(mktime(&nowtime), mktime(&end_time_2)) >=0) && (difftime(mktime(&nowtime),mktime(&end_time_3))<0)) {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,紫光灯打开,黄绿蓝光灯关!!!\n");
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_ON, GREEN_OFF);
- device_status.work_status = 1;
- }
- else if ((difftime(mktime(&nowtime), mktime(&end_time_3)) >=0) && (difftime(mktime(&nowtime),mktime(&end_time_4))<0)) {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,蓝光灯打开,黄绿紫光灯关!!!\n");
- ctr_LED(BLUE_ON, YELLOW_OFF, PURPLE_OFF, GREEN_OFF);
- device_status.work_status = 1;
- }
- else if ((difftime(mktime(&nowtime), mktime(&end_time_4)) >=0)) {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,紫光灯打开,黄绿蓝光灯关!!!\n");
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_ON, GREEN_OFF);
- device_status.work_status = 1;
- }
- else if ((difftime(mktime(&nowtime),mktime(&end_time_4))<0)&&(difftime(mktime(&nowtime),mktime(&end_time_5)) < 0)) {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,紫光灯打开,黄绿蓝光灯关!!!\n");
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_ON, GREEN_OFF);
- device_status.work_status = 1;
- }
- else if ((difftime(mktime(&nowtime), mktime(&end_time_5)) >=0) && (difftime(mktime(&nowtime),mktime(&end_time_6))<0)) {
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_OFF, GREEN_OFF);
- device_status.work_status = 0;
- rt_kprintf("全部关灯,节约用电!!!\n");
- }
- else if ((difftime(mktime(&nowtime), mktime(&end_time_6)) >=0) && (difftime(mktime(&nowtime),mktime(&end_time_7))<0)) {
- rt_kprintf("当前时间大于开始时间,小于结束时间,工作中,黄绿灯打开,紫蓝光灯关!!!\n");
- ctr_LED(BLUE_OFF, YELLOW_ON, PURPLE_OFF, GREEN_ON);
- device_status.work_status = 1;
- }
- else {
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_OFF, GREEN_OFF);
- device_status.work_status = 0;
- rt_kprintf("全部关灯,进入充电程序!!!\n");
- }
- 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");
- rt_thread_delay(5000); //待灯管启动再往下执行,否则会触发电机过流
- //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_LED(BLUE_OFF, YELLOW_OFF, PURPLE_OFF, GREEN_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);
- ctr_LED(BLUE_OFF, YELLOW_OFF, PURPLE_OFF, GREEN_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);
- //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(3000);
- //关闭下仓门
- 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(350);
- //清扫失效则使用震动
- //ctr_shake(ON, 100);
- }
- }
- 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_LED();
- rt_thread_delay(1);
- }
- }
- /*入口函数-工作流程*/
- static void Work_Loop_thread_entry(void *parameter)
- {
- rt_thread_delay(1000);
- ResetInit();
- while(1)
- {
- WorkTast();
- 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;
- }
- 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>);
|