blob: 62daca1d2fe5340386862822207a25d093c789e1 [file] [log] [blame]
Alex Perry20762632019-01-21 17:48:02 -05001#!/usr/bin/python
2
3"""Wrapper around spline.h/cc through spline_array.cc."""
4
5__author__ = 'Alex Perry (alex.perry96@gmail.com)'
6
7import ctypes as ct
8import numpy as np
9import os
10
11libSpline = None
12for path in os.environ.get('PYTHONPATH').split(':'):
13 try:
14 libSpline = ct.cdll.LoadLibrary(os.path.join(path, 'frc971/control_loops/drivetrain/spline.so'))
15 except OSError, e:
16 pass
17
18# Define required output types.
19libSpline.Theta.restype = ct.c_double
20libSpline.DTheta.restype = ct.c_double
21libSpline.DDTheta.restype = ct.c_double
22
23class Spline:
24 """A wrapper around spline.h/cc through spline_array.cc."""
25
26 def __init__(self):
27 self.__points = np.zeros((2,6))
28 self.__spline = libSpline.newSpline(np.ctypeslib.as_ctypes(self.__points[0]),
29 np.ctypeslib.as_ctypes(self.__points[1]))
30
31 def __del__(self):
32 libSpline.deleteSpline(self.__spline)
33
34 def setPoint(self, index, x, y):
35 self.__points[0, index] = x
36 self.__points[1, index] = y
37 libSpline.deleteSpline(self.__spline)
38 self.__spline = libSpline.newSpline(np.ctypeslib.as_ctypes(self.__points[0]),
39 np.ctypeslib.as_ctypes(self.__points[1]))
40
41 def Point(self, alpha):
42 result = np.zeros(2)
43 libSpline.Point(self.__spline, ct.c_double(alpha), np.ctypeslib.as_ctypes(result))
44 return result
45
46 def DPoint(self, alpha):
47 result = np.zeros(2)
48 libSpline.DPoint(self.__spline, ct.c_double(alpha), np.ctypeslib.as_ctypes(result))
49 return result
50
51 def DDPoint(self, alpha):
52 result = np.zeros(2)
53 libSpline.DDPoint(self.__spline, ct.c_double(alpha), np.ctypeslib.as_ctypes(result))
54 return result
55
56 def DDDPoint(self, alpha):
57 result = np.zeros(2)
58 libSpline.DDDPoint(self.__spline, ct.c_double(alpha), np.ctypeslib.as_ctypes(result))
59 return result
60
61 def Theta(self, alpha):
62 return libSpline.Theta(self.__spline, ct.c_double(alpha))
63
64 def DTheta(self, alpha):
65 return libSpline.DTheta(self.__spline, ct.c_double(alpha))
66
67 def DDTheta(self, alpha):
68 return libSpline.DDTheta(self.__spline, ct.c_double(alpha))
69
70 def ControlPoints(self):
71 return self.__points;