From e49d0638dabcfd46a5b7a4295ff783b9318e16bd Mon Sep 17 00:00:00 2001 From: Paul Buzuloiu Date: Wed, 23 May 2018 20:13:46 -0400 Subject: [PATCH 1/4] gross math --- app/rocket/kinetics.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/rocket/kinetics.py b/app/rocket/kinetics.py index 9fc3828..cc8f1a3 100644 --- a/app/rocket/kinetics.py +++ b/app/rocket/kinetics.py @@ -33,6 +33,12 @@ def deactivate(self): raise Exception('Failed to deactivate Kinetics Model') def predicted_apogee(self): + g = -9.81 + m = 20 + rho = + b = (1/2)*rho*area*drag_cofficient + n = np.sqrt(b/m*g) + t - t_apogee = (-1/g*n)(np.arctan(n*velocity)-np.arctan(n*burnout_velocity)) return 0 def acceleration(self): From 009c7d281a7e0eba70a4908c1debcf8209a1ad96 Mon Sep 17 00:00:00 2001 From: Paul Buzuloiu Date: Mon, 28 May 2018 22:13:11 -0400 Subject: [PATCH 2/4] figuring it out --- app/rocket/kinetics.py | 30 ++++++++++++++++++++++++------ test/rocket/test_kinetics.py | 9 ++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/rocket/kinetics.py b/app/rocket/kinetics.py index cc8f1a3..239e28a 100644 --- a/app/rocket/kinetics.py +++ b/app/rocket/kinetics.py @@ -33,13 +33,19 @@ def deactivate(self): raise Exception('Failed to deactivate Kinetics Model') def predicted_apogee(self): - g = -9.81 - m = 20 - rho = - b = (1/2)*rho*area*drag_cofficient + #g = -9.81 # gravity + g = self.acceleration() + m = 20 # rocket mass + rho = 2 # mass density of fluid + b = (1/2)*rho*self.get_rocket_area()*self.drag_cofficient() n = np.sqrt(b/m*g) - t - t_apogee = (-1/g*n)(np.arctan(n*velocity)-np.arctan(n*burnout_velocity)) - return 0 + t_burnout = 0 + + t_apogee = t_burnout + (-1/g*n)(np.arctan(n*self.velocity())-np.arctan(n*burnout_velocity)) + + p_apogee = p_burnout - (1/((g**2)*(n**2)))*ln(np.cos(g*n*(t_apogee-t_burnout)+c)) + + return p_apogee def acceleration(self): return self.acceleration_window.last() @@ -53,6 +59,18 @@ def position(self): def compute_brakes_percentage(self): return 1.0 + def drag_cofficient(self): + return 1.0 + + def compute_area(self): + return 1.0 + + def dict_to_matrix(self, dict): + return np.array([[dict['x']], + [dict['y']], + [dict['z']]]) + + def run(self): while self.active: measurement = self.imu.read_accel_filtered() diff --git a/test/rocket/test_kinetics.py b/test/rocket/test_kinetics.py index f52debe..6a11662 100644 --- a/test/rocket/test_kinetics.py +++ b/test/rocket/test_kinetics.py @@ -1,6 +1,6 @@ import pytest import time -import numpy +import numpy as np from app.rocket.kinetics import Kinetics from test.fixtures.dummy_device_factory import DummyDeviceFactory @@ -33,3 +33,10 @@ def test_start_asynchonously_updates_position(kinetics): final_position = kinetics.position() kinetics.deactivate() assert initial_position != final_position + +def test_matrix_conversion(kinetics): + dict = {'x': 1, 'y': 2, 'z': 3 } + matrix = kinetics.dict_to_matrix(dict) + assert matrix is np.array([[1], + [2], + [3]]) From 3520941a9d4dd7bf580d2604f1e57d0fa801de66 Mon Sep 17 00:00:00 2001 From: Paul Buzuloiu Date: Fri, 1 Jun 2018 00:15:03 -0400 Subject: [PATCH 3/4] better --- app/rocket/kinetics.py | 23 ++++++++++++++--------- test/rocket/test_kinetics.py | 10 +++++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/rocket/kinetics.py b/app/rocket/kinetics.py index 239e28a..af1a212 100644 --- a/app/rocket/kinetics.py +++ b/app/rocket/kinetics.py @@ -33,17 +33,23 @@ def deactivate(self): raise Exception('Failed to deactivate Kinetics Model') def predicted_apogee(self): - #g = -9.81 # gravity - g = self.acceleration() - m = 20 # rocket mass + accel = self.dict_to_matrix(self.acceleration()) + m = 20 # empty rocket mass + accel_force = m*accel + + c = self.drag_cofficient() + area = self.compute_area() rho = 2 # mass density of fluid - b = (1/2)*rho*self.get_rocket_area()*self.drag_cofficient() - n = np.sqrt(b/m*g) - t_burnout = 0 + b = (1/2)*rho*area*c # nice variable to work with + velocity = self.dict_to_matrix(self.velocity()) + drag_force = -1*b*np.square(velocity) + + n = np.sqrt(b/accel_force) # another nice variable - t_apogee = t_burnout + (-1/g*n)(np.arctan(n*self.velocity())-np.arctan(n*burnout_velocity)) + p_burnout = self.position() # TODO: make this return the position at start of TimeWindow + v_burnout = self.velocity() # TODO: make this return the velocity at start of TimeWindow - p_apogee = p_burnout - (1/((g**2)*(n**2)))*ln(np.cos(g*n*(t_apogee-t_burnout)+c)) + p_apogee = p_burnout - dot((1/((np.square(accel))*(np.sqare(n)))),np.ln(np.cos(np.arctan(2*np.dot(n,v_burnout))))) return p_apogee @@ -70,7 +76,6 @@ def dict_to_matrix(self, dict): [dict['y']], [dict['z']]]) - def run(self): while self.active: measurement = self.imu.read_accel_filtered() diff --git a/test/rocket/test_kinetics.py b/test/rocket/test_kinetics.py index 6a11662..04c3d07 100644 --- a/test/rocket/test_kinetics.py +++ b/test/rocket/test_kinetics.py @@ -37,6 +37,10 @@ def test_start_asynchonously_updates_position(kinetics): def test_matrix_conversion(kinetics): dict = {'x': 1, 'y': 2, 'z': 3 } matrix = kinetics.dict_to_matrix(dict) - assert matrix is np.array([[1], - [2], - [3]]) + assert np.array_equal(matrix, np.array([[1], + [2], + [3]])) + +def test_apogee_prediction(kinetics): + # TODO: this^ + pass From f4348592b627646d7224cc884c453fa12c90b59d Mon Sep 17 00:00:00 2001 From: Paul Buzuloiu Date: Sat, 2 Jun 2018 23:42:41 -0400 Subject: [PATCH 4/4] division by 0 problems --- app/rocket/kinetics.py | 25 +++++++++++++------------ test/rocket/test_kinetics.py | 9 ++++++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/rocket/kinetics.py b/app/rocket/kinetics.py index af1a212..f0e4850 100644 --- a/app/rocket/kinetics.py +++ b/app/rocket/kinetics.py @@ -6,6 +6,7 @@ WINDOW_SIZE = 50 + class Kinetics(Thread): def __init__(self, device_factory): self.imu = device_factory.imu @@ -34,22 +35,22 @@ def deactivate(self): def predicted_apogee(self): accel = self.dict_to_matrix(self.acceleration()) - m = 20 # empty rocket mass + m = 20 # empty rocket mass accel_force = m*accel c = self.drag_cofficient() area = self.compute_area() - rho = 2 # mass density of fluid - b = (1/2)*rho*area*c # nice variable to work with + rho = 2 # mass density of fluid + b = (1/2)*rho*area*c # nice variable to work with velocity = self.dict_to_matrix(self.velocity()) - drag_force = -1*b*np.square(velocity) - - n = np.sqrt(b/accel_force) # another nice variable + try: + drag_force = -1*b*np.square(velocity) - p_burnout = self.position() # TODO: make this return the position at start of TimeWindow - v_burnout = self.velocity() # TODO: make this return the velocity at start of TimeWindow + n = np.sqrt(b/accel_force) # another nice variable + p_burnout = self.position() # TODO: make this return the position at start of TimeWindow + v_burnout = self.velocity() # TODO: make this return the velocity at start of TimeWindow - p_apogee = p_burnout - dot((1/((np.square(accel))*(np.sqare(n)))),np.ln(np.cos(np.arctan(2*np.dot(n,v_burnout))))) + p_apogee = p_burnout - np.dot((1/((np.square(accel))*(np.square(n)))), np.log(np.cos(np.arctan(2*np.dot(n, v_burnout))))) return p_apogee @@ -89,7 +90,6 @@ def run(self): y=prev_velocity['y'] + delta_velocity[1], z=prev_velocity['z'] + delta_velocity[2]) - prev_position = self.position_window.last() delta_position = self.velocity_window.integrate_last(self.time_series[-2], self.time_series[-1]) self.position_window.append( @@ -104,6 +104,7 @@ def run(self): position = self.position() logging.debug("Position x: {}, y: {}, z: {}".format(position['x'], position['y'], position['z'])) + class TimeWindow(object): def __init__(self, size=WINDOW_SIZE): self.x = deque(np.zeros(size), maxlen=size) @@ -124,6 +125,6 @@ def integrate_last(self, t0, t1): def last(self, count=1): if count == 1: - return {'x': self.x[-1], 'y': self.y[-1], 'z': self.z[-1] } + return {'x': self.x[-1], 'y': self.y[-1], 'z': self.z[-1]} else: - return {'x': self.x[-1:-1*count], 'y': self.y[-1:-1*count], 'z': self.z[-1:-1*count] } + return {'x': self.x[-1:-1*count], 'y': self.y[-1:-1*count], 'z': self.z[-1:-1*count]} diff --git a/test/rocket/test_kinetics.py b/test/rocket/test_kinetics.py index 04c3d07..8aaed40 100644 --- a/test/rocket/test_kinetics.py +++ b/test/rocket/test_kinetics.py @@ -5,11 +5,13 @@ from test.fixtures.dummy_device_factory import DummyDeviceFactory + @pytest.fixture def kinetics(): device_factory = DummyDeviceFactory() return Kinetics(DummyDeviceFactory()) + def test_start_asynchonously_updates_acceleration(kinetics): kinetics.activate() initial_acceleration = kinetics.acceleration() @@ -18,6 +20,7 @@ def test_start_asynchonously_updates_acceleration(kinetics): kinetics.deactivate() assert initial_acceleration != final_acceleration + def test_start_asynchonously_updates_velocity(kinetics): kinetics.activate() initial_velocity = kinetics.velocity() @@ -26,6 +29,7 @@ def test_start_asynchonously_updates_velocity(kinetics): kinetics.deactivate() assert initial_velocity != final_velocity + def test_start_asynchonously_updates_position(kinetics): kinetics.activate() initial_position = kinetics.position() @@ -41,6 +45,5 @@ def test_matrix_conversion(kinetics): [2], [3]])) -def test_apogee_prediction(kinetics): - # TODO: this^ - pass +def test_apogee_prediction_zero_acceleration(kinetics): + kinetics.predicted_apogee() # TODO: finish me