void MainWindow::moveAgvs_(){ timer =new QTimer(this); timer->start(100); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);} 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++) { 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; } //模擬小車行駛 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(); // 在窗口中重绘 }); } } } },黨agv小車到達最後一個任務的終點時候,結束qtime
时间: 2023-12-06 21:45:37 浏览: 63
您可以在到达最后一个任务的终点时停止计时器,具体方法如下:
1. 在MainWindow类中添加一个成员变量,例如bool isFinished,表示任务是否完成。
2. 在任务完成的地方设置isFinished为true。
3. 在moveAgvs函数中,在处理完所有AGV的路径后,判断isFinished是否为true,如果为true,则停止计时器。
示例代码如下:
```
void MainWindow::moveAgvs_() {
timer = new QTimer(this);
timer->start(100);
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);
}
void MainWindow::moveAgvs() {
Astar astar;
std::vector<std::vector<Node*>> paths(agvs.size());
// 得到agv的路綫
for (int i = 0; i < agvs.size(); i++) {
// 省略部分代码
}
// 模擬小車行駛
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;
QTimer::singleShot(time, this, [&, i, next_node] {
agvs[i].setCurrentX(next_node->x);
agvs[i].setCurrentY(next_node->y);
this->update(); // 在窗口中重绘
if (i == agvs.size() - 1 && next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) {
isFinished = true;
}
if (isFinished) {
timer->stop();
}
});
}
}
}
```
阅读全文