void MainWindow::moveAgvs() { Astar astar; std::vector<std::vector<Node*>> paths(agvs.size()); // 得到agv的路綫 for (int i = 0; i < agvs.size(); i++) { if (agvs[i].getState() == false) { if (agvs[i].getLoad()){ //如果是負載的狀態,則任務的起點到任務的終點 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { agvs[i].setState(true); agvs[i].setLoad(false); tasks[i].setCompleted(2); task_to_agv(); } Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY()); std::vector<Node*> path_to_end = astar.getPath(start_node, end_node1); path_to_end.erase(path_to_end.begin()); std::vector<Node*> path; path.insert(path.end(), path_to_end.begin(), path_to_end.end()); paths[i] = path; } else { //如果是空載的狀態,則行駛到任務的起點 //如果agv已經到達任務起點,變爲負載狀態 if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { agvs[i].setLoad(true); } Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); std::vector<Node*> path_to_start = astar.getPath(start_node, end_node); std::vector<Node*> path; path.insert(path.end(), path_to_start.begin() + 1, path_to_start.end()); paths[i] = path; } } //模擬小車行駛 for (int i = 0; i < agvs.size(); i++) { if (! paths[i].empty()) { Node* next_node = paths[i][0]; float speed = agvs[i].getSpeed(); float distance = sqrt(pow(next_node->x - agvs[i].getCurrentX(), 2) + pow(next_node->y - agvs[i].getCurrentY(), 2)); float time = distance / speed * 1000; //node_Value[next_node->x][next_node->y] = 10; QTimer::singleShot(time, this, &, i, next_node { agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); this->update(); // 在窗口中重绘 }); } } } },輸出path的坐標
时间: 2023-12-06 09:45:21 浏览: 54
Qt5主窗体程序:文本编辑器的实现(Qt5开发及实例)
这段代码用于模拟小车行驶,会获取每辆小车的路径并将其行驶到目的地。为了输出路径的坐标,需要在获取路径的时候记录下来。代码中使用了一个二维向量 paths 来存储每辆车的路径,可以通过循环遍历 paths 中的每个向量,然后输出其中每个节点的坐标。
示例代码如下:
```
for (int i = 0; i < agvs.size(); i++) {
std::cout << "path of AGV " << i << ": ";
for (int j = 0; j < paths[i].size(); j++) {
std::cout << "(" << paths[i][j]->x << ", " << paths[i][j]->y << ") ";
}
std::cout << std::endl;
}
```
这段代码会输出每辆 AGV 的路径坐标。
阅读全文