agvs[i].setbattery(agvs[i].battery_ - power_consumption / steps);添加代碼:agv的battery最低為0
时间: 2024-02-23 07:01:51 浏览: 9
好的,你可以将代码改成如下形式,确保agv的battery不会低于0:
```
if (agvs[i].battery_ - power_consumption / steps < 0) {
agvs[i].setbattery(0);
} else {
agvs[i].setbattery(agvs[i].battery_ - power_consumption / steps);
}
```
这样,如果计算后的电量小于0,则将电量设置为0,否则将电量设置为计算后的电量。这样就可以确保agv的battery不会低于0了。
相关问题
QTimer::singleShot(time, this, &, i, next_node, cur_x, cur_y, power_consumption, j, steps { // 离开当前位置时将标记设为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: "<< 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); } });,用chrono的方法改寫這段代碼
以下是使用<chrono>头文件重写的示例代码:
```c++
#include <iostream>
#include <chrono>
#include <thread>
void moveAgv(int i) {
// 移动AGV的代码实现
}
void moveAgvs(int i, Node* next_node, double cur_x, double cur_y, double power_consumption, int steps) {
std::chrono::milliseconds interval(1000); // 定义定时器间隔为1秒
auto start_time = std::chrono::steady_clock::now(); // 获取当前时间
for (int j = 0; j < steps; j++) {
auto current_time = std::chrono::steady_clock::now(); // 获取当前时间
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time); // 计算已经过去的时间
if (elapsed_time >= interval) { // 如果已经达到定时器间隔
// 离开当前位置时将标记设为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);
// 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();
// 如果new_x和new_y为整数型,将其转换为浮点型后调用moveAgv()函数
if (agvs[i].getBattery() > 0 && std::floor(new_x) == new_x && std::floor(new_y) == new_y) {
moveAgv(i);
}
start_time = current_time; // 重置计时器
}
// 这里可以放一些其他的代码,但是不要阻塞线程
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 稍微休眠一下,避免CPU占用率过高
}
}
```
在使用<chrono>头文件时,可以使用std::chrono::steady_clock::now()函数获取当前时间,使用std::chrono::duration_cast函数计算已经过去的时间,并使用std::this_thread::sleep_for函数进行休眠。以上代码仅供参考,请根据具体情况进行修改。
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(); // 在窗口中重绘 })添加代碼,等qtime停止的時候,輸出“wsn“
您可以使用 QTimer 的 finished() 信号来检测所有 QTimer 是否都已经停止,并在所有 QTimer 停止后输出 "wsn"。
以下是示例代码:
```
// 创建 QTimer 对象并连接到槽函数
QTimer *timer = new QTimer(this);
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 << ")" << endl;
agvs[i].setpower(agvs[i].power_ - power_consumption / steps);
this->update();
});
// 创建一个计数器来跟踪 QTimer 的数量
int timer_count = 5;
// 连接到所有 QTimer 的 finished() 信号
connect(timer, &QTimer::finished, this, [=]() {
timer_count--;
if (timer_count == 0) {
std::cout << "wsn" << std::endl;
}
});
// 启动 QTimer
timer->setSingleShot(true);
timer->start(time);
```
请注意,上面的示例代码中计数器的初始值为 5,这意味着您需要手动将计数器的初始值设置为您实际使用的 QTimer 的数量。