gpt4 book ai didi

c++ - WEBOTS - 将 Controller 数据写入外部 CSV 文件

转载 作者:行者123 更新时间:2023-11-28 01:14:08 26 4
gpt4 key购买 nike

我有来自 webots lidar.wbt 的这段代码。我需要为另一个项目使用 opengl 绘制一些激光雷达数据。

#include <webots/distance_sensor.h>
#include <webots/lidar.h>
#include <webots/motor.h>
#include <webots/robot.h>

#include <stdio.h>

#define TIME_STEP 32
#define LEFT 0
#define RIGHT 1

using namespace std;

int main(int argc, char **argv) {
// iterator used to parse loops
int i, k;

// init Webots stuff
wb_robot_init();

// init camera
WbDeviceTag lidar = wb_robot_get_device("lidar");
wb_lidar_enable(lidar, TIME_STEP);
wb_lidar_enable_point_cloud(lidar);

// init distance sensors
WbDeviceTag us[2];
double us_values[2];
us[LEFT] = wb_robot_get_device("us0");
us[RIGHT] = wb_robot_get_device("us1");
for (i = 0; i < 2; ++i)
wb_distance_sensor_enable(us[i], TIME_STEP);

// get a handler to the motors and set target position to infinity (speed control).
WbDeviceTag left_motor = wb_robot_get_device("left wheel motor");
WbDeviceTag right_motor = wb_robot_get_device("right wheel motor");
wb_motor_set_position(left_motor, INFINITY);
wb_motor_set_position(right_motor, INFINITY);
wb_motor_set_velocity(left_motor, 0.0);
wb_motor_set_velocity(right_motor, 0.0);

// set empirical coefficients for collision avoidance
double coefficients[2][2] = {{12.0, -6.0}, {-10.0, 8.0}};
double base_speed = 6.0;



// init speed values
double speed[2];

while (wb_robot_step(TIME_STEP) != -1) {
// read sensors
for (i = 0; i < 2; ++i)
us_values[i] = wb_distance_sensor_get_value(us[i]);

// compute speed
for (i = 0; i < 2; ++i) {
speed[i] = 0.0;
for (k = 0; k < 2; ++k)
speed[i] += us_values[k] * coefficients[i][k];
}

// set actuators
wb_motor_set_velocity(left_motor, base_speed + speed[LEFT]);
wb_motor_set_velocity(right_motor, base_speed + speed[RIGHT]);
}

wb_robot_cleanup();

return 0;
}

数据应该存储在 us_values[i] 中。因此,我需要将数组 us_values[i] 中的数据保存到一个 csv 文件中。

我已经尝试了 coutprintf 但没有给出任何输出。请提供解决方案。谢谢

最佳答案

您提供的代码中没有printfstd::cout。如果您只是在寻找创建 CSV 文件的方法,那么这里有一个示例,说明我如何使用 std::fstream 创建 CSV 文件,它与您使用 std 的方式非常相似: :cout 除了有一点额外的开销。

#include <fstream>

int main(int argc, char* argv[]) {
//generate values to be stored in CSV
constexpr size_t arraySize(100);
double values[arraySize];
for(int i = 0; i < arraySize; i++) {
values[i] = 1.0 / (i + 1);
}

//actually store those values...
std::fstream file;
file.open("Output.csv", std::fstream::out);
for(int i = 0; i < arraySize; i++) {
file << values[i] << ", ";
}
file.close();

return 0;
}

如果你想打印到 stdout 进行管道传输,那么 std::cout 将是最好的。如果您想直接打印到一个文件,那么您可以使用 std::fstream

关于c++ - WEBOTS - 将 Controller 数据写入外部 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59263637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com