避障機器人設計與調(diào)試講解
避障機器人設計與調(diào)試、實訓目的1 了解機器人大賽中避障的規(guī)則,進一步理解電機和紅外測距傳感器的原理;2 掌握避障機器人的設計方法。二、實訓設備1 硬件: HOST 機一臺、基于機器人項目驅(qū)動的嵌入式教學實訓平臺一套;2 軟件: WIN2000 或 xp 操作系統(tǒng)、Siliconlab IDE 開發(fā)環(huán)境、調(diào)試器。三、實訓原理實現(xiàn)避障的功能從原理上是通過分析紅外測距傳感器的測量值判斷障礙物的位置,120然后驅(qū)動電機避開障礙物。通過連接三個紅外測距傳感器,機器人可以探測到度的視角范圍的障礙物。磯耦人IT動四、實訓步驟1、正確連接PC機、調(diào)試器和基于機器人項目驅(qū)動的嵌入式教學實訓平臺;2、打開電源,打開Siliconlab IDE ;3、打開避障的例程,正確調(diào)試并運行該程序。4、燒錄完成后斷電拔掉調(diào)試器,把組裝好的機器人放入模擬的參賽場地,再次打開電 源,觀察機器人避障的情況。void main()unsigned int ad_test;unsigned int i = 0;SystemInit();while(1)DodgeObstruction();void DodgeObstruction()unsigned char ad_distance_left = 0;unsigned char ad_distance_midl = 0;unsigned char ad_distance_rigt = 0;while(1)ad_distance_left = GetIR_Distance(2);ad_distance_midl = GetIR_Distance(3);ad_distance_rigt = GetIR_Distance(4);if(ad_distance_left>40 && ad_distance_midl>40 && ad_distance_rigt>40) DC_Motor(1,0,60);DC_Motor(3,0,60);DC_Motor(2,0,60);DC_Motor(4,0,60);if(ad_distance_rigt<40)DC_Motor(1,0,60);DC_Motor(3,0,60);DC_Motor(2,2,10);DC_Motor(4,2,10);if(ad_distance_midl<40)DC_Motor(1,2,70);DC_Motor(3,2,70);DC_Motor(2,2,70);DC_Motor(4,2,70);if(ad_distance_left<40)DC_Motor(1,2,10);DC_Motor(3,2,10);DC_Motor(2,0,60);DC_Motor(4,0,60);voidDC_Motor(unsigned char motor_num,unsigned char direction, unsigned char motor_speed) unsigned char SFRPAGE_save = SFRPAGE;SFRPAGE = CONFIG_PAGE; if(!motor_speed)switch(motor_num)case 1:PCA0CPH0 = 255; break;case 2:PCA0CPH1 = 255;break;case 3:PCA0CPH2 = 255; break;case 4:PCA0CPH3 = 255; break;case 5:PCA0CPH4 = 255;break;case 6:PCA0CPH5 = 255;break; default: break;elseswitch(motor_num) case 1:PCA0CPH0 = 255 - (motor_speed+116); break;case 2:PCA0CPH1 = 255 - (motor_speed+116); break;case 3:PCA0CPH2 = 255 - (motor_speed+116); break;case 4:PCA0CPH3 = 255 - (motor_speed+116);Break;case 5:PCA0CPH4 = 255 - (motor_speed+116); break;case 6:PCA0CPH5 = 255 - (motor_speed+116);break;default: break;switch(direction)case 0:if(motor_num=1) P3 &=0x20; /P1.3 = 0,即DIR0置0if(motor_num=2) P1 &=0x10; /P1.4 = 0,即DIR1置0if(motor_num=3) P1 &=0x20; /P1.5 = 0,即DIR2置0if(motor_num=4) P1 &=0x40; /P1.6 = 0,即DIR3置0if(motor_num=5) P3 &=0x40; /P3.6 = 0,即DIR4置0break;case 1:if(motor_num=1) PCA0CPH0 = 255;if(motor_num=2) PCA0CPH1 = 255;if(motor_num=3) PCA0CPH2 = 255;if(motor_num=4) PCA0CPH3 = 255;if(motor_num=5) PCA0CPH4 = 255;if(motor_num=6) PCA0CPH5 = 255;break;case 2:if(motor_num=1) P3 |=if(motor_num=2) P1 |=0x20;0x10;/P3.5 =/P1.4 =1,即1,即DIR0 置1;DIR1置 1;if(motor_num=3) P1 |=0x20;/P1.5 =1,即DIR2置 1;if(motor_num=4) P1 |=0x40;/P1.6 =1,即DIR3置 1;if(motor_num=5) P3 |=0x40;/P3.6 =1,即DIR4置 1;break;default: break;SFRPAGE = SFRPAGE_save;