float time1 = j * 100.0 / speed;// 每步所需时间,std::this_thread::sleep_for(std::chrono::milliseconds(time1));,報錯怎麽修改
时间: 2024-02-05 18:05:18 浏览: 30
根据您提供的代码片段,我猜测您遇到的错误可能是以下两种情况之一:
1. 错误提示为“类型转换错误”或“无法从double转换为int”等,这是因为time1的类型为float而std::this_thread::sleep_for()函数需要接受一个整数类型的参数。您可以将time1强制转换为整数类型,例如:
```
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(time1)));
```
2. 错误提示为“找不到std::this_thread::sleep_for()函数”,这可能是因为您没有包含所需的头文件。请确保您的代码中包含以下头文件:
```
#include <chrono>
#include <thread>
```
希望这些信息能帮助您解决问题!
相关问题
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 * 1)); // 计算需要移动的步数 float power_consumption = distance / 20; //_MAP[cur_x][cur_y] = 1; for (int j = 1; j <= steps; j++) { float time = j * 100.0 / 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 << "("<< agvs[i].getCurrentX() << "," << agvs[i].getCurrentY() << ")" << endl; // 更新电量 agvs[i].setpower(agvs[i].power - power_consumption / steps); this->update(); // 在窗口中重绘 }); } }Qtimer的time是多少
在这段代码中,根据机器人的速度和移动距离计算出需要移动的步数 steps,然后通过循环变量 j 来迭代每一步的移动。在每一步移动中,通过计算每步所需时间 time,使用 QTimer::singleShot 函数来实现定时器,以便在一定的时间间隔后执行回调函数。
具体来说,每步所需时间 time 的计算公式为:
```float time = j * 100.0 / speed;```
其中,j 表示当前步数,100.0 是一个常数,表示每个时间单位的长度,speed 是机器人的速度。
因此,每步所需的时间间隔是根据机器人的速度和步数计算出来的,单位是毫秒。
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.01)); // 计算需要移动的步数 float power_consumption = distance / 20; for (int j = 1; j <= steps; j++) { float time = j * 100.0 / 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(); // 在窗口中重绘 }); } },添加代碼:在执行下一步移动前,先判断当前步骤的移动是否已经完成,如果未完成,就等待 QTimer 完成当前步骤的移动再进行下一步移动。
可以在每次启动 QTimer 时,将其返回的 QTimer 对象保存到一个数组中,然后在下一步移动前,判断前一步移动是否已完成,如果未完成,则等待 QTimer 完成当前步骤的移动再进行下一步移动。具体的代码如下:
```
QTimer* timer = nullptr;
QVector<QTimer*> timers;
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.01)); // 计算需要移动的步数
float power_consumption = distance / 20;
for (int j = 1; j <= steps; j++) {
float time = j * 100.0 / speed;// 每步所需时间
timer = new QTimer(this);
timers.append(timer);
connect(timer, &QTimer::timeout, this, [=]() {
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 << ")" << std::endl;
// 更新电量
agvs[i].setpower(agvs[i].power_ - power_consumption / steps);
this->update(); // 在窗口中重绘
});
if (!timers.isEmpty()) {
QTimer* preTimer = timers.last();
if (preTimer->isActive()) {
QTimer::singleShot(preTimer->remainingTime(), this, [=]() {
timer->start(time - preTimer->remainingTime());
});
} else {
timer->start(time);
}
} else {
timer->start(time);
}
}
}
```
这样,在每次启动 QTimer 时,就将其返回的 QTimer 对象保存到 `timers` 数组中,然后判断前一步移动是否已完成,如果未完成,则等待 QTimer 完成当前步骤的移动再进行下一步移动。这样可以避免 AGV 倒退的现象。