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); 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; QTimer::singleShot(time, this, &, i, next_node { agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); std::cout << "AGV " << agvs[i].getid() << " current_x: " << agvs[i].getCurrentX() << " current_y: " << agvs[i].getCurrentY() <<std::endl; this->update(); }); } } } },給出代碼畫出agv獲取path的路綫
时间: 2023-11-22 22:56:50 浏览: 61
这段代码是一个在 Qt 框架下实现的 AGV(自动引导车)的运动控制函数。其中,通过 A* 算法实现 AGV 的路径规划并将规划的路径存储在 `std::vector<std::vector<Node*>> paths` 中。
以下是代码注释和画出 AGV 路线的过程:
```c++
void MainWindow::moveAgvs_() {
timer = new QTimer(this);
timer->start(100); // 每 100 毫秒更新一次 AGV 的位置
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);
}
void MainWindow::moveAgvs() {
Astar astar; // 创建 A* 算法实例
std::vector<std::vector<Node*>> paths(agvs.size()); // 存储 AGV 的路径
// 得到 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);
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;
QTimer::singleShot(time, this, [&, i, next_node]() {
agvs[i].setCurrentX(next_node->x);
agvs[i].setCurrentY(next_node->y);
std::cout << "AGV " << agvs[i].getid() << " current_x: " << agvs[i].getCurrentX() << " current_y: " << agvs[i].getCurrentY() << std::endl;
this->update();
});
}
}
}
```
AGV 路线的绘制过程,可以在 Qt 的界面上通过 `paintEvent` 函数实现。在 `paintEvent` 函数内部,我们可以使用 `QPainter` 类绘制出 AGV 的路线。
```c++
void MainWindow::paintEvent(QPaintEvent* event) {
QPainter painter(this);
QPen pen_black(Qt::black, 1, Qt::SolidLine); // 黑色画笔
QPen pen_red(Qt::red, 3, Qt::SolidLine); // 红色画笔
// 绘制 AGV
for (int i = 0; i < agvs.size(); i++) {
int x = agvs[i].getCurrentX();
int y = agvs[i].getCurrentY();
painter.setPen(pen_black);
painter.drawEllipse(x - 5, y - 5, 10, 10);
painter.drawText(x - 5, y - 5, QString::number(agvs[i].getid()));
}
// 绘制 AGV 的路径
for (int i = 0; i < agvs.size(); i++) {
std::vector<Node*> path = paths[i];
if (!path.empty()) {
painter.setPen(pen_red);
Node* prev = path[0];
for (int j = 1; j < path.size(); j++) {
Node* next = path[j];
painter.drawLine(prev->x, prev->y, next->x, next->y);
prev = next;
}
}
}
}
```
阅读全文