blob: ca20f61f88c618231c99a98589e771f63ecb7ccc [file] [log] [blame]
Alex Perry20762632019-01-21 17:48:02 -05001#!/usr/bin/python
Alex Perry20762632019-01-21 17:48:02 -05002"""Wrapper around spline.h/cc through spline_array.cc."""
3
4__author__ = 'Alex Perry (alex.perry96@gmail.com)'
5
6import ctypes as ct
7import numpy as np
8import os
9
10libSpline = None
11for path in os.environ.get('PYTHONPATH').split(':'):
12 try:
Alex Perrya60da442019-01-21 19:00:27 -050013 libSpline = ct.cdll.LoadLibrary(
14 os.path.join(path, 'frc971/control_loops/drivetrain/spline.so'))
Alex Perry20762632019-01-21 17:48:02 -050015 except OSError, e:
16 pass
17
18# Define required output types.
Alex Perrya60da442019-01-21 19:00:27 -050019libSpline.SplineTheta.restype = ct.c_double
20libSpline.SplineDTheta.restype = ct.c_double
21libSpline.SplineDDTheta.restype = ct.c_double
22libSpline.DistanceSplineTheta.restype = ct.c_double
23libSpline.DistanceSplineDTheta.restype = ct.c_double
24libSpline.DistanceSplineDThetaDt.restype = ct.c_double
25libSpline.DistanceSplineDDTheta.restype = ct.c_double
26libSpline.DistanceSplineLength.restype = ct.c_double
27
Alex Perry20762632019-01-21 17:48:02 -050028
29class Spline:
Alex Perrya60da442019-01-21 19:00:27 -050030 """A wrapper around spline.h/cc through libspline.cc."""
Alex Perry20762632019-01-21 17:48:02 -050031
Alex Perrya60da442019-01-21 19:00:27 -050032 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 Perry20762632019-01-21 17:48:02 -050038
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 Perrya60da442019-01-21 19:00:27 -050046 self.__spline = libSpline.newSpline(
47 np.ctypeslib.as_ctypes(self.__points[0]),
48 np.ctypeslib.as_ctypes(self.__points[1]))
Alex Perry20762632019-01-21 17:48:02 -050049
50 def Point(self, alpha):
51 result = np.zeros(2)
Alex Perrya60da442019-01-21 19:00:27 -050052 libSpline.SplinePoint(self.__spline, ct.c_double(alpha),
53 np.ctypeslib.as_ctypes(result))
Alex Perry20762632019-01-21 17:48:02 -050054 return result
55
56 def DPoint(self, alpha):
57 result = np.zeros(2)
Alex Perrya60da442019-01-21 19:00:27 -050058 libSpline.SplineDPoint(self.__spline, ct.c_double(alpha),
59 np.ctypeslib.as_ctypes(result))
Alex Perry20762632019-01-21 17:48:02 -050060 return result
61
62 def DDPoint(self, alpha):
63 result = np.zeros(2)
Alex Perrya60da442019-01-21 19:00:27 -050064 libSpline.SplineDDPoint(self.__spline, ct.c_double(alpha),
65 np.ctypeslib.as_ctypes(result))
Alex Perry20762632019-01-21 17:48:02 -050066 return result
67
68 def DDDPoint(self, alpha):
69 result = np.zeros(2)
Alex Perrya60da442019-01-21 19:00:27 -050070 libSpline.SplineDDDPoint(self.__spline, ct.c_double(alpha),
71 np.ctypeslib.as_ctypes(result))
Alex Perry20762632019-01-21 17:48:02 -050072 return result
73
74 def Theta(self, alpha):
Alex Perrya60da442019-01-21 19:00:27 -050075 return libSpline.SplineTheta(self.__spline, ct.c_double(alpha))
Alex Perry20762632019-01-21 17:48:02 -050076
77 def DTheta(self, alpha):
Alex Perrya60da442019-01-21 19:00:27 -050078 return libSpline.SplineDTheta(self.__spline, ct.c_double(alpha))
Alex Perry20762632019-01-21 17:48:02 -050079
80 def DDTheta(self, alpha):
Alex Perrya60da442019-01-21 19:00:27 -050081 return libSpline.SplineDDTheta(self.__spline, ct.c_double(alpha))
Alex Perry20762632019-01-21 17:48:02 -050082
83 def ControlPoints(self):
Alex Perrya60da442019-01-21 19:00:27 -050084 return self.__points
85
86 def Spline(self):
87 return self.__spline
88
89
90class 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)