Accelerometer#
The accelerometer sensor is used to measure the three-axis linear acceleration at the mounting point in the local coordinate system. In robot simulation, accelerometers are commonly used to perceive the robotโs motion state and provide important feedback information for control algorithms.
๐ฏ Functional Description#
The accelerometer measures the linear acceleration at the sensor mounting point in the site local coordinate system, including the influence of gravitational acceleration. This sensor returns an array of 3 floating-point numbers, representing the acceleration components in the X, Y, and Z axis directions respectively.
๐ Return Value Format#
acceleration = model.get_sensor_value("accelerometer_name", data)
# Type: numpy.ndarray[float32]
# Shape: shape = (*data.shape, 3)
# Unit: m/sยฒ
acceleration[โฆ, 0]: X-axis acceleration component (local coordinate system)
acceleration[โฆ, 1]: Y-axis acceleration component (local coordinate system)
acceleration[โฆ, 2]: Z-axis acceleration component (local coordinate system)
โ๏ธ MJCF Configuration Parameters#
In MotrixSim, accelerometer sensors support the following MJCF configuration fields:
Basic Configuration#
<sensor>
<accelerometer name="sensor_name"
site="site_name"/>
</sensor>
Supported Attributes#
Attribute Name |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
name |
string |
โ |
- |
Unique identifier name of sensor |
site |
string |
โ |
- |
Name of reference point where sensor is mounted |
Note: MotrixSim currently does not support the cutoff, noise, and user attributes from the MJCF standard.
๐ Configuration Examples#
Basic Accelerometer Configuration#
<!-- Define mounting point in body -->
<site name="car_imu" type="sphere" size="0.03" rgba="0 1 0 1" pos="0 0 0"/>
<!-- Define accelerometer sensor -->
<sensor>
<accelerometer name="car_accelerometer" site="car_imu"/>
</sensor>
Multiple Accelerometer Configuration#
<!-- Install multiple accelerometers at different positions -->
<site name="imu_base" pos="0 0 0" size="0.02"/>
<site name="imu_end" pos="0 0 1" size="0.02"/>
<sensor>
<accelerometer name="acc_base" site="imu_base"/>
<accelerometer name="acc_end" site="imu_end"/>
</sensor>
๐ Usage Examples#
Python API Usage#
import numpy as np
from motrixsim import load_model, SceneData, step
# Load scene
model = load_model("scene_with_accelerometer.xml")
data = SceneData(model)
# Run simulation and get accelerometer data
for step_count in range(1000):
step(model, data)
# Get accelerometer data
acc_data = model.get_sensor_value("car_accelerometer", data)
# If single environment simulation, data shape is (3,)
if acc_data.ndim == 1:
print(f"Acceleration: [{acc_data[0]:.3f}, {acc_data[1]:.3f}, {acc_data[2]:.3f}] m/sยฒ")
# Calculate acceleration magnitude
acc_magnitude = np.linalg.norm(acc_data)
print(f"Acceleration magnitude: {acc_magnitude:.3f} m/sยฒ")
else:
# Vectorized environment case
print(f"Acceleration data shape: {acc_data.shape}")
Practical Application Scenarios#
# Robot fall detection
def detect_fall(acceleration, threshold=15.0):
"""Detect fall based on accelerometer data"""
acc_magnitude = np.linalg.norm(acceleration)
return acc_magnitude > threshold
# Stationary detection (considering gravity)
def is_stationary(acceleration, gravity_threshold=9.5):
"""Detect if device is stationary (considering gravity influence)"""
acc_magnitude = np.linalg.norm(acceleration)
# When stationary, acceleration magnitude should be close to gravitational acceleration
return abs(acc_magnitude - gravity_threshold) < 0.5
# Calculate tilt angle
def calculate_tilt_angle(acceleration):
"""Calculate device tilt angle based on accelerometer"""
# Assume device is mainly affected by gravity
gravity = np.array([0, 0, -9.81])
# Normalize vectors
acc_norm = acceleration / np.linalg.norm(acceleration)
gravity_norm = gravity / np.linalg.norm(gravity)
# Calculate angle
cos_angle = np.dot(acc_norm, gravity_norm)
angle = np.arccos(np.clip(cos_angle, -1.0, 1.0))
return np.degrees(angle)
๐ Physical Principles#
The accelerometer measurement is based on Newtonโs second law. In the simulation environment:
Local coordinate system measurement: The returned acceleration values are represented in the siteโs local coordinate system
Gravity influence: When the sensor is stationary relative to the ground, it will measure the projection of gravitational acceleration in the local coordinate system
Linear acceleration: Measures the pure linear acceleration at the mounting point, excluding rotational components
For example, when a robot is tilted, even when stationary, the accelerometer will produce non-zero readings due to the projection of gravity in the local coordinate system.
โ ๏ธ Important Notes#
Local coordinate system: The returned acceleration values are represented in the siteโs local coordinate system, not the global coordinate system
Gravity influence: When stationary, the accelerometer will measure the projection of gravitational acceleration in the local coordinate system
Mounting position: The sensor measures the acceleration at the site mounting point position
No advanced attribute support: MotrixSim currently does not support
cutoff,noise, anduserattributesData type: The return value is
numpy.ndarraytype, with shapes supporting vectorized environments