WEBOTS - 将控制器数据写入外部 CSV 文件
WEBOTS - Write controller data to external CSV file
我从 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 文件中。
我尝试了 cout 和 printf 但没有给出任何输出。请提供解决方案。谢谢
您提供的代码中没有 printf
或 std::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
.
我从 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 文件中。
我尝试了 cout 和 printf 但没有给出任何输出。请提供解决方案。谢谢
您提供的代码中没有 printf
或 std::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
.