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.02)); // 计算需要移动的步数 float power_consumption = distance / 1; for (int j = 1; j <= steps; j++) { const int MOVE_INTERVAL_MILLISECONDS = 20; // 每步移动间隔为20ms std::this_thread::sleep_for(std::chrono::milliseconds(MOVE_INTERVAL_MILLISECONDS)); 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: " << i + 1 << "(" << new_x << "," << new_y << ")" << endl; // 更新电量 if (agvs[i].battery_ - power_consumption / steps < 0) { agvs[i].setbattery(0); } else { agvs[i].setbattery(agvs[i].battery_ - power_consumption / steps); } this->update(); // 在窗口中重绘 if (agvs[i].getBattery() > 0 && std::floor(new_x) == new_x && std::floor(new_y) == new_y) { // 如果new_x和new_y为整数型,将其转换为浮点型后调用moveAgv()函数 moveAgvs(i); } } }翻譯一下
时间: 2024-02-10 20:33:38 浏览: 25
这是一段 C++ 代码,用于模拟 AGV(自动引导车)的移动。在此代码中,每个 AGV 都有一个路径,该路径由一组节点构成。代码通过以下步骤移动 AGV:
- 获取 AGV 的当前位置(cur_x和cur_y)以及要移动到的下一个节点(next_node)。
- 计算 AGV 的速度(speed)和到达下一个节点所需的距离(distance)。
- 根据速度计算需要移动的步数(steps)。
- 计算移动过程中的电力消耗(power_consumption)。
- 循环移动 AGV,每次移动一个步长,直到到达下一个节点。在每个步长中,代码会休眠一段时间(MOVE_INTERVAL_MILLISECONDS),然后更新 AGV 的位置,并更新电池电量。
- 如果 AGV 的电量充足,并且移动后的位置为整数坐标,则调用 moveAgvs() 函数。
总之,这段代码模拟了 AGV 的移动,并更新其状态。
相关问题
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.015)); // 计算需要移动的步数 float power_consumption = distance / 20; //_MAP[cur_x][cur_y] = 1; for (int j = 1; j <= steps; j++) { float time = j *100 / 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(); // 在窗口中重绘 }); } }小車隨著new_x的坐標移動,爲什麽會出現倒退的情況,怎麽修改代碼
在代码中,小车的移动是通过计算每一步所需的时间,然后使用`QTimer::singleShot`函数设置定时器,在每个时间间隔内更新小车的位置实现的。如果出现小车出现倒退的情况,可能是因为小车在前面的步骤中没有完全到达目标节点,但下一步已经开始了,导致小车回到了之前的位置。
为了解决这个问题,可以在计算下一步所需的时间之前,先判断小车是否已经到达了目标节点。如果小车已经到达了目标节点,那么就可以将该节点从路径列表中删除,然后再计算下一步的位置。
修改代码如下:
```c++
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.015)); // 计算需要移动的步数
float power_consumption = distance / 20;
//_MAP[cur_x][cur_y] = 1;
// 判断是否到达目标节点
if (distance <= 0.01) {
paths[i].erase(paths[i].begin());
return;
}
for (int j = 1; j <= steps; j++) {
float time = j *100 / speed;
QTimer::singleShot(time, this, &, i, next_node, cur_x, cur_y, power_consumption, j, steps {
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(); // 在窗口中重绘
});
}
}
```
这样,当小车到达目标节点时,就会将该节点从路径列表中删除,避免了出现倒退的情况。
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(); // 在窗口中重绘 }); },添加代碼,黨agv移動3個坐標的時候,電量減1
您可以在代码中增加以下内容来实现您的要求:
```c++
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 / 3.0;
//_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].getPower() - power_consumption);
this->update(); // 在窗口中重绘
});
}
```
这段代码会在 AGV 移动时,根据移动的距离计算电量的减少量,并将电量减去该值来更新 AGV 的电量。其中,电量的减少量通过计算 AGV 移动的距离并除以 3 来得到。