void MainWindow::moveAgvs_(){ Astar astar; std::vector<std::vector<Node*>> paths(agvs.size()); // 得到agv的路綫 for (int i = 0; i < agvs.size(); i++) { Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY()); std::vector<Node*> path_to_start = astar.getPath(start_node, end_node); std::vector<Node*> path_to_end = astar.getPath(end_node, end_node1); // 去掉 end_node1 path_to_end.erase(path_to_end.begin()); std::vector<Node*> path; path.insert(path.end(), path_to_start.begin()+1 , path_to_start.end()); path.insert(path.end(), path_to_end.begin(), path_to_end.end()); paths[i] = path; //輸出agv的路綫 std::cout << "AGV " << i << " path: "; for (int j = 0; j < path.size(); j++) { std::cout << "(" << path[j]->x << ", " <<path[j]->y << ")"; if (j != path.size() - 1) { std::cout << " -> "; } } std::cout << std::endl; } // 模擬運動 timer =new QTimer(this); timer->start(500); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);} void MainWindow::moveAgvs() { 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, = { agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); std::cout << "A " << agvs[i].getid() << " next_node : " << next_node->x <<" , " << next_node->y << std::endl; std::cout << "AGV " << agvs[i].getid() << " current_x: " << agvs[i].getCurrentX() << " current_y: " << agvs[i].getCurrentY() << std::endl; this->update(); if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) { //task_to_agv(i); } }); } } },paths未識別項怎麽修改
时间: 2023-07-15 09:15:43 浏览: 256
在您的代码中,函数moveAgvs_()中定义了一个名为paths的std::vector<std::vector<Node*>>类型的变量,存储了每个agv的路线信息,但是在moveAgvs()函数中未定义paths变量,导致编译错误。
解决方法:您可以将paths变量定义为MainWindow类的私有变量,在类定义中添加如下代码:
```c++
private:
std::vector<std::vector<Node*>> paths;
```
然后在moveAgvs_()函数中将paths赋值给私有变量即可,修改后的代码如下:
```c++
void MainWindow::moveAgvs_(){
Astar astar;
paths.resize(agvs.size()); // 得到agv的路线
for (int i = 0; i < agvs.size(); i++) {
Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY());
Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY());
Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY());
std::vector<Node*> path_to_start = astar.getPath(start_node, end_node);
std::vector<Node*> path_to_end = astar.getPath(end_node, end_node1);
// 去掉 end_node1
path_to_end.erase(path_to_end.begin());
std::vector<Node*> path;
path.insert(path.end(), path_to_start.begin()+1 , path_to_start.end());
path.insert(path.end(), path_to_end.begin(), path_to_end.end());
paths[i] = path;
//輸出agv的路綫
std::cout << "AGV " << i << " path: ";
for (int j = 0; j < path.size(); j++) {
std::cout << "(" << path[j]->x << ", " <<path[j]->y << ")";
if (j != path.size() - 1) {
std::cout << " -> ";
}
}
std::cout << std::endl;
}
// 模擬運動
timer = new QTimer(this);
timer->start(500);
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);
}
void MainWindow::moveAgvs() {
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, [=]{
agvs[i].setCurrentX(next_node->x);
agvs[i].setCurrentY(next_node->y);
std::cout << "A " << agvs[i].getid() << " next_node : " << next_node->x <<" , " << next_node->y << std::endl;
std::cout << "AGV " << agvs[i].getid() << " current_x: " << agvs[i].getCurrentX() << " current_y: " << agvs[i].getCurrentY() << std::endl;
this->update();
if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) {
//task_to_agv(i);
}
});
paths[i].erase(paths[i].begin());
}
}
}
```
阅读全文