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)); }} },添加代碼:黨path不爲空時,繪製agv從path獲取到的坐標兩點之間的直綫
时间: 2023-11-27 18:52:22 浏览: 46
你想要在 AGV 移动的过程中绘制 AGV 的运动轨迹,可以在 AGV 计算出路径后,通过路径中的节点坐标来绘制直线。
具体实现可以在第一个 for 循环中添加以下代码:
```c++
for (int i = 0; i < agvs.size(); i++) {
// ...
if (!paths[i].empty()) { // 当路径不为空时
for (int j = 0; j < paths[i].size() - 1; j++) {
Node* node1 = paths[i][j];
Node* node2 = paths[i][j + 1];
painter.drawLine(node1->getX() * 25 + 200, (node1->getY() + 1) * 25 + 50, node2->getX() * 25 + 200, (node2->getY() + 1) * 25 + 50);
}
}
// ...
}
```
这段代码会遍历路径中的节点,绘制相邻节点之间的直线。注意,在绘制直线时需要将节点的坐标转换为像素坐标。
相关问题
void MainWindow::moveAgvs() { Astar astar; std::vector<std::vector<Node*>> paths(agvs.size());,怎麽在別的函數里引用moveagvs的局部變量path
在C++中,局部变量只能在其所在的函数内部使用,不能在其他函数中直接引用。如果想要在其他函数中使用局部变量,有以下两种方法:
1. 将局部变量声明为类的成员变量,这样其他函数就可以通过类的对象访问该变量。
例如,在MainWindow类中声明一个std::vector<std::vector<Node*>> paths成员变量,并在moveAgvs函数中将其赋值,然后就可以在其他函数中通过MainWindow对象访问该变量:
```
class MainWindow {
public:
void moveAgvs();
std::vector<std::vector<Node*>> paths;
};
void MainWindow::moveAgvs() {
Astar astar;
paths.resize(agvs.size());
// ...
}
void otherFunction(MainWindow& mainWindow) {
// 使用MainWindow对象的成员变量paths
// ...
}
```
2. 将局部变量作为参数传递给其他函数。
例如,在其他函数中声明一个std::vector<std::vector<Node*>>类型的参数,然后在调用该函数时将局部变量paths作为参数传递:
```
void otherFunction(std::vector<std::vector<Node*>>& paths) {
// 使用传递进来的paths变量
// ...
}
void MainWindow::moveAgvs() {
Astar astar;
std::vector<std::vector<Node*>> paths(agvs.size());
// ...
otherFunction(paths);
}
```
void MainWindow::moveAgvs_(){ timer =new QTimer(this); timer->start(1000); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);} void MainWindow::moveAgvs() { Astar astar; std::vector<Node*> path; 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; if (agvs[i].getLoad() == false) { end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); } else { end_node = new Node(agvs[i].getEndX(), agvs[i].getEndY()); } std::vector<Node*> path = astar.getPath(start_node, end_node); 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; } 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(); if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) { //task_to_agv(i); } }); } } },agv沒有模擬運行,修改一下
有几个问题需要修正:
1. `moveAgvs_()`函数中,应该将`timer->start(1000)`放在`connect`语句的后面,因为如果`timeout`信号在`start()`之前被触发,会导致`moveAgvs()`函数在启动定时器之前就被调用。
2. 在`moveAgvs()`函数中,定义了一个名为`path`的局部变量,覆盖了函数开头定义的`std::vector<Node*> path`。因此,在后面对`paths`进行赋值时,实际上是没有生效的。应该将第6行的`std::vector<Node*> path;`修改为`path = astar.getPath(start_node, end_node);`。
3. 在`QTimer::singleShot()`函数的第三个参数中,应该将`&`去掉,并在lambda表达式中加入`i`和`next_node`的捕获列表。即将`&, i, next_node`修改为`[=, i, next_node]() mutable {`。
4. 在`QTimer::singleShot()`函数中,应该将花括号中的代码块用圆括号括起来,即将`{ agvs[i].setCurrentX(next_node->x);`修改为`{ agvs[i].setCurrentX(next_node->x); }`。
下面是修改后的代码:
```c++
void MainWindow::moveAgvs_()
{
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);
timer->start(1000);
}
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;
if (agvs[i].getLoad() == false)
{
end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY());
}
else
{
end_node = new Node(agvs[i].getEndX(), agvs[i].getEndY());
}
std::vector<Node*> path = astar.getPath(start_node, end_node);
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;
}
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, [=, i, next_node]() mutable {
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();
if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY())
{
//task_to_agv(i);
}
});
}
}
}
```
阅读全文