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 (agvs[i].getLoad()){ painter.drawPixmap(agvs[i].getCurrentX()*25+200-(nodeSpacing-nodeSize)/2,(agvs[i].getCurrentY()+1)*25+50-(nodeSpacing-nodeSize)/2,25,25,QPixmap(":/new/prefix1/agvload.png").scaled(25,25)); } else { painter.drawPixmap(agvs[i].getCurrentX()*25+200-(nodeSpacing-nodeSize)/2,(agvs[i].getCurrentY()+1)*25+50-(nodeSpacing-nodeSize)/2,25,25,QPixmap(":/new/prefix1/agv1.png").scaled(25,25)); }} },畫上agv從path獲取到的路徑
时间: 2023-11-27 15:51:36 浏览: 36
在上述代码中,moveAgvs() 函数中的 paths 存储了每个 AGV 的路径(使用 A*算法计算得到),接下来需要根据这些路径来绘制 AGV 的行驶轨迹。具体实现方法如下:
```c++
for (int i = 0; i < agvs.size(); i++) {
// 根据 AGV 路径绘制行驶轨迹
QPainterPath path;
for (int j = 0; j < paths[i].size(); j++) {
if (j == 0) {
path.moveTo(paths[i][j]->x * 25 + 200, paths[i][j]->y * 25 + 50);
} else {
path.lineTo(paths[i][j]->x * 25 + 200, paths[i][j]->y * 25 + 50);
}
}
// 将行驶轨迹画在界面上
painter.setPen(QPen(Qt::red, 3));
painter.drawPath(path);
// 绘制 AGV
if (agvs[i].getLoad()) {
painter.drawPixmap(agvs[i].getCurrentX()*25+200-(nodeSpacing-nodeSize)/2,
(agvs[i].getCurrentY()+1)*25+50-(nodeSpacing-nodeSize)/2,
25,25,QPixmap(":/new/prefix1/agvload.png").scaled(25,25));
} else {
painter.drawPixmap(agvs[i].getCurrentX()*25+200-(nodeSpacing-nodeSize)/2,
(agvs[i].getCurrentY()+1)*25+50-(nodeSpacing-nodeSize)/2,
25,25,QPixmap(":/new/prefix1/agv1.png").scaled(25,25));
}
}
```
在上述代码中,我们首先使用 QPainterPath 对象创建了 AGV 的行驶轨迹,然后将该轨迹绘制到界面上。接着,我们根据 AGV 的状态和位置绘制了 AGV 的图像。需要注意的是,AGV 的位置是以节点为单位进行存储的,所以绘制 AGV 时需要使用节点的位置进行计算。
阅读全文