blob: df622f32766dfe018ee724057867da4b7614e6ac [file] [log] [blame]
John Park91e69732019-03-03 13:12:43 -08001import os
2import numpy as np
3
4
5class SplineWriter(object):
6 def __init__(self, namespaces=None, filename="auto_splines.cc"):
7 if namespaces:
8 self._namespaces = namespaces
9 else:
10 self._namespaces = ['frc971', 'autonomous']
11
12 self._namespace_start = '\n'.join(
13 ['namespace %s {' % name for name in self._namespaces])
14
15 self._namespace_end = '\n'.join([
16 '} // namespace %s' % name for name in reversed(self._namespaces)
17 ])
18
19 self.filename_ = filename
20
21 def make_string_list(self, list):
22 string = "{{"
23 for i in range(0, len(list)):
24 if i == len(list) - 1:
25 string += str(list[i])
26 else:
27 string += str(list[i]) + ", "
28 return string + "}}"
29
30 def Write(self, spline_name, spline_idx, control_points):
31 """Writes the cc file to the file named cc_file."""
32 xs = control_points[:, 0]
33 ys = control_points[:, 1]
34 spline_count = (len(xs) - 6) / 5 + 1
35 with open(self.filename_, 'a') as fd:
36 fd.write(self._namespace_start)
37 #write the name
38 fd.write("\n\n::frc971::MultiSpline " + spline_name + "() {\n")
39 # write the objs, at the moment assumes a single constraint, needs fixing
40 fd.write(
41 "\t::frc971::MultiSpline spline;\n\t::frc971::Constraint constraints;\n"
42 )
43 fd.write(
44 "\tconstraints.constraint_type = 0;\n\tconstraints.value = 0;\n"
45 )
46 fd.write(
47 "\tconstraints.start_distance = 0;\n\tconstraints.end_distance = 0;\n"
48 )
49 fd.write('\n')
50 fd.write("\tspline.spline_idx = " + str(spline_idx) +
51 ";\n\tspline.spline_count = " + str(spline_count) + ";\n")
52 fd.write("\tspline.spline_x = " + self.make_string_list(xs) +
53 ";\n\tspline.spline_y = " + self.make_string_list(ys) +
54 ";\n")
55 fd.write(
56 "\tspline.constraints = {{constraints}};\n\treturn spline;\n")
57 fd.write("}")
58 fd.write("\n\n")
59 fd.write(self._namespace_end)
60 fd.write("\n\n")
61
62
63def main():
64 writer = SplineWriter()
65 points = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0],
66 [9.0, 10.0], [11.0, 12.0]])
67 spline_name = "test_spline"
68 spline_idx = 1
69 writer.Write(spline_name, spline_idx, points)
70
71
72main()