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)); },//模擬小車行駛 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; // 计算电量的减少量 float power_consumption = distance /20; //_MAP[cur_x][cur_y] = 1; QTimer::singleShot(time, this, &, i, next_node, cur_x, cur_y, power_consumption { // 离开当前位置时将标记设为0 //MAP[cur_x][cur_y] = 0; agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); // 更新电量 agvs[i].setpower(agvs[i].power- power_consumption); this->update(); // 在窗口中重绘 }); } },修改代碼為:讓agv實現在坐標點之間模擬移動,而不是一跳一跳
时间: 2024-02-17 17:00:34 浏览: 56
C++实现的一款为目标检测与目标跟踪任务打造的视频标记工具源代码,窗口界面使用Qt开发 系统依赖ffmpeg和opencv库
可以尝试将路径上的每个节点之间的距离拆分成多个小段来模拟移动,修改代码如下:
```
if (! paths[i].empty()) {
int cur_x = agvs[i].getCurrentX();
int cur_y = agvs[i].getCurrentY();
float speed = agvs[i].getSpeed();
Node next_node = paths[i][0];
float distance = sqrt(pow(next_node->x - cur_x, 2) + pow(next_node->y - cur_y, 2));
float power_consumption = distance / 20; // 计算电量的减少量
int steps = ceil(distance / speed); // 将路径拆分成多个小段
float step_x = (next_node->x - cur_x) / steps;
float step_y = (next_node->y - cur_y) / steps;
for (int step = 1; step <= steps; step++) {
int next_x = cur_x + step_x * step;
int next_y = cur_y + step_y * step;
QTimer::singleShot(step * 1000 / speed, this, [=]() {
// 更新 agv 的状态
agvs[i].setCurrentX(next_x);
agvs[i].setCurrentY(next_y);
agvs[i].setpower(agvs[i].power - power_consumption);
this->update(); // 在窗口中重绘
});
}
}
```
这样,agv 就会在路径上平滑地移动了。
阅读全文