Writing a CSV File

From Mech
Jump to navigationJump to search
Youbot.jpg

Several of the V-REP simulation scenes require a comma-separated values (CSV) file as input. A CSV file may represent the trajectory of a robot, where each row contains the configuration of the robot at an instant in time, with a fixed time between each row. If the robot has joints, then each row has numbers separated by commas. For example, five rows of a CSV file for a six-joint robot might look like this:

2.950000,-1.570000,0.000000,0.000000,0.000000,0.000000
2.987484,-1.540050,0.019967,0.019992,0.012495,0.009996
3.024875,-1.510399,0.039734,0.039933,0.024958,0.019967
3.062079,-1.481344,0.059104,0.059775,0.037360,0.029888
3.099002,-1.453174,0.077884,0.079468,0.049667,0.039734

Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files.

Getting_Started_with_the_V-REP_Simulator'''This page''' contains information on getting started quickly with V-REP. This page contains a number of scenes that accept CSV file inputs for visualization of robot trajectories.

Python

import numpy as np

# Generate random 3x4 matrix of floats y, 3x1 vector of ints d
y = np.random.rand(3, 4)
d = np.random.randint(-100, 100, 3)

# Open a file for output
# Overwrite
f = open("output.csv", "w") 
# Append
#f = open("output.csv", "a")

# For loop running 3 times to print each csv row
for i in range(len(d)):
    output = " %10.6f, %10.6f, %10.6f, %10.6f, %d\n" % (y[i,0], y[i,1], y[i,2], y[i,3], d[i])
    f.write(output)
    
# close file
f.close()

The code below is a somewhat simpler version.

import numpy as np

# Generate random 3x4 matrix of floats y, 3x1 vector of ints d
y = np.random.rand(3, 4)
d = np.random.randint(-100, 100, 3)

# Set number precision
y = np.round(y, 6)

# Overwrite csv file
np.savetxt("output.csv", np.asarray(np.c_[y, d]), delimiter = ",") 

MATLAB

Mathematica