-
Notifications
You must be signed in to change notification settings - Fork 16
/
generator.py
183 lines (136 loc) · 5.79 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
import sys, os, getopt
from sympy import *
import re
def makePow(expr):
'''Convert Python x**y syntax to C math.pow(x, y)'''
return re.sub(r'([\w]+(?:\[\d\])?)\*\*([\d])', r'pow(\1, \2)', str(expr))
def genSigns(n):
'''Generate the signs of XPeak[n] for each period'''
if (n == 1):
return [1]
else:
return genSigns(n-1) + [1] + [-i for i in genSigns(n-1)]
def genOrders(n, val):
'''Generate which Xpeak is constant for each period'''
if (val == 1):
return [n]
else:
previous = genOrders(n, val-1)
new = previous + [ n-val+1 ] + previous
return new
def genTemplates(n):
'''Generate the intervals and the functions generating every X[p]'''
Xpeak = IndexedBase('Xpeak')
T = IndexedBase('T')
i, j, t = symbols('i, j, t', integer=True)
signs = genSigns(n)
orders = genOrders(n, n)
timePeriods = [ T[order] for order in orders]
timeFrames = [T[0]] + [ sum(timePeriods[:i+1]) for i in range(len(timePeriods)) ]
intervals = [[timeFrames[i], timeFrames[i+1]] for i in range(len(timeFrames)-1)]
templates = [ [ 0 for inter in intervals] for i in range(n+1) ]
for i in range(n, -1, -1):
for j, order in enumerate(orders):
peakValue = signs[j] * Xpeak[order]
if (i == order):
templates[i][j] = peakValue
elif (i < n):
func = integrate(templates[i+1][j], t)
for k in range(j+1, len(orders)):
templates[i][k] += func.subs({ t: T[order] })
templates[i][j] += func
return templates, intervals
def genPolyA(n, p):
Xpeak = IndexedBase('Xpeak')
T = IndexedBase('T')
i, j = symbols('i, j', integer=True)
A = ((Xpeak[n]/2**(n)) * Product( Sum(2**j * T[n+1-i+j], (j, 0, i-1)).doit() + T[n+1-i], (i, 1, n)).doit() - Xpeak[0]).expand()
return Poly(A, T[p])
def genExprB(n, q):
Xpeak = IndexedBase('Xpeak')
T = IndexedBase('T')
i, j = symbols('i, j', integer=True)
B = (Xpeak[n]/2**(n-q)) * Product( Sum(2**j * T[n+1-i+j], (j, 0, i-1)).doit() + T[n+1-i], (i, 1, n-q)).doit()
return B
def genPolyC(n, p, q):
Xpeak = IndexedBase('Xpeak')
T = IndexedBase('T')
i, j = symbols('i, j', integer=True)
C = ((Xpeak[n]/2**(n-q)) * Product( Sum(2**j * T[n+1-i+j], (j, 0, i-1)).doit() + T[n+1-i], (i, 1, n-q)).doit() - Xpeak[q])
return Poly(C, T[p])
def generateFiles(n, outDir):
Xpeak = IndexedBase('Xpeak')
T = IndexedBase('T')
i, j, t = symbols('i, j, t', integer=True)
templates, intervals = genTemplates(n)
try:
with open(os.path.join(outDir, 'motionPlanning.h'), 'wr') as file:
file.write('#include <math.h>\n\n')
file.write('void computePeriods(double Xpeak[], double T[]);\n')
file.write('double getSetpoint(double Xpeak[], double T[], double t);\n')
except Exception as err:
error(err)
try:
with open(os.path.join(outDir, 'motionPlanning.c'), 'w') as file:
file.write('#include "motionPlanning.h"\n')
file.write('#include "bbpr.h"\n\n')
file.write('void computePeriods(double Xpeak[], double T[]) {\n')
file.write(' int n = 0;\n')
file.write(' double coeffs[%d] = {%s};\n' % (n+1, ', '.join([ '0' for k in range(n+1)])))
file.write(' double Xmax = 0;\n\n')
for p in range(n, 1-1, -1):
polyA = genPolyA(n, p)
degreeA = polyA.degree()
coeffsA = polyA.all_coeffs()
file.write(' n = %d;\n' % degreeA)
for k, coeff in enumerate(coeffsA):
file.write(' coeffs[%d] = %s;\n' % (k, makePow(coeff)))
file.write(' T[%d] = findRoot(coeffs, n);\n\n' % p)
for q in range(1, p-1+1):
B = genExprB(n, q)
polyC = genPolyC(n, p, q)
degreeC = polyC.degree()
coeffsC = polyC.all_coeffs();
file.write(' Xmax = %s;\n\n' % makePow(B))
file.write(' if (Xmax > Xpeak[%d]) {\n' % (q))
file.write(' n = %d;\n' % (degreeC))
for k, coeff in enumerate(coeffsC):
file.write(' coeffs[%d] = %s;\n' % (k, makePow(coeff)))
file.write(' T[%d] = findRoot(coeffs, n);\n' % p)
file.write(' } else {\n')
file.write(' Xpeak[%d] = Xmax;\n' % q)
file.write(' }\n\n')
file.write('}\n\n')
file.write('double getSetpoint(double Xpeak[], double T[], double t) {\n')
for i, template in enumerate(templates[0]):
tA = intervals[i][0]
tB = intervals[i][1]
file.write(' if (%s <= t && t <= %s) {\n' % (tA, tB))
file.write(' return %s;\n' % makePow(template).replace('t', '(t-(%s))' % tA))
file.write(' }\n')
file.write(' else {\n')
file.write(' return Xpeak[0];\n')
file.write(' }\n')
file.write('}\n')
except Exception as err:
error(err)
def error(err):
print err
sys.exit(1)
def exit():
print 'usage: generator.py -n <order> -o <outputDirectory>'
sys.exit(2)
def main(argv):
opts = []
try:
opts = getopt.getopt(argv, "n:o:h", ["order", "out", "help"])[0]
except getopt.GetoptError:
exit()
if len(opts) < 2:
exit()
order = int(opts[0][1])
outDir = opts[1][1]
generateFiles(order, outDir)
if __name__ == "__main__":
main(sys.argv[1:])