if (! paths[i].empty()) { double cur_x = agvs[i].getCurrentX(); double cur_y = agvs[i].getCurrentY(); 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 / 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(); // 在窗口中重绘 }); } },爲什麽輸出的currentx和y還會有整數AGV(1,1.08333) AGV(1,1.16667) AGV(1,1.25) AGV(1,1.33333) AGV(1,1.41667) AGV(1,1.5) AGV(1,1.58333) AGV(1,1.66667) AGV(1,1.75) AGV(1,1.83333) AGV(1,1.91667) AGV(1,2) AGV(1,2.08333) AGV(1,2.16667) AGV(1,2.25) AGV(1,2.33333) AGV(1,2.41667) AGV(1,2.5) AGV(1,2.58333) AGV(1,2.66667) AGV(1,2.75) AGV(1,2.83333) AGV(1,2.91667) AGV(1,3)
时间: 2023-12-07 17:03:28 浏览: 24
这是因为每次循环中,new_x和new_y都是通过浮点数运算得出的,但是在输出时没有进行四舍五入的处理。因此,如果new_x或new_y的小数部分小于0.5,那么输出的值就会是整数部分加上小数部分;如果小数部分大于等于0.5,那么输出的值就会是整数部分加上小数部分加1。
如果你想要输出更加精确的坐标值,可以使用C++中的round函数来进行四舍五入的处理。例如:
```
double new_x_rounded = round(new_x * 100) / 100;
double new_y_rounded = round(new_y * 100) / 100;
std::cout << "AGV" << "(" << new_x_rounded << "," << new_y_rounded << ")" << std::endl;
```
这会将new_x和new_y乘以100后进行四舍五入,再除以100输出。这样就可以得到更加准确的坐标值。
相关问题
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 来得到。
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(); // 在窗口中重绘
});
}
}
```
这样,当小车到达目标节点时,就会将该节点从路径列表中删除,避免了出现倒退的情况。