Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
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')) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 15 | except OSError, e: |
| 16 | pass |
| 17 | |
| 18 | # Define required output types. |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 19 | libSpline.SplineTheta.restype = ct.c_double |
| 20 | libSpline.SplineDTheta.restype = ct.c_double |
| 21 | libSpline.SplineDDTheta.restype = ct.c_double |
| 22 | libSpline.DistanceSplineTheta.restype = ct.c_double |
| 23 | libSpline.DistanceSplineDTheta.restype = ct.c_double |
| 24 | libSpline.DistanceSplineDThetaDt.restype = ct.c_double |
| 25 | libSpline.DistanceSplineDDTheta.restype = ct.c_double |
| 26 | libSpline.DistanceSplineLength.restype = ct.c_double |
| 27 | |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 28 | |
| 29 | class Spline: |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 30 | """A wrapper around spline.h/cc through libspline.cc.""" |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 31 | |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 32 | def __init__(self, points): |
| 33 | assert points.shape == (2, 6) |
| 34 | self.__points = points |
| 35 | self.__spline = libSpline.NewSpline( |
| 36 | np.ctypeslib.as_ctypes(self.__points[0]), |
| 37 | np.ctypeslib.as_ctypes(self.__points[1])) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 38 | |
| 39 | def __del__(self): |
| 40 | libSpline.deleteSpline(self.__spline) |
| 41 | |
| 42 | def setPoint(self, index, x, y): |
| 43 | self.__points[0, index] = x |
| 44 | self.__points[1, index] = y |
| 45 | libSpline.deleteSpline(self.__spline) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 46 | self.__spline = libSpline.newSpline( |
| 47 | np.ctypeslib.as_ctypes(self.__points[0]), |
| 48 | np.ctypeslib.as_ctypes(self.__points[1])) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 49 | |
| 50 | def Point(self, alpha): |
| 51 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 52 | libSpline.SplinePoint(self.__spline, ct.c_double(alpha), |
| 53 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 54 | return result |
| 55 | |
| 56 | def DPoint(self, alpha): |
| 57 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 58 | libSpline.SplineDPoint(self.__spline, ct.c_double(alpha), |
| 59 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 60 | return result |
| 61 | |
| 62 | def DDPoint(self, alpha): |
| 63 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 64 | libSpline.SplineDDPoint(self.__spline, ct.c_double(alpha), |
| 65 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 66 | return result |
| 67 | |
| 68 | def DDDPoint(self, alpha): |
| 69 | result = np.zeros(2) |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 70 | libSpline.SplineDDDPoint(self.__spline, ct.c_double(alpha), |
| 71 | np.ctypeslib.as_ctypes(result)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 72 | return result |
| 73 | |
| 74 | def Theta(self, alpha): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 75 | return libSpline.SplineTheta(self.__spline, ct.c_double(alpha)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 76 | |
| 77 | def DTheta(self, alpha): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 78 | return libSpline.SplineDTheta(self.__spline, ct.c_double(alpha)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 79 | |
| 80 | def DDTheta(self, alpha): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 81 | return libSpline.SplineDDTheta(self.__spline, ct.c_double(alpha)) |
Alex Perry | 2076263 | 2019-01-21 17:48:02 -0500 | [diff] [blame] | 82 | |
| 83 | def ControlPoints(self): |
Alex Perry | a60da44 | 2019-01-21 19:00:27 -0500 | [diff] [blame] | 84 | return self.__points |
| 85 | |
| 86 | def Spline(self): |
| 87 | return self.__spline |
| 88 | |
| 89 | |
| 90 | class DistanceSpline: |
| 91 | """A wrapper around distance_spline.h/cc through libdistancespline.cc.""" |
| 92 | |
| 93 | def __init__(self, splines): |
| 94 | self.__spline = None |
| 95 | spline_ptrs = [] |
| 96 | for spline in splines: |
| 97 | spline_ptrs.append(spline.Spline()) |
| 98 | spline_ptrs = np.array(spline_ptrs) |
| 99 | |
| 100 | spline_array = np.ctypeslib.as_ctypes(spline_ptrs) |
| 101 | self.__spline = libSpline.NewDistanceSpline( |
| 102 | ct.byref(spline_array), len(splines)) |
| 103 | |
| 104 | def __del__(self): |
| 105 | libSpline.deleteDistanceSpline(self.__spline) |
| 106 | |
| 107 | def XY(self, distance): |
| 108 | result = np.zeros(2) |
| 109 | libSpline.DistanceSplineXY(self.__spline, ct.c_double(distance), |
| 110 | np.ctypeslib.as_ctypes(result)) |
| 111 | return result |
| 112 | |
| 113 | def DXY(self, distance): |
| 114 | result = np.zeros(2) |
| 115 | libSpline.DistanceSplineDXY(self.__spline, ct.c_double(distance), |
| 116 | np.ctypeslib.as_ctypes(result)) |
| 117 | return result |
| 118 | |
| 119 | def DDXY(self, distance): |
| 120 | result = np.zeros(2) |
| 121 | libSpline.DistanceSplineDDXY(self.__spline, ct.c_double(distance), |
| 122 | np.ctypeslib.as_ctypes(result)) |
| 123 | return result |
| 124 | |
| 125 | def Theta(self, distance): |
| 126 | return libSpline.DistanceSplineTheta(self.__spline, |
| 127 | ct.c_double(distance)) |
| 128 | |
| 129 | def DTheta(self, distance): |
| 130 | return libSpline.DistanceSplineDTheta(self.__spline, |
| 131 | ct.c_double(distance)) |
| 132 | |
| 133 | def DThetaDt(self, distance, velocity): |
| 134 | return libSpline.DistanceSplineDThetaDt(self.__spline, |
| 135 | ct.c_double(distance), |
| 136 | ct.c_double(velocity)) |
| 137 | |
| 138 | def DDTheta(self, distance): |
| 139 | return libSpline.DistanceSplineDDTheta(self.__spline, |
| 140 | ct.c_double(distance)) |
| 141 | |
| 142 | def Length(self): |
| 143 | return libSpline.DistanceSplineLength(self.__spline) |