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