[$is_event_jimu] 2 [$group_e] 元件事件库.超声波测距模块 [$group] 元件库-传感器.$pname$ [$last_ver_update_time] 2021-04-03 [$min_need_ver] 1.23 [$p_list] [$short_show_UIname] [$code_area_type] codes,exe_per_1ms,init_code [$blocks_type] line [$name] 超声波测距模块的初始化积木 [$引脚占用列表,用于提示冲突] [$寄存器占用列表,用于提示冲突,但=>后的值没变则不提示冲突] [$中断函数号占用列表,用于提示冲突] [$interrupt_1] flag_$rnd_varname_pre$=1; [$thread_do] [$cs] [$function_ref_define_in_headfile] @@file=chaoshenbo.h extern u8 flag_$rnd_varname_pre$; //标志定时器是否溢出 char do_a_jiance_$rnd_varname_pre$(struct pt *process_pt); extern u16 resualt_$rnd_varname_pre$; u16 get_distance_$rnd_varname_pre$(); [$init_code_for_define_area_once] @@file=chaoshenbo.c #include "protothread.h" #include "app_process.h" #include "sys/timer.h" #include "Extline_Lib.h" #include "Lib_base.h" //考虑PROCESS2_WAIT_UNTIL宏 [$init_code_for_define_area] @@file=chaoshenbo.c struct timer timer_csb2_$rnd_varname_pre$; u16 resualt_$rnd_varname_pre$; u16 resualt_zong_$rnd_varname_pre$; void Delay10us_$rnd_varname_pre$() { u16 a; u8 b; for(b=1;b>0;b--) for(a=#get_10us_delay_num_stm32f10x(group_xml,rand1,block_name,ref_complier,extinfo)%;a>0;a--); } //卡尔曼滤波变量设置 float P_$rnd_varname_pre$=1; float P__$rnd_varname_pre$; //公式中的p' int X_$rnd_varname_pre$=0; float X__$rnd_varname_pre$; //公式中的X' float K_$rnd_varname_pre$=0; float Q_$rnd_varname_pre$=0.01;//噪声 //R大,更相信预测,那么传感器反应就会迟钝,反之亦然 float R_$rnd_varname_pre$=0.2;//这里0.5算有点点大 //卡尔曼滤波函数 float KLM_$rnd_varname_pre$(float Z_$rnd_varname_pre$) { X__$rnd_varname_pre$=X_$rnd_varname_pre$+0; P__$rnd_varname_pre$=P_$rnd_varname_pre$+Q_$rnd_varname_pre$; K_$rnd_varname_pre$=P__$rnd_varname_pre$/(P__$rnd_varname_pre$+R_$rnd_varname_pre$); X_$rnd_varname_pre$=X__$rnd_varname_pre$+K_$rnd_varname_pre$*(Z_$rnd_varname_pre$-X__$rnd_varname_pre$); P_$rnd_varname_pre$=P__$rnd_varname_pre$-K_$rnd_varname_pre$*P__$rnd_varname_pre$; return X_$rnd_varname_pre$; } char do_a_jiance_$rnd_varname_pre$(struct pt *process_pt){ PT_BEGIN(process_pt); IO_Write([($_xgm$tiger),type=pin] ,1) ; Delay10us_$rnd_varname_pre$(); IO_Write([($_xgm$tiger),type=pin] ,0) ; resualt_$rnd_varname_pre$=0; timer_set(&timer_csb2_$rnd_varname_pre$, 160 ); timer_restart(&timer_csb2_$rnd_varname_pre$); //加|| timer_expired(&timer_csb2_$rnd_varname_pre$)!=0来防止线程死机: PROCESS2_WAIT_UNTIL(process_pt , IO_Read([($_xgm$value3),type=pin])!=0 || timer_expired(&timer_csb2_$rnd_varname_pre$)!=0); timer_set(&timer_csb2_$rnd_varname_pre$, 250 ); timer_restart(&timer_csb2_$rnd_varname_pre$); //加|| timer_expired(&timer_csb2_$rnd_varname_pre$)!=0来防止线程死机: PROCESS2_WAIT_UNTIL(process_pt , IO_Read([($_xgm$value3),type=pin])!=1|| timer_expired(&timer_csb2_$rnd_varname_pre$)!=0); resualt_zong_$rnd_varname_pre$= (u16)KLM_$rnd_varname_pre$((((float)resualt_$rnd_varname_pre$ * 0.0001) * 34000.000) / 2.000); PT_END(process_pt); } u16 get_distance_$rnd_varname_pre$(){ return resualt_zong_$rnd_varname_pre$; //340m/s= 34000cm/s 1ms=0.001s,100us=0.0001s //return (u16)KLM_$rnd_varname_pre$((((float)resualt_$rnd_varname_pre$ * 0.0001) * 34000.001) / 2.001); } [$init_code_for_init_val] #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$value3),type=pin]","0","echo超声信号输入")% #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$tiger),type=pin]","1","超声tiger信号输出")% IO_Write([($_xgm$value3),type=pin] ,1) ; IO_Write([($_xgm$tiger),type=pin] ,0) ; //TT_init_from_jimu([($_xgm$dev_no_to_use2),type=num],[($_xgm$value3),type=pin], u8 Rising1_or_Falling2_or_all3); [$timer0_1ms_code] [$timer0_100us_code] if(IO_Read([($_xgm$value3),type=pin])==1){ resualt_$rnd_varname_pre$++; } [$toolbox_block_code] [$replace_map] $_xgm$value3 = $field$echo ,mytype|pin, ptype|TIMES $_xgm$tiger = $field$tiger ,mytype|pin, ptype|TIMES