机器人程序.docx
《机器人程序.docx》由会员分享,可在线阅读,更多相关《机器人程序.docx(15页珍藏版)》请在冰豆网上搜索。
![机器人程序.docx](https://file1.bdocx.com/fileroot1/2023-1/27/a8aa5088-7973-4b78-ab7b-52e89db63555/a8aa5088-7973-4b78-ab7b-52e89db635551.gif)
机器人程序
亮灯交叉闪烁
#include
#include
intmain(void)
{
uart_Init();//初始化串口
printf("TheLEDconnectedtoP1_0isblinking!
\n");
while
(1)
{
P1_0=0;//P1_0输出低电平
delay_nms(500);//延时500ms
P1_0=1;//P1_0输出高电平
delay_nms(500);//延时500ms
P1_1=0;//P1_1输出低电平
delay_nms(500);//延时500ms
P1_1=1;//P1_1输出高电平
delay_nms(500);//延时500ms
}
}
走8字程序
#include
#include
intmain(void)
{intCounter;
uart_Init();//初始化串口
printf("TheLEDsconnectedtoP1_0andP1_1areblinking!
\n");
for(Counter=1;Counter<=450;Counter++)
{
P1_1=1;
delay_nus(1300);
P1_1=0;
delay_nms(20);
P1_0=1;
delay_nus(1539);
P1_0=0;
delay_nms(20);
}
for(Counter=1;Counter<=470;Counter++)
{
P1_0=1;
delay_nus(1700);
P1_0=0;
delay_nms(20);
P1_1=1;
delay_nus(1461);
P1_1=0;
delay_nms(20);
}
while
(1);
}
红外避障
#include
#include
#include
#defineLeftIRP1_2//左边红外接收连接到P1_2
#defineRightIRP3_5//右边红外接收连接到P3_5
#defineLeftLaunchP1_3//左边红外发射连接到P1_3
#defineRightLaunchP3_6//右边红外发射连接到P3_6
voidIRLaunch(unsignedcharIR)
{
intcounter;
if(IR=='L')
for(counter=0;counter<38;counter++)
{
LeftLaunch=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
LeftLaunch=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
if(IR=='R')
for(counter=0;counter<38;counter++)//右边发射
{
RightLaunch=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RightLaunch=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
voidForward(void)//向前行走子程序
{
P1_1=1;
delay_nus(1700);
P1_1=0;
P1_0=1;
delay_nus(1300);
P1_0=0;
delay_nms(20);
}
voidLeft_Turn(void)//左转子程序
{
inti;
for(i=1;i<=80;i++)
{
P1_1=1;
delay_nus(1300);
P1_1=0;
P1_0=1;
delay_nus(1300);
P1_0=0;
delay_nms(20);
}
}
voidRight_Turn(void)//右转子程序
{
inti;
for(i=1;i<=80;i++)
{
P1_1=1;
delay_nus(1700);
P1_1=0;
P1_0=1;
delay_nus(1700);
P1_0=0;
delay_nms(20);
}
}
voidBackward(void)//向后行走子程序
{
inti;
for(i=1;i<=80;i++)
{
P1_1=1;
delay_nus(1300);
P1_1=0;
P1_0=1;
delay_nus(1700);
P1_0=0;
delay_nms(20);
}
}
intmain(void)
{
intirDetectLeft,irDetectRight;
uart_Init();
printf("ProgramRunning!
\n");
while
(1)
{
IRLaunch('R');//右边发射
irDetectRight=RightIR;//右边接收
IRLaunch('L');//左边发射
irDetectLeft=LeftIR;//左边接收
if((irDetectLeft==0)&&(irDetectRight==0))//两边同时接收到红外线
{
Backward();
Left_Turn();
Left_Turn();
}
elseif(irDetectLeft==0)//只有左边接收到红外线
{
Backward();
Right_Turn();
}
elseif(irDetectRight==0)//只有右边接收到红外线
{
Backward();
Left_Turn();
}
else
Forward();
}
}
俯视的探测器
#include
#include
#include
#defineLeftIRP1_2//左边红外接收连接到P12
#defineRightIRP3_5//右边红外接收连接到P35
#defineLeftLaunchP1_3//左边红外发射连接到P13
#defineRightLaunchP3_6//右边红外发射连接到P36
intIRLaunch(unsignedcharIR)
{
intcounter;
if(IR=='L')
for(counter=0;counter<38;counter++)//左边发射
{
LeftLaunch=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
LeftLaunch=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
else
for(counter=0;counter<38;counter++)//右边发射
{
RightLaunch=1;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
RightLaunch=0;
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
intmain(void)
{
inti,pulseCount;
intpulseLeft,pulseRight;
bitirDetectLeft,irDetectRight;
uart_Init();
printf("ProgramRunning!
\n");
do
{
IRLaunch('R');
irDetectRight=RightIR;//右边接收
IRLaunch('L');
irDetectLeft=LeftIR;//左边接收
if((irDetectLeft==0)&&(irDetectRight==0))//向前走
{
pulseCount=1;
pulseLeft=1700;
pulseRight=1300;
}
elseif((irDetectLeft==1)&&(irDetectRight==0))//右转
{
pulseCount=10;
pulseLeft=1300;
pulseRight=1300;
}
elseif((irDetectLeft==0)&&(irDetectRight==1))//左转
{
pulseCount=10;
pulseLeft=1700;
pulseRight=1700;
}
else//后退
{
pulseCount=15;
pulseLeft=1300;
pulseRight=1700;
}
for(i=0;i{
P1_1=1;
delay_nus(pulseLeft);
P1_1=0;
P1_0=1;
delay_nus(pulseRight);
P1_0=0;
delay_nms(20);
}
}
while
(1);
}
/**************************************************************************
编程说明:
----------------
该程序仅是搬运机器人比赛一个案例,采用方式是通过QTI传感器沿黑色道路循迹将色块
搬运到对应堆放区。
堆放区从左到右固定颜色依次为:
黄白红黑蓝
因为比赛规则要求比赛时任意拿出五个色块中的3个色块进行比赛并随机放置在ACE位置
所以用QTI循迹的方式来搬运色块就有5*3种方式。
以下案例是演示的是ACE位置上依次为黄红蓝时循迹案例,给大家做个提示。
----------------
需说明:
比赛使用的策略程序有千千万,在规则允许的范围内使用的传感器亦有很多,怎么
配合程序完成比赛需发挥大家的创意。
本案例是在用最常见最少传感器的情况下做出的,
仅供参考。
**************************************************************************/
#include
#include"Global.h"
#include"delay.h"
#include"QTI.h"
#include"motion.h"
#defineDIR_LEFT0//跨线方向:
左
#defineDIR_RIGHT1//跨线方向:
右
#defineuintunsignedint
#defineucharunsignedchar
ucharQTIState;
voidForward(void)//向前行走子程序
{
right_motor=1;
delay_nus(1300);
right_motor=0;
left_motor=1;
delay_nus(1700);
left_motor=0;
delay_nms(20);
}
voidPivot_Left(void)//左转子程序
{
right_motor=1;
delay_nus(1360);
right_motor=0;
left_motor=1;
delay_nus(1500);
left_motor=0;
delay_nms(20);
}
voidPivot_Right(void)//右转子程序
{
right_motor=1;
delay_nus(1500);
right_motor=0;
left_motor=1;
delay_nus(1660);
left_motor=0;
delay_nms(20);
}
voidRotate_right(void)
{
right_motor=1;
delay_nus(1350);
right_motor=0;
left_motor=1;
delay_nus(1350);
left_motor=0;
delay_nms(20);
}
voidRotate_Left(void)
{
right_motor=1;
delay_nus(1650);
right_motor=0;
left_motor=1;
delay_nus(1650);
left_motor=0;
delay_nms(20);
}
voidGet_QTI_State(void)
{
QTIState=P2&0x1e;
}
voidFollow_Line(void)
{
Get_QTI_State();
switch(QTIState)
{
case0x0c:
Forward();
break;
case0x0e:
Pivot_Right();
break;
case0x1c:
Pivot_Left();
break;
case0x1e:
break;
default:
break;
}
}
voidrun_online(uint8_tsteps)
{
while(steps)
{
Follow_Line();
}
}
voidgoto_across1()
{
while
(1)
{
Follow_Line();
}
}
//沿着线走到十字路口
voidgoto_across()
{
while
(1)
{
Follow_Line();
}
}
//放置物块
voidput_material()
{
while
(1)
{
//中线控制
//机器人右偏判断0x10xx或01000x
if((!
P14_state())&&P22_state()&&(!
P23_state())||(!
P14_state())&&P21_state()&&(!
P22_state())&&(!
P23_state())&&(!
P24_state()))
{
motor_motion(1500,1300,1);//左转修正
}
//机器人左偏判断xx01x0或x00010
elseif((!
P16_state())&&((!
P22_state())&&P23_state())||((!
P14_state())&&(!
P21_state())&&(!
P22_state())&&(!
P23_state())&&P24_state()))
{
motor_motion(1700,1500,1);//右转修正
}
//机器人堆放物块1x00x1或xx00x1
elseif((P14_state()&&(!
P22_state())&&(!
P23_state())&&P16_state())||(!
P22_state())&&(!
P23_state())&&P16_state())//到堆垛区1001或1x00x1
{
motor_motion(LEFT_BWD,RIGHT_BWD,40);//倒退,避免马上转身造成物块被再次夹走
delay_nms(100);
motor_motion(1250,1250,42);//180度旋转
break;
}
else
{
motor_motion(LEFT_FWD,RIGHT_FWD,1);//前进
}
}
}
voidybr02()
{
run_online(40);
//put_material();//放置黄色物料
motor_motion(1500,1500,0);
delay_nms(800);
motor_motion(LEFT_BWD,RIGHT_BWD,35);
motor_motion(1250,1250,52);//180度旋转
goto_across();
motor_motion(1500,1500,0);
delay_nms(200);
motor_motion(1500,1250,26);//90度支点左旋
run_online(40);//沿线走40个脉冲
motor_motion(LEFT_BWD,RIGHT_BWD,35);
motor_motion(1250,1250,42);//180度旋转
//put_material();//放置红色物料
goto_across();
motor_motion(1500,1500,0);
delay_nms(200);
/*motor_motion(1500,1250,26);//90度支点左旋
run_online(100);//沿线走100个脉冲
motor_motion(LEFT_BWD,RIGHT_BWD,40);
motor_motion(1250,1250,52);//180度旋转
//put_material();//放置lan色物料
goto_across();
motor_motion(1500,1500,0);
delay_nms(200);
motor_motion(1500,1250,26);//90度支点左旋
run_online(100);//沿线走100个脉冲
delay_nms(500);*/
}
voidmain()
{
delay_nms(2000);
motor_motion(1680,1300,80);//前进,躲避出发区的三角地带(QTI看到的是1111)
goto_across();
motor_motion(1500,1500,0);
motor_motion(1680,1300,20);
delay_nms(300);//到十字路口,暂停100ms,使机器人不抖动(尾部抬高),造成机器人的方向的不准确
motor_motion(1650,1650,42);while
(1);//90度支点左旋
ybr02();//黄红蓝次序
while
(1);
}