⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avoidwall.cpp

📁 AI-CODE坦克机器人 《C++语言学习利器 —AI-CODE坦克机器人》-杜飞雪-源代码
💻 CPP
字号:
#include <airobot/cpp/SimpleRobot.hpp>

/**
 * 这是一个不会撞墙的机器人,演示了如何用迭代的方法控制机器人的移动
 * @author xiemin
 */
class AvoidWall : public SimpleRobot
{
	private:
		//计算目标点时采用的搜索半径
		static const double SEARCH_DIS;
		//每次迭代的递增量
		static const double GAP;
		//离墙的距离
		static const double AWAY_FROM_WALL;
	
	public:
		void onTick(TickAction* action)
		{
			//机器人的移动方向
			double moveHeading = getHeading();
			//如果机器人的速度小于0,说明机器人向后运动,移动的方向和车身方向相反
			if(getVelocity()<0) moveHeading += Math::PI;
			
			double bearing = 0;
			//进入迭代
			while(true)
			{
				//计算目标点
				Point2D destination = Math::nextPoint(getLocation(), 
					moveHeading+bearing, SEARCH_DIS);
				if(isValid(destination))
				{
					//目标点是有效的,迭代结束
					moveTo(destination);
					return;
				}
				else bearing += GAP; //进行下一次迭代
			}
		}
		
		/*
		 * 判断给定的点是否是一个有效的目标点
		 */
		bool isValid(const Point2D& point)
		{
			return inCourt(point, AWAY_FROM_WALL);
		}
};

//计算目标点时采用的搜索半径
const double AvoidWall::SEARCH_DIS = 100;
//每次迭代的递增量
const double AvoidWall::GAP = Math::toRadians(5);
//离墙的距离
const double AvoidWall::AWAY_FROM_WALL = 50;
		

//启动机器人程序
int main(int argC, char* argV[])
{
	Robot* robot = new AvoidWall();
	return startup(argC, argV, robot);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -