Skip to content

Commit

Permalink
sensors: apply pylint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
harderthan committed Jan 23, 2024
1 parent bb17602 commit 58d8cde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
| File: geo_mag_utils.py
| Description: Provides utilities for computing latitude, longitude, and magnetic strength
given the position of the vehicle in the simulated world. These computations and table constants are in agreement
with the PX4 stil_gazebo implementation (https://github.com/PX4/PX4-SITL_gazebo). Therefore, PX4 should behave similarly
with the PX4 stil_gazebo implementation (https://github.com/PX4/PX4-SITL_gazebo). Therefore, PX4 should behave similarly
to a gazebo-based simulation.
"""
import numpy as np
Expand All @@ -15,6 +15,7 @@
# --------------------------------------------------------------------

# Declination data in degrees
# pylint: disable=line-too-long, for the sake of readability.
DECLINATION_TABLE = [
[ 47,46,45,43,42,41,39,37,33,29,23,16,10,4,-1,-6,-10,-15,-20,-27,-34,-42,-49,-56,-62,-67,-72,-74,-75,-73,-61,-22,26,42,47,48,47 ],
[ 31,31,31,30,30,30,30,29,27,24,18,11,3,-4,-9,-13,-15,-18,-21,-27,-33,-40,-47,-52,-56,-57,-56,-52,-44,-30,-14,2,14,22,27,30,31 ],
Expand All @@ -29,8 +30,10 @@
[ 5,8,11,13,14,15,14,11,5,-2,-9,-15,-17,-16,-13,-10,-6,-3,0,3,4,5,6,6,6,5,4,2,-1,-5,-8,-9,-9,-6,-3,1,5 ],
[ 3,8,11,15,17,17,16,12,5,-4,-12,-18,-19,-18,-16,-12,-8,-4,-0,3,5,7,9,10,10,9,7,4,-1,-6,-10,-12,-12,-9,-5,-1,3 ],
[ 3,8,12,16,19,20,18,13,4,-8,-18,-24,-25,-23,-20,-16,-11,-6,-1,3,7,11,14,16,17,17,14,8,-0,-8,-13,-15,-14,-11,-7,-2,3 ]]
# pylint: disable=line-too-long, for the sake of readability.

# Inclination data in degrees
# pylint: disable=line-too-long, for the sake of readability.
INCLINATION_TABLE = [
[ -78,-76,-74,-72,-70,-68,-65,-63,-60,-57,-55,-54,-54,-55,-56,-57,-58,-59,-59,-59,-59,-60,-61,-63,-66,-69,-73,-76,-79,-83,-86,-87,-86,-84,-82,-80,-78 ],
[ -72,-70,-68,-66,-64,-62,-60,-57,-54,-51,-49,-48,-49,-51,-55,-58,-60,-61,-61,-61,-60,-60,-61,-63,-66,-69,-72,-76,-78,-80,-81,-80,-79,-77,-76,-74,-72 ],
Expand All @@ -45,6 +48,7 @@
[ 53,54,56,57,59,61,64,66,67,68,67,65,62,60,57,55,55,54,55,56,57,58,59,59,60,60,60,60,60,60,59,57,55,53,52,52,53 ],
[ 62,63,64,65,67,69,71,73,75,75,74,73,70,68,67,66,65,65,65,66,66,67,68,68,69,70,70,71,71,70,69,67,65,63,62,62,62 ],
[ 71,71,72,73,75,77,78,80,81,81,80,79,77,76,74,73,73,73,73,73,73,74,74,75,76,77,78,78,78,78,77,75,73,72,71,71,71 ]]
# pylint: disable=line-too-long, for the sake of readability.

# Strength data in centi-Tesla
STRENGTH_TABLE = [
Expand Down
45 changes: 25 additions & 20 deletions exts/stride.simulator/stride/simulator/vehicles/sensors/imu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
| File: imu.py
| Author: Marcelo Jacinto ([email protected])
| License: BSD-3-Clause. Copyright (c) 2023, Marcelo Jacinto. All rights reserved.
| Description: Simulates an imu. Based on the implementation provided in PX4 stil_gazebo (https://github.com/PX4/PX4-SITL_gazebo)
| Description: Simulates an imu. Based on the implementation provided in PX4 stil_gazebo
(https://github.com/PX4/PX4-SITL_gazebo)
"""
__all__ = ["IMU"]
__all__ = ["Imu"]

import numpy as np
from scipy.spatial.transform import Rotation
Expand All @@ -15,14 +16,15 @@
from pegasus.simulator.logic.sensors.geo_mag_utils import GRAVITY_VECTOR


class IMU(Sensor):
"""The class that implements the IMU sensor. This class inherits the base class Sensor.
class Imu(Sensor):
"""The class that implements the Imu sensor. This class inherits the base class Sensor.
"""
def __init__(self, config={}):
"""Initialize the IMU class
def __init__(self, config=None):
"""Initialize the Imu class
Args:
config (dict): A Dictionary that contains all teh parameters for configuring the IMU - it can be empty or only have some of the parameters used by the IMU.
config (dict): A Dictionary that contains all teh parameters for configuring the Imu - it can be empty or
only have some of the parameters used by the Imu.
Examples:
The dictionary default parameters are
Expand All @@ -42,7 +44,7 @@ def __init__(self, config={}):
"""

# Initialize the Super class "object" attributes
super().__init__(sensor_type="IMU", update_rate=config.get("update_rate", 250.0))
super().__init__(sensor_type="Imu", update_rate=config.get("update_rate", 250.0))

# Orientation noise constant
self._orientation_noise: float = 0.0
Expand All @@ -66,7 +68,7 @@ def __init__(self, config={}):
# Auxiliar variable used to compute the linear acceleration of the vehicle
self._prev_linear_velocity = np.zeros((3,))

# Save the current state measured by the IMU
# Save the current state measured by the Imu
self._state = {
"orientation": np.array([1.0, 0.0, 0.0, 0.0]),
"angular_velocity": np.array([0.0, 0.0, 0.0]),
Expand All @@ -82,12 +84,13 @@ def state(self):

@Sensor.update_at_rate
def update(self, state: State, dt: float):
"""Method that implements the logic of an IMU. In this method we start by generating the random walk of the
gyroscope. This value is then added to the real angular velocity of the vehicle (FLU relative to ENU inertial frame
expressed in FLU body frame). The same logic is followed for the accelerometer and the accelerations. After this step,
the angular velocity is rotated such that it expressed a FRD body frame, relative to a NED inertial frame, expressed
in the FRD body frame. Additionally, the acceleration is also rotated, such that it becomes expressed in the body
FRD frame of the vehicle. This sensor outputs data that follows the PX4 adopted standard.
"""Method that implements the logic of an Imu. In this method we start by generating the random walk of the
gyroscope. This value is then added to the real angular velocity of the vehicle (FLU relative to ENU inertial
frame expressed in FLU body frame). The same logic is followed for the accelerometer and the accelerations.
After this step, the angular velocity is rotated such that it expressed a FRD body frame, relative to a NED
inertial frame, expressed in the FRD body frame. Additionally, the acceleration is also rotated, such that it
becomes expressed in the body FRD frame of the vehicle. This sensor outputs data that follows the PX4 adopted
standard.
Args:
state (State): The current state of the vehicle.
Expand Down Expand Up @@ -117,10 +120,10 @@ def update(self, state: State, dt: float):
self._gyroscope_bias[i] = phi_g_d * self._gyroscope_bias[i] + sigma_b_g_d * np.random.randn()
angular_velocity[i] = state.angular_velocity[i] + sigma_g_d * np.random.randn() + self._gyroscope_bias[i]

# Accelerometer terms
# Accelerometer terms.
tau_a: float = self._accelerometer_bias_correlation_time

# Discrete-time standard deviation equivalent to an "integrating" sampler with integration time dt
# Discrete-time standard deviation equivalent to an "integrating" sampler with integration time dt.
sigma_a_d: float = 1.0 / np.sqrt(dt) * self._accelerometer_noise_density
sigma_b_a: float = self._accelerometer_random_walk

Expand All @@ -130,14 +133,16 @@ def update(self, state: State, dt: float):
# Compute state-transition.
phi_a_d: float = np.exp(-1.0 / tau_a * dt)

# Compute the linear acceleration from diferentiating the velocity of the vehicle expressed in the inertial frame
# Compute the linear acceleration from diferentiating the velocity of the vehicle expressed in the inertial
# frame.
linear_acceleration_inertial = (state.linear_velocity - self._prev_linear_velocity) / dt
linear_acceleration_inertial = linear_acceleration_inertial - GRAVITY_VECTOR

# Update the previous linear velocity for the next computation
self._prev_linear_velocity = state.linear_velocity

# Compute the linear acceleration of the body frame, with respect to the inertial frame, expressed in the body frame
# Compute the linear acceleration of the body frame, with respect to the inertial frame, expressed in the body
# frame.
linear_acceleration = np.array(Rotation.from_quat(state.attitude).inv().apply(linear_acceleration_inertial))

# Simulate the accelerometer noise processes and add them to the true linear aceleration values
Expand All @@ -150,7 +155,7 @@ def update(self, state: State, dt: float):
# TODO - Add small "noisy" to the attitude

# --------------------------------------------------------------------------------------------
# Apply rotations such that we express the IMU data according to the FRD body frame convention
# Apply rotations such that we express the Imu data according to the FRD body frame convention
# --------------------------------------------------------------------------------------------

# Convert the orientation to the FRD-NED standard
Expand Down

0 comments on commit 58d8cde

Please sign in to comment.