-
Notifications
You must be signed in to change notification settings - Fork 63
/
quadcopter.py
142 lines (123 loc) · 6.38 KB
/
quadcopter.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
import numpy as np
import math
import scipy.integrate
import time
import datetime
import threading
class Propeller():
def __init__(self, prop_dia, prop_pitch, thrust_unit='N'):
self.dia = prop_dia
self.pitch = prop_pitch
self.thrust_unit = thrust_unit
self.speed = 0 #RPM
self.thrust = 0
def set_speed(self,speed):
self.speed = speed
# From http://www.electricrcaircraftguy.com/2013/09/propeller-static-dynamic-thrust-equation.html
self.thrust = 4.392e-8 * self.speed * math.pow(self.dia,3.5)/(math.sqrt(self.pitch))
self.thrust = self.thrust*(4.23e-4 * self.speed * self.pitch)
if self.thrust_unit == 'Kg':
self.thrust = self.thrust*0.101972
class Quadcopter():
# State space representation: [x y z x_dot y_dot z_dot theta phi gamma theta_dot phi_dot gamma_dot]
# From Quadcopter Dynamics, Simulation, and Control by Andrew Gibiansky
def __init__(self,quads,gravity=9.81,b=0.0245):
self.quads = quads
self.g = gravity
self.b = b
self.thread_object = None
self.ode = scipy.integrate.ode(self.state_dot).set_integrator('vode',nsteps=500,method='bdf')
self.time = datetime.datetime.now()
for key in self.quads:
self.quads[key]['state'] = np.zeros(12)
self.quads[key]['state'][0:3] = self.quads[key]['position']
self.quads[key]['state'][6:9] = self.quads[key]['orientation']
self.quads[key]['m1'] = Propeller(self.quads[key]['prop_size'][0],self.quads[key]['prop_size'][1])
self.quads[key]['m2'] = Propeller(self.quads[key]['prop_size'][0],self.quads[key]['prop_size'][1])
self.quads[key]['m3'] = Propeller(self.quads[key]['prop_size'][0],self.quads[key]['prop_size'][1])
self.quads[key]['m4'] = Propeller(self.quads[key]['prop_size'][0],self.quads[key]['prop_size'][1])
# From Quadrotor Dynamics and Control by Randal Beard
ixx=((2*self.quads[key]['weight']*self.quads[key]['r']**2)/5)+(2*self.quads[key]['weight']*self.quads[key]['L']**2)
iyy=ixx
izz=((2*self.quads[key]['weight']*self.quads[key]['r']**2)/5)+(4*self.quads[key]['weight']*self.quads[key]['L']**2)
self.quads[key]['I'] = np.array([[ixx,0,0],[0,iyy,0],[0,0,izz]])
self.quads[key]['invI'] = np.linalg.inv(self.quads[key]['I'])
self.run = True
def rotation_matrix(self,angles):
ct = math.cos(angles[0])
cp = math.cos(angles[1])
cg = math.cos(angles[2])
st = math.sin(angles[0])
sp = math.sin(angles[1])
sg = math.sin(angles[2])
R_x = np.array([[1,0,0],[0,ct,-st],[0,st,ct]])
R_y = np.array([[cp,0,sp],[0,1,0],[-sp,0,cp]])
R_z = np.array([[cg,-sg,0],[sg,cg,0],[0,0,1]])
R = np.dot(R_z, np.dot( R_y, R_x ))
return R
def wrap_angle(self,val):
return( ( val + np.pi) % (2 * np.pi ) - np.pi )
def state_dot(self, time, state, key):
state_dot = np.zeros(12)
# The velocities(t+1 x_dots equal the t x_dots)
state_dot[0] = self.quads[key]['state'][3]
state_dot[1] = self.quads[key]['state'][4]
state_dot[2] = self.quads[key]['state'][5]
# The acceleration
x_dotdot = np.array([0,0,-self.quads[key]['weight']*self.g]) + np.dot(self.rotation_matrix(self.quads[key]['state'][6:9]),np.array([0,0,(self.quads[key]['m1'].thrust + self.quads[key]['m2'].thrust + self.quads[key]['m3'].thrust + self.quads[key]['m4'].thrust)]))/self.quads[key]['weight']
state_dot[3] = x_dotdot[0]
state_dot[4] = x_dotdot[1]
state_dot[5] = x_dotdot[2]
# The angular rates(t+1 theta_dots equal the t theta_dots)
state_dot[6] = self.quads[key]['state'][9]
state_dot[7] = self.quads[key]['state'][10]
state_dot[8] = self.quads[key]['state'][11]
# The angular accelerations
omega = self.quads[key]['state'][9:12]
tau = np.array([self.quads[key]['L']*(self.quads[key]['m1'].thrust-self.quads[key]['m3'].thrust), self.quads[key]['L']*(self.quads[key]['m2'].thrust-self.quads[key]['m4'].thrust), self.b*(self.quads[key]['m1'].thrust-self.quads[key]['m2'].thrust+self.quads[key]['m3'].thrust-self.quads[key]['m4'].thrust)])
omega_dot = np.dot(self.quads[key]['invI'], (tau - np.cross(omega, np.dot(self.quads[key]['I'],omega))))
state_dot[9] = omega_dot[0]
state_dot[10] = omega_dot[1]
state_dot[11] = omega_dot[2]
return state_dot
def update(self, dt):
for key in self.quads:
self.ode.set_initial_value(self.quads[key]['state'],0).set_f_params(key)
self.quads[key]['state'] = self.ode.integrate(self.ode.t + dt)
self.quads[key]['state'][6:9] = self.wrap_angle(self.quads[key]['state'][6:9])
self.quads[key]['state'][2] = max(0,self.quads[key]['state'][2])
def set_motor_speeds(self,quad_name,speeds):
self.quads[quad_name]['m1'].set_speed(speeds[0])
self.quads[quad_name]['m2'].set_speed(speeds[1])
self.quads[quad_name]['m3'].set_speed(speeds[2])
self.quads[quad_name]['m4'].set_speed(speeds[3])
def get_position(self,quad_name):
return self.quads[quad_name]['state'][0:3]
def get_linear_rate(self,quad_name):
return self.quads[quad_name]['state'][3:6]
def get_orientation(self,quad_name):
return self.quads[quad_name]['state'][6:9]
def get_angular_rate(self,quad_name):
return self.quads[quad_name]['state'][9:12]
def get_state(self,quad_name):
return self.quads[quad_name]['state']
def set_position(self,quad_name,position):
self.quads[quad_name]['state'][0:3] = position
def set_orientation(self,quad_name,orientation):
self.quads[quad_name]['state'][6:9] = orientation
def get_time(self):
return self.time
def thread_run(self,dt,time_scaling):
rate = time_scaling*dt
last_update = self.time
while(self.run==True):
time.sleep(0)
self.time = datetime.datetime.now()
if (self.time-last_update).total_seconds() > rate:
self.update(dt)
last_update = self.time
def start_thread(self,dt=0.002,time_scaling=1):
self.thread_object = threading.Thread(target=self.thread_run,args=(dt,time_scaling))
self.thread_object.start()
def stop_thread(self):
self.run = False