| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535 |
- #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"
- #include "dtu_uart2.h"
- #include "sht30_collect.h"
- #include <user_mb_app.h>
- #include <selfcheck.h>
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_TAG "control"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
- //工作检测
- #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 2048 //栈大小
- #define WORK_LOOP_THREAD_TIMESLICE 5 //时间片
- #define PWM_EN 1 //0关闭,1-打开
- //设备状态表结构体
- Device_Status_typedef device_status;
- //定时器
- rt_timer_t timer_shibie = RT_NULL;
- rt_timer_t timer_motor = RT_NULL;
- rt_timer_t timer_idle = RT_NULL;
- //线程
- rt_thread_t tid_workloop = RT_NULL;
- rt_thread_t tid_heat = RT_NULL;
- rt_thread_t tid_485_play = RT_NULL;
- rt_event_t event_485_play;
- extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
- /***************************************************************************/
- int thread_delete_workloop(void)
- {
- rt_err_t result;
- if(tid_workloop!=RT_NULL)
- {
- result = rt_thread_delete(tid_workloop);
- }
- return result;
- }
- int thread_restart_workloop(void)
- {
- rt_err_t result;
- result = Work_Loop_Task();
- return result;
- }
- /**************************1.0定制2000W*************************************/
- /****************************5.0***********************************/
- void ctr_turnv5(void)
- {
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- // 转出
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- //转入
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- rt_thread_mdelay(300);
- //停止
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- rt_thread_mdelay(300);
- }
- /*************************吸虫塔*********************************/
- //吸虫电机
- uint8_t ctr_Motor_xichong(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR1_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR1_CTR_PIN, OFF);
- }
- return 0;
- }
- uint8_t ctr_Camera_Power(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR1_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR1_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR1_CTR_PIN, OFF);
- }
- return 0;
- }
- //吸附电机
- uint8_t ctr_Motor_xifu(uint8_t status_ws)
- {
- rt_pin_mode(MOTOR2_CTR_PIN, PIN_MODE_OUTPUT);
- if(status_ws==ON)
- {
- rt_pin_write(MOTOR2_CTR_PIN,ON);
- }
- else {
- rt_pin_write(MOTOR2_CTR_PIN, OFF);
- }
- return 0;
- }
- //清扫-卷带电机
- void ctr_JD_Clear(uint8_t shake_sw)
- {
- rt_uint16_t turn_timeout = 0;
- rt_pin_mode(MOTOR_C_LIMIT,PIN_MODE_INPUT);
- ctr_cangmen_up(OFF); //清扫传送带的电机
- if(shake_sw==ON)
- {
- ctr_Heat(ON); //清洁风机
- Motor_shake(ON, 0);
- ctr_cangmen_up(OPEN); //打开清扫
- }
- else {
- Motor_shake(OFF, 0);
- ctr_cangmen_up(OPEN); //打开清扫
- }
- while(rt_pin_read(MOTOR_C_LIMIT)==0)
- {
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==1000) break;
- }
- turn_timeout = 0;
- while(rt_pin_read(MOTOR_C_LIMIT)==1)
- {
- Motor_Juandai(ON);
- turn_timeout++;
- rt_thread_delay(50);
- if(turn_timeout==1000) break;
- }
- Motor_shake(OFF, 1);
- Motor_Juandai(OFF);
- ctr_cangmen_up(OFF); //关闭清扫
- ctr_Heat(OFF);
- }
- /**********************************************************************/
- /*光控*/
- 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)
- {
- LOG_I("操作加热:%d",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;
- }
- /*制冷*/
- uint8_t ctr_Cold(uint8_t status_cold)
- {
- device_status.cold_status = status_cold;
- rt_pin_mode(Cold_Pin, PIN_MODE_OUTPUT);
- rt_pin_write(Cold_Pin,status_cold);
- return status_cold;
- }
- /*相机供电*/
- void ctr_photo(uint8_t status_photo)
- {
- rt_pin_mode(PHOTO,PIN_MODE_OUTPUT);
- rt_pin_write(PHOTO,status_photo);
- }
- /*************************************************
- Function: ctr_Cool_Fan
- Description: 散热风机的开关
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: ON-开,OFF-关
- Output: NULL
- Return: ON-开,OFF-关
- Others: NULL
- *************************************************/
- uint8_t ctr_Cool_Fan(uint8_t status_cool)
- {
- device_status.cool_fan_status = status_cool;
- rt_pin_mode(Cool_Fan_L, PIN_MODE_OUTPUT);
- rt_pin_mode(Cool_Fan_R, PIN_MODE_OUTPUT);
- if(status_cool==OFF)
- {
- rt_pin_write(Cool_Fan_L,OFF);
- rt_pin_write(Cool_Fan_R,OFF);
- }
- if(status_cool==ON)
- {
- rt_pin_write(Cool_Fan_L,ON);
- rt_pin_write(Cool_Fan_R,OFF);
- }
- return device_status.cool_fan_status;
- }
- /*************************************************
- Function: ctr_Collect_Fan
- Description: 采集风机的开关
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: ON-开,OFF-关
- Output: NULL
- Return: ON-开,OFF-关
- Others: NULL
- *************************************************/
- uint8_t ctr_Collect_Fan(uint8_t status_collect)
- {
- LOG_I("操作风机:%d",status_collect);
- device_status.collect_fan_status = status_collect;
- rt_pin_mode(Collect_Fan, PIN_MODE_OUTPUT);
- if(status_collect==OFF)
- {
- rt_pin_write(Collect_Fan,OFF);
- }
- if(status_collect==ON)
- {
- rt_pin_write(Collect_Fan,ON);
- }
- return device_status.collect_fan_status;
- }
- /*************************************************
- Function: ctr_develop
- Description: 培养载玻片孢子
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: v=0(复位) v=90转90度
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- /*上仓门*/
- 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,uint8_t lamp_num)
- {
- rt_pin_mode(LAMP_SW, PIN_MODE_OUTPUT);
- if(status_lamp==ON)
- {
- switch(lamp_num){
- case 1:
- rt_pin_write(LAMP_SW,ON);
- device_status.lamp_status = status_lamp;
- break;
- default:
- break;
- }
- }
- else {
- switch(lamp_num){
- case 1:
- rt_pin_write(LAMP_SW,OFF);
- device_status.lamp_status = status_lamp;
- break;
- default:
- break;
- }
- }
- HMI_WRITE("sysTest.switchLamp.val=%d",device_status.lamp_status);
- return device_status.lamp_status;
- }
- //震动电机
- uint8_t ctr_shake(uint8_t status_shake,uint16_t shake_sec)
- {
- static rt_uint16_t shake_i = 0;
- shake_i = atoi(device_status.power);
- LOG_I("震动电机待机电流:%d",shake_i);
- if(device_status.dtype==7&&shake_sec!=0)
- DTU_Write("drop");
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- #if 0
- rt_pin_write(SHAKE_SW,ON);
- return 0;
- #endif
- if(status_shake==ON&&device_status.shake_ws==1)
- {
- #if 0
- for(int8_t s = 0;s++;s<2)
- {
- rt_pin_write(SHAKE_SW,ON);
- rt_thread_delay(100);
- rt_pin_write(SHAKE_SW,OFF);
- rt_thread_delay(1000);
- }
- #endif
- rt_pin_write(SHAKE_SW,ON);
- if(device_status.dtype==3)
- {
- while(shake_sec--)
- {
- rt_thread_delay(100);
- if(shake_sec==20&&device_status.clear_status==0&&device_status.work_status==1&&atoi(device_status.power)-shake_i<100&&shake_i!=0&&shake_i<5000)
- {
- char * shake_warn = makeJson_Warn("电流不足", "震动电机");
- DTU_Write("%s",shake_warn);
- rt_free(shake_warn);
- }
- LOG_D("震动电机:%d",atoi(device_status.power));
- }
- }
- if(device_status.dtype==7) rt_thread_delay(shake_sec*1000);
- 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);
- }
- if(device_status.dtype==7)
- HMI_WRITE("sysTest.switchDrop.val=%d",OFF);
- return status_shake;
- }
- //补光
- uint8_t ctr_led(uint8_t status_led)
- {
- rt_pin_mode(LED_SW, PIN_MODE_OUTPUT);
- if(device_status.version_type==6)
- rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
- if(status_led==ON)
- {
- rt_pin_write(LED_SW,ON);
- if(device_status.version_type==6)
- rt_pin_write(LED_P,ON);
- HMI_WRITE("sysTest.switchFill.val=%d",ON);
- }
- else if(status_led==OFF){
- rt_pin_write(LED_SW,OFF);
- if(device_status.version_type==6)
- rt_pin_write(LED_P,OFF);
- HMI_WRITE("sysTest.switchPhoto.val=%d",OFF);
- HMI_WRITE("sysTest.switchFill.val=%d",OFF);
- }
- return status_led;
- }
- /*
- *
- * */
- void ctr_Motor_Driver_Power(uint8_t status_ws)
- {
- rt_pin_mode(SHAKE_SW, PIN_MODE_OUTPUT);
- rt_pin_write(SHAKE_SW, status_ws);
- }
- /*************************************************
- Function: ctr_Motor_Driver
- Description: 步进电机驱动
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: 50000000
- Output: NULL
- Return: shake_sec
- Others: NULL
- *************************************************/
- void ctr_Motor_Driver(uint8_t EN,uint8_t DIR,rt_uint32_t pwm,rt_uint32_t time_out)
- {
- beep_init();
- rt_pin_mode(LIMIT_UP, PIN_MODE_INPUT);
- rt_pin_mode(LIMIT_DOWN, PIN_MODE_INPUT);
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
- rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
- if(EN==0)
- {
- rt_pin_write(Motor_EN, EN);
- }
- rt_pin_write(Motor_DIR, DIR);
- beep_set(pwm, pwm/2); //200kzh - 50%
- while(DIR==1&&rt_pin_read(LIMIT_DOWN)==1)
- {
- if(time_out==0)
- {
- break;
- }
- rt_thread_delay(50);
- time_out --;
- }
- while(DIR==0&&rt_pin_read(LIMIT_UP)==1)
- {
- if(time_out==0)
- {
- break;
- }
- rt_thread_delay(50);
- time_out --;
- }
- //rt_pin_write(Motor_EN, OFF);
- //if(device_status.autotakepic==0)
- //ctr_lamp(OFF, 1);
- beep_set(0, 0); //200kzh - 50%
- rt_thread_delay(50);
- }
- /*************************************************
- Function: ctr_Petroleum_jelly
- Description: 涂凡士林
- Calls:
- Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序)
- Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
- Input: NULL
- Output: NULL
- Return: NULL
- Others: NULL
- *************************************************/
- void ctr_Petroleum_jelly(rt_uint32_t delay)
- {
- beep_init();
- pwm_on(2,50000);
- rt_thread_delay(delay*1000);
- pwm_off(2,0);
- rt_thread_delay(1000);
- pwm_on(3, 50000);
- rt_thread_delay(delay*1000);
- pwm_off(3,0);
- device_status.fsl_sw = 0;
- }
- //1.0转仓
- 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);
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==600)
- {
- LOG_E("检测不到限位,电机强制停止");
- char *ctr_zp_limit_warn =makeJson_Warn("电流不足/限位超时","转盘电机");
- DTU_Write("%s",ctr_zp_limit_warn);
- rt_free(ctr_zp_limit_warn);
- break;
- }
- }
- break;
- }
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- rt_thread_delay(20);
- while(rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- // rt_pin_write(CLEAR_R,ON);
- // rt_pin_write(CLEAR_L,OFF);
- Motor_Juandai(ON);
- rt_thread_delay(50);
- turn_timeout++;
- if(turn_timeout==600)
- {
- LOG_E("检测不到离开限位,电机或者限位开关异常,电机强制停止");
- char *ctr_zp_limit_warn =makeJson_Warn("检测不到离开限位,电机未启动或者限位开关故障","转盘电机");
- DTU_Write("%s",ctr_zp_limit_warn);
- rt_free(ctr_zp_limit_warn);
- 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);
- Motor_Juandai(OFF);
- break;
- }
- else {
- break;
- }
- }
- Motor_Juandai(OFF);
- // 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;
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- #endif
- 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);
- ************************************************************/
- //停止
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(50);
- clear_num++;
- //已到达检测位置
- if(rt_pin_read(CLEAR_L_LIMIT)==1||rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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"));
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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"));
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- 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);
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(2000);
- break;
- }
- while(rt_pin_read(CLEAR_L_LIMIT)==1&&rt_pin_read(CLEAR_R_LIMIT)==1)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- if(device_status.version_type==41)
- {
- 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);
- }
- return 0;
- }
- clear_num = 0;
- rt_thread_delay(2000);
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- 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);
- //rt_pin_mode(POWER_CLEAR, PIN_MODE_OUTPUT);
- if(status_router==ON)
- {
- rt_pin_write(ROUTER,ON);
- }
- else {
- rt_pin_write(ROUTER,OFF);
- }
- }
- //清扫-推杆电机
- void ctr_TG_Clear(uint16_t clear_timeout)
- {
- #if 0
- int current_clear[200];
- int16_t Electric_current = atoi(device_status.power); //获取电机运动前的电流
- LOG_I("清扫电机待机:%d",Electric_current);
- #endif
- static uint16_t clear_count = 0;
- clear_count = clear_timeout;
- #if PWM_EN == 0
- rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT);
- rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT);
- #endif
- while(clear_count!=0)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,OFF);
- #else
- Motor_Qingsao_R_PWM(ON);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- clear_count--;
- rt_thread_delay(100);
- }
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- rt_thread_delay(3000);
- clear_count = 0;
- while(clear_timeout!=clear_count)
- {
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,OFF);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(ON);
- #endif
- // if(clear_count<200)
- // current_clear[clear_count] = atoi(device_status.power);
- clear_count++;
- rt_thread_delay(100);
- }
- #if PWM_EN == 0
- rt_pin_write(CLEAR_R,ON);
- rt_pin_write(CLEAR_L,ON);
- #else
- Motor_Qingsao_R_PWM(OFF);
- Motor_Qingsao_L_PWM(OFF);
- #endif
- #if 0
- bubblesort(current_clear,200);
- if(current_clear[199]-Electric_current <100&&device_status.work_status==1&&Electric_current!=0&&Electric_current<5000)
- {
- LOG_E("清扫电机电流不足:%d!",current_clear[199]);
- char *ctr_clear_warn = makeJson_Warn("电流不足","清扫电机");
- DTU_Write("%s",ctr_clear_warn);
- rt_free(ctr_clear_warn);
- }
- #endif
- rt_thread_delay(2000);
- device_status.clear_status = 0;
- 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
- {
- LOG_W("RTU电源复位!!!");
- rt_pin_write(RTU_DTU,OFF);
- }
- }
- //海康拍照
- void Hk_Take_io(void)
- {
- rt_pin_mode(HK_TAKE, PIN_MODE_OUTPUT);
- rt_pin_mode(HK_IO, PIN_MODE_OUTPUT);
- rt_pin_write(HK_TAKE, 1);
- rt_pin_write(HK_IO, 0);
- if(device_status.dtype==3)
- Beep_Tip(1000);
- if(device_status.dtype==7)
- rt_thread_mdelay(1000);
- rt_pin_write(HK_TAKE, 0);
- rt_pin_write(HK_IO, 1);
- }
- //拍照
- void HK_TakePhoto(void)
- {
- //打开补光灯
- //DTU_Write("takephoto.mp3");
- rt_thread_delay(10000);
- rt_timer_start(timer_takephoto);
- char *takephotojson = makeJson_takephoto();
- DTU_Write("%s",takephotojson);
- rt_free(takephotojson);
- //吸虫塔
- if(device_status.version_type!=6)
- {
- ctr_led(ON);
- if(device_status.version_type==48)
- {
- rt_thread_delay(1000*60);
- }
- else {
- rt_thread_delay(1000*15);
- }
- }
- //触发海康相机拍照
- Hk_Take_io();
- if(device_status.version_type!=6)
- {
- rt_thread_delay(1000*45);
- }
- else {
- ctr_led(ON);
- rt_thread_delay(1000*19);
- }
- ctr_led(OFF);
- }
- //光控测试任务
- void SunTest(void)
- {
- rt_uint16_t ui_timeout_l = 0;
- //未检测到光照,一秒扫描等待,超时30S
- while(Read_Light_Voltage()<500)
- {
- ui_timeout_l ++;
- rt_thread_delay(1000);
- if(ui_timeout_l==30) break;
- }
- 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,1);
- }
- HMI_WRITE("sysTest.switchSun.val=%d",0);
- device_status.sun_test = 0;
- device_status.work_status=0;
- }
- /*上电初始化*/
- void ResetInit(void)
- {
- if(device_status.version_type==5)
- {
- ctr_lamp(OFF, 1); //镇流器
- ctr_led(OFF); //补光灯关闭
- ctr_Heat(OFF); //风机关闭
- ctr_Motor_Driver_Power(ON); //步进电机驱动电源开启
- ctr_Motor86_move(0, 500000, 0); //86电机复位
- device_status.ctr_up_motor = 2; //推杆电机缩进去(落虫)
- ctr_Collect_Fan(OFF);
- PWM2_CH3_PB10_Fan_LCTD(OFF);
- ctr_Motor_Driver_Power(OFF);
- }
- if(device_status.power_status==1)
- {
- //1.打开路由器供电
- ctr_router(ON);
- ctr_RTU_DTU(ON);
- //2.打开相机供电
- ctr_photo(ON);
- }
- }
- void WorkCheck(void)
- {
- static rt_uint32_t light_timeout = 3600;
- while(device_status.ds == 1 &&
- device_status.rain_status == 0 &&
- device_status.temp_status == 0 &&
- device_status.bat_status == 0)
- {
- if(device_status.timeSwitch == 1) // 时控模式
- {
- LOG_I("***********时控*****************");
- time_t now;
- time(&now);
- struct tm now_tm = *localtime(&now);
- if(device_status.startBar == device_status.stopBar)
- {
- // 开始结束时间相同则24小时模式
- device_status.work_status = 1;
- rt_thread_delay(10000);
- LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
- device_status.work_status ? "工作" : "待机",
- now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
- device_status.startBar, device_status.stopBar);
- continue;
- }
- rt_uint32_t curr_sec = now_tm.tm_hour*3600 + now_tm.tm_min*60 + now_tm.tm_sec;
- rt_uint32_t start_sec = device_status.startBar * 3600;
- rt_uint32_t end_sec = device_status.stopBar * 3600;
- // 修复跨天判断逻辑
- if (start_sec < end_sec)
- {
- // 不跨天模式
- device_status.work_status = (curr_sec >= start_sec && curr_sec < end_sec) ? 1 : 0;
- }
- else
- {
- // 跨天模式
- device_status.work_status = (curr_sec >= start_sec || curr_sec < end_sec) ? 1 : 0;
- }
- LOG_I("时控模式:状态[%s] 当前时间:%02d:%02d:%02d 设置:%02d:00-%02d:00",
- device_status.work_status ? "工作" : "待机",
- now_tm.tm_hour, now_tm.tm_min, now_tm.tm_sec,
- device_status.startBar, device_status.stopBar);
- rt_thread_delay(10000);
- }
- else // 光控模式
- {
- LOG_I("光控定时模式!");
- if(device_status.sun_status == 0 && device_status.light_over_tim != 0)
- {
- device_status.light_over_tim--;
- light_timeout = 3600; // 重置超时计数器
- device_status.work_status = 1;
- LOG_I("光控定时,计时中>>>>>>>>>:%d",device_status.light_over_tim);
- }
- else
- {
- // 仅在白天递减超时计数器
- if(device_status.sun_status == 1 && light_timeout > 0) {
- light_timeout--;
- }
- device_status.work_status = 0;
- //没有在测试并且没有在自检
- if(device_status.test_flag==0 && device_status.selfcheck_ws==0)
- {
- ctr_lamp(OFF,1);
- }
- // 连续1小时白天后重置
- if(light_timeout == 0) {
- device_status.light_over_tim = 0;
- light_timeout = 3600; // 重置防止负数
- }
- // 白天且倒计时结束:重置夜晚时长
- if(device_status.light_over_tim == 0 &&
- device_status.sun_status == 1)
- {
- device_status.light_over_tim = device_status.sunBar * 3600;
- }
- }
- }
- rt_thread_delay(1000);
- }
- // 异常状态处理
- device_status.work_status = 0;
- rt_thread_delay(1000);
- }
- void guanshan_on(void *args)
- {
- LOG_W("guanshan_on!");
- rt_timer_start(timer_shibie);
- rt_timer_stop(timer_shibie);
- rt_event_send(event_485_play, 0x01);
- }
- void pin_guangshan_bind(void)
- {
- LOG_W("pin_guangshan_bind");
- /* PC14引脚为输入模式 */
- rt_pin_mode(GUANG_SHAN, PIN_MODE_INPUT);
- /* 绑定中断,下降沿模式,回调函数名为guanshan_on */
- rt_pin_attach_irq(GUANG_SHAN, PIN_IRQ_MODE_RISING, guanshan_on, RT_NULL);
- /* 使能中断 */
- rt_pin_irq_enable(GUANG_SHAN, PIN_IRQ_ENABLE);
- }
- void pin_guangshan_unbind(void)
- {
- LOG_D("pin_guangshan_unbind");
- rt_pin_detach_irq(GUANG_SHAN);
- }
- #define PULES 1 //延时时间 ms 5.0测报灯电机归零延时使用
- void slidelock()
- {
- ctr_Motor_Driver_Power(ON); //电机驱动供电
- rt_thread_delay(100);
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
- rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
- }
- void motor_zero()
- {
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
- rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
- rt_pin_mode(CTR_RO_LIMIT, PIN_MODE_OUTPUT); //CCW PB15 配置为输出模式
- rt_pin_mode(CTR_RC_LIMIT, PIN_MODE_OUTPUT); //CW PC6 配置为输出模式
- rt_thread_delay(300);
- rt_thread_delay(300);
- rt_pin_mode(Motor_Direction_Button, PIN_MODE_INPUT); //GET_PIN(B,8)
- rt_pin_write(CTR_RO_LIMIT, OFF); //CCW back配置
- uint32_t j;
- for(j=0;j<3200;j++) //多配置1600个脉冲,保证肯定能归零;
- {
- rt_pin_write(CTR_RC_LIMIT, OFF);
- rt_thread_delay(PULES);
- rt_pin_write(CTR_RC_LIMIT, ON);
- rt_thread_delay(PULES);
- if( rt_pin_read(Motor_Direction_Button)==0)//判断得到限位信号
- {
- rt_thread_delay(5);
- if( rt_pin_read(Motor_Direction_Button)==0)
- {
- slidelock();
- break;
- }
- }
- }
- rt_thread_delay(300);
- }
- uint16_t ctr_Motor86_move(uint32_t xdis,uint32_t pulse,uint8_t dir)
- {
- //rt_kprintf("run here1");
- LOG_W("ctr_Motor86_move:%d-%d-%d",xdis,pulse,dir); //两个脉冲移动0.72度,轴方向上移动2um
- uint32_t limit_tm= 0;
- rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT); //使能电机驱动引脚 PB14
- rt_pin_write(Motor_EN, ON); //引脚输出高使能有效
- rt_pin_mode(CTR_RO_LIMIT, PIN_MODE_OUTPUT); //CCW PB15 配置为输出模式
- rt_pin_write(CTR_RO_LIMIT, dir); //CCW 置高
- rt_thread_delay(50);
- pwm_motor86_enable(pulse);
- rt_thread_delay(50);
- rt_timer_start(timer_motor);
- //启动定时器
- return xdis;
- }
- /*
- * 灯管
- *
- *
- * */
- void WorkTask_CB5(void)
- {
- static uint8_t guangshan_hand = 0;
- static int work_loop = 0;
- 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)
- {
- work_loop++;
- device_status.work_done = 0;
- //发送串口消息,通知yolo是被及相机采集模块启动,定期将模块的运行状态展示到屏幕上
- if(work_loop%60==0||work_loop==1)
- {
- rt_timer_start(timer_idle);
- //推杆电机1打开,导流板到落虫通道
- device_status.ctr_up_motor = 1;
- while(device_status.ctr_up_motor!=0)
- {
- rt_thread_delay(1000);
- LOG_I("等待上仓门打开....");
- }
- //打开86步进电机驱动器供电
- ctr_Motor_Driver_Power(ON);
- //风机启动
- rt_thread_delay(1000);
- //打开灯管
- ctr_lamp(ON,1);
- DTU_Write("{\"cmd\":\"workstart\"}");
- //打开补光灯
- ctr_led(ON);
- rt_thread_delay(1000);
- //光栅启动
- ctr_photo(ON);
- rt_thread_delay(1000);
- //上风机启动
- //PWM2_CH3_PB10_Fan_LCTD(ON);
- ctr_Collect_Fan(ON);
- rt_thread_delay(1000);
- //下风机启动
- ctr_Heat(ON);
- rt_thread_delay(1000);
- //光栅检测启动
- pin_guangshan_bind();
- //电机限位检测启动
- pin_motor_86_bind();
- rt_thread_delay(1000);
- while(rt_pin_read(GUANG_SHAN)==0)
- {
- guangshan_hand++;
- rt_thread_delay(100);
- if(guangshan_hand>5) break;
- }
- if(guangshan_hand>5)
- {
- guangshan_hand = 0;
- device_status.autotakepic = 11;
- }
- }
- rt_thread_delay(1000);
- }
- device_status.work_status = 0;
- //工作结束
- if(device_status.work_done==0&&device_status.work_status==0)
- {
- pin_guangshan_unbind();
- pin_motor_86_unbind();
- //打开推杆电机,益虫放生
- DTU_Write("{\"cmd\":\"workdone\"}");
- ResetInit();
- rt_timer_stop(timer_idle);
- }
- work_loop = 0;
- while(device_status.work_status==0)
- {
- work_loop++;
- LOG_I("WorkTask_CB5 sleeping......%d",work_loop);
- if(work_loop>120)
- {
- work_loop = 0;
- DTU_Write("{\"cmd\":\"workdone\"}");
- }
- rt_thread_delay(5000);
- }
- }
- /* 入口函数-工作状态检测 */
- static void Work_status_thread_entry(void *parameter)
- {
- static uint16_t sun_status_num = 120;
- //如果是光控定时,进行光控监测的消抖
- 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);
- }
- device_status.light_over_tim = device_status.sunBar * 3600;
- device_status.test_flag = 0;
- }
- while(1)
- {
- WorkCheck();
- }
- }
- /*入口函数-工作流程*/
- static void Work_Loop_thread_entry(void *parameter)
- {
- rt_thread_delay(1000);
- ResetInit();
- while(1)
- {
- WorkTask_CB5();
- rt_thread_delay(1000);
- }
- }
- /*入口函数清扫*/
- static void Control_Clear_thread_entry(void *parameter)
- {
- while(1)
- {
- if(device_status.update_flag==1) break;
- rt_thread_delay(100);
- }
- }
- /*入口函数,自检*/
- static void Control_Check_thread_entry(void *parameter)
- {
- static int total_seconds = 200;
- static int current_second = 0;
- static int process = 0;
- #if MATCH_DEVICE == 1
- rt_pin_mode(LED_P, PIN_MODE_OUTPUT);
- rt_pin_write(LED_P,ON);
- #endif
- device_status.C01 = 0;
- rt_thread_delay(10000);
- while(1)
- {
- if(device_status.update_flag==1) break;
- if(device_status.version_type==5) break;
- if(device_status.selfcheck_ws==2)
- {
- while(current_second <= total_seconds)
- {
- process = (current_second * 100) / total_seconds;
- device_status.process = process / 100.0;
- LOG_I("process:%d",process);
- HMI_WRITE("selfcheck.s106.val=%d",process);
- rt_thread_delay(1000);
- current_second++;
- if(device_status.selfcheck_ws==0)
- {
- HMI_WRITE("selfcheck.s106.val=100");
- break;
- }
- }
- HMI_WRITE("selfcheck.s106.val=100");
- }else {
- process = 0;
- current_second = 0;
- }
- rt_thread_delay(1000);
- }
- }
- static void thread_485_play_entry(void *param)
- {
- static USHORT sound105[] = {0x736F,0x756E,0x6431,0x3037};
- rt_uint32_t e;
- // 创建事件控制块
- event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
- if (event_485_play == RT_NULL)
- {
- LOG_E("Event create failed!");
- return;
- }
- while(1)
- {
- if(device_status.update_flag==1) break;
- //等待事件
- if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
- {
- LOG_I("Interrupt occurred and processed.");
- _485_play(sound105, 4);
- rt_thread_delay(3000);
- //发送串口消息,通知JETSON计算卡进行拍照
- DTU_Write("{\"guangshan\":\"on\"}");
- }
- }
- }
- /*清扫线程*/
- int Control_clear(void)
- {
- /* 创建线程*/
- rt_thread_t tid_ctr = rt_thread_create("Control_Clear_thread_entry",
- Control_Clear_thread_entry, RT_NULL,
- 2048, //2048
- 24, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_ctr!=RT_NULL)
- {
- rt_thread_startup(tid_ctr);
- }
- return 0;
- }
- /*恒温加热线程*/
- int Control_Check(void)
- {
- /* 创建线程*/
- tid_heat = rt_thread_create("Control_Check_thread_entry",
- Control_Check_thread_entry, RT_NULL,
- 1024, //2048
- 25, //23
- WORK_CHECK_THREAD_TIMESLICE); //5
- if(tid_heat!=RT_NULL)
- {
- rt_thread_startup(tid_heat);
- }
- return 0;
- }
- int thread_485_play(void)
- {
- tid_485_play = rt_thread_create("thread_485play",
- thread_485_play_entry, RT_NULL,
- 1024, //1024
- 26, //23
- 5); //5
- if(tid_485_play!=RT_NULL)
- {
- rt_thread_startup(tid_485_play);
- }
- else {
- LOG_E("thread_485_play start failed!");
- }
- return 0;
- }
- /* 创建线程 -工作状态检测*/
- int Workstatus_check(void)
- {
- /* 创建线程*/
- rt_thread_t tid_wkstc = rt_thread_create("work_status_check_thread",
- Work_status_thread_entry, RT_NULL,
- WORK_CHECK_THREAD_STACK_SIZE, //1024
- 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)
- {
- rt_err_t result_loop = 0;
- /* 创建线程*/
- 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)
- {
- result_loop = rt_thread_startup(tid_workloop);
- }
- return result_loop;
- }
- void timer_shibie_stop()
- {
- rt_timer_stop(timer_shibie);
- LOG_W("timer_shibie_stop!");
- }
- void timer_motor_stop()
- {
- rt_timer_stop(timer_motor);
- LOG_W("timer_motor_stop!");
- }
- static void t_shibie_callback(void *parameter)
- {
- LOG_E("识别超时!");
- device_status.autotakepic = 10;
- }
- static void t_motor_callback(void *parameter)
- {
- LOG_E("电机超时!");
- stop_motor_86();
- }
- int rt_software_timer_init(void)
- {
- //创建拍照超时定时器
- timer_shibie = rt_timer_create("t_shibie",t_shibie_callback,RT_NULL,15000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER);
- timer_idle = rt_timer_create("t_idle",guanshan_on,RT_NULL,1000*60*10, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER);
- //创建电机运行超时定时器
- timer_motor = rt_timer_create("t_motor",t_motor_callback,RT_NULL,2000, RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_HARD_TIMER); //2S
- if(timer_motor==RT_NULL||timer_shibie==RT_NULL||timer_idle==RT_NULL)
- {
- LOG_E("rt_timer_create t_timer failed!");
- return RT_ERROR;
- }
- else
- {
- return RT_EOK;
- }
- }
- INIT_APP_EXPORT(rt_software_timer_init);
- MSH_CMD_EXPORT(ctr_lamp,ctr_lamp<ON|OFF>);
- MSH_CMD_EXPORT(ctr_Heat,ctr_Heat<ON|OFF>);
- MSH_CMD_EXPORT(ctr_clear,ctr_clear<ON|OFF>);
- MSH_CMD_EXPORT(ctr_led,sample:ctr_led <ON|OFF>);
- //INIT_APP_EXPORT(Control_heat);
|