/// <summary> /// 计算路线 /// </summary> /// <param name="preRoute">之前的路径,用于判断是否有死循环</param> /// <param name="targetNum">目标位置</param> /// <param name="maxStep">计算的最大次数</param> /// <returns></returns> private string GetRoute(string preRoute, string targetNum, ref int maxStep)//函数重载 { int currStep = maxStep; if (targetNum != TargetNum)//不相等表明TargetNum被重新赋值了,需要重新计算路径,应该就是有一个新的任务 { //target changed, reset route //Route为空说明不是所有方向都计算过了,需要继续计算 Route = ""; } if (Route == NOROUTE)//route为“NOROUTE”表示经过计算没有可用路径,与route为空是两种情况,route为空是还未开始计算路径的状态 { return NOROUTE; } //到达目的地 if (TargetNum == CellNum) { this.Route = ROUTESPLIT + CellNum + ROUTESPLIT;//cellnum表示计算路径中循环遍历过程的动态方块坐标 } //如果路经不为空,说明之前已经计算出结果 if (Route != "") { //计算本次路径的长度 currStep = GetRouteStep(preRoute) + GetRouteStep(Route); //如果本次路径长度为最小长度, 则限定其它路径的计算步数 if (currStep < maxStep)//maxstep为当前可用路径的计算步数 { maxStep = currStep; } //返回路径 return Route; } string newRoute = NOROUTE; //Route为空的情况 preRoute = preRoute + CellNum + ROUTESPLIT; currStep = GetRouteStep(preRoute); if (currStep >= maxStep)//表示现有路径长度大于已有路径的长度,此判断为确保选择最短路径 { //路线长度超出范围 return NOROUTE; } Cell c; Route route = new Route(); //计算向上的路径 if (UpRoute == "") { c = UpCell; if (c != null) { //route.GetDirection(cmdId, c.CellNum); //if (route.cell_yf == -1) //{ if (preRoute.IndexOf(ROUTESPLIT + c.CellNum + ROUTESPLIT) == -1) { UpRoute = c.GetRoute(preRoute, targetNum, ref maxStep); } //} } else { UpRoute = NOROUTE; } } //计算向下的路径 if (DownRoute == "") { c = DownCell; if (c != null) { //route.GetDirection(cmdId, c.CellNum); //if (route.cell_yb == -1) //{ if (preRoute.IndexOf(ROUTESPLIT + c.CellNum + ROUTESPLIT) == -1) { DownRoute = c.GetRoute(preRoute, targetNum, ref maxStep); } //} } else { DownRoute = NOROUTE; } } //计算向右的路径 if (RightRoute == "") //为空表示未计算 { c = RightCell; if (c != null) {//查找c.CellNum在preRoute中出现的位置,返回-1表示没有找到,即preRoute中不包括c.CellNum,c.CellNum此处为rightcell的坐标位置,是为了保证不走回头路 //route.GetDirection(cmdId, c.CellNum); //if (route.cell_xf == -1) //{ if (preRoute.IndexOf(ROUTESPLIT + c.CellNum + ROUTESPLIT) == -1) //如果返回值大于0,说明历史路径已经包含本节点,需要排除,否则会有死循环 { RightRoute = c.GetRoute(preRoute, targetNum, ref maxStep); } //} } else { //无右边节点,向右无法到达目的地 RightRoute = NOROUTE; } } //计算向左的路径 if (LeftRoute == "") { c = LeftCell; if (c != null) { //route.GetDirection(cmdId, c.CellNum); //if (route.cell_xb == -1) //{ if (preRoute.IndexOf(ROUTESPLIT + c.CellNum + ROUTESPLIT) == -1) { LeftRoute = c.GetRoute(preRoute, targetNum, ref maxStep); } //} } else { LeftRoute = NOROUTE; } } //计算最短路径 string r = GetShortRoute(GetShortRoute(LeftRoute, RightRoute), GetShortRoute(UpRoute, DownRoute)); if (r == NOROUTE) { newRoute = r;//newRoute用来表示计算最终路径 } else { newRoute = ROUTESPLIT + CellNum + r; //计算当前路径的步数 currStep = currStep + GetRouteStep(newRoute); if (currStep < maxStep) { maxStep = currStep; } } if (LeftRoute != "" && RightRoute != "" && UpRoute != "" && DownRoute != "") { //四个方向都计算过了,保存路径,避免下次重复计算 this.Route = newRoute; } return newRoute; }
/// <summary> /// 定时器回调函数 /// </summary> /// <param name="sender"></param> void timer_Elapsed(object sender) { //if (tcp != null && tcp.Connected) //{ // DataSet ds = GetTaskCmd();//获取发送命令 // string cmdId = ""; // int cnt = 0; // byte[] buffer = null; // for (int i = 0; i < ds.Tables[0].Rows.Count; i++) // { // if (CanSendCmd((byte[])ds.Tables[0].Rows[i]["status"]))//判断小车状态是否能接收命令 // { // buffer = (byte[])ds.Tables[0].Rows[i]["cmd"]; // workStream.Write(buffer, 0, buffer.Length); // cmdId = Fn.ToString(ds.Tables[0].Rows[i]["cmd_id"]); // cnt = Fn.ToInt(ds.Tables[0].Rows[i]["send_cnt"]) + 1; // UpdateCmdSendCnt(cmdId, cnt); //更新命令的发送次数 // } // } // //发送路权令牌 // DataSet dsToken = GetRoadRightCMD(); // byte[] bufferRight = null; // for (int j = 0; j < dsToken.Tables[0].Rows.Count; j++) // { // bufferRight = Asrv.GetSendData(Asrv.GetTokenCmd(Fn.ToInt(dsToken.Tables[0].Rows[j]["step"])), (byte[])(dsToken.Tables[0].Rows[j]["address"])); // cmdId = Fn.ToString(dsToken.Tables[0].Rows[j]["cmd_id"]); // if (CheckTokenSendCnt(cmdId, bufferRight)) //如果相同的令牌发送次数没有超过最大发送次数则发送路权令牌 // { // workStream.Write(bufferRight, 0, bufferRight.Length); // InsertCmdBreakdown(cmdId, bufferRight); // } // } //} //else //{ // Connect();//连接HOST并等待接收数据 //} string cmdId = GetCmdId(); //如果没有未分配的任务,只计算路权 if (cmdId == "0") { SetRoadRight(); UpdateCmd(); return; } Route r = new Route(); r.CalcRoute(cmdId); SetRoadRight(); UpdateCmd(); }