-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_gen.py
129 lines (105 loc) · 4.48 KB
/
open_gen.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
import opengen as og
import casadi.casadi as cs
import numpy as np
## Problem size
N = 20
dt = 1.0/20
nMAV = 1 #Number of MAVs to be included in the centralized scheme
## Weight matrices
Qx = (8,8, 40, 2, 2, 3, 8, 8)
#Qx = (4,4, 40, 4, 4, 3, 8, 8)
P = 2*Qx; #final state weight
Ru = (3, 10, 10) # input weights
Rd = (3, 15, 15) # input rate weights
## Objective function generation
nu = 3; #Number of control inputs per MAV
ns = 8; #Number of states per MAV
np = nMAV*ns + nMAV*ns + nu + nu*nMAV + 3 + 2
#print(np)
u = cs.SX.sym('u', nu*nMAV*N)
z0 = cs.SX.sym('z0', np)
#print(z0)
x = z0[0:nMAV*ns]
#print(x)
x_ref = z0[nMAV*ns:nMAV*ns + nMAV*ns]
#print(x_ref)
u_ref = z0[nMAV*ns + nMAV*ns:nMAV*ns + nMAV*ns + nu]
#print(u_ref)
u_old = z0[nMAV*ns + nMAV*ns + nu:nMAV*ns + nMAV*ns + nu + nMAV*nu]
f_nmhe = z0[nMAV*ns + nMAV*ns + nu + nu:nMAV*ns + nMAV*ns + nu + nu + 3]
Qx_adapt = z0[nMAV*ns + nMAV*ns + nu + nu + 3:nMAV*ns + nMAV*ns + nu + nu + 3 + 2]
cost = 0
c = 0
for i in range(0, N):
###State Cost
for j in range(0,nMAV):
cost += Qx_adapt[0]*(x[ns*j]-x_ref[ns*j])**2 + Qx_adapt[1]*(x[ns*j+1]-x_ref[ns*j+1])**2 + Qx[2]*(x[ns*j+2]-x_ref[ns*j+2])**2 + Qx[3]*(x[ns*j+3]-x_ref[ns*j+3])**2 + Qx[4]*(x[ns*j+4]-x_ref[ns*j+4])**2 + Qx[5]*(x[ns*j+5]-x_ref[ns*j+5])**2 + Qx[6]*(x[ns*j+6]-x_ref[ns*j+6])**2 + Qx[7]*(x[ns*j+7]-x_ref[ns*j+7])**2
####Input Cost
u_n = u[(i*nMAV*nu):(i*nMAV*nu+nu*nMAV)]
for j in range(0,nMAV):
cost += Ru[0]*(u_n[nu*j] - u_ref[0])**2 + Ru[1]*(u_n[nu*j+1] - u_ref[1])**2 + Ru[2]*(u_n[nu*j+2] - u_ref[2])**2 #Input weights
cost += Rd[0]*(u_n[nu*j] - u_old[nu*j])**2 + Rd[1]*(u_n[nu*j+1] - u_old[nu*j+1])**2 + Rd[2]*(u_n[nu*j+2] - u_old[nu*j+2])**2 #Input rate weights
#Input rate constraints
c = cs.vertcat(c, cs.fmax(0, u_n[nu*j+1] - u_old[nu*j+1] - 0.05))
c = cs.vertcat(c, cs.fmax(0, u_old[nu*j+1] - u_n[nu*j+1] - 0.05))
c = cs.vertcat(c, cs.fmax(0, u_n[nu*j+2] - u_old[nu*j+2] - 0.05))
c = cs.vertcat(c, cs.fmax(0, u_old[nu*j+2] - u_n[nu*j+2] - 0.05))
##Velocity Constraints##
#c = cs.vertcat(c, cs.fmax(0, x[3] - v_max[0]))
#c = cs.vertcat(c, cs.fmax(0, -x[3] - v_max[0]))
#c = cs.vertcat(c, cs.fmax(0, x[4] - v_max[1]))
#c = cs.vertcat(c, cs.fmax(0, -x[4] - v_max[1]))
####State update
u_old = u_n
for j in range(0,nMAV):
x[ns*j] = x[ns*j] + dt * x[ns*j+3]
x[ns*j+1] = x[ns*j+1] + dt * x[ns*j+4]
x[ns*j+2] = x[ns*j+2] + dt * x[ns*j+5]
x[ns*j+3] = x[ns*j+3] + dt * (cs.sin(x[ns*j+7]) * cs.cos(x[ns*j+6]) * u_n[nu*j] - 0.1 * x[ns*j+3] + f_nmhe[0])
x[ns*j+4] = x[ns*j+4] + dt * (-cs.sin(x[ns*j+6]) * u_n[nu*j] - 0.1*x[ns*j+4] + f_nmhe[1])
x[ns*j+5] = x[ns*j+5] + dt * (cs.cos(x[ns*j+7]) * cs.cos(x[ns*j+6]) * u_n[nu*j] - 0.2 * x[ns*j+5] - 9.81 + f_nmhe[2])
x[ns*j+6] = x[ns*j+6] + dt * ((1 / 0.20) * (u_n[nu*j+1] - x[ns*j+6]))
x[ns*j+7] = x[ns*j+7] + dt * ((1 / 0.17) * (u_n[nu*j+2] - x[ns*j+7]))
#print(x[ns*j])
umin = [3, -0.25, -0.25] * (N*nMAV)
umax = [15.5, 0.25, 0.25] * (N*nMAV)
bounds = og.constraints.Rectangle(umin, umax)
problem = og.builder.Problem(u, z0, cost).with_penalty_constraints(c) \
.with_constraints(bounds)
tcp_config = og.config.TcpServerConfiguration(bind_port=3301)
build_config = og.config.BuildConfiguration() \
.with_build_directory("MAV") \
.with_build_mode("release") \
.with_tcp_interface_config(tcp_config)
#.with_build_c_bindings()
meta = og.config.OptimizerMeta() \
.with_optimizer_name("shafter_nmpc_1")
#.with_rebuild(True)
solver_config = og.config.SolverConfiguration() \
.with_tolerance(1e-5) \
.with_initial_tolerance(1e-5) \
.with_max_duration_micros(40000) \
.with_max_outer_iterations(5) \
.with_penalty_weight_update_factor(5) \
.with_initial_penalty(1000.0)
builder = og.builder.OpEnOptimizerBuilder(problem, meta,
build_config, solver_config) \
.with_verbosity_level(1)
builder.build()
# Use TCP server
# ------------------------------------
mng = og.tcp.OptimizerTcpManager('MAV/shafter_nmpc_1')
mng.start()
x0 = [2.0,2.0,1.0,0.0,0.0,0.0,0.0,0.0]*nMAV
xref= [-2.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0]*nMAV
uold =[9.81,0.0,0.0]*nMAV
uref =[9.81,0.0,0.0]
qx_adapt = [5,5]
z0 = x0 + xref + uref + uold + [0,0,0] + qx_adapt
print(z0)
print(len(z0)) ##Length is equal to np
#obsdata = (0.0,0.0,1.0,1.0)
mng.ping()
solution = mng.call(z0, initial_guess=[9.81,0,0.0]*(N*nMAV),buffer_len = 8*4096)
print(solution['solution'])
mng.kill()