Difference between revisions of "Writing a CSV File"
(Created page with "right 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, w...") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[image: |
[[image:ur5-img.png|right|x200px]] |
||
Several of the |
Several of the CoppeliaSim simulation scenes require a plain-text 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 <math>n</math> joints, then each row has <math>n</math> 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.950000, -1.570000, 0.000000, 0.000000, 0.000000, 0.000000 |
||
2.987484,-1.540050,0.019967,0.019992,0.012495,0.009996 |
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.024875, -1.510399, 0.039734, 0.039933, 0.024958, 0.019967 |
||
3.062079,-1.481344,0.059104,0.059775,0.037360,0.029888 |
3.062079, -1.481344, 0.059104, 0.059775, 0.037360, 0.029888 |
||
3.099002,-1.453174,0.077884,0.079468,0.049667,0.039734 |
3.099002, -1.453174, 0.077884, 0.079468, 0.049667, 0.039734 |
||
where each number is a joint angle in radians. |
|||
Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files. |
Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files. |
||
[[ |
[[Getting_Started_with_the_CoppeliaSim_Simulator|'''This page''']] contains information on getting started quickly with CoppeliaSim. [[CoppeliaSim_Introduction|'''This page''']] contains a number of scenes that accept CSV file inputs for visualization of robot trajectories. |
||
===Python=== |
===Python=== |
||
<nowiki> |
|||
import numpy as np |
import numpy as np |
||
<code> |
|||
# Generate random 3x4 matrix of floats y, 3x1 vector of ints d |
# Generate random 3x4 matrix of floats y, 3x1 vector of ints d |
||
y = np.random.rand(3, 4) |
y = np.random.rand(3, 4) |
||
Line 30: | Line 32: | ||
# For loop running 3 times to print each csv row |
# For loop running 3 times to print each csv row |
||
for i in range(len(d)): |
for i in range(len(d)): |
||
output = " % |
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) |
f.write(output) |
||
# close file |
# close file |
||
f.close() |
f.close() |
||
</ |
</nowiki> |
||
The code below is a somewhat simpler version. |
|||
<nowiki> |
|||
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 = ",") |
|||
</nowiki> |
|||
===MATLAB=== |
===MATLAB=== |
||
<nowiki> |
|||
% Generate random 3x4 matrix of floats y, 3x1 vector of ints d |
|||
y = rand(3, 4); |
|||
d = randi([-100, 100], 3, 1); |
|||
% Open a file for output |
|||
% Overwrite |
|||
f = fopen('output.csv', 'w'); |
|||
% Append |
|||
%f = fopen('output.csv', 'a'); |
|||
% For loop running 3 times to print each csv row |
|||
for i = 1: length(d) |
|||
fprintf(f, ' %10.6f, %10.6f, %10.6f, %10.6f, %d\n', y(i, :), d(i)); |
|||
end |
|||
% Close file |
|||
fclose(f); |
|||
</nowiki> |
|||
The code below is a somewhat simpler version. |
|||
<nowiki> |
|||
% Generate random 3x4 matrix of floats y, 3x1 vector of ints d |
|||
y = rand(3, 4); |
|||
d = randi([-100, 100], 3, 1); |
|||
% Set number precision |
|||
y = round(y, 6); |
|||
% Overwrite csv file |
|||
csvwrite('output.csv', [y, d]); |
|||
</nowiki> |
|||
===Mathematica=== |
===Mathematica=== |
||
<nowiki> |
|||
(* Generate random 3x4 matrix of floats y,3x1 vector of ints d *) |
|||
y = RandomReal[1, {3, 4}]; |
|||
d = RandomInteger[{-100, 100}, {3, 1}]; |
|||
(* Open a file for output *) |
|||
(* Overwrite *) |
|||
f = OpenWrite[FileNameJoin[{NotebookDirectory[], "output.csv"}]]; |
|||
(* Append *) |
|||
(* \ |
|||
f=OpenAppend[FileNameJoin[{NotebookDirectory[],"output.csv"}]]; *) |
|||
(* For loop running 3 times to print each csv row *) |
|||
Do[WriteString[f, |
|||
ExportString[{Flatten[{SetAccuracy[y[[i, ;;]], 6], d[[i]]}]}, |
|||
"CSV"]], {i, Length[d]}]; |
|||
(* Close file *) |
|||
Close[f]; |
|||
</nowiki> |
|||
The code below is a somewhat simpler version. |
|||
<nowiki> |
|||
(* Generate random 3x4 matrix of floats y,3x1 vector of ints d *) |
|||
y = RandomReal[1, {3, 4}]; |
|||
d = RandomInteger[{-100, 100}, {3, 1}]; |
|||
(* Set number precision *) |
|||
y = SetAccuracy[y, 6]; |
|||
(* Overwrite csv file *) |
|||
Export[FileNameJoin[{NotebookDirectory[], "output.csv"}], |
|||
ArrayFlatten[{{y, d}}], "CSV"]; |
|||
</nowiki> |
Latest revision as of 11:05, 13 October 2020
Several of the CoppeliaSim simulation scenes require a plain-text 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
where each number is a joint angle in radians.
Below are code snippets in Python, MATLAB, and Mathematica that you can modify to create your own CSV files.
This page contains information on getting started quickly with CoppeliaSim. 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
% Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = rand(3, 4); d = randi([-100, 100], 3, 1); % Open a file for output % Overwrite f = fopen('output.csv', 'w'); % Append %f = fopen('output.csv', 'a'); % For loop running 3 times to print each csv row for i = 1: length(d) fprintf(f, ' %10.6f, %10.6f, %10.6f, %10.6f, %d\n', y(i, :), d(i)); end % Close file fclose(f);
The code below is a somewhat simpler version.
% Generate random 3x4 matrix of floats y, 3x1 vector of ints d y = rand(3, 4); d = randi([-100, 100], 3, 1); % Set number precision y = round(y, 6); % Overwrite csv file csvwrite('output.csv', [y, d]);
Mathematica
(* Generate random 3x4 matrix of floats y,3x1 vector of ints d *) y = RandomReal[1, {3, 4}]; d = RandomInteger[{-100, 100}, {3, 1}]; (* Open a file for output *) (* Overwrite *) f = OpenWrite[FileNameJoin[{NotebookDirectory[], "output.csv"}]]; (* Append *) (* \ f=OpenAppend[FileNameJoin[{NotebookDirectory[],"output.csv"}]]; *) (* For loop running 3 times to print each csv row *) Do[WriteString[f, ExportString[{Flatten[{SetAccuracy[y[[i, ;;]], 6], d[[i]]}]}, "CSV"]], {i, Length[d]}]; (* Close file *) Close[f];
The code below is a somewhat simpler version.
(* Generate random 3x4 matrix of floats y,3x1 vector of ints d *) y = RandomReal[1, {3, 4}]; d = RandomInteger[{-100, 100}, {3, 1}]; (* Set number precision *) y = SetAccuracy[y, 6]; (* Overwrite csv file *) Export[FileNameJoin[{NotebookDirectory[], "output.csv"}], ArrayFlatten[{{y, d}}], "CSV"];