# Creation of Spline Interpolated Path from Sample Data for Industrial Robots

https://youtu.be/SgokWpqiYDw

In this tutorial we demonstrate how to create a spline-interpolated toolpath from sample data and create a JSON file which can be imported into IRBCAM. The generated JSON data follows the format specified in the Documentation. The toolpath in this example is configured and simulated using a MOTOMAN-MH80 robot.

The Python code to generate the spline interpolated JSON file is given below. To run the code, some packages are required: `pip3 install numpy scipy matplotlib`

``````import json
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
from math import pi

# Vector lengths
L1 = 10   # Length of sample data
L2 = 100  # Length of smooth data

# Sample data points
x = np.linspace(0, 500, L1)
y = 100*np.sin(x) + 100*np.sin(x) ** 2 + 50*np.cos(x)

# Create a spline of the data
spline = UnivariateSpline(x, y)

# Generate finer points for a smooth curve
x_smooth = np.linspace(0, 500, L2)
y_smooth = spline(x_smooth)

# Generate JSON data for IRBCAM
data = \
{ \
"targets": { \
"pX": [], "pY": [], "pZ": [], "rZ": [], "rY": [], "rz2": [], "type": [] \
}, \
"velocity": {"i": [0], "value": [100]}, \
"tool": {"i": [0], "value": [1]}, \
"spindle": {"i": [0], "value": [1000]} \
}

data["targets"]["pX"] = x_smooth.tolist()
data["targets"]["pY"] = y_smooth.tolist()
data["targets"]["pZ"] = np.zeros(L2).tolist()
data["targets"]["rZ"] = np.zeros(L2).tolist()
data["targets"]["rY"] = (pi * np.ones(L2)).tolist()
data["targets"]["rz2"] = np.zeros(L2).tolist()
data["targets"]["type"] = [0] * L2

with open('spline.json', 'w') as f:
json.dump(data, f)

# Plotting the original data points and the spline
plt.figure(figsize=(8, 4))
plt.plot(x, y, 'ro', label='Original data')
plt.plot(x_smooth, y_smooth, label='Spline curve')
plt.legend()
plt.title('Spline Interpolation Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()

``````