毕业设计(论文)
开题报告书
20##年 3月
机器人足球仿真比赛
课程设计
组 别:A组 自动化0409班 组 员:程钊 012004014613 郝洵 012004014603 潘越 012004014604 王立 012004014626 指导老师:彭刚 提交时间:20xx年11月13日
机器人足球仿真课程设计
目 录
1. 需求分析 1.1课题要求
1.2编写目的
1.3背 景
2. 用户手册
2.1运行环境要求
2.2使用方法
2.3注意事项
3. 系统设计
3.1程序预期功能
3.2功能模块的划分
4. 详细设计
4.1进攻的详细设计及算法
4.2防守的详细设计及算法
4.3数据结构
4.4程序流程
4.5函数说明
5. 源代码清单
5.1引用源代码
5.2自编源代码
3 3 3 3 4 4 4 4 4 4 5 5 5 6 8 10 11 13 13 28 第 1 页
机器人足球仿真课程设计
6. 总结 52
6.1存在的不足 6.1体会
52 52
7. 参考文献 52
第 2 页
机器人足球仿真课程设计
1. 需求分析
1.1课题要求
(1)了解5Vs5机器人足球仿真比赛平台及其策略。
(2)在C语言编程环境下,完成策略的编写和调试。
(3)运用所写的程序进行足球机器人比赛,并在比赛中完善所写的策略。
1.2编写目的
通过对机器人足球仿真这一课程设计的了解,设计,编写和调试,实现足球策略在机器人上的优化应用,使己方机器人仿真足球队在比赛中获胜。最终达到能熟练运用C语言程序设计知识解决实际问题,实现具体目标的目的,并初步掌握工程化项目化软件开发的方法及过程。
1.3背景
机器人足球如今已为越来越多的人所关注。其国际认可的比赛按机器人可分为三大类:大中型机器人足球实物组比赛,微型机器人足球实物组比赛,机器人足球仿真组比赛;按数量分可分为:3vs3,5vs5,11vs11等。机器人足球仿真5vs5组比赛是在国际认可的同一平台The Robot Soccer Simulator Director 8.5上运行各队的策略,从而实现比赛的目的。而其策略是基于Visual C++编写的,只要掌握了C语言课程设计的知识就可以实现策略的编写。
第 3 页
机器人足球仿真课程设计
2. 用户手册
2.1运行环境要求
?
?
?
?
?
?
?
?
?
?
? Pentium III 600 MHz 256 megabytes of ram TNT2 3d Graphics accelerator with 32 megabytes of ram 24x CD-ROM Screen resolution of 800 x 600 16 bit Sound card Microsoft Windows 98 Direct X 8.0 10 megabytes of free hard drive space Director 8.5 Visual C++ 6.0
2.2使用方法
将包含源代码的工程文件用Visual C++ 5.0以上版本打开,编译(快捷
键F7),链接生成动态链接库。
? 把生成的动态链接库文件复制到C:\Stratege\blue目录下。并更具需要更换文件名称。
? 打开5Vs5平台程序,将文件名称输入到STRATEGIES目录下的Blue栏中,再用鼠标单击Lingo把链接状态更改为C++,然后在点击Send选项。链接文件过程完成。
? 在右侧的工具栏中,点击STARE,开始比赛。
? 如需进一步了解该软件的用法,单击右下角HELP选项。 ?
2.3注意事项
? 本文中所提供的策略程序,只能在蓝队(右队)中使用。
3. 系统设计
3.1程序预期功能
实现进攻防守两大功能的合理灵活的切换;通过实现快速反应,精确定点定向移动,路线优化设计等方法实现高效进攻和防守。
第 4 页
机器人足球仿真课程设计
3.2功能模块的划分
4. 详细设计及算法
4.1进攻的详细设计及算法
(1)当球在对方半场时为进攻模式1
主攻1:当球接近对方禁区时,在对方球门罚球区守侯,伺机射门;否则辅助助攻球员进攻。
主攻2:当球接近对方禁区时,在对方球门大禁区一侧守侯,伺机射门;否则辅助助攻球员进攻,控球。
助攻1:控球,将球带进对方禁区,辅助主攻机器人进攻。
助攻2:在助攻1机器人后方适当位置定点,随时接应助攻1机器人。 守门员:调整位置到初始位置
(2)当球在我方半场且不由对方控球时为进攻模式2
主攻1:控球,将球带进对方禁区,辅助主攻机器人进攻。
主攻2:在助攻1机器人后方适当位置定点,随时接应助攻1机器人。 助攻:准备进入对方半场并寻找有利攻击位置。
防守:定位到球与我方球门之间的适当位置,随时准备截球。
第 5 页
机器人足球仿真课程设计
(3)另外一套进攻方案:
每个机器人都作为一个独立的实体,分别进行判断:
机器人1:当球在对方半场大禁区线外时,追球;当球在对方半场大禁区线内上方时,留守下方,准备接反弹出来的球;当球在对方半场大禁区线内下方时,留守中间,伺机射门。
机器人2:当球在对方半场大禁区上下方时,将球传向门前;当球在对方大禁区和中场线之间时,控球,将球带向球门。
机器人3:。始终追球
机器人4:当球在对方半场大禁区线外时,追球;当球在对方半场大禁区线内上方时,留守中间,伺机射门;当球在对方半场大禁区线内下方时,留守上方,准备接反弹出来的球。
4.2防守的详细设计及算法
当球在我方半场且对方控球时为防守模式
主防:定位到球前方截球,使球向对方半场移动。
助防:定位到球与球门之间的适当位置截球,协助主防和守门员。
后卫1:定位到球门前适当位置做好截球接应准备。
后卫2:定位到球门前适当位置做好截球接应准备。
守门员:开始就自行运动到如图所示位置,这样可借助球门柱防止守门员因惯性偏离球门,可大大提高其运动速度,并可减少与其他队员的碰撞。不足之处在于因碰撞而姿态变化后调整空间较小。
第 6 页
机器人足球仿真课程设计
(1)当球在图中所示阴影内时,守门员的Y坐标尽量与球保持一致,当球坐标在球门范围外时,守门员保持在离球最近位置侯球。
(2)当球在图中所示阴影内时,守门员进入积极防守状态。
第 7 页
机器人足球仿真课程设计
积极防守状态按球的方向和位置不同分四种状态:
(1)球的运动方向指向球门,此时守门员应位于球的运动方向上,阻截球的运动。
(2)球的运动方向背离球门。此时守门员应位于球与两门柱连线夹角的角平分线方向上,为下一次截球作准备。
(3)在图示情况下,守门员的Y坐标尽量与球保持一致,当球坐标在球门范围外时保持 ,球门内时运动。
(4)当球在如图虚线内运动时,守门员与球相反运动,以把球撞出.
4.3数据结构
机器人球员的数据 (Vector3D pos表示机器人的三维坐标; rotation表示机器人的方向; velocityLeft表示机器人的左轮速度, velocityRight表示机器人的右轮速度;)
typedef struct
{
Vector3D pos;
第 8 页
机器人足球仿真课程设计
double rotation;
double velocityLeft, velocityRight;
} Robot;
对方机器人 (成员意义同上)
typedef struct
{
Vector3D pos;
double rotation;
} OpponentRobot;
总的环境参量结构
(home[PLAYERS_PER_SIDE]表示我方几号机器人;opponent[PLAYERS_PER_SIDE]表示对方几号机器人;currentBall表示当前球的位置,lastBall表示上个周期球的位置,predictedBall表示下个周期球的位置,用来预测球下个周期位置;fieldBounds表示场地边界坐标,goalBounds表示球门边线坐标;gameState表示比赛状态, whosBall表示球的掌控状态, *userData预留给用户的数据指针)
typedef struct
{
Robot home[PLAYERS_PER_SIDE];
OpponentRobot opponent[PLAYERS_PER_SIDE];
Ball currentBall, lastBall, predictedBall;
Bounds fieldBounds, goalBounds;
long gameState;
long whosBall;
void *userData;
} Environment;
第 9 页
机器人足球仿真课程设计
4.4程序流程
第 10 页
机器人足球仿真课程设计
4.5函数说明
基本动作
void PredictBall ( Environment *env );
预测球的位置,单步预测,运用微量调节。入口参数:环境参量。
void Velocity ( Robot *robot, double vl, double vr );
将响应产生的机器人速度写入系统参量中,即引发机器人运动。入口参数:机器人指针,左轮速度,右轮速度。
void Angle1(Robot *robot,int desired_angle);
使机器人转到预定角度。入口参数:机器人指针,预定角度。
void AngleOfPosition(Robot *robot,double x, double y);
使机器人转某一角度,指向特定点。入口参数:机器人指针,特定点横坐标,特定点纵坐标。
以下函数有方向性,基于以上的基本动作
void NormalGame_Right( Environment *env );
右队总策略。入口参数:环境参量。
void Defender_Right(Environment *env);
void Defend1_Right(Environment *env);
void Defend2_Right(Environment *env);
void Defend_Right_py(Environment *env);
右队防守策略。入口参数:球员指针,环境参量。
void Defence1_Right(Robot *robot,Environment *env);
第 11 页
机器人足球仿真课程设计
右队防守策略。入口参数:球员指针,环境参量。
void GoalKeeper_In_Right(Robot *robot,Environment *env);
右队守门员策略。入口参数:球员指针,环境参量。
新加入的策略
void Kick( Environment *env, Robot *robot, double aim_angle );
基本动作,绕到球后方,带球运动到对方球场。入口参数:环境参量,球员指针,目标角度。
void Position1_cz( Robot *robot, double x, double y );
移动到预定点,这是整个程序最重要的底层函数基础。入口参数:球员指针,预定点横坐标,预定点纵坐标。
void Position_py(Robot *robot, double x, double y);
移动到预定点,无速度衰减。入口参数:球员指针,预定点横坐标,预定点纵坐标。
void Attack_cz1( Robot *robot, Environment *env );
void Attack_hx( Robot *robot, Environment *env );
机器人攻击策略。入口参数:球员指针,环境参量。
void Shoot_Right_cz( Robot *robot, Environment *env ,double aimx ,double aimy);
射门函数,这是进攻中最重要的底层函数基础,引导机器人相指定点射门。入口参数:球员指针,环境参量,指定点横坐标,指定点纵坐标
void Goalkeeper_right_hx( Robot *robot, Environment *env )
根据球的运动轨迹,预计球将到达球门的位置,移动到该点截球。入口参数:球员指针,环境参量。
。
void Goalkeeper_Right_py2( Robot *robot, Environment *env )
第 12 页
机器人足球仿真课程设计
快速反应,在球的运动方向上拦截。入口参数:球员指针,环境参量。
5.源代码清单
5.1引用源代码
void Angle1(Robot *robot,int desired_angle)//让机器人原地旋转到某一角度,不分前后
{
int theta_e, vl, vr;
double Kp=1.2;
theta_e = desired_angle - (int)robot->rotation;
while (theta_e > 180) theta_e -= 360;
while (theta_e < -180) theta_e += 360;
if (theta_e < -90) theta_e += 180;
else if (theta_e > 90) theta_e -= 180;
vl = (int)(0 - Kp*theta_e);
vr = (int)(0 + Kp*theta_e);
Velocity (robot, vl, vr);
}
void AngleOfPosition(Robot *robot,double x, double y)//让机器人原地旋转始终对着某一点,不分前后
{
int vl, vr;
double dx,dy,d_e,desired_angle,theta_e;
double Kp=0.8;
dx=x-robot->pos.x;
dy=y-robot->pos.y;
d_e=sqrt(dx*dx+dy*dy);
if(dx==0 && dy==0)
{
vl=0;
vr=0;
Velocity(robot,vl,vr);
}
第 13 页
机器人足球仿真课程设计
else
desired_angle = (int)(180./PI*atan2((double)(dy),(double)(dx)));
theta_e = desired_angle-(int)robot->rotation;
while(theta_e >180) theta_e -= 360;
while(theta_e <-180) theta_e += 360;
if (theta_e < -90) theta_e += 180;
else if (theta_e > 90) theta_e -= 180;
vl = (int)(0 - Kp*theta_e);
vr = (int)(0 + Kp*theta_e);
Velocity(robot, vl, vr);
}
void Position1( Robot *robot, double x, double y )//改造原始Position函数,提高直线和转弯速度,提速至125,两方向前进
{
double vl, vr, vc = 120 ;
double desired_angle = 0, theta_e = 0, d_angle = 0 ;
double dx, dy, d_e, Ka = 10.0/90.0;
dx = x - robot->pos.x;
dy = y - robot->pos.y;
d_e = sqrt(dx * dx + dy * dy);
if (dx == 0 && dy == 0)
desired_angle = 90;
else
desired_angle = (int)(180. / PI * atan2((double)(dy), (double)(dx)));
theta_e = desired_angle - (int)robot->rotation;
while (theta_e > 180) theta_e -= 360;
while (theta_e < -180) theta_e += 360;
if (d_e > 100.)
Ka = 1.2; //17. / 90.;
else if (d_e > 50)
Ka = 0.9; //19. / 90.;
else if (d_e > 30)
Ka = 0.8; //21. / 90.;
else if (d_e > 20)
第 14 页
机器人足球仿真课程设计
Ka = 0.7; //23. / 90.;
else
Ka = 0.6; //25. / 90.;
if (theta_e > 95 || theta_e < -95)
{
theta_e += 180;
if (theta_e > 180)
theta_e -= 360;
if (theta_e > 80)
theta_e = 80;
if (theta_e < -80)
theta_e = -80;
if (d_e < 5.0 && fabs(theta_e) < 40)
Ka = 0.5;//0.1;
vr = (int)(-vc * (1.0 / (1.0 + exp(-3.0 * d_e)) - 0.2) + Ka * theta_e);//距离为零时,速度不为零
vl = (int)(-vc * (1.0 / (1.0 + exp(-3.0 * d_e)) - 0.2) - Ka * theta_e);
}
else if (theta_e < 85 && theta_e > -85)
{
if (d_e < 5.0 && fabs(theta_e) < 40)
Ka = 0.5;//0.1;
vr = (int)( vc * (1.0 / (1.0 + exp(-3.0 * d_e)) - 0.2) + Ka * theta_e);
vl = (int)( vc * (1.0 / (1.0 + exp(-3.0 * d_e)) - 0.2) - Ka * theta_e);
}
else
{
vr = (int)(+.8 * theta_e);
vl = (int)(-.8 * theta_e);
}
if(fabs(vl)>=fabs(vr))
{
if(vl>0)
{
vl=125;
vr=125*vr/vl;
}
if(vl<0)
{
第 15 页
机器人足球仿真课程设计
vl=-125;
vr=-125.0*vr/vl;
}
}
if(fabs(vl)<fabs(vr))
{
if(vr>0)
{
vr=125;
vl=125.0*vl/vr;
}
if(vr<0)
{
vr=-125;
vl=-125.0*vl/vr;
}
}
Velocity(robot, vl, vr);
}
bool Position2(Robot *robot,double x, double y)//将GoaliePosition提速至125,两方向前进 {
int desired_angle=0, theta_e = 0;
double dx, dy, d_e, Kd, Ka, vl, vr;//, dx1, dy1, dx2, dy2;
dx=x-robot->pos.x;
dy=y-robot->pos.y;
d_e=sqrt(dx*dx+dy*dy);
if(dx==0 && dy==0)
desired_angle = 90;
else
desired_angle = (int)(180./PI*atan2((double)(dy),(double)(dx)));
theta_e = desired_angle-(int)robot->rotation;
while(theta_e > 180) theta_e -= 360;
while(theta_e < -180) theta_e += 360;
Kd = 8; //0.85; //This is the Robot's overall speed.
Ka = 10./90.;
if( d_e<0.1 )
{
第 16 页
机器人足球仿真课程设计
vl=0;
vr=0;
Velocity(robot, vl, vr);
return TRUE;
}
if( d_e>=0.1 && theta_e > -90 && theta_e < 90) {
d_e=sqrt(dx*dx+dy*dy);
vr=vl=(int)(d_e*Kd);
if ( vl > 120 ) vl = 120;
if ( vr > 120 ) vr = 120;
if ( vl < 1 ) vl = 1;
if ( vr < 1 ) vr = 1;
if ( d_e < 2 || ( theta_e >= -20 && theta_e <=20 ) ) {
if ( d_e < 1 || ( theta_e >= -35 && theta_e <=35 ) ) {
Ka=0.22;//0.12;
}
else
{
Ka=0.32;//0.22;
} }
else
{
Ka=0.35;//0.25;
}
vl = (int)(vl - (Ka*theta_e));
vr = (int)(vr + (Ka*theta_e));
}
if( d_e>=0.1 && (theta_e <= -90 || theta_e >= 90) ) {
d_e=sqrt(dx*dx+dy*dy);
vr=vl=(int)(d_e*-Kd);
if ( vr < -120 ) vr = -120;
if ( vl < -120 ) vl = -120;
if ( vr > -1 ) vr = -1;
if ( vl > -1 ) vl = -1;
if ( d_e < 2 || ( theta_e >= -20 && theta_e <=20 ) )
第 17 页
机器人足球仿真课程设计
{
if ( d_e <1 || ( theta_e >= -35 && theta_e <=35 ) ) {
Ka = 0.22;//0.12;
}
else
{
Ka = 0.32;//0.22;
}
}
else
{
Ka = 0.35;//0.25;
}
vl = (int)(vl - (Ka*theta_e));
vr = (int)(vr + (Ka*theta_e));
}
if(fabs(vl)>=fabs(vr))
{
if(vl>0)
{
vl=125;
vr=125.0*vr/vl;
}
if(vl<0)
{
vl=-125;
vr=-125.0*vr/vl;
}
}
if(fabs(vl)<fabs(vr))
{
if(vr>0)
{
vr=125;
vl=125.0*vl/vr;
}
if(vr<0)
{
vr=-125;
vl=-125.0*vl/vr;
}
}
第 18 页
机器人足球仿真课程设计
Velocity(robot, vl, vr);
return FALSE;
}
bool GoaliePosition(Robot *robot,double x, double y)//引用自实物组的GoaliePosition函数,做了改动,主要是提高转动速度
{ //和直线速度,否则太慢
int desired_angle=0, theta_e = 0, vl, vr;
double dx, dy, d_e, Kd, Ka;
dx=x-robot->pos.x;
dy=y-robot->pos.y;
d_e=sqrt(dx*dx+dy*dy);
if(dx==0 && dy==0)
desired_angle = 90;
else
desired_angle = (int)(180./PI*atan2((double)(dy),(double)(dx)));
theta_e = desired_angle-(int)robot->rotation;
while(theta_e > 180) theta_e -= 360;
while(theta_e < -180) theta_e += 360;
Kd = 8; //0.85;
Ka = 10./90.;
if( d_e<0.1 )
{
vl=0;
vr=0;
Velocity(robot, vl, vr);
return TRUE;
}
if( d_e>=0.1 && theta_e > -90 && theta_e < 90)
{
d_e=sqrt(dx*dx+dy*dy);
vr=vl=(int)(d_e*Kd);
if ( vl > 120 ) vl = 120;
if ( vr > 120 ) vr = 120;
if ( vl < 1 ) vl = 1;
if ( vr < 1 ) vr = 1;
第 19 页
机器人足球仿真课程设计
if ( d_e < 2 || ( theta_e >= -20 && theta_e <=20 ) ) {
if ( d_e < 1 || ( theta_e >= -35 && theta_e <=35 ) ) {
Ka=0.22;//0.12;
}
else
{
Ka=0.32;//0.22;
}
}
else
{
Ka=0.35;//0.25;
}
vl = (int)(vl - (Ka*theta_e));
vr = (int)(vr + (Ka*theta_e));
}
if( d_e>=0.1 && (theta_e <= -90 || theta_e >= 90) ) {
d_e=sqrt(dx*dx+dy*dy);
vr=vl=(int)(d_e*-Kd);
if ( vr < -120 ) vr = -120;
if ( vl < -120 ) vl = -120;
if ( vr > -1 ) vr = -1;
if ( vl > -1 ) vl = -1;
if(theta_e<-90) theta_e += 180;
else if(theta_e>90) theta_e -= 180;
if ( d_e < 2 || ( theta_e >= -20 && theta_e <=20 ) ) {
if ( d_e <1 || ( theta_e >= -35 && theta_e <=35 ) ) {
Ka = 0.22;//0.12;
}
else
{
Ka = 0.32;//0.22;
}
}
else
{
第 20 页
机器人足球仿真课程设计
Ka = 0.35;//0.25;
}
vl = (int)(vl - (Ka*theta_e));
vr = (int)(vr + (Ka*theta_e));
}
Velocity(robot, vl, vr);
return FALSE;
}
void Defence1_Right(Robot *robot,Environment *env)//右队防守动作,绕到球与球门之间 {
Ball ball=env->currentBall;
double k1= ( 41.80605-ball.pos.y ) / ( 93.4259-ball.pos.x );
double theta1_PI = atan2(41.80605-ball.pos.y , 93.4259-ball.pos.x);
if( robot->pos.x <= ball.pos.x-k1*(robot->pos.y-ball.pos.y) )
{
double a_x = ball.pos.x - 10*sin(theta1_PI);// + 6*cos(theta1_PI);
double a_y = ball.pos.y + 10*cos(theta1_PI);// + 6*sin(theta1_PI);
double b_x = ball.pos.x + 10*sin(theta1_PI);// + 6*cos(theta1_PI);
double b_y = ball.pos.y - 10*cos(theta1_PI);// + 6*sin(theta1_PI);
if( ball.pos.y >= (GTOP+GBOT)/2 )
{
Position1(robot,b_x,b_y);
}
if( ball.pos.y < (GTOP+GBOT)/2 )
{
Position1(robot,a_x,a_y);
}
}
else Position1_cz(robot,ball.pos.x,ball.pos.y);
}
void Defender_Right(Environment *env)
{
if(env->currentBall.pos.x>FRIGHT-7 && env->currentBall.pos.y<GTOP+3
&& env->currentBall.pos.y>GBOT-3)//球在禁区
Defend3_Right(env);
else
{
if(env->currentBall.pos.x>FRIGHT-7)
Defend2_Right(env);//底线防守阵形
第 21 页
机器人足球仿真课程设计
else
Defend1_Right(env);//一般防守阵形
}
}
void Defend1_Right(Environment *env)//防守策略(暂考虑把转换成防守的条件放在函数外面)//一般防守阵形
{
int count=0;
double leng[5];
leng[0]=0;
int i,flag[5];
for(i=1;i<=4;i++)
{
if(env->home[i].pos.x<=env->currentBall.pos.x)
count++;
}
for(i=1;i<=4;i++)
{
leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);
flag[i]=i;
}
for(i=1;i<=4;i++)
{
for(int j=i+1;j<=4;j++)
{
if(leng[i]>=leng[j])
{
int temp=flag[i];
flag[i]=flag[j];
flag[j]=temp;
}
}
}
if(count==0 || count==1 ||count==2)
{
Defence1_Right(&env->home[flag[1]],env);
if(env->home[flag[2]].pos.x<=env->currentBall.pos.x)
{
第 22 页
机器人足球仿真课程设计
if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y+20); else
GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y-20); }
else
{
if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x+10,env->currentBall.pos.y)) {
AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y); }
}
if(env->currentBall.pos.y<=(FBOT+FTOP)/2)
{
if(GoaliePosition(&env->home[flag[3]],env->currentBall.pos.x+10,env->currentBall.pos.y+10)) {
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y); }
}
else
{
if(GoaliePosition(&env->home[flag[3]],env->currentBall.pos.x+10,env->currentBall.pos.y-10)) {
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y); }
}
double estimate_x=FRIGHT-8;
double estimate_y=env->currentBall.pos.y;
double enter_y;
if(env->currentBall.pos.x>env->lastBall.pos.x)
{
double
k=(env->currentBall.pos.y-env->lastBall.pos.y)/(env->currentBall.pos.x-env->lastBall.pos.x); enter_y=k*(estimate_x-env->currentBall.pos.x)+env->currentBall.pos.y;
第 23 页
机器人足球仿真课程设计
}
if(enter_y>33.9320-5 && enter_y<49.6801+5)
{
estimate_y=enter_y;
}
if(estimate_y>49.6801+5)
estimate_y=49.6801+5;
else if(estimate_y<33.9320-5)
estimate_y=33.9320-5;
if(GoaliePosition(&env->home[flag[4]], estimate_x, estimate_y))
AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y); }
if(count>=3)
{
for(i=1;i<=4;i++)
{
if(env->home[i].pos.x<=env->currentBall.pos.x)
{
if(env->currentBall.pos.y>=GTOP+8)
Position1_cz(&env->home[i],FRIGHT,GTOP+8);
if(env->currentBall.pos.y<=GTOP-8)
Position1_cz(&env->home[i],FRIGHT,GBOT-8);
if(env->currentBall.pos.y<GTOP+8&&env->currentBall.pos.y>GTOP-8) {
if(env->home[i].pos.y>=env->currentBall.pos.y)
Position1_cz(&env->home[i],env->currentBall.pos.x,env->currentBall.pos.y+20);
else
Position1_cz(&env->home[i],env->currentBall.pos.x,env->currentBall.pos.y-20);
}
}
else
Defence1_Right(&env->home[i],env);
}
}
}
void Defend2_Right(Environment *env)//底线防守阵形
{
double leng[5];
leng[0]=0;
int i,flag[5];
第 24 页
机器人足球仿真课程设计
for(i=1;i<=4;i++)
{
leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);
flag[i]=i;
}
for(i=1;i<=4;i++)
{
for(int j=i+1;j<=4;j++)
{
if(leng[i]>=leng[j])
{
int temp=flag[i];
flag[i]=flag[j];
flag[j]=temp;
}
}
}
Defence1_Right(&env->home[flag[1]],env);
if(env->currentBall.pos.y<=FBOT+6 || env->currentBall.pos.y>=FTOP-6)
{
if(env->currentBall.pos.y<=(FBOT+FTOP)/2)
{
if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x+2,env->currentBall.pos.y+6))
AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y); }
else
{
if(GoaliePosition(&env->home[flag[2]],env->currentBall.pos.x+2,env->currentBall.pos.y-6))
AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y); }
}
else
Defence1_Right(&env->home[flag[2]],env);
if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
{
if(GoaliePosition(&env->home[flag[3]],FLEFT+10,GBOT))
第 25 页
机器人足球仿真课程设计
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y); if(GoaliePosition(&env->home[flag[4]],FLEFT+15,GTOP+6))
AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y); }
else
{
if(GoaliePosition(&env->home[flag[3]],FLEFT+10,GTOP))
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y); if(GoaliePosition(&env->home[flag[4]],FLEFT+15,GBOT-6))
AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y); }
/* if(abs(env->home[flag[1]].pos.y-41.80605) >= abs(env->home[flag[2]].pos.y-41.80605))
{
Defence1_Right(&env->home[flag[1]],env);
if(env->currentBall.pos.y<=FBOT+6 || env->currentBall.pos.y>=FTOP-6)
{
if(GoaliePosition(&env->home[flag[2]],
(env->currentBall.pos.x+env->home[flag[3]].pos.x)/2,(env->currentBall.pos.y+env->home[flag[3]].pos.y)/2))
AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y); }
else
Defence1_Right(&env->home[flag[2]],env);
}
else
{
Defence1_Right(&env->home[flag[2]],env);
if(env->currentBall.pos.y<=FBOT+6 || env->currentBall.pos.y>=FTOP-6)
{
if(GoaliePosition(&env->home[flag[1]],
(env->currentBall.pos.x+env->home[flag[3]].pos.x)/2,(env->currentBall.pos.y+env->home[flag[3]].pos.y)/2))
AngleOfPosition(&env->home[flag[1]],env->currentBall.pos.x,env->currentBall.pos.y); }
else
Defence1_Right(&env->home[flag[1]],env);
}*/
if(env->currentBall.pos.y<=(FTOP+FBOT)/2)
第 26 页
机器人足球仿真课程设计
{
if(GoaliePosition(&env->home[flag[3]],FRIGHT-10,GBOT))
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y); if(GoaliePosition(&env->home[flag[4]],FRIGHT-15,GTOP+6))
AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y); }
else
{
if(GoaliePosition(&env->home[flag[3]],FRIGHT-10,GTOP))
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y); if(GoaliePosition(&env->home[flag[4]],FRIGHT-15,GBOT-6))
AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y); }
}
void Defend_Right_py(Environment *env)//球在禁区时的防守策略
{
double HOME[5];
HOME[0]=0;
int i,flag[5];
for(i=1;i<=4;i++)
{
HOME[i]=env->home[i].pos.y;
flag[i]=i;
}
for(i=1;i<=4;i++)
{
for(int j=i+1;j<=4;j++)
{
if(HOME[i]>=HOME[j])
{
int temp=flag[i];
flag[i]=flag[j];
flag[j]=temp;
}
}
}
if(GoaliePosition(&env->home[flag[1]],FRIGHT-10,GBOT-10))
AngleOfPosition(&env->home[flag[1]],env->currentBall.pos.x,env->currentBall.pos.y); if(GoaliePosition(&env->home[flag[2]],FRIGHT-9,GBOT+2))
AngleOfPosition(&env->home[flag[2]],env->currentBall.pos.x,env->currentBall.pos.y); if(GoaliePosition(&env->home[flag[3]],FRIGHT-9,GTOP-2))
AngleOfPosition(&env->home[flag[3]],env->currentBall.pos.x,env->currentBall.pos.y);
第 27 页
机器人足球仿真课程设计
if(GoaliePosition(&env->home[flag[4]],FRIGHT-10,GTOP+10))
AngleOfPosition(&env->home[flag[4]],env->currentBall.pos.x,env->currentBall.pos.y); }
5.2自编源代码
#include "stdafx.h"
#include "Strategy.h"
#include <math.h>
#define square(a) ((a)*(a))
#define leng(a,b,c,d) sqrt(square((a)-(c))+square((b)-(d)))
const double PI = 3.1415923;
//基本动作
void PredictBall ( Environment *env );
void Velocity ( Robot *robot, double vl, double vr );
bool GoaliePosition(Robot *robot,double x, double y);
void Position1( Robot *robot, double x, double y );
bool Position2(Robot *robot,double x, double y);
void Angle1(Robot *robot,int desired_angle);
void AngleOfPosition(Robot *robot,double x, double y);
//有方向性
void NormalGame_Right0( Environment *env );
void NormalGame_Right( Environment *env );
void Defender_Right(Environment *env);
void Defend1_Right(Environment *env);
void Defend2_Right(Environment *env);
void Defend_Right_py(Environment *env);
void Defence1_Right(Robot *robot,Environment *env);
void GoalKeeper_In_Right(Robot *robot,Environment *env);
//新的策略
void Kick( Environment *env, Robot *robot, double aim_angle );
void Position1_cz( Robot *robot, double x, double y );
void Position0_py(Robot *robot, double x, double y);
void Attack_cz1( Robot *robot, Environment *env , double x, double y );
void Attack_hx( Robot *robot, Environment *env );
void Shoot_Right_cz( Robot *robot, Environment *env );
void pass(Robot *robot , Environment *env);
void shoot(Robot *robot, Environment *env);
void Attack1( Robot *robot , Environment *env);
第 28 页
机器人足球仿真课程设计
void Attack2( Robot *robot , Environment *env);
void Attack3( Robot *robot , Environment *env);
void Attack4( Robot *robot , Environment *env);
void NormalGame_Right_wl( Environment *env );
void Goalkeeper_right_hx(Robot *robot,Environment *env);
void Goalkeeper_Right_py2( Robot *robot, Environment *env );
extern "C" STRATEGY_API void Create ( Environment *env )
{
}
extern "C" STRATEGY_API void Destroy ( Environment *env )
{
}
extern "C" STRATEGY_API void Strategy ( Environment *env )
{
switch (env->gameState)
{
case 0:
PredictBall ( env );
NormalGame_Right0( env );
// NormalGame_Right_wl( env );
GoalKeeper_In_Right( &env->home [0], env );
// Goalkeeper_Right_py2( &env->home [0], env );
// Goalkeeper_right_hx( &env->home [0], env);
// Shoot_Right_cz( &env->home [1], env );
// Position1_cz( &env->home[3] , env->predictedBall.pos.x, env->predictedBall.pos.y ); break;
case FREE_BALL:
NormalGame_Right(env);
break;
case PLACE_KICK:
NormalGame_Right(env);
break;
case PENALTY_KICK:
第 29 页
机器人足球仿真课程设计
break;
case FREE_KICK:
NormalGame_Right(env);
break;
case GOAL_KICK:
NormalGame_Right(env);
break;
}
}
void NormalGame_Right( Environment *env )//右队总策略
{
//动态分配角色
int i, j=0, flag[5];
double leng[5] ;
for(i=1;i<=4;i++)
{
leng[i]=leng(env->currentBall.pos.x,env->currentBall.pos.y,env->home[i].pos.x,env->home[i].pos.y);
flag[i]=i;
}
for(i=1;i<=4;i++)
{
for(int j=i+1;j<=4;j++)
{
if(leng[i]>=leng[j])
{
int temp=flag[i];
flag[i]=flag[j];
flag[j]=temp;
}
}
}
if(env->currentBall.pos.x>(FRIGHT+FLEFT)/2)//球在己方半场
{
//防守
if(env->whosBall==YELLOW_BALL || (env->whosBall==ANYONES_BALL && env->currentBall.pos.x>env->lastBall.pos.x) || env->currentBall.pos.x>FRIGHT-23)
{
第 30 页
机器人足球仿真课程设计
Defender_Right( env );
// Defend1_Right( env );
}
//进攻
else
{
// Attack_Right(env);
Position1_cz( &env->home [flag[1]], 82.0, 30.0 );//防守
Kick( env, &env->home [flag[2]], 180 );
Kick( env, &env->home [flag[3]], 180 );
Kick( env, &env->home [flag[4]], 180 );
}
}
else
{
// Attack_Right(env);
// kick( env, &env->home [], 13*PI/6 );
// Attack_cz2( &env->home [3], env );
Kick( env, &env->home [flag[1]], 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
Kick( env, &env->home [flag[2]], 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
Attack_cz1( &env->home [flag[4]], env , 18.0, 18.0 );
Attack_cz1( &env->home [flag[3]], env , 30.0, 42.0 );
}
}
void NormalGame_Right0( Environment *env )//右队总策略
{
//动态分配角色
if(env->currentBall.pos.x>(FRIGHT+FLEFT)/2)//球在己方半场
{
//防守
if(env->whosBall==YELLOW_BALL || (env->whosBall==ANYONES_BALL &&
env->currentBall.pos.x>env->lastBall.pos.x) || env->currentBall.pos.x>FRIGHT-23) {
Defender_Right( env );
// Defend1_Right( env );
}
第 31 页
机器人足球仿真课程设计
//进攻
else
{
// Attack_Right(env);
Position1_cz( &env->home [1], 82.0, 30.0 );//防守
Kick( env, &env->home [2], 180 );
Kick( env, &env->home [3], 180 );
Kick( env, &env->home [4], 180 );
}
}
else
{
// Attack_Right(env);
// kick( env, &env->home [], 13*PI/6 );
// Attack_cz2( &env->home [3], env );
Kick( env, &env->home [3], 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
Kick( env, &env->home [4], 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
Attack_cz1( &env->home [1], env , 18.0, 18.0 );
Attack_cz1( &env->home [2], env , 30.0, 42.0 );
}
}
void PredictBall ( Environment *env )//预测球的位置(一步)
{
double dx = env->currentBall.pos.x - env->lastBall.pos.x;
double dy = env->currentBall.pos.y - env->lastBall.pos.y;
env->predictedBall.pos.x = env->currentBall.pos.x + dx;
env->predictedBall.pos.y = env->currentBall.pos.y + dy;
}
void Velocity ( Robot *robot, double vl, double vr )
{
robot->velocityLeft = vl;
robot->velocityRight = vr;
}
//改造原始Position函数,提高直线和转弯速度,提速至125,两方向前进
//进一步提高直线和适当减小转弯速度--cz
void Position1_cz( Robot *robot, double x, double y )
{
int vl, vr, vc = 120 ;
第 32 页
机器人足球仿真课程设计
double desired_angle = 0, theta_e = 0, d_angle = 0 ;
double f_d, d_e, dx, dy, Ka = 5.0/90.0, kd = 250;//kd = 200
dx= x - robot->pos.x;
dy= y - robot->pos.y;
d_e = sqrt( dx*dx + dy*dy );
f_d=1.0 / (1.0 + exp(-0.3 * d_e))- 0.5;
if ( dx == 0 && dy == 0 )
desired_angle = 90;
else
desired_angle = (int)(180. / PI * atan2((double)(dy), (double)(dx))); theta_e = desired_angle - (int)robot->rotation;
while (theta_e > 180) theta_e -= 360;
while (theta_e < -180) theta_e += 360;
if (d_e > 100.)
Ka = 1.2; //17. / 90.;
else if (d_e > 50)
Ka = 0.9; //19. / 90.;
else if (d_e > 30)
Ka = 0.8; //21. / 90.;
else if (d_e > 20)
Ka = 0.7; //23. / 90.;
else
Ka = 0.6; //25. / 90.;
if (theta_e > 85 || theta_e < -85)
{
theta_e += 180;
if (theta_e > 180)
theta_e -= 360;
if (theta_e > 80)
theta_e = 80;
if (theta_e < -80)
theta_e = -80;
if (d_e < 5.0 && fabs(theta_e) < 40)
Ka = 0.5;//0.1;
vr = (int)(-kd * f_d + Ka * theta_e);
vl = (int)(-kd * f_d - Ka * theta_e);
}
else if (theta_e < 85 && theta_e > -85)
第 33 页
机器人足球仿真课程设计
{
if (d_e < 5.0 && fabs(theta_e) < 40)
Ka = 0.5;//0.1;
vr = (int)( kd * f_d + Ka * theta_e);
vl = (int)( kd * f_d - Ka * theta_e);
}
else
{
vr = (int)(+.8 * theta_e);
vl = (int)(-.8 * theta_e);
}
Velocity(robot, vl, vr);
}
//没有边界防撞、调整策略
//利用摩擦减速经验公式减速
void Position0_cz(Robot *robot, double x, double y)
{
int desired_angle=0, theta_e = 0, vl, vr, V = 120;
double dx, dy, d_e, Ka=0.85*0.5;//Kd, ka由自己调试!
dx=x-robot->pos.x;
dy=y-robot->pos.y;
d_e=sqrt(dx*dx+dy*dy);
if(dx==0 && dy==0) //prevents dx to be divided as '0'
desired_angle = 90;
else
desired_angle = (int)(180./PI*atan2((double)(dy),(double)(dx)));
theta_e = desired_angle - (int)robot->rotation;
while(theta_e > 180)
theta_e -= 360;
while(theta_e < -180)
theta_e += 360;
if (theta_e > 85 || theta_e < -85)
{
theta_e += 180;
if (theta_e > 180)
第 34 页
机器人足球仿真课程设计
theta_e -= 360;
if (d_e < 10.0 && fabs(theta_e) < 40)
Ka = 0.2;//0.1;
vl = V - (int)(Ka*theta_e);
vr = V + (int)(Ka*theta_e);
}
else if (theta_e < 85 && theta_e > -85)
{
if (d_e < 10.0 && fabs(theta_e) < 40)
Ka = 0.2;//0.1;
vl = V - (int)(Ka*theta_e);
vr = V + (int)(Ka*theta_e);
}
else
{
vr = (int)(+.8 * theta_e);
vl = (int)(-.8 * theta_e);
}
if( d_e < 5 ) //接近时减速,靠惯性到达,场地摩擦系数很大。3.5----v=125
{
vl=0;
vr=0;
}
Velocity( robot, vl, vr);
}
void GoalKeeper_In_Right(Robot *robot,Environment *env)//蓝队(右队)守门员在里面 {
double estimate_x=FRIGHT-0.324;//93.75;
double estimate_y=env->currentBall.pos.y;
double enter_y=env->currentBall.pos.y;
if(env->currentBall.pos.x<=FRIGHT-7)//预测,对estimate_y作出修正
{
if(env->currentBall.pos.x>env->lastBall.pos.x)
{
double
k=(env->currentBall.pos.y-env->lastBall.pos.y)/(env->currentBall.pos.x-env->lastBall.pos.x); enter_y=k*(estimate_x-env->currentBall.pos.x)+env->currentBall.pos.y; }
if(enter_y>33.9320 && enter_y<49.6801)
{
estimate_y=enter_y;
第 35 页
机器人足球仿真课程设计
}
}
if(estimate_y>49.6801-1.85)
estimate_y=49.6801-1.85;
else if(estimate_y<33.9320+1.85)
estimate_y=33.9320+1.85;
if(GoaliePosition(robot,estimate_x,estimate_y))
{
if(sqrt((robot->pos.x-env->currentBall.pos.x)*(robot->pos.x-env->currentBall.pos.x)
+(robot->pos.y-env->currentBall.pos.y)*(robot->pos.y-env->currentBall.pos.y))<=4) {
if(env->currentBall.pos.y>robot->pos.y)
Velocity(robot,125,-125);
else
Velocity(robot,-125,+125);
}
else
Angle1(robot,90);
}
}
void Kick( Environment *env, Robot *robot, double aim_angle )
{
double L=2.9528;
double d_angle, angle, d_length, vl, vr;
d_angle=180/PI*atan2(robot->pos.y-env->predictedBall.pos.y,robot->pos.x-env->predictedBall.pos.x);
d_length=leng(robot->pos.x,env->predictedBall.pos.x,robot->pos.y,env->predictedBall.pos.y); angle=aim_angle-d_angle;
while(angle > 180) //limit of the range of as -180~180
angle -= 360;
while(angle < -180)
angle += 360;
// Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
if( fabs(d_angle) > 45 || env->predictedBall.pos.x > 32.0)//待调整
{
if( d_length > 2*L )
{
Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y );
}
第 36 页
机器人足球仿真课程设计
else if(d_angle>15)
{
vl=100. + 15.*angle*L/2;
vr=100. - 15.*angle*L/2;
Velocity ( robot, vl, vr );
}
else //d_length<L/2
{
Position1_cz( robot, env->predictedBall.pos.x, env->predictedBall.pos.y ); }
}
else
{
Shoot_Right_cz( robot, env );
}
}
//主攻
void Attack_cz1( Robot *robot, Environment *env , double x, double y )
{
// if ( t.y > env->fieldBounds.top - 2.5 ) t.y = env->fieldBounds.top - 2.5;//边角调整 // if ( t.y < env->fieldBounds.bottom + 2.5 ) t.y = env->fieldBounds.bottom + 2.5; // if ( t.x > env->fieldBounds.right - 3 ) t.x = env->fieldBounds.right - 3;
// if ( t.x < env->fieldBounds.left + 3 ) t.x = env->fieldBounds.left + 3;
if( env->currentBall.pos.y > 41.8 )//球在上半区域
{
if( env->currentBall.pos.x < 28.0 )
{
if( env->currentBall.pos.y < GTOP+3 && env->currentBall.pos.y > GBOT-3 ) {
Shoot_Right_cz( robot ,env );
}
else if( leng( robot->pos.x, 18., robot->pos.y, 18. ) > 2.5 )
{
Position1_cz( robot, x, y );
}
else
{
AngleOfPosition( robot, 6.8, 41.8 );
}
}
else if( env->currentBall.pos.x < 40.0 )
{
第 37 页
机器人足球仿真课程设计
if( env->currentBall.pos.y < GTOP-6 && env->currentBall.pos.y > GBOT+6 ) //FTOP=77.2392;FBOT=6.3730;GTOP=49.6801;GBOT=33.9320; {
Shoot_Right_cz( robot ,env );
}
else if( leng( robot->pos.x, 18., robot->pos.y, 18. ) > 2.5 )
{
Position1_cz( robot, x, y );
}
else
{
AngleOfPosition( robot, 6.8, 41.8 );
}
}
else//定位
{
Position1_cz( robot, env->predictedBall.pos.x + 5, (FTOP+FBOT)/2 ); }
}
else//球在下半区域
{
if( env->currentBall.pos.x < 28.0 )
{
if( env->currentBall.pos.y < GTOP+3 && env->currentBall.pos.y > GBOT-3 ) {
Shoot_Right_cz( robot ,env );
}
else if( leng( robot->pos.x, 18., robot->pos.y, 18. ) > 2.5 )
{
Position1_cz( robot, x, 83.6-y );
}
else
{
AngleOfPosition( robot, 6.8, 41.8 );
}
}
else if( env->currentBall.pos.x < 40.0 )
{
if( env->currentBall.pos.y < GTOP-6 && env->currentBall.pos.y > GBOT+6 ) //FTOP=77.2392;FBOT=6.3730;GTOP=49.6801;GBOT=33.9320; {
Shoot_Right_cz( robot ,env );
}
else if( leng( robot->pos.x, 18., robot->pos.y, 18. ) > 2.5 )
第 38 页
机器人足球仿真课程设计
{
Position1_cz( robot, x, 83.6-y );
}
else
{
AngleOfPosition( robot, 6.8, 41.8 );
}
}
else//定位
{
Position1_cz( robot, env->predictedBall.pos.x + 5, (FTOP+FBOT)/2 );
}
}
}
//助攻
void Attack_cz2( Robot *robot, Environment *env )
{
if( env->currentBall.pos.x < 20.0 )
{
Kick( env, robot, 180/PI*atan2(env->predictedBall.pos.y - 41.8, env->predictedBall.pos.x -6.8) );
}
else if( env->currentBall.pos.x < 40.0 )
{
double x= 1.3*env->predictedBall.pos.x -0.3*6.82 ;//系数1.3----1.3(x-X)+X
double y= ( env->predictedBall.pos.y + 41.8 )/2 ;//定位于球右中的适当位置
Position1_cz( robot, x, y );
}
else
Position1_cz( robot, env->predictedBall.pos.x + 5, env->predictedBall.pos.y );
}
void Shoot_Right_cz( Robot *robot, Environment *env )//射门动作,中位线算法
{
double k1 ,b1, k2, b2, L=2.9, x, y, d_angle, aim_angle ,aimx=6.8, aimy=41.8;
Ball ball=env->predictedBall;
OpponentRobot keeper=env->opponent[0] ;
if( aimx == ball.pos.x ) aim_angle = 90;
else aim_angle = atan( (aimy - ball.pos.x)/(aimx - ball.pos.x) );
第 39 页
机器人足球仿真课程设计
while( fabs(aim_angle) < 90 ) aim_angle = 180 - aim_angle;
d_angle= aim_angle - robot->rotation;
//计算变化中的aimx,aimy
double up_angle, down_angle;
up_angle = atan2( ball.pos.y-keeper.pos.y, ball.pos.y-keeper.pos.y )
-atan2( ball.pos.y - GTOP, ball.pos.y - FLEFT );
down_angle = atan2( ball.pos.y-keeper.pos.y, ball.pos.y-keeper.pos.y ) -atan2( ball.pos.y - GBOT, ball.pos.y - FLEFT );
if( fabs(up_angle) > fabs(up_angle) )
{
aimy=( GTOP + keeper.pos.y )/2;
}
else
{
aimy=( GBOT + keeper.pos.y )/2;
}
//分别讨论三个作为分母的减法式子是否为零
if( ball.pos.x == aimx && ball.pos.y == robot->pos.y )
{
x=ball.pos.x;
y=ball.pos.y;
}
else if( ball.pos.x == aimx )
{
k2=-(ball.pos.x-robot->pos.x)/(ball.pos.y-robot->pos.y);
b2=(ball.pos.x+robot->pos.y)/2+
( square(ball.pos.x)-square(robot->pos.x) )/(ball.pos.y-robot->pos.y)/2;
// x = aimx;
// y = k2*aimx+b2;
x=ball.pos.x;
y=ball.pos.y;
}
else if( ball.pos.y == robot->pos.y )
{
k1 = (ball.pos.y-aimy)/(ball.pos.x-aimx);
b1 = (ball.pos.x*aimy - ball.pos.y*aimx)/(ball.pos.x-aimx);
x = (robot->pos.y - b1)/k1;
y = robot->pos.y;
第 40 页
机器人足球仿真课程设计
}
else
{
k1 = (ball.pos.y-aimy)/(ball.pos.x-aimx);
b1 = (ball.pos.x*aimy - ball.pos.y*aimx)/(ball.pos.x-aimx);
k2=-(ball.pos.x-robot->pos.x)/(ball.pos.y-robot->pos.y);
b2=(ball.pos.x+robot->pos.y)/2+
( square(ball.pos.x)-square(robot->pos.x) )/(ball.pos.y-robot->pos.y)/2;
if( k1 == k2 )
{
x=ball.pos.x;
y=ball.pos.y;
}
else
{
x=(b2-b1)/(k1-k2);
y=(k1*b2-k2*b1)/(k1-k2);
}
}
if( fabs(d_angle) > 10 )
{
Position1_cz( robot, x, y );
if( leng(ball.pos.x,ball.pos.y,robot->pos.x,robot->pos.y) < L )//靠近球时,对速度修正 {
double aim_angle = atan( (aimy - ball.pos.x)/(aimx - ball.pos.x) ),
d_angle= aim_angle - robot->rotation,
k = 0.5 ;
robot->velocityLeft = robot->velocityLeft - k*d_angle ;
robot->velocityRight = robot->velocityRight + k*d_angle ;
}
}
else
{
Position1( robot, aimx, aimy );
}
}
//************************************************************************// void NormalGame_Right_wl( Environment *env )
{
Attack1( &env->home[1], env);
Attack2( &env->home[2], env);
Attack3( &env->home[3], env);
第 41 页
机器人足球仿真课程设计
Attack4( &env->home[4], env);
}
void Attack1( Robot *robot , Environment *env)
{
Vector3D t=env->currentBall.pos;
double vl=0, vr=0;
double r=robot->rotation;
if(r<0)r+=360;
if(r>360)r-=360;
if(t.y>env->fieldBounds.top-2.5)t.y=env->fieldBounds.top-2.5;
if(t.y<env->fieldBounds.bottom+2.5)t.y=env->fieldBounds.bottom+2.5;
if(t.x>env->fieldBounds.right-3)t.x=env->fieldBounds.right-3;
if(t.x<env->fieldBounds.left+3)t.x=env->fieldBounds.left+3;
double dx = robot->pos.x-t.x;
double dy = robot->pos.y-t.y;
if(env->currentBall.pos.x>25&&env->currentBall.pos.x<50) //当球在对方半场禁区线后
{
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<25)
{
Defence1_Right( robot, env );
}
if(env->currentBall.pos.y>60&&env->currentBall.pos.y<77)
{
Defence1_Right( robot, env );
}
else
{
Defence1_Right( robot, env );
}
}
//当球在对方半场禁区线内
if(env->currentBall.pos.x > env->fieldBounds.left&&env->currentBall.pos.x<25)
{
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<25)
第 42 页
机器人足球仿真课程设计
{
Position1_cz(robot, env->currentBall.pos.x+18,42);
Angle1(robot, (int) (180/PI*atan2(robot->pos.y - 42, robot->pos.x - 6.5 )) ); }
if(env->currentBall.pos.y>60&&env->currentBall.pos.y<77)
{
Position1_cz(robot, env->currentBall.pos.x+15,20);
Angle1(robot, (int) (180/PI*atan2(robot->pos.y - 42, robot->pos.x - 6.5 ))); }
else if(env->currentBall.pos.y>25&&env->currentBall.pos.y<60)
{
shoot(robot, env);
}
}
else
半场
{
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<24)
{
Defence1_Right( robot, env );
}
else if(env->currentBall.pos.y>60&&env->currentBall.pos.y<77)
{
Defence1_Right( robot, env );
}
else
{
Position1_cz(robot, env->currentBall.pos.x+5, env->currentBall.pos.y );
}
}
}
void Attack2( Robot *robot , Environment *env)
{
Vector3D t=env->currentBall.pos;
double vl=0,vr=0;
double r=robot->rotation;
if(r<0)r+=360;
if(r>360)r-=360;
//当球在我方 第 43 页
机器人足球仿真课程设计
if(t.y>env->fieldBounds.top-2.5)t.y=env->fieldBounds.top-2.5;
if(t.y<env->fieldBounds.bottom+2.5)t.y=env->fieldBounds.bottom+2.5;
if(t.x>env->fieldBounds.right-3)t.x=env->fieldBounds.right-3;
if(t.x<env->fieldBounds.left+3)t.x=env->fieldBounds.left+3;
double dx = robot->pos.x-t.x;
double dy = robot->pos.y-t.y;
if(env->currentBall.pos.x>6.5&&env->currentBall.pos.x<50) //当球在对方半场
{
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<25) //当球对方下半场且在机器人右边
{
pass(robot, env);
}
else if(env->currentBall.pos.y>60&&env->currentBall.pos.y<77)
{
pass(robot, env);
}
else
{
shoot(robot, env);
}
}
else //当球在我方半场
{
Defence1_Right( robot, env );
}
}
void Attack3( Robot *robot , Environment *env)
{
Vector3D t=env->currentBall.pos;
double vl=0, vr=0;
double r=robot->rotation;
if(r<0)r+=360;
if(r>360)r-=360;
第 44 页
机器人足球仿真课程设计
if(t.y>env->fieldBounds.top-2.5)t.y=env->fieldBounds.top-2.5;
if(t.y<env->fieldBounds.bottom+2.5)t.y=env->fieldBounds.bottom+2.5;
if(t.x>env->fieldBounds.right-3)t.x=env->fieldBounds.right-3;
if(t.x<env->fieldBounds.left+3)t.x=env->fieldBounds.left+3;
double dx = robot->pos.x-t.x;
double dy = robot->pos.y-t.y;
if(env->currentBall.pos.x>6.5&&env->currentBall.pos.x<50) //当球在对方半场 {
Position1_cz(robot, env->currentBall.pos.x , env->currentBall.pos.y);
}
else //当球在我方半场 {
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<42)
{
Defence1_Right( robot, env );
}
else
{
Defence1_Right( robot, env );
}
}
}
void Attack4( Robot *robot , Environment *env)
{
Vector3D t=env->currentBall.pos;
double vl=0, vr=0;
double r=robot->rotation;
if(r<0)r+=360;
if(r>360)r-=360;
if(t.y>env->fieldBounds.top-2.5)t.y=env->fieldBounds.top-2.5;
if(t.y<env->fieldBounds.bottom+2.5)t.y=env->fieldBounds.bottom+2.5;
if(t.x>env->fieldBounds.right-3)t.x=env->fieldBounds.right-3;
if(t.x<env->fieldBounds.left+3)t.x=env->fieldBounds.left+3;
第 45 页
机器人足球仿真课程设计
double dx = robot->pos.x-t.x;
double dy = robot->pos.y-t.y;
if(env->currentBall.pos.x>25&&env->currentBall.pos.x<50) //当球在对方半场禁区线后
{
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<25)
{
Defence1_Right( robot, env );
}
if(env->currentBall.pos.y>60&&env->currentBall.pos.y<77)
{
Defence1_Right( robot, env );
}
else
{
shoot(robot, env);
}
}
//当球在对方半场禁区线内
else if(env->currentBall.pos.x >6.5&&env->currentBall.pos.x<25)
{
if(env->currentBall.pos.y>6.5&&env->currentBall.pos.y<25)
{
Position1_cz(robot, env->currentBall.pos.x + 15,65);
Angle1(robot, (int) (180/PI*atan2(robot->pos.y - 42, robot->pos.x - 6.5 )));
}
if(env->currentBall.pos.y>60&&env->currentBall.pos.y<77)
{
Position1_cz(robot, env->currentBall.pos.x+18,42);
Angle1(robot, (int) (180/PI*atan2(robot->pos.y - 42, robot->pos.x - 6.5 )));
}
else //当球进入中间时,抢点
{
shoot(robot, env);
}
}
else //当球在我方半场
第 46 页
机器人足球仿真课程设计
{
Position1_cz(robot, 50, 42);
}
}
void shoot(Robot *robot , Environment *env)
{
double x=10, y=10;
double K1= (env->currentBall.pos.y - 42)/(env->currentBall.pos.x - 20),
K2=(env->currentBall.pos.x - robot->pos.x)/(robot->pos.y - env->currentBall.pos.y);
double b1 = 42 - 20 * K1,b2=(env->currentBall.pos.y +robot->pos.y)/2 - K2 * (env->currentBall.pos.x + robot->pos.x)/2;
// y = x * K1 + b1;
// y = x * K2 + b2;
if(K1=K2)
{
Position1_cz(robot, env->currentBall.pos.x, env->currentBall.pos.y);
}
else
{
x = (b1-b2)/(K1-K2);
y = K1*(b1-b2)/(K1-K2)+b1;
Defence1_Right( robot, env );
}
}
void pass(Robot *robot , Environment *env)
{
double x=10, y=10;
double K1= (env->currentBall.pos.y - 42)/(env->currentBall.pos.x - 20),
K2=(env->currentBall.pos.x - robot->pos.x)/(robot->pos.y - env->currentBall.pos.y);
double b1 = 42 - 20 * K1,b2=(env->currentBall.pos.y +robot->pos.y)/2 - K2 * (env->currentBall.pos.x + robot->pos.x)/2;
// y = x * K1 + b1;
// y = x * K2 + b2;
if(K1=K2)
{
Position1_cz(robot, env->currentBall.pos.x, env->currentBall.pos.y);
}
第 47 页
机器人足球仿真课程设计
else
{
x = (b1-b2)/(K1-K2);
y = K1*(b1-b2)/(K1-K2)+b1;
Defence1_Right( robot, env );
}
}
//**************************************************************************************//
void Goalkeeper_right_hx(Robot *robot,Environment *env)
{
double velocityLeft=0.0,velocityRight=0.0;
double dx=env->currentBall.pos.x-env->lastBall.pos.x,
dy=env->currentBall.pos.y-env->lastBall.pos.y;
double lastball=env->currentBall.pos.y+dy*env->currentBall.pos.x/dx;
if(robot->pos.x==93.4259)
{
if(env->currentBall.pos.x>55.0&&env->currentBall.pos.y>7.00&&env->currentBall.pos.y<70.0)//禁区内
{
if(dx>0&&env->currentBall.pos.x<93.4259-2.95)
{
if(lastball<27.56+7||lastball>43.31+7)//超过球门
{
velocityLeft=0;
velocityRight=0;
}
else
{
double a=0;
a=(lastball-robot->pos.y)*dx/env->currentBall.pos.x*70;
if(a>125||a<-125)
{
velocityLeft=velocityRight=125*dy/fabs(dy);
}
else
{
velocityRight=velocityLeft=a;
第 48 页
机器人足球仿真课程设计
}
}
}
else if(dx>=0.0&&env->currentBall.pos.x>=93.4-2.95)
{
if(lastball<27.56+7&&env->currentBall.pos.y<27.56+7||lastball>43.31+7&&env->currentBall.pos.y>49.6801)
{
//GoaliePosition(robot,env->currentBall.pos.x,env->currentBall.pos.y); Position1_cz( robot ,env->currentBall.pos.x,env->currentBall.pos.y ); //Angle1(robot,0);
}
else
{
velocityLeft=velocityRight=-125*dy/fabs(dy);
}
}
else if(dx<0.0&&env->currentBall.pos.x>=93.4-2.95)
{
velocityLeft=velocityRight=-125*dy/fabs(dy);
}
else if(dx==0.0&&env->currentBall.pos.x>=93.4-2.95)
{
if(robot->pos.y=env->currentBall.pos.x)
{
velocityLeft=velocityRight=-125*dy/fabs(dy);
}
else
{
Position1_cz( robot ,env->currentBall.pos.x,env->currentBall.pos.y );
Angle1(robot,90);
}
}
else if(dx<0&&env->currentBall.pos.x<93.4-2.95)
{
double dy_a=env->currentBall.pos.y-49.6801;
double dy_b=env->currentBall.pos.y-33.9320;
double x=93.4259-env->currentBall.pos.x;
double d_ea=sqrt(dy_a*dy_a+x*x);
double d_eb=sqrt(dy_b*dy_b+x*x);
第 49 页
机器人足球仿真课程设计
double aimplace=33.9320+(49.6801-33.9320)*d_eb/(d_eb+d_ea); //GoaliePosition(robot,93.4259,aimplace);
Position1_cz( robot ,94.0,aimplace );
Angle1(robot,90);
}
else
{
if(env->currentBall.pos.y>50.5&&dy<0)
{
velocityLeft=velocityRight=125;
}
else if(env->currentBall.pos.y<32.50&&dy>0)
{
velocityLeft=velocityRight=-125;
}
else
{
velocityLeft=velocityRight=0;
}
}
}
}
else
{
double Ax=env->currentBall.pos.x-robot->pos.x;
double Ay=env->currentBall.pos.y-robot->pos.y;
double aimplace=Ay*(93.4259-env->currentBall.pos.x)/Ax+env->currentBall.pos.y; if(aimplace>=33.9320&&aimplace<=49.6801)
{
//GoaliePosition(robot,93.4259,aimplace);
Position1_cz( robot ,94.00,aimplace );
//
//Angle1(robot,90);
}
else
{
////GoaliePosition(robot,94.0,41.806);
Position1_cz( robot ,94.0, 41.806 );
//Angle1(robot,90);
}
}
第 50 页
机器人足球仿真课程设计
}
void Goalkeeper_Right_py2( Robot *robot, Environment *env )//右队总策略//守门员(二)的程序
{
double vl=0, vr=0, dy =robot->pos.y ,dx = robot->pos.x,K=50 ;
double gx = env->currentBall.pos.x - env->lastBall.pos.x;
double gy = env->currentBall.pos.y - env->lastBall.pos.y;
GoaliePosition(robot,94.5,41.6 ); //调整初始位置
if(env->currentBall.pos.x<(FRIGHT+FLEFT)/2+3.0)//球在对方半场
{
if(((fabs( robot->pos.x - 94.5)) <= 1.0)&&((fabs( robot->pos.y - 41.6)) <= 1.0))
{
Angle1(robot,90);
}
}
else if(env->currentBall.pos.x>(FRIGHT+FLEFT)/2)//球在己方半场
{
vl = -K*( dy - env->currentBall.pos.y);
vr = -K*( dy - env->currentBall.pos.y);
Velocity( robot, vl, vr );
}
}
第 51 页
机器人足球仿真课程设计
6.总结
6.1存在的不足
(1) 定位函数在Shoot时,击球定位的过程中精度很低,有时会丢球。
(2) 精确到点的位置函数还有待进一步改善,现阶段在实现时还有一定的误差,有
一定的振荡现象。
(3) 守门员角度校正函数还有待改善,现阶段其实现角度校正时不分前后,导致守
门员有时遭碰撞后复位前后相反,不能很好防守。
6.1我们的体会
我们的程序,建立在稳定、快速的底层函数中,并且在强大有效的上层策略分配下,结合动态分配角色技术,组织成为一支能与正式比赛队相抗衡的仿真机器人足球队。另外,改进了现有的定位运动函数,使我方进攻、防守的整体性能大幅提高;自行编写射门函数,使机器人具备智能射门攻击能力;采用了动态分配技术,对高效进攻进行了有益的尝试;新增了机器人“独立思考-综合攻防”的新思路,为今后高级智能化足球机器人进行了一次超前并且大胆的尝试。
同时,我们也有一些策略与功能未能付诸实现,如利用一阶微分量来精确控制下车的运动没有实现;在动态分配角色时,还只是逐次刷新分配,没有保证角色连续性的算法;守门员程序还没有做好精确有效的纵向运动的专用底层程序。
小组在熟悉C++界面、学习现有代码、尝试编写、正式编写、组合调试的各个过程中,相互交流、帮助,各自充分发挥自己的长处,最后让我们小组在并不很长的时间内很好的完成了课题任务。
我们最大的收获还在于,在实际编写中领悟模块化思想,在实际的合作中体会编程规范性的重要性,由此建立起的工程学意识将使我们受益终生。大家在一起的这段时间中,团队的有机合作让我们在看似繁琐、枯燥的过程中不仅收获了知识,更收获了快乐。
我相信这次的课程设计的完成并不是终点,而是一个新的起点,在这个起点上,我们会更加努力的学习与实践,让自己更加优秀。
7.参考文献
[1] 周纯杰 刘正林 何顶新 周凯波 编著.标准C语言程序设计及应用.武汉:华中科技大学出版社,2005.3
[2]
[3] 谭浩强编著.C语言程序设计.北京:清华大学出版社,1999 吴丽娟.基于机器人足球比赛的进攻策略的实现,微计算机应用2002.11: (第23卷第6期).
第 52 页
一、选题背景与意义(说明所选课题的历史背景、国内外研究现状和发展趋势)1选题背景随着我国网球运动的普及和发展,越来越多的人参加了网…
重庆三峡学院本科生毕业论文开题报告重庆三峡学院毕业论文设计题目现代足球中后卫防守与进攻特征的研究所在系体育系专业体育教育学号作者姓…
教学研究方法开题报告教学研究方法开题报告报告题目论足球全球化之下的中国足球学院体育科学学院专业体育教育年级班10级学号姓名指导教师…
菏泽学院HezeUniversity本科生毕业论文开题报告题目姓名职业化以来中国职业足球运动员留洋特征与对策分析高清信学号20xx…
学院毕业论文开题报告表课题来源1教师拟订2学生建议3企业和社会征集4科研单位提供课题类型1A工程设计艺术设计B技术开发C软件工程D…
教学研究方法开题报告教学研究方法开题报告报告题目论足球全球化之下的中国足球学院体育科学学院专业体育教育年级班10级学号姓名指导教师…
衡阳师范学院本科生毕业论文开题报告衡阳师范学院毕业论文设计题目现代足球中后卫防守与进攻特征的研究所在系体育系专业体育教育学号作者姓…
上海金融学院信息管理系毕业论文设计开题报告论文题目足球俱乐部管理信息系统的设计与实现学生姓名孙瑞学号20xx122230指导教师赵…
南京大学毕业生论文开题报告范文开题报告是指开题者对科研课题的一种文字说明材料这是一种新的应用写作文体这种文字体裁是随着现代科学研究…