//模擬小車行駛 for (int i = 0; i < agvs.size(); i++) { for (int j = 0; j < tasks.size(); j++) { if (tasks[j].id == agvs[i].get_task_id()) { completed_task_index = j; break; } } if (tasks[completed_task_index].completed == 2 ) { // 如果已经完成任务 paths[i].clear(); continue; // 跳过此次循环 } if (! paths[i].empty()) { int cur_x = agvs[i].getCurrentX(); int cur_y = agvs[i].getCurrentY(); 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; //_MAP[cur_x][cur_y] = 1; QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y]() { // 离开当前位置时将标记设为0 //_MAP[cur_x][cur_y] = 0; agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); this->update(); // 在窗口中重绘 }); } } }, 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-12-27 16:04:33 浏览: 40
物流agv小车模拟代码
可以在第一个for循环的内部添加如下代码:
```
if (!paths[i].empty() && paths[i][0] != nullptr) {
Node* next_node = paths[i][0];
painter.setPen(QPen(Qt::red, 2));
painter.drawLine(agvs[i].getCurrentX()*25+200, (agvs[i].getCurrentY()+1)*25+50, next_node->x*25+200, (next_node->y+1)*25+50);
}
```
这段代码会在每次AGV移动时,根据AGV当前位置和路径中下一个节点的位置画一条红线作为路径线路。需要注意的是,要在画线之前先判断paths[i]是否为空,和paths[i][0]是否为空指针,以避免程序崩溃。
阅读全文