【发布时间】:2019-12-10 20:47:34
【问题描述】:
我有这个来自 webbots 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 但没有给出任何输出。请提供解决方案。谢谢
【问题讨论】:
-
在哪里您尝试打印
us_values的值?请向我们展示您的实际尝试。 -
我尝试在
us_values[i] = wb_distance_sensor_get_value(us[i]);下方使用printf("Data %f\n", us_values[i]);,但控制台没有显示任何内容 -
请编辑您的问题以更新minimal reproducible example。
-
一个更好的问题应该是:如何在 C 中创建一个 CSV 文件。我相信有很多关于这方面的资源。