From 00302e73cbb310b0c1dda0d7aaae9e5b2d880e29 Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Thu, 17 Nov 2022 21:12:34 +0100 Subject: [PATCH 1/2] MAINT: refactor bearing calculation --- rocketpy/Flight.py | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/rocketpy/Flight.py b/rocketpy/Flight.py index cb908dcab..6b0d06680 100644 --- a/rocketpy/Flight.py +++ b/rocketpy/Flight.py @@ -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 @@ -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 From 505c2012ef37d315628a8ca494fec071055cf33e Mon Sep 17 00:00:00 2001 From: Gui-FernandesBR Date: Thu, 17 Nov 2022 21:13:52 +0100 Subject: [PATCH 2/2] TEST: prevent from numerical errors --- tests/test_flight.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_flight.py b/tests/test_flight.py index 9c66a7b3a..6038d1807 100644 --- a/tests/test_flight.py +++ b/tests/test_flight.py @@ -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