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 20:01:36 浏览: 44
C++实现的一款为目标检测与目标跟踪任务打造的视频标记工具源代码,窗口界面使用Qt开发 系统依赖ffmpeg和opencv库
可以尝试使用QPropertyAnimation来实现agv在坐标点之间的平滑移动,修改代码如下:
```
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);
QPropertyAnimation *animation = new QPropertyAnimation(agvs[i], "pos");
animation->setDuration(time);
animation->setStartValue(QPoint(cur_x*25+200-(nodeSpacing-nodeSize)/2, (cur_y+1)*25+50-(nodeSpacing-nodeSize)/2));
animation->setEndValue(QPoint(next_node->x*25+200-(nodeSpacing-nodeSize)/2, (next_node->y+1)*25+50-(nodeSpacing-nodeSize)/2));
animation->start(QAbstractAnimation::DeleteWhenStopped);
connect(animation, &QPropertyAnimation::finished, [this, i, next_node, power_consumption]() {
//MAP[next_node->x][next_node->y] = 1;
this->update(); // 在窗口中重绘
});
paths[i].erase(paths[i].begin());
}
}
```
这里使用QPropertyAnimation实现agv的平滑移动,将agv的位置属性作为动画属性。动画的起始值和结束值分别为当前位置和下一个节点的位置。动画的持续时间为从当前位置到下一个节点的距离除以agv的速度。当动画完成时,更新agv的电量并在窗口中重绘。同时,将已经到达的节点从路径列表中删除。
阅读全文