1

I have this code from webots lidar.wbt . I need to get some lidar data to be plotted using opengl for another project.

#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;
}

The data is supposed to be stored in us_values[i] . Hence, I need the the data inside array us_values[i] to a csv file.

I have tried cout and printf but is not giving any output. Please provide a solution. Thanks

4
  • Where do you attempt to print the values of us_values? Please show us your actual attempt. Commented Dec 10, 2019 at 9:10
  • 1
    I attempted using printf("Data %f\n", us_values[i]); below us_values[i] = wb_distance_sensor_get_value(us[i]); but the console was not showing anything Commented Dec 10, 2019 at 9:36
  • 1
    Please edit your question to update the minimal reproducible example. Commented Dec 10, 2019 at 9:37
  • A better question would have been: how to create a CSV file in C. I'm sure there are a lot of resources about this. Commented Dec 12, 2019 at 7:50

1 Answer 1

4

There is no printf or std::cout in the code you provided. If you're just looking for a way to create CSV files then here's a sample for how I create CSV files using std::fstream, it's fairly similar to how you'd use std::cout except there's a little bit of extra overhead.

#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;
}

If you want to print to stdout for piping then std::cout would be best. If you want to print directly to a file then you can use std::fstream.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.