-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensors.py
247 lines (192 loc) · 6.81 KB
/
sensors.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
This file defines multiple classes, one for each sensor. Each class has an
update function that takes a rocket as a parameter, which it performs updates on.
"""
import numpy as np
from pyquaternion import Quaternion
class Sensor: # TODO: add sensor noise to sensor class
"""
A class used to represent a Sensor object.
...
Attributes
----------
calibration: float
"""
def __init__(self, calibration):
self.calibration = calibration
class Accelerometer(Sensor):
"""
A class used to represent a Accelerometer object.
...
Attributes
----------
calibration: float
body_acceleration: numpy.array
"""
def __init__(self, calibration=1):
self.body_acceleration = np.array([0.0, 0.0, 0.0]) # [m/s^2]
super().__init__(calibration)
def update(self, rocket):
"""
Calculates the body (proper) acceleration of the rocket
Parameters
----------
rocket: A rocket object
An rocket instance. Calculations will be done according to this rocket's state
Returns
-------
body_acceleration
Numpy array representing the body (proper) acceleration
of the rocket in m/s^2.
"""
quaternion = Quaternion(rocket.orientation)
# world_mag_field is in NED so it needs to be converted to ENU first (x=y, y=x, z=-z)
body_acceleration = quaternion.rotate(
rocket.world_acceleration * self.calibration)
return body_acceleration
class Baro_Pressure_Sensor(Sensor):
"""
A class used to represent a Barometric Pressure Sensor object.
...
Attributes
----------
calibration: float
baro_pressure: float
"""
def __init__(self, calibration=1):
self.baro_pressure = 0 # [KPa]
super().__init__(calibration)
def update(self, rocket):
"""
Calculates the barometric pressure of the atmosphere around the Rocket.
Uses NASA formulas:
https://www.grc.nasa.gov/WWW/K-12/rocket/atmosmet.html
Parameters
----------
rocket: A rocket object
An rocket instance. Calculations will be done according to this rocket's state
Returns
---------
baro_pressure: float
Barometric pressure of the atmosphere around the rocket, in kilopascals.
"""
# Use NASA formula to calculate barometric pressure
if rocket.altitude < 11000:
baro_pressure = 101.29 * (
(rocket.temperature + 273.1) / 288.08) ** 5.256
else:
baro_pressure = 22.65 * np.exp(1.73 - 0.000157 * rocket.altitude)
return baro_pressure * self.calibration
class Gyro(Sensor):
"""
A class used to represent a Gyroscopic Sensor object.
...
Attributes
----------
calibration: float
orientation: numpy.array
Notes
-----
orientation is in the format of [w, x, y, z], where [w] is the scalar part
of the quaternion and [x, y, z] are the vector parts.
"""
def __init__(self, calibration=1):
self.orientation = np.array([1.0, 0.0, 0.0, 0.0]) # identity quat.
super().__init__(calibration)
def update(self, rocket, angular_rates, delta_time) -> np.array([]):
"""
Calculates the orientation quaternion of the Rocket object based on
fixed angular rates.
Parameters
----------
rocket: A rocket object
An rocket instance. Calculations will be done according to this rocket's state
angular_rates: numpy.array
The angular (pitch, yaw, and roll) rates of the Rocket object.
delta_time: float
The change in time since the last update of the Rocket flight in
seconds.
Returns
-------
numpy.array
Numpy array (containing data with float type) representing the
orientation of the Rocket object.
"""
orientation_quaternion = Quaternion(rocket.orientation)
orientation_quaternion.integrate(angular_rates * self.calibration,
delta_time)
return orientation_quaternion.elements
class Magnetometer(Sensor):
"""
A class used to represent a Magnetometer object.
...
Attributes
----------
calibration: float
body_mag_field: numpy.array
Notes
-----
world_mag_field is in the format of [D, I, H, X, Y, Z, F], where:
D = Geomagnetic declination [deg]
I = Geomagnetic inclination [deg]
H = Horizontal geomagnetic field intensity [nT]
X = North component of geomagnetic field intensity [nT]
Y = East component of geomagnetic field intensity [nT]
Z = Vertical component of geomagnetic field intensity [nT]
F = Total geomagnetic field intensity [nT]
However, body_mag_field only includes the X,Y,Z components.
"""
def __init__(self, calibration=1):
self.body_mag_field = np.array(
[0.0, 0.0, 0.0]) # See class docstring
super().__init__(calibration)
def update(self, rocket):
"""
Calculates the magnetic field around the rocket (Tesla)
Parameters
----------
rocket: A rocket object
An rocket instance. Calculations will be done according to this rocket's state
Returns
-------
body_magnetic_field
Numpy array representing the magnetic field around
the rocket (Note: in ENU coordinate frame).
"""
quaternion = Quaternion(rocket.orientation)
body_magnetic_field = quaternion.rotate(
np.array([rocket.world_mag_field[4], rocket.world_mag_field[3],
-rocket.world_mag_field[5]]) * self.calibration)
return body_magnetic_field
class Thermistor(Sensor):
"""
A class used to represent a Thermistor object.
...
Attributes
----------
calibration: float
temperature: float
"""
def __init__(self, calibration=1):
self.temperature = 0 # [Celsius]
super().__init__(calibration)
def update(self, rocket):
"""
Calculates the temperature around the rocket, in celsius.
Uses NASA formulas:
https://www.grc.nasa.gov/WWW/K-12/rocket/atmosmet.html
Parameters
----------
rocket: A rocket object
An rocket instance. Calculations will be done according to this rocket's state
Returns
-------
temperature: float
Temperature of the air around the rocket, in celsius.
"""
# Use NASA formula to calculate temperature
if rocket.altitude < 11000:
temperature = 15.04 - 0.00649 * rocket.altitude
else:
temperature = -56.46
return temperature * self.calibration