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