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還會有整數
时间: 2024-01-27 13:05:23 浏览: 30
这是因为在计算新的坐标时,使用了浮点数运算,但是最终的坐标仍然是以double类型的形式存储的。而在输出时,cout默认只保留小数点后6位,因此看起来像是整数。如果你想输出更多的小数位数,可以使用cout的setprecision函数来设置精度,例如:
```
std::cout << std::setprecision(10) << "AGV" << "(" << new_x << "," << new_y << ")" << std::endl;
```
这将输出10位小数的坐标值。
相关问题
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(); // 在窗口中重绘
});
}
}
```
这样,当小车到达目标节点时,就会将该节点从路径列表中删除,避免了出现倒退的情况。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)