Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Sensors #583

Merged
merged 83 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
e403872
ENH: sensors class
MateusStano Apr 1, 2024
6cd3598
ENH: add accelerometer and gyroscope classes
MateusStano Apr 1, 2024
3b81777
ENH: euler to quaternions tool function
MateusStano Apr 1, 2024
4c8aa13
ENH: add sensors to other classes
MateusStano Apr 1, 2024
09e0d75
ENH: prints and sensors draw
MateusStano Apr 1, 2024
38866f4
ENH: add sensors to parachutes
MateusStano Apr 1, 2024
b386e38
TST: add sensors fixtures
MateusStano Apr 1, 2024
6ff2dde
TST: add sensors tests
MateusStano Apr 1, 2024
5ae01b9
DEV: sensors testing notebook
MateusStano Apr 1, 2024
1dd5781
ENH: add option to add noise to each axis seperatly
MateusStano Apr 2, 2024
943542a
MNT: run isort
MateusStano Apr 2, 2024
28ebc46
ENH: calculate u_dot only once for all sensors
MateusStano Apr 2, 2024
378bb54
MNT: change `measured_values` to `measured_data`
MateusStano Apr 2, 2024
d8440f2
TST: unite gyro and accel tests
MateusStano Apr 2, 2024
99f445f
Update rocketpy/plots/rocket_plots.py
MateusStano Apr 11, 2024
4db26f0
ENH: normalize quaternions for transform matrix
MateusStano Apr 12, 2024
4356af7
TST: lower tolerances
MateusStano Apr 12, 2024
5c37f06
BUG: handle zero norm differently due to numpy.float64
MateusStano Apr 15, 2024
8d64998
MNT: quantization typos
MateusStano Apr 15, 2024
28da8f3
DOCS: mention Hadamard product
MateusStano Apr 15, 2024
cef72a0
DOC: complete sensors class docstrings
MateusStano Apr 15, 2024
5f8f1f4
Squashed commit of the following:
MateusStano Apr 29, 2024
9a6b052
MNT: fix component repr for sensors
MateusStano Apr 30, 2024
7953cb0
ENH: add _attatched_rockets to sensors
MateusStano Apr 30, 2024
5a37553
ENH: add sensors saving methods
MateusStano Apr 30, 2024
09ea252
MNT: simplify sensors repr
MateusStano Apr 30, 2024
9851392
ENH: use _save_data in accel and gyro
MateusStano Apr 30, 2024
0f81bc3
ENH: improve accelerometer export
MateusStano Apr 30, 2024
a740fc2
ENH: imrpove gyroscope export
MateusStano Apr 30, 2024
cf6c26d
ENH: add sensor initialization
MateusStano Apr 30, 2024
2a14f1d
ENH: speed up measure call
MateusStano Apr 30, 2024
216523c
ENH: add final sensor cache
MateusStano Apr 30, 2024
09288d4
ENH: add export sensor data
MateusStano Apr 30, 2024
aa6fcdf
BUG: wrong initialization order
MateusStano Apr 30, 2024
bf6b083
TST: test for new measured_data and exports
MateusStano Apr 30, 2024
5f86223
ENH: abstract noise printings
MateusStano Apr 30, 2024
4dcc26b
MNT: run isort
MateusStano May 1, 2024
32898c5
Merge branch 'develop' into enh/sensors
MateusStano May 1, 2024
f7332d8
Merge branch 'develop' into enh/sensors
Gui-FernandesBR May 2, 2024
2131ee9
ENH: add noise variance
MateusStano May 2, 2024
b2da0c3
BUG: fix prints for noise variance
MateusStano May 2, 2024
123d033
TST: add variances
MateusStano May 2, 2024
7d0e6f3
Merge branch 'enh/sensors' of https://github.com/RocketPy-Team/Rocket…
MateusStano May 2, 2024
00f0f3a
Update rocketpy/rocket/rocket.py
MateusStano May 3, 2024
de2d8bd
MNT: remove .type attribute
MateusStano May 6, 2024
fec6725
DOC: fix units of transformation_euler_anges
MateusStano May 6, 2024
ce2a63d
DOC: add examples to transformation_euler_angles
MateusStano May 6, 2024
f2656c5
DOC: mention Euler parameters in docs
MateusStano May 6, 2024
41bf9e9
TST: test_euler_to_quaternions
MateusStano May 6, 2024
11873cd
TST: fix doc tests
MateusStano May 6, 2024
b795031
MNT: privatize components attributes and improve docs
MateusStano May 6, 2024
968f55a
TST: add index to var names
MateusStano May 6, 2024
d827b8e
MNT: return temp drift directly
MateusStano May 6, 2024
0b0f201
ENH: improve export methods
MateusStano May 6, 2024
bad3b07
DOC: typos
MateusStano May 6, 2024
0891c6c
MNT: improve flight init
MateusStano May 6, 2024
fa1b6a8
Fix code style issues with Black
lint-action May 6, 2024
b7439f4
Merge branch 'enh/sensors' of https://github.com/RocketPy-Team/Rocket…
MateusStano May 6, 2024
8c71432
TST: overall improvements on sensor testing
MateusStano May 6, 2024
5885992
ENH: change format of sensor_data dict
MateusStano May 6, 2024
02cad05
MNT: improve flight init
MateusStano May 6, 2024
69f17cd
TST: increase rel tolerances
MateusStano May 6, 2024
6894f58
Merge branch 'enh/sensors' of https://github.com/RocketPy-Team/Rocket…
MateusStano May 6, 2024
8d96e58
TST: fix export sensor data test
MateusStano May 6, 2024
ec4e25e
TST: fix doctests
MateusStano May 6, 2024
adb2dbb
TST: fix doctests
MateusStano May 6, 2024
e0812a2
ENH: change units in prints
MateusStano May 9, 2024
6ec4c0b
ENH: add .round to Matrix and remove broken doctests
MateusStano May 10, 2024
78e67f2
TST: test transfromation euler angles
MateusStano May 10, 2024
d49b5c4
TST: imrpove test_euler_to_quaternions
MateusStano May 10, 2024
57b41ab
DEV: update sensors_testing notebook
Gui-FernandesBR May 15, 2024
7a2e50a
MNT: minor fixes - typos and isort
Gui-FernandesBR May 15, 2024
a57308d
MNT: remove aft double underscore from __units
MateusStano May 16, 2024
11a8ab6
DOC: remove .type
MateusStano May 16, 2024
2984065
MNT: remove .units underscores
MateusStano May 16, 2024
82f79e7
MNT: remove repetition from sensors prints
MateusStano May 16, 2024
5016283
ENH: normalize_quaternions in tools.py
MateusStano May 16, 2024
6806912
BUG: remove Matrix circuklar import in tools
MateusStano May 16, 2024
b99b301
MNT: remove unused variable
MateusStano May 16, 2024
75f8d9e
ENH: explicit for loop variables
MateusStano May 16, 2024
c1864b2
Merge branch 'enh/sensors' of https://github.com/RocketPy-Team/Rocket…
MateusStano May 16, 2024
a20d31d
DOC: improve orientation docs
MateusStano May 16, 2024
ce1d179
ENH: x_position to x_offset
MateusStano May 16, 2024
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
690 changes: 690 additions & 0 deletions docs/notebooks/sensors_testing.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rocketpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
Tail,
TrapezoidalFins,
)
from .sensors import Accelerometer, Gyroscope, Sensors
from .simulation import Flight
53 changes: 51 additions & 2 deletions rocketpy/control/controller.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from inspect import signature

from ..prints.controller_prints import _ControllerPrints


Expand Down Expand Up @@ -52,6 +54,10 @@
the controller function can interact with. The objects are
listed in the same order as they are provided in the
`interactive_objects`.
7. `sensors` (list): A list of sensors that are attached to the
rocket. The most recent measurements of the sensors are provided
with the ``sensor.measurement`` attribute. The sensors are
listed in the same order as they are added to the rocket

This function will be called during the simulation at the specified
sampling rate. The function should evaluate and change the interactive
Expand All @@ -78,7 +84,7 @@
None
"""
self.interactive_objects = interactive_objects
self.controller_function = controller_function
self.controller_function = self.__init_controller_function(controller_function)
self.sampling_rate = sampling_rate
self.name = name
self.prints = _ControllerPrints(self)
Expand All @@ -88,7 +94,44 @@
else:
self.observed_variables = []

def __call__(self, time, state_vector, state_history):
def __init_controller_function(self, controller_function):
"""Checks number of arguments of the controller function and initializes
it with the correct number of arguments. This is a workaround to allow
the controller function to receive sensors without breaking changes"""
sig = signature(controller_function)
if len(sig.parameters) == 6:

def new_controller_function(
time,
sampling_rate,
state_vector,
state_history,
observed_variables,
interactive_objects,
sensors,
):
return controller_function(
time,
sampling_rate,
state_vector,
state_history,
observed_variables,
interactive_objects,
)

elif len(sig.parameters) == 7:
new_controller_function = controller_function

Check warning on line 123 in rocketpy/control/controller.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/control/controller.py#L122-L123

Added lines #L122 - L123 were not covered by tests
else:
raise ValueError(

Check warning on line 125 in rocketpy/control/controller.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/control/controller.py#L125

Added line #L125 was not covered by tests
"The controller function must have 6 or 7 arguments. "
"The arguments must be in the following order: "
"(time, sampling_rate, state_vector, state_history, "
"observed_variables, interactive_objects, sensors)."
"Sensors argument is optional."
MateusStano marked this conversation as resolved.
Show resolved Hide resolved
)
return new_controller_function
Comment on lines +101 to +132
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, as far as I remember, the controller function is still private.

Can't we simply change the expected behavior of controller function to always receive 7 arguments?

Of course current applications would breake, but the class was still private...

I'm just not 100% sure about the air brakes...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would change it for the airbrakes too, so its technically a breaking change. I do think having the sensors argument be optional can be good since not all rockets will have sensors.

One thing I just thought of, maybe we should have all this arguments be passed as key word arguments in the user's controller_function. The user's controller functions would then have to receive **kwargs and just unpack it as needed:

def controller_function(**kwargs):
    time = kwargs["time"]    
    sensors = kwargs["sensors"]

    ...

This would allow us to never need to care about how many params we are passing and in what order.

What do you think of this? It would be a breaking change, but only for the air brakes usage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your proposal is really good.

Why would it be a breaking change? Couldn't you individually handle the case where the key "sensors" is not present in the kwargs dict?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a breaking change because any controller_function defined by a user would need to define its parameters differently


def __call__(self, time, state_vector, state_history, sensors):
"""Call the controller function. This is used by the simulation class.

Parameters
Expand All @@ -104,6 +147,11 @@
history is a list of every state vector of every step of the
simulation. The state history is a list of lists, where each
sublist is a state vector and is ordered from oldest to newest.
sensors : list
A list of sensors that are attached to the rocket. The most recent
measurements of the sensors are provided with the
``sensor.measurement`` attribute. The sensors are listed in the same
order as they are added to the rocket.

Returns
-------
Expand All @@ -116,6 +164,7 @@
state_history,
self.observed_variables,
self.interactive_objects,
sensors,
)
if observed_variables is not None:
self.observed_variables.append(observed_variables)
Expand Down
51 changes: 49 additions & 2 deletions rocketpy/mathutils/vector_matrix.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cmath import isclose
from itertools import product

from rocketpy.tools import cached_property
from rocketpy.tools import cached_property, euler_to_quaternions


class Vector:
Expand Down Expand Up @@ -153,7 +153,10 @@
@cached_property
def unit_vector(self):
"""R3 vector with the same direction of self, but normalized."""
return self / abs(self)
try:
return self / abs(self)
except ZeroDivisionError:
return self

Check warning on line 159 in rocketpy/mathutils/vector_matrix.py

View check run for this annotation

Codecov / codecov/patch

rocketpy/mathutils/vector_matrix.py#L158-L159

Added lines #L158 - L159 were not covered by tests

@cached_property
def cross_matrix(self):
Expand Down Expand Up @@ -239,6 +242,29 @@
]
)

def __and__(self, other):
"""Element wise multiplication between two R3 vectors.

Parameters
----------
other : Vector
R3 vector to be multiplied with self.
MateusStano marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Vector
R3 vector resulting from the element wise multiplication between
self and other.

Examples
--------
>>> v = Vector([1, 7, 3])
>>> u = Vector([2, 5, 6])
>>> (v & u)
Vector(2, 35, 18)
"""
return Vector([self.x * other[0], self.y * other[1], self.z * other[2]])

def __matmul__(self, other):
"""Dot product between two R3 vectors."""
return self.x * other.x + self.y * other.y + self.z * other.z
Expand Down Expand Up @@ -984,6 +1010,27 @@
]
)

@staticmethod
def transformation_euler_angles(roll, pitch, yaw):
phmbressan marked this conversation as resolved.
Show resolved Hide resolved
"""Returns the transformation Matrix from frame B to frame A, where B
is rotated by the Euler angles roll, pitch and yaw with respect to A.

Parameters
----------
roll : float
The roll angle in radians.
pitch : float
The pitch angle in radians.
yaw : float
The yaw angle in radians.
Gui-FernandesBR marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Matrix
The transformation matrix from frame B to frame A.
"""
return Matrix.transformation(euler_to_quaternions(roll, pitch, yaw))


if __name__ == "__main__":
import doctest
Expand Down
Loading
Loading