-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquadrotor.py
165 lines (128 loc) · 4.99 KB
/
quadrotor.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mp
import matplotlib.animation as animation
import IPython
MASS = 0.600 # mass of the quadrotor
INERTIA = 0.15 # inertia of the quadrotor
LENGTH = 0.2 # length of the quadrotor
GRAVITY=9.81 #gravity constant
DELTA_T = 0.01 #integration step
NUMBER_STATES = 6 # number of states
NUMBER_CONTROLS = 2 # number of controls
def get_next_state(z,u):
"""
Inputs:
z: state of the quadrotor as a numpy array (x, vx, y, vy, theta, omega)
u: control as a numpy array (u1, u2)
Output:
the new state of the quadrotor as a numpy array
"""
x = z[0]
vx = z[1]
y = z[2]
vy = z[3]
theta = z[4]
omega = z[5]
dydt = np.zeros([NUMBER_STATES,])
dydt[0] = vx
dydt[1] = (-(u[0] + u[1]) * np.sin(theta)) / MASS
dydt[2] = vy
dydt[3] = ((u[0] + u[1]) * np.cos(theta) - MASS * GRAVITY) / MASS
dydt[4] = omega
dydt[5] = (LENGTH * (u[0] - u[1])) / INERTIA
z_next = z + dydt * DELTA_T
return z_next
def simulate(z0, controller, horizon_length, disturbance = False):
"""
This function simulates the quadrotor for horizon_length steps from initial state z0
Inputs:
z0: the initial conditions of the quadrotor as a numpy array (x, vx, y, vy, theta, omega)
controller: a function that takes a state z as argument and index i of the time step and returns a control u
horizon_length: the horizon length
disturbance: if True will generate a random push every seconds during the simulation
Output:
t[time_horizon+1] contains the simulation time
z[4, time_horizon+1] and u[2, time_horizon] containing the time evolution of states and control
"""
t = np.zeros([horizon_length+1,])
z=np.empty([NUMBER_STATES, horizon_length+1])
z[:,0] = z0
u=np.zeros([NUMBER_CONTROLS, horizon_length])
for i in range(horizon_length):
u[:,i] = controller(z[:,i],i)
z[:,i+1] = get_next_state(z[:,i], u[:,i])
if disturbance and np.mod(i,100)==0:
dist = np.zeros([NUMBER_STATES, ])
dist[1::2] = np.random.uniform(-1.,1,(3,))
z[:,i+1] += dist
t[i+1] = t[i] + DELTA_T
return t, z, u
def animate_robot(x, u, dt = 0.01):
"""
This function makes an animation showing the behavior of the quadrotor
takes as input the result of a simulation (with dt=0.01s)
"""
min_dt = 0.1
if(dt < min_dt):
steps = int(min_dt/dt)
use_dt = int(np.round(min_dt * 1000))
else:
steps = 1
use_dt = int(np.round(dt * 1000))
#what we need to plot
plotx = x[:,::steps]
plotx = plotx[:,:-1]
plotu = u[:,::steps]
fig = mp.figure.Figure(figsize=[8.5,8.5])
mp.backends.backend_agg.FigureCanvasAgg(fig)
ax = fig.add_subplot(111, autoscale_on=False, xlim=[-4,4], ylim=[-4,4])
ax.grid()
list_of_lines = []
#create the robot
# the main frame
line, = ax.plot([], [], 'k', lw=6)
list_of_lines.append(line)
# the left propeller
line, = ax.plot([], [], 'b', lw=4)
list_of_lines.append(line)
# the right propeller
line, = ax.plot([], [], 'b', lw=4)
list_of_lines.append(line)
# the left thrust
line, = ax.plot([], [], 'r', lw=1)
list_of_lines.append(line)
# the right thrust
line, = ax.plot([], [], 'r', lw=1)
list_of_lines.append(line)
def _animate(i):
for l in list_of_lines: #reset all lines
l.set_data([],[])
theta = plotx[4,i]
x = plotx[0,i]
y = plotx[2,i]
trans = np.array([[x,x],[y,y]])
rot = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
main_frame = np.array([[-LENGTH, LENGTH], [0,0]])
main_frame = rot @ main_frame + trans
left_propeller = np.array([[-1.3 * LENGTH, -0.7*LENGTH], [0.1,0.1]])
left_propeller = rot @ left_propeller + trans
right_propeller = np.array([[1.3 * LENGTH, 0.7*LENGTH], [0.1,0.1]])
right_propeller = rot @ right_propeller + trans
left_thrust = np.array([[LENGTH, LENGTH], [0.1, 0.1+plotu[0,i]*0.04]])
left_thrust = rot @ left_thrust + trans
right_thrust = np.array([[-LENGTH, -LENGTH], [0.1, 0.1+plotu[0,i]*0.04]])
right_thrust = rot @ right_thrust + trans
list_of_lines[0].set_data(main_frame[0,:], main_frame[1,:])
list_of_lines[1].set_data(left_propeller[0,:], left_propeller[1,:])
list_of_lines[2].set_data(right_propeller[0,:], right_propeller[1,:])
list_of_lines[3].set_data(left_thrust[0,:], left_thrust[1,:])
list_of_lines[4].set_data(right_thrust[0,:], right_thrust[1,:])
return list_of_lines
def _init():
return _animate(0)
ani = animation.FuncAnimation(fig, _animate, np.arange(0, len(plotx[0,:])),
interval=use_dt, blit=True, init_func=_init)
plt.close(fig)
plt.close(ani._fig)
IPython.display.display_html(IPython.core.display.HTML(ani.to_html5_video()))