if (! paths[i].empty()) { double cur_x = agvs[i].current_x; double cur_y = agvs[i].current_y; Node* next_node = paths[i][0]; float speed = agvs[i].getSpeed(); float distance = sqrt(pow(next_node->x - cur_x, 2) + pow(next_node->y - cur_y, 2)); int steps = ceil(distance / (speed * 0.1)); // 计算需要移动的步数 float power_consumption = distance / 20; //_MAP[cur_x][cur_y] = 1; for (int j = 1; j <= steps; j++) { float time = j *500 / speed; // 每步所需时间 QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y, power_consumption, j, steps]() { // if (j == 1) { // //离开当前位置时将标记设为0 // //_MAP[cur_x][cur_y] = 0; // } float ratio = static_cast<float>(j) / steps; double new_x = cur_x + ratio * (next_node->x - cur_x); double new_y = cur_y + ratio * (next_node->y - cur_y); agvs[i].setCurrentX(new_x); agvs[i].setCurrentY(new_y); std:: cout <<"AGV"<<"("<< new_x << "," << new_y << ")" << endl; // 更新电量 agvs[i].setpower(agvs[i].power_ - power_consumption / steps); this->update(); // 在窗口中重绘 }); } },AGV(1,1.5) AGV(1,2) AGV(1,1.5) AGV(1,2) AGV(1,2.5) AGV(1,2.5) AGV(1,3),爲什麽agv會出現倒退的情況,怎麽修改
时间: 2024-01-17 20:05:16 浏览: 58
为了避免AGV倒退,需要在每次移动时将原先位置的标记清除。可以通过以下方式解决:
1. 在每次移动前,将当前位置的标记清除,即_MAP[cur_x][cur_y] = 0。
2. 在移动到下一个节点时,将该节点的位置标记为1,即_MAP[next_node->x][next_node->y] = 1。
修改后的代码如下:
```
if (! paths[i].empty()) {
double cur_x = agvs[i].current_x;
double cur_y = agvs[i].current_y;
Node* next_node = paths[i][0];
float speed = agvs[i].getSpeed();
float distance = sqrt(pow(next_node->x - cur_x, 2) + pow(next_node->y - cur_y, 2));
int steps = ceil(distance / (speed * 0.1)); // 计算需要移动的步数
float power_consumption = distance / 20;
//_MAP[cur_x][cur_y] = 1;
for (int j = 1; j <= steps; j++) {
float time = j *500 / speed; // 每步所需时间
QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y, power_consumption, j, steps]() {
//_MAP[cur_x][cur_y] = 0;
float ratio = static_cast<float>(j) / steps;
double new_x = cur_x + ratio * (next_node->x - cur_x);
double new_y = cur_y + ratio * (next_node->y - cur_y);
agvs[i].setCurrentX(new_x);
agvs[i].setCurrentY(new_y);
std:: cout <<"AGV"<<"("<< new_x << "," << new_y << ")" << endl;
// 更新电量
agvs[i].setpower(agvs[i].power_ - power_consumption / steps);
//_MAP[next_node->x][next_node->y] = 1;
this->update(); // 在窗口中重绘
});
}
}
```
阅读全文