【问题标题】:WEBOTS - Write controller data to external CSV fileWEBOTS - 将控制器数据写入外部 CSV 文件
【发布时间】: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 文件中。

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

【问题讨论】:

  • 在哪里您尝试打印us_values 的值?请向我们展示您的实际尝试。
  • 我尝试在us_values[i] = wb_distance_sensor_get_value(us[i]); 下方使用printf("Data %f\n", us_values[i]);,但控制台没有显示任何内容
  • 请编辑您的问题以更新minimal reproducible example
  • 一个更好的问题应该是:如何在 C 中创建一个 CSV 文件。我相信有很多关于这方面的资源。

标签: c++ webots


【解决方案1】:

您提供的代码中没有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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2014-03-14
    相关资源
    最近更新 更多