Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 4 additions & 32 deletions rocketpy/Flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -2934,9 +2934,8 @@ def drift(self):
return drift

@cached_property
def bearing(self, interpolation="spline", extrapolation="natural"):
def bearing(self):
"""Rocket bearing compass, in degrees, at each time step.

Returns
-------
bearing: Function
Expand All @@ -2946,41 +2945,14 @@ def bearing(self, interpolation="spline", extrapolation="natural"):
# Get some nicknames
t = self.x[:, 0]
x, y = self.x[:, 1], self.y[:, 1]
bearing = []
for i in range(len(t)):
# Forcing arctan2(0, 0) = self.heading
if abs(x[i]) < 1e-6 and abs(y[i]) < 1e-6:
bearing.append(np.deg2rad(self.heading))
elif abs(x[i]) < 1e-6: # check if the rocket is on x axis
if y[i] > 0:
bearing.append(0)
else:
bearing.append(np.pi)
elif abs(y[i]) < 1e-6: # check if the rocket is on x axis
if x[i] > 0:
bearing.append(np.pi / 2)
else:
bearing.append(3 * np.pi / 2)
else:
# Calculate bearing as the azimuth considering different quadrants
if x[i] * y[i] < 0 and x[i] < 0: # Fourth quadrant
bearing.append(-np.pi / 2 + np.arctan(abs(y[i]) / abs(x[i])))
elif x[i] * y[i] < 0 and x[i] > 0: # Second quadrant
bearing.append(np.pi / 2 + np.arctan(abs(x[i]) / abs(y[i])))
elif x[i] * y[i] > 0 and x[i] < 0: # Third quadrant
bearing.append(np.pi + np.arctan(abs(x[i]) / abs(y[i])))
else: # First quadrant
bearing.append(np.arctan(abs(x[i]) / abs(y[i])))

bearing = np.rad2deg(bearing)

# Calculate the bearing and to a Function object
bearing = (2 * np.pi - np.arctan2(-x, y)) * (180 / np.pi)
bearing = np.column_stack((t, bearing))
print(bearing)
bearing = Function(
bearing,
"Time (s)",
"Bearing (deg)",
interpolation,
extrapolation,
)

return bearing
Expand Down
2 changes: 1 addition & 1 deletion tests/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,5 +747,5 @@ def test_latlon_conversions2(mock_show):

test_flight.postProcess()

assert test_flight.longitude(test_flight.tFinal) == 0
assert abs(test_flight.longitude(test_flight.tFinal) - 0) < 1e-12
assert test_flight.latitude(test_flight.tFinal) > 0