I've got a robot and I want to make this robot follow a predefined path in the form of an eight figure on a field outside. The robot is not easy to steer and there are some external factors such as wind which make it very likely that the robot will not follow the exact path so it will often be next to the path.
The path I want to make it follow was formed by defining five points and create a line over those points using a cubic spline (the code I used to define this path is below this message):
I always want to be able to supply the robot a point to steer to which is on the cubic spline line. To be able to do this I thought the easiest way is to:
- Calculate the nearest point on the cubic spline from the current location of the robot
- Advance
0.2
units along the cubic spline line to determine the new waypoint for the robot to aim at.
For example, if the location of the robot in the grid above is x=0.4, y=-0.5
, the nearest point is approximately x=0.4, y=-0.28
and the new waypoint would be approximately x=0.22, y=-0.18
:
Now I've got three questions:
- How do I find the nearest point on the cubic spline?
- How do I "advance" 0.2 units from the found point on the cubic spline?
- How do I stay on the given path, even when the path crosses itself in the middle?
All tips are welcome!
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
x = [-0.5, 0, 0.5, 0.5, 0, -0.5, -0.5, 0, 0.5]
y = [0.25, 0, -0.25, 0.25, 0, -0.25, 0.25, 0, -0.25]
tck, u = interpolate.splprep([x, y], s=0)
unew = np.arange(0, 1.01, 0.01)
out = interpolate.splev(unew, tck)
plt.figure()
plt.plot(x, y, 'o', out[0], out[1])
plt.legend(['Predefined points', 'Cubic Spline'])
plt.axis([-0.75, 0.75, -0.75, 0.75])
plt.show()