Skip to content

Commit 3f2a2c5

Browse files
Gui-FernandesBRMateusStanophmbressan
authored
Merge pull request #683 from RocketPy-Team/enh/sensors-impl
* ENH: sensors class * ENH: add accelerometer and gyroscope classes * ENH: euler to quaternions tool function * ENH: add sensors to other classes * ENH: prints and sensors draw * ENH: add sensors to parachutes * TST: add sensors fixtures * TST: add sensors tests * DEV: sensors testing notebook * ENH: add option to add noise to each axis seperatly * MNT: run isort * ENH: calculate u_dot only once for all sensors * MNT: change `measured_values` to `measured_data` * TST: unite gyro and accel tests * Update rocketpy/plots/rocket_plots.py Co-authored-by: Pedro Henrique Marinho Bressan <[email protected]> * ENH: normalize quaternions for transform matrix * TST: lower tolerances * BUG: handle zero norm differently due to numpy.float64 * MNT: quantization typos * DOCS: mention Hadamard product * DOC: complete sensors class docstrings * Squashed commit of the following: commit b18ae9c Merge: 67515f6 9a8e0c1 Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 25 18:15:24 2024 -0400 Merge branch 'develop' into bug/airbrakes-postprocess commit 67515f6 Author: MateusStano <[email protected]> Date: Thu Apr 25 23:02:11 2024 +0200 ENH: automatically disable time overshoot for controllers commit f60c53b Merge: 4974e8d 3df7586 Author: MateusStano <[email protected]> Date: Thu Apr 25 22:53:10 2024 +0200 Merge branch 'bug/airbrakes-postprocess' of https://github.com/RocketPy-Team/RocketPy into bug/airbrakes-postprocess commit 4974e8d Author: MateusStano <[email protected]> Date: Thu Apr 25 22:52:48 2024 +0200 BUG: initialize state derivatives for controllers commit a49baf4 Author: MateusStano <[email protected]> Date: Thu Apr 25 22:52:13 2024 +0200 MNT: move controller initialization to private method commit 9a8e0c1 Merge: 14375ed 2cbaa77 Author: MateusStano <[email protected]> Date: Thu Apr 25 15:37:05 2024 -0300 Merge pull request #580 from RocketPy-Team/mnt/modularize-rocket-draw MNT: Modularize Rocket Draw commit 2cbaa77 Merge: 822a89e 14375ed Author: MateusStano <[email protected]> Date: Thu Apr 25 11:44:22 2024 -0300 Merge branch 'develop' into mnt/modularize-rocket-draw commit 3df7586 Merge: bf8c68b 14375ed Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 25 06:49:58 2024 -0400 Merge branch 'develop' into bug/airbrakes-postprocess commit 14375ed Merge: c31c6f8 b899064 Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 25 06:47:36 2024 -0400 Merge pull request #588 from RocketPy-Team/enh/exponential-backoff-decorator ENH: Exponential backoff decorator (fix #449) commit c31c6f8 Merge: a011c5a 684b997 Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 25 06:45:26 2024 -0400 Merge pull request #587 from RocketPy-Team/mnt/delete-cached-property DEP: delete deprecated rocketpy.tools.cached_property commit b899064 Author: Gui-FernandesBR <[email protected]> Date: Fri Apr 19 12:13:23 2024 -0400 DEV: adds PR 588 to the CHANGELOG commit a8a185e Author: Gui-FernandesBR <[email protected]> Date: Fri Apr 19 12:08:58 2024 -0400 TST: Fix tests with Environment class after exponential_backoff decorator was used commit d0fbcd7 Author: Gui-FernandesBR <[email protected]> Date: Fri Apr 19 12:07:45 2024 -0400 MNT: Refactor code to use the exponential_backoff decorator commit ccd78af Author: Gui-FernandesBR <[email protected]> Date: Fri Apr 19 12:07:17 2024 -0400 ENH: Add exponential backoff decorator to tools.py commit 684b997 Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 18 21:47:19 2024 -0400 DEV: Adds PR 587 to the CHANGELOG.md commit bb0f46a Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 18 21:41:38 2024 -0400 MNT: refactors cached_property imports commit afb3b32 Author: dyu056 <[email protected]> Date: Fri Jan 26 00:35:36 2024 +1300 Completed changes commit bf8c68b Merge: aaa3f7b a011c5a Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 18 21:17:06 2024 -0400 Merge branch 'develop' into bug/airbrakes-postprocess commit a011c5a Merge: 624cc15 6b26f61 Author: Gui-FernandesBR <[email protected]> Date: Thu Apr 18 15:12:40 2024 -0400 Merge pull request #585 from RocketPy-Team/enh/swap-diff-for-flow-rate BUG: Swap `rocket.total_mass.differentiate` for `motor.total_mass_flow rate` commit 6b26f61 Author: MateusStano <[email protected]> Date: Wed Apr 17 22:19:16 2024 +0200 DEV: changelog commit 8ee2c95 Author: MateusStano <[email protected]> Date: Wed Apr 17 22:16:59 2024 +0200 ENH: create rocket total_mass_flow_rate attribute commit aaa3f7b Author: MateusStano <[email protected]> Date: Mon Apr 15 16:52:49 2024 +0200 DOCS: change accelerations names in docs commit 7fe3d0b Author: MateusStano <[email protected]> Date: Mon Apr 15 16:12:39 2024 +0200 ENH: optmize post process loop commit a66fc53 Author: MateusStano <[email protected]> Date: Mon Apr 15 15:18:37 2024 +0200 ENH: add reset function commit b9a97a4 Author: MateusStano <[email protected]> Date: Mon Apr 15 15:06:01 2024 +0200 ENH: add warning for improper time_overshoot commit 830ea15 Author: MateusStano <[email protected]> Date: Mon Apr 15 15:05:31 2024 +0200 ENH: refactor retrieve arrays cached properties and controller sim post processing commit e2a94c8 Author: MateusStano <[email protected]> Date: Tue Apr 9 18:03:08 2024 +0200 TST: fix tests commit e5c7533 Author: MateusStano <[email protected]> Date: Tue Apr 9 17:59:22 2024 +0200 ENH: use mass_flow_rate instead of differentiate commit 822a89e Merge: e3e1a59 624cc15 Author: MateusStano <[email protected]> Date: Sun Apr 7 13:52:02 2024 +0200 Merge branch 'develop' into mnt/modularize-rocket-draw commit e3e1a59 Author: MateusStano <[email protected]> Date: Thu Apr 4 14:53:37 2024 +0200 DEV: fix changelog order commit 624cc15 Merge: 0a4d89b 61bf4d7 Author: Pedro Henrique Marinho Bressan <[email protected]> Date: Tue Apr 2 14:23:05 2024 -0300 Merge pull request #565 from RocketPy-Team/doc/environment-docstring DOC: Improvements of Environment docstring phrasing. commit 61bf4d7 Author: Pedro Bressan <[email protected]> Date: Mon Apr 1 15:00:10 2024 -0300 MNT: add docs change to CHANGELOG. commit 9c1a461 Author: Pedro Bressan <[email protected]> Date: Mon Apr 1 14:47:03 2024 -0300 DOC: fix typing issues regarding Environment docs. commit 4776208 Author: Pedro Henrique Marinho Bressan <[email protected]> Date: Fri Mar 29 08:47:18 2024 -0300 DOC: Update Longitude value ranges. Co-authored-by: Gui-FernandesBR <[email protected]> commit 32a28c1 Merge: 27dd73d 0a4d89b Author: Pedro Henrique Marinho Bressan <[email protected]> Date: Thu Mar 28 18:26:46 2024 -0300 Merge branch 'develop' into doc/environment-docstring commit 27dd73d Author: Pedro Bressan <[email protected]> Date: Thu Mar 28 18:24:23 2024 -0300 DOC: standardize grammar on time zones. commit 4b42c9e Author: Pedro Bressan <[email protected]> Date: Thu Mar 28 18:20:01 2024 -0300 DOC: clarify timezones and dates Environment docs. commit 95b61ef Author: Pedro Bressan <[email protected]> Date: Thu Mar 28 16:38:17 2024 -0300 DOC: specify latitude and longitude defaults. commit 6b84c03 Author: Pedro Bressan <[email protected]> Date: Mon Mar 25 19:53:37 2024 -0300 DOC: improve wording of Environment init. Co-authored-by: Gui-FernandesBR <[email protected]> commit 826efcc Author: Pedro Bressan <[email protected]> Date: Sun Mar 24 16:47:14 2024 -0300 DOC: specify datum as optional. commit a2aa714 Author: Pedro Bressan <[email protected]> Date: Sun Mar 24 16:41:39 2024 -0300 DOC: add examples and improvements to Environment gravity docstring. commit 51bdd91 Author: MateusStano <[email protected]> Date: Sun Mar 24 15:14:45 2024 +0100 MNT: changelog commit 6805e32 Author: MateusStano <[email protected]> Date: Sun Mar 24 14:05:00 2024 +0100 MNT: refactor rocket drawing plot methods commit 3cc91e7 Merge: f637a9d 65b3315 Author: Pedro Henrique Marinho Bressan <[email protected]> Date: Thu Feb 29 20:06:09 2024 -0300 Merge branch 'develop' into doc/environment-docstring commit f637a9d Author: Pedro Bressan <[email protected]> Date: Thu Feb 29 17:49:38 2024 -0300 STY: run black for code linting. commit 9198ac7 Author: Pedro Bressan <[email protected]> Date: Thu Feb 29 17:46:23 2024 -0300 DOC: phrasing improvements of Environment docstring. * MNT: fix component repr for sensors * ENH: add _attatched_rockets to sensors * ENH: add sensors saving methods * MNT: simplify sensors repr * ENH: use _save_data in accel and gyro * ENH: improve accelerometer export * ENH: imrpove gyroscope export * ENH: add sensor initialization * ENH: speed up measure call * ENH: add final sensor cache * ENH: add export sensor data * BUG: wrong initialization order * TST: test for new measured_data and exports * ENH: abstract noise printings * MNT: run isort * ENH: add noise variance * BUG: fix prints for noise variance * TST: add variances * Update rocketpy/rocket/rocket.py Co-authored-by: Pedro Henrique Marinho Bressan <[email protected]> * ENH: add pressure to .measure params * ENH: add InertialSensors and ScalarSensors * ENH: add Barometer class * BUG: fix drawing for scalar sensors * ENH: barometer prints * DOC: change docs for scalar sensors * ENH: add barometer export data * BUG: fix scalars sensors prints * TST: add barometers to tests * DEV: update sensors testing * MNT: remove .type attribute * DOC: fix units of transformation_euler_anges * DOC: add examples to transformation_euler_angles * DOC: mention Euler parameters in docs * TST: test_euler_to_quaternions * TST: fix doc tests * MNT: privatize components attributes and improve docs * TST: add index to var names * MNT: return temp drift directly * ENH: improve export methods * DOC: typos * MNT: improve flight init Co-authored-by: Gui-FernandesBR <[email protected]> * Fix code style issues with Black * TST: overall improvements on sensor testing * ENH: change format of sensor_data dict * MNT: improve flight init Co-authored-by: Gui-FernandesBR <[email protected]> * TST: increase rel tolerances * TST: fix export sensor data test * TST: fix doctests * TST: fix doctests * ENH: change units in prints * ENH: add .round to Matrix and remove broken doctests * TST: test transfromation euler angles * TST: imrpove test_euler_to_quaternions * DEV: update sensors_testing notebook * MNT: minor fixes - typos and isort * MNT: remove aft double underscore from __units * DOC: remove .type * MNT: remove .units underscores * MNT: remove repetition from sensors prints * ENH: normalize_quaternions in tools.py * BUG: remove Matrix circuklar import in tools * MNT: remove unused variable Co-authored-by: Gui-FernandesBR <[email protected]> * ENH: explicit for loop variables * DOC: improve orientation docs * ENH: x_position to x_offset * TST: merge tests * ENH: inherited export method * TST: improve export data tests * TST: Refactor sensor tests and export method * TST: fix fixture names * BUG: duplicate IntertialSensors * TST: calisto_sensors to calisto_with_sensors * MNT: isort * MNT: remove type docs * ENH: move export_sensor_measured_data to tools.py * MNT: pylint fixes * ENH: simplify sensors prints * MNT: rename sensors classes * MNT: sensor.py rename * DOC: improve inertialsensor and scalar sensor doc * MNT: sensors to sensor rename imports * TST: format argument * ENH: move generic_export_data back to Sensor class * ENH: rename test files * ENH: change from celsius to kelvin * TST: fix celsius to kelvin convertion * merge develop * MNT: unecessary variable * DEV: solve merge issues * ENH: Adds GNSS Receiver sensor (#636) * ENH: class GNSS * ENH: gnss prints * ENH: add params to measure call * ENH: export data * ENH: pass env in .measure * DOC: gnss attribute docs * MNT: units in prints * MNT: fix merge errors * TST: add gnss tests * TST: fix time nodes tests * DEV: add gnss to sensors testing notebook * DEV: fix merge errors * MNT: lint and isort * MNT: black notebooks * TST: fix test by inversion of rotation * TST: lower rel tolerances and delete duplicated tests * MNT: minor fixes * ENH: export sensor data by sensor name * MNT: use inverted_haversine in GNSS * BUG: bearing unit in inverted_haversine * MNT: pylint * TST: add test for export with sensor name * MNT: rename `GNSS` class to `GnssReceiver` * MNT: fix lint * MNT: rename gnss file --------- Co-authored-by: Gui-FernandesBR <[email protected]> * DEV: updates changelog * MNT: fix lint * DOC: improve sensors documentation * DOC: rename sensors notebook * MNT: Add warning for experimental development in Sensor classes * MNT: rename euler-quaternion conversions * MNT: minor adjustments before merging sensors --------- Co-authored-by: MateusStano <[email protected]> Co-authored-by: MateusStano <[email protected]> Co-authored-by: Pedro Henrique Marinho Bressan <[email protected]>
2 parents 2685eba + 3c759f7 commit 3f2a2c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4373
-88
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,14 @@
132132
"github",
133133
"Glauert",
134134
"gmaps",
135+
"Gnss",
135136
"Gomes",
136137
"Gonçalvez",
137138
"grav",
138139
"Guilherme",
139140
"Haim",
141+
"headlength",
142+
"headwidth",
140143
"hemis",
141144
"hgtprs",
142145
"hgtsfc",
@@ -245,6 +248,7 @@
245248
"reversesort",
246249
"reynolds",
247250
"rightarrow",
251+
"rmul",
248252
"ROABs",
249253
"rocketpy",
250254
"rocketusage",

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Attention: The newest changes should be on top -->
3232

3333
### Added
3434

35-
- DOC : Cavour Flight Example [#682](https://github.com/RocketPy-Team/RocketPy/pull/682)
35+
- ENH: Adds Sensors classes [683](https://github.com/RocketPy-Team/RocketPy/pull/683)
36+
- DOC: Cavour Flight Example [#682](https://github.com/RocketPy-Team/RocketPy/pull/682)
3637
- DOC: Halcyon Flight Example [#681](https://github.com/RocketPy-Team/RocketPy/pull/681)
3738
- ENH: Adds GenericMotor.load_from_eng_file() method [#676](https://github.com/RocketPy-Team/RocketPy/pull/676)
3839
- ENH: Introducing local sensitivity analysis [#575](https://github.com/RocketPy-Team/RocketPy/pull/575)

docs/notebooks/sensors.ipynb

Lines changed: 899 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Sensors Abstract Classes
2+
========================
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
:caption: Contents:
7+
8+
Sensor Class <sensor>
9+
Inertial Sensor Class <inertial_sensor>
10+
Scalar Sensor Class <scalar_sensor>
11+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Inertial Class
2+
--------------
3+
4+
.. autoclass:: rocketpy.sensors.InertialSensor
5+
:members:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Scalar Sensor Class
2+
-------------------
3+
4+
.. autoclass:: rocketpy.sensors.ScalarSensor
5+
:members:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Sensor Class
2+
------------
3+
4+
.. autoclass:: rocketpy.sensors.Barometer
5+
:members:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Accelerometer Class
2+
---------------
3+
4+
.. autoclass:: rocketpy.sensors.Accelerometer
5+
:members:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Barometer Class
2+
---------------
3+
4+
.. autoclass:: rocketpy.sensors.Barometer
5+
:members:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GNSS Receiver Class
2+
-------------------
3+
4+
.. autoclass:: rocketpy.sensors.GnssReceiver
5+
:members:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Gyroscope Class
2+
---------------
3+
4+
.. autoclass:: rocketpy.sensors.Gyroscope
5+
:members:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Sensor Classes
2+
==============
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
:caption: Contents:
7+
8+
Sensors Abstract Classes <abstract/index>
9+
Accelerometer Class <accelerometer>
10+
Barometer Class <barometer>
11+
Gyroscope Class <gyroscope>
12+
GNSS Receiver Class <gnss>

docs/user/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ RocketPy's User Guide
2424
Compare Flights Class<compare_flights.rst>
2525
Deployable Payload <deployable.rst>
2626
Air Brakes Example <airbrakes.rst>
27+
../notebooks/sensors.ipynb
2728
../matlab/matlab.rst
2829

2930
.. toctree::

rocketpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
TrapezoidalFins,
3939
)
4040
from .sensitivity import SensitivityModel
41+
from .sensors import Accelerometer, Barometer, GnssReceiver, Gyroscope
4142
from .simulation import Flight, MonteCarlo
4243
from .stochastic import (
4344
StochasticEllipticalFins,

rocketpy/control/controller.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from inspect import signature
2+
13
from ..prints.controller_prints import _ControllerPrints
24

35

@@ -52,6 +54,10 @@ def __init__(
5254
the controller function can interact with. The objects are
5355
listed in the same order as they are provided in the
5456
`interactive_objects`.
57+
7. `sensors` (list): A list of sensors that are attached to the
58+
rocket. The most recent measurements of the sensors are provided
59+
with the ``sensor.measurement`` attribute. The sensors are
60+
listed in the same order as they are added to the rocket
5561
5662
This function will be called during the simulation at the specified
5763
sampling rate. The function should evaluate and change the interactive
@@ -78,7 +84,7 @@ def __init__(
7884
None
7985
"""
8086
self.interactive_objects = interactive_objects
81-
self.controller_function = controller_function
87+
self.controller_function = self.__init_controller_function(controller_function)
8288
self.sampling_rate = sampling_rate
8389
self.name = name
8490
self.prints = _ControllerPrints(self)
@@ -88,7 +94,45 @@ def __init__(
8894
else:
8995
self.observed_variables = []
9096

91-
def __call__(self, time, state_vector, state_history):
97+
def __init_controller_function(self, controller_function):
98+
"""Checks number of arguments of the controller function and initializes
99+
it with the correct number of arguments. This is a workaround to allow
100+
the controller function to receive sensors without breaking changes"""
101+
sig = signature(controller_function)
102+
if len(sig.parameters) == 6:
103+
104+
# pylint: disable=unused-argument
105+
def new_controller_function(
106+
time,
107+
sampling_rate,
108+
state_vector,
109+
state_history,
110+
observed_variables,
111+
interactive_objects,
112+
sensors,
113+
):
114+
return controller_function(
115+
time,
116+
sampling_rate,
117+
state_vector,
118+
state_history,
119+
observed_variables,
120+
interactive_objects,
121+
)
122+
123+
elif len(sig.parameters) == 7:
124+
new_controller_function = controller_function
125+
else:
126+
raise ValueError(
127+
"The controller function must have 6 or 7 arguments. "
128+
"The arguments must be in the following order: "
129+
"(time, sampling_rate, state_vector, state_history, "
130+
"observed_variables, interactive_objects, sensors)."
131+
"Sensors argument is optional."
132+
)
133+
return new_controller_function
134+
135+
def __call__(self, time, state_vector, state_history, sensors):
92136
"""Call the controller function. This is used by the simulation class.
93137
94138
Parameters
@@ -104,6 +148,11 @@ def __call__(self, time, state_vector, state_history):
104148
history is a list of every state vector of every step of the
105149
simulation. The state history is a list of lists, where each
106150
sublist is a state vector and is ordered from oldest to newest.
151+
sensors : list
152+
A list of sensors that are attached to the rocket. The most recent
153+
measurements of the sensors are provided with the
154+
``sensor.measurement`` attribute. The sensors are listed in the same
155+
order as they are added to the rocket.
107156
108157
Returns
109158
-------
@@ -116,6 +165,7 @@ def __call__(self, time, state_vector, state_history):
116165
state_history,
117166
self.observed_variables,
118167
self.interactive_objects,
168+
sensors,
119169
)
120170
if observed_variables is not None:
121171
self.observed_variables.append(observed_variables)

rocketpy/environment/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def utm_to_geodesic( # pylint: disable=too-many-locals,too-many-statements
532532
x, y, utm_zone, hemis, semi_major_axis=6378137.0, flattening=1 / 298.257223563
533533
):
534534
# NOTE: already documented in the Environment class.
535-
# TODO: deprecated the static method from the environment class, use only this one.
535+
# TODO: deprecate the static method from the environment class, use only this one.
536536

537537
if hemis == "N":
538538
y = y + 10000000

rocketpy/mathutils/vector_matrix.py

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from functools import cached_property
33
from itertools import product
44

5+
from rocketpy.tools import euler321_to_quaternions, normalize_quaternions
6+
57

68
class Vector:
79
"""Pure python basic R3 vector class designed for simple operations.
@@ -152,7 +154,10 @@ def __len__(self):
152154
@cached_property
153155
def unit_vector(self):
154156
"""R3 vector with the same direction of self, but normalized."""
155-
return self / abs(self)
157+
try:
158+
return self / abs(self)
159+
except ZeroDivisionError:
160+
return self
156161

157162
@cached_property
158163
def cross_matrix(self):
@@ -238,6 +243,30 @@ def __xor__(self, other):
238243
]
239244
)
240245

246+
def __and__(self, other):
247+
"""Element wise multiplication between two R3 vectors. Also known as
248+
Hadamard product.
249+
250+
Parameters
251+
----------
252+
other : Vector
253+
R3 vector to be multiplied with self.
254+
255+
Returns
256+
-------
257+
Vector
258+
R3 vector resulting from the element wise multiplication between
259+
self and other.
260+
261+
Examples
262+
--------
263+
>>> v = Vector([1, 7, 3])
264+
>>> u = Vector([2, 5, 6])
265+
>>> (v & u)
266+
Vector(2, 35, 18)
267+
"""
268+
return Vector([self.x * other[0], self.y * other[1], self.z * other[2]])
269+
241270
def __matmul__(self, other):
242271
"""Dot product between two R3 vectors."""
243272
return self.x * other.x + self.y * other.y + self.z * other.z
@@ -914,6 +943,47 @@ def dot(self, other):
914943
"""
915944
return self @ (other)
916945

946+
def round(self, decimals=0):
947+
"""Round all the values matrix to a given number of decimals.
948+
949+
Parameters
950+
----------
951+
decimals : int, optional
952+
Number of decimal places to round to. Defaults to 0.
953+
954+
Returns
955+
-------
956+
Matrix
957+
The rounded matrix.
958+
959+
Examples
960+
--------
961+
>>> M = Matrix([[1.1234, 2.3456, 3.4567], [4.5678, 5.6789, 6.7890], [7.8901, 8.9012, 9.0123]])
962+
>>> M.round(2)
963+
Matrix([1.12, 2.35, 3.46],
964+
[4.57, 5.68, 6.79],
965+
[7.89, 8.9, 9.01])
966+
"""
967+
return Matrix(
968+
[
969+
[
970+
round(self.xx, decimals),
971+
round(self.xy, decimals),
972+
round(self.xz, decimals),
973+
],
974+
[
975+
round(self.yx, decimals),
976+
round(self.yy, decimals),
977+
round(self.yz, decimals),
978+
],
979+
[
980+
round(self.zx, decimals),
981+
round(self.zy, decimals),
982+
round(self.zz, decimals),
983+
],
984+
]
985+
)
986+
917987
def __str__(self):
918988
return (
919989
f"[{self.xx}, {self.xy}, {self.xz}]\n"
@@ -949,7 +1019,6 @@ def transformation(quaternion):
9491019
The quaternion representing the rotation from frame A to frame B.
9501020
Example: (cos(phi/2), 0, 0, sin(phi/2)) represents a rotation of
9511021
phi around the z-axis.
952-
Note: the quaternion must be normalized.
9531022
9541023
Returns
9551024
-------
@@ -960,27 +1029,60 @@ def transformation(quaternion):
9601029
---------
9611030
https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
9621031
"""
963-
q_w, q_x, q_y, q_z = quaternion
1032+
# normalize quaternion
1033+
q_w, q_x, q_y, q_z = normalize_quaternions(quaternion)
1034+
1035+
# pre-compute common terms
1036+
q_x2 = q_x**2
1037+
q_y2 = q_y**2
1038+
q_z2 = q_z**2
1039+
q_wx = q_w * q_x
1040+
q_wy = q_w * q_y
1041+
q_wz = q_w * q_z
1042+
q_xy = q_x * q_y
1043+
q_xz = q_x * q_z
1044+
q_yz = q_y * q_z
9641045
return Matrix(
9651046
[
9661047
[
967-
1 - 2 * (q_y**2 + q_z**2),
968-
2 * (q_x * q_y - q_w * q_z),
969-
2 * (q_x * q_z + q_w * q_y),
1048+
1 - 2 * (q_y2 + q_z2),
1049+
2 * (q_xy - q_wz),
1050+
2 * (q_xz + q_wy),
9701051
],
9711052
[
972-
2 * (q_x * q_y + q_w * q_z),
973-
1 - 2 * (q_x**2 + q_z**2),
974-
2 * (q_y * q_z - q_w * q_x),
1053+
2 * (q_xy + q_wz),
1054+
1 - 2 * (q_x2 + q_z2),
1055+
2 * (q_yz - q_wx),
9751056
],
9761057
[
977-
2 * (q_x * q_z - q_w * q_y),
978-
2 * (q_y * q_z + q_w * q_x),
979-
1 - 2 * (q_x**2 + q_y**2),
1058+
2 * (q_xz - q_wy),
1059+
2 * (q_yz + q_wx),
1060+
1 - 2 * (q_x2 + q_y2),
9801061
],
9811062
]
9821063
)
9831064

1065+
@staticmethod
1066+
def transformation_euler_angles(roll, pitch, yaw):
1067+
"""Returns the transformation Matrix from frame B to frame A, where B
1068+
is rotated by the Euler angles roll, pitch and yaw with respect to A.
1069+
1070+
Parameters
1071+
----------
1072+
roll : float
1073+
The roll angle in degrees.
1074+
pitch : float
1075+
The pitch angle in degrees.
1076+
yaw : float
1077+
The yaw angle in degrees.
1078+
1079+
Returns
1080+
-------
1081+
Matrix
1082+
The transformation matrix from frame B to frame A.
1083+
"""
1084+
return Matrix.transformation(euler321_to_quaternions(roll, pitch, yaw))
1085+
9841086

9851087
if __name__ == "__main__":
9861088
import doctest

0 commit comments

Comments
 (0)