Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 2 | """Wrapper around spline.h/cc through spline_array.cc.""" |
| 3 | |
| 4 | __author__ = 'Alex Perry (alex.perry96@gmail.com)' |
| 5 | |
| 6 | import ctypes as ct |
| 7 | import numpy as np |
| 8 | import os |
| 9 | |
| 10 | libSpline = None |
| 11 | for path in os.environ.get('PYTHONPATH').split(':'): |
| 12 | try: |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 13 | libSpline = ct.cdll.LoadLibrary( |
| 14 | os.path.join(path, 'frc971/control_loops/drivetrain/spline.so')) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 15 | except (OSError): |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 16 | pass |
| 17 | |
| 18 | # Define required output types. |
James Kuszmaul | 7725387 | 2019-12-28 11:59:57 -0800 | [diff] [blame] | 19 | libSpline.NewSpline.restype = ct.c_void_p |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 20 | libSpline.SplineTheta.restype = ct.c_double |
| 21 | libSpline.SplineDTheta.restype = ct.c_double |
| 22 | libSpline.SplineDDTheta.restype = ct.c_double |
James Kuszmaul | 7725387 | 2019-12-28 11:59:57 -0800 | [diff] [blame] | 23 | libSpline.NewDistanceSpline.restype = ct.c_void_p |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 24 | libSpline.DistanceSplineTheta.restype = ct.c_double |
| 25 | libSpline.DistanceSplineDTheta.restype = ct.c_double |
| 26 | libSpline.DistanceSplineDThetaDt.restype = ct.c_double |
| 27 | libSpline.DistanceSplineDDTheta.restype = ct.c_double |
| 28 | libSpline.DistanceSplineLength.restype = ct.c_double |
James Kuszmaul | 7725387 | 2019-12-28 11:59:57 -0800 | [diff] [blame] | 29 | libSpline.NewTrajectory.restype = ct.c_void_p |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 30 | libSpline.TrajectoryLength.restype = ct.c_double |
James Kuszmaul | 7725387 | 2019-12-28 11:59:57 -0800 | [diff] [blame] | 31 | libSpline.TrajectoryDistance.restype = ct.c_double |
| 32 | libSpline.TrajectoryGetPlanXVAPtr.restype = ct.c_void_p |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 33 | |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 34 | # Required for trajectory |
| 35 | libSpline.SetUpLogging() |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 36 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 37 | |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 38 | class Spline: |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 39 | """ |
| 40 | A wrapper around spline.h/cc through libspline.cc. |
| 41 | The functions return values parameterized by alpha, a number that varies |
| 42 | between 0 and 1 along the length of the spline. |
| 43 | """ |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 44 | |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 45 | def __init__(self, points): |
| 46 | assert points.shape == (2, 6) |
| 47 | self.__points = points |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 48 | self.__spline = ct.c_void_p( |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 49 | libSpline.NewSpline(np.ctypeslib.as_ctypes(self.__points[0]), |
| 50 | np.ctypeslib.as_ctypes(self.__points[1]))) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 51 | |
| 52 | def __del__(self): |
| 53 | libSpline.deleteSpline(self.__spline) |
| 54 | |
| 55 | def setPoint(self, index, x, y): |
| 56 | self.__points[0, index] = x |
| 57 | self.__points[1, index] = y |
| 58 | libSpline.deleteSpline(self.__spline) |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 59 | self.__spline = ct.c_void_p( |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 60 | libSpline.newSpline(np.ctypeslib.as_ctypes(self.__points[0]), |
| 61 | np.ctypeslib.as_ctypes(self.__points[1]))) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 62 | |
| 63 | def Point(self, alpha): |
| 64 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 65 | libSpline.SplinePoint(self.__spline, ct.c_double(alpha), |
| 66 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 67 | return result |
| 68 | |
| 69 | def DPoint(self, alpha): |
| 70 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 71 | libSpline.SplineDPoint(self.__spline, ct.c_double(alpha), |
| 72 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 73 | return result |
| 74 | |
| 75 | def DDPoint(self, alpha): |
| 76 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 77 | libSpline.SplineDDPoint(self.__spline, ct.c_double(alpha), |
| 78 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 79 | return result |
| 80 | |
| 81 | def DDDPoint(self, alpha): |
| 82 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 83 | libSpline.SplineDDDPoint(self.__spline, ct.c_double(alpha), |
| 84 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 85 | return result |
| 86 | |
| 87 | def Theta(self, alpha): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 88 | return libSpline.SplineTheta(self.__spline, ct.c_double(alpha)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 89 | |
| 90 | def DTheta(self, alpha): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 91 | return libSpline.SplineDTheta(self.__spline, ct.c_double(alpha)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 92 | |
| 93 | def DDTheta(self, alpha): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 94 | return libSpline.SplineDDTheta(self.__spline, ct.c_double(alpha)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 95 | |
| 96 | def ControlPoints(self): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 97 | return self.__points |
| 98 | |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 99 | def GetSplinePtr(self): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 100 | return self.__spline |
| 101 | |
| 102 | |
| 103 | class DistanceSpline: |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 104 | """ |
| 105 | A wrapper around distance_spline.h/cc through libspline.cc. |
| 106 | The functions return values parameterized by the distance along the spline, |
| 107 | starting at 0 and and ending at the value returned by Length(). |
| 108 | """ |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 109 | |
| 110 | def __init__(self, splines): |
| 111 | self.__spline = None |
| 112 | spline_ptrs = [] |
| 113 | for spline in splines: |
James Kuszmaul | 7725387 | 2019-12-28 11:59:57 -0800 | [diff] [blame] | 114 | spline_ptrs.append(spline.GetSplinePtr().value) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 115 | spline_ptrs = np.array(spline_ptrs) |
| 116 | |
| 117 | spline_array = np.ctypeslib.as_ctypes(spline_ptrs) |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 118 | self.__spline = ct.c_void_p( |
| 119 | libSpline.NewDistanceSpline(ct.byref(spline_array), len(splines))) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 120 | |
| 121 | def __del__(self): |
| 122 | libSpline.deleteDistanceSpline(self.__spline) |
| 123 | |
| 124 | def XY(self, distance): |
| 125 | result = np.zeros(2) |
| 126 | libSpline.DistanceSplineXY(self.__spline, ct.c_double(distance), |
| 127 | np.ctypeslib.as_ctypes(result)) |
| 128 | return result |
| 129 | |
| 130 | def DXY(self, distance): |
| 131 | result = np.zeros(2) |
| 132 | libSpline.DistanceSplineDXY(self.__spline, ct.c_double(distance), |
| 133 | np.ctypeslib.as_ctypes(result)) |
| 134 | return result |
| 135 | |
| 136 | def DDXY(self, distance): |
| 137 | result = np.zeros(2) |
| 138 | libSpline.DistanceSplineDDXY(self.__spline, ct.c_double(distance), |
| 139 | np.ctypeslib.as_ctypes(result)) |
| 140 | return result |
| 141 | |
| 142 | def Theta(self, distance): |
| 143 | return libSpline.DistanceSplineTheta(self.__spline, |
| 144 | ct.c_double(distance)) |
| 145 | |
| 146 | def DTheta(self, distance): |
| 147 | return libSpline.DistanceSplineDTheta(self.__spline, |
| 148 | ct.c_double(distance)) |
| 149 | |
| 150 | def DThetaDt(self, distance, velocity): |
| 151 | return libSpline.DistanceSplineDThetaDt(self.__spline, |
| 152 | ct.c_double(distance), |
| 153 | ct.c_double(velocity)) |
| 154 | |
| 155 | def DDTheta(self, distance): |
| 156 | return libSpline.DistanceSplineDDTheta(self.__spline, |
| 157 | ct.c_double(distance)) |
| 158 | |
| 159 | def Length(self): |
| 160 | return libSpline.DistanceSplineLength(self.__spline) |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 161 | |
| 162 | def GetSplinePtr(self): |
| 163 | return self.__spline |
| 164 | |
| 165 | |
| 166 | class Trajectory: |
| 167 | """A wrapper around trajectory.h/cc through libspline.cc.""" |
| 168 | |
| 169 | def __init__(self, distance_spline, vmax=10, num_distance=0): |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 170 | self.__trajectory = ct.c_void_p( |
| 171 | libSpline.NewTrajectory(distance_spline.GetSplinePtr(), |
| 172 | ct.c_double(vmax), num_distance)) |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 173 | |
| 174 | def __del__(self): |
| 175 | libSpline.deleteTrajectory(self.__trajectory) |
| 176 | |
Ravago Jones | 3b92afa | 2021-02-05 14:27:32 -0800 | [diff] [blame] | 177 | def SetLongitudinalAcceleration(self, accel): |
| 178 | libSpline.TrajectorySetLongitudinalAcceleration( |
| 179 | self.__trajectory, ct.c_double(accel)) |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 180 | |
| 181 | def SetLateralAcceleration(self, accel): |
| 182 | libSpline.TrajectorySetLateralAcceleration(self.__trajectory, |
| 183 | ct.c_double(accel)) |
| 184 | |
| 185 | def SetVoltageLimit(self, limit): |
| 186 | libSpline.TrajectorySetVoltageLimit(self.__trajectory, |
| 187 | ct.c_double(limit)) |
| 188 | |
| 189 | def LimitVelocity(self, start_distance, end_distance, max_vel): |
| 190 | libSpline.TrajectoryLimitVelocity(self.__trajectory, |
| 191 | ct.c_double(start_distance), |
| 192 | ct.c_double(end_distance), |
| 193 | ct.c_double(max_vel)) |
| 194 | |
| 195 | def Plan(self): |
| 196 | """ |
| 197 | Call this to compute the plan, if any of the limits change, a new plan |
| 198 | must be generated. |
| 199 | """ |
| 200 | libSpline.TrajectoryPlan(self.__trajectory) |
| 201 | |
Alex Perry | 50baefc | 2019-01-27 13:26:29 -0800 | [diff] [blame] | 202 | def Voltage(self, distance): |
| 203 | """ |
| 204 | Returns a pair of voltages for a given distance. |
| 205 | Order is left-right. |
| 206 | """ |
| 207 | result = np.zeros(2) |
| 208 | libSpline.TrajectoryVoltage(self.__trajectory, ct.c_double(distance), |
| 209 | np.ctypeslib.as_ctypes(result)) |
| 210 | return result |
| 211 | |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 212 | def Length(self): |
| 213 | return libSpline.TrajectoryLength(self.__trajectory) |
| 214 | |
| 215 | def Distance(self, index): |
| 216 | return libSpline.TrajectoryDistance(self.__trajectory, index) |
| 217 | |
| 218 | def Distances(self): |
| 219 | """ |
| 220 | Returns an array of distances used to compute the plan. The linear |
| 221 | distance between each distance is equal. |
| 222 | """ |
| 223 | path_length = libSpline.TrajectoryGetPathLength(self.__trajectory) |
| 224 | distances = numpy.zeros(path_length) |
| 225 | libSpline.TrajectoryDistances(self.__trajectory, |
| 226 | np.ctypeslib.as_ctypes(distances)) |
| 227 | return distances |
| 228 | |
| 229 | def GetPlan(self): |
| 230 | """ |
| 231 | Returns the plan as an array of velocities matched to each distance |
| 232 | from Distances. |
| 233 | """ |
| 234 | path_length = libSpline.TrajectoryGetPathLength(self.__trajectory) |
| 235 | velocities = numpy.zeros(path_length) |
| 236 | libSpline.TrajectoryGetPlan(self.__trajectory, |
| 237 | np.ctypeslib.as_ctypes(velocities)) |
| 238 | return velocities |
| 239 | |
| 240 | def GetPlanXVA(self, dt): |
| 241 | """ |
| 242 | dt is in seconds |
| 243 | Returns the position, velocity, and acceleration as a function of time. |
| 244 | This is returned as a 3xN numpy array where N is proportional to how |
| 245 | long it takes to run the path. |
| 246 | This is slow so don't call more than once with the same data. |
| 247 | """ |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 248 | XVAPtr = ct.c_void_p( |
| 249 | libSpline.TrajectoryGetPlanXVAPtr(self.__trajectory, |
| 250 | int(dt * 1e9))) |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 251 | XVALength = libSpline.TrajectoryGetVectorLength(XVAPtr) |
James Kuszmaul | c3eaa47 | 2021-03-03 19:43:45 -0800 | [diff] [blame] | 252 | if XVALength == 0: |
| 253 | libSpline.TrajectoryDeleteVector(XVAPtr) |
| 254 | return None |
Alex Perry | 0603b54 | 2019-01-25 20:29:51 -0800 | [diff] [blame] | 255 | X = np.zeros(XVALength) |
| 256 | V = np.zeros(XVALength) |
| 257 | A = np.zeros(XVALength) |
| 258 | libSpline.TrajectoryGetPlanXVA(XVAPtr, np.ctypeslib.as_ctypes(X), |
| 259 | np.ctypeslib.as_ctypes(V), |
| 260 | np.ctypeslib.as_ctypes(A)) |
| 261 | libSpline.TrajectoryDeleteVector(XVAPtr) |
| 262 | return np.vstack([X, V, A]) |