sensors.InertialMotionUnit

class sensors.InertialMotionUnit(port: Optional[int] = None)

Sensor for measuring relative acceleration, rotation, and angular velocity.

acceleration() vector.Vector3

Instantaneous acceleration in meters per second per second relative to the sensors current orientation. A slight filter is applied for an unfiltered version, see acceleration_stream().

The following code block assumes an interial motion unit is assigned to port 0 and a TextScreen is attatched to port 1.

from sensors import InertialMotionUnit
from controllables import TextScreen
from color import Color

imu = InertialMotionUnit(0)
screen = TextScreen(1)

while True:
    screen.text = f"{imu.acceleration(): 4.0f}"
    magnitude = min(1,max(0,imu.acceleration_magnitude() / 10))
    screen.color = Color(magnitude,1 - magnitude,0)
acceleration_x() float

The acceleration of the inertial motion unit in its own x direction.

acceleration_y() float

The acceleration of the inertial motion unit in its own y direction.

acceleration_z() float

The acceleration of the inertial motion unit in its own z direction.

acceleration_magnitude() float

The total magnitude of the acceleration that the inertial motion unit is experiencing. The value is in meters per second per second.

acceleration_stream() streams.Stream[vector.Vector3]

Creates an unfiltered stream of acceleration values in meters per second per second relative to the sensors current orientation.

rotation_axis() vector.Vector3

Axis of rotation whose magnitude is the magnitude of the rotation in radians. A value of <x = tau/2.,y = 0,z = 0> means a half rotation about the x axis. A rotation of <x = 1/sqrt(2),y = 1/sqrt(2),z = 0> * tau/4.0 does not have anything to do with rotations about either the x or y axis, but is instead a quarter rotation around the diagonal vector pointing up and to the right.

The returned axis is the rotation required to rotate from the sensors initial orientation to its current one.

The following example assumes that an inertial motion unit is assigned to port 0 and a ServoMotor is assigned to port 1. If the servo is placed facing up, this will try keep its arm pointed in the initial direction.

from sensors import InertialMotionUnit
from controllables import ServoMotor`
from vector import Vector3

imu = InertialMotionUnit(0)
servo = ServoMotor(1)

while True:
    servo.spin_to(-Vector3.dot(imu.rotation_axis(),(0,1,0)))`
rotation_stream() streams.Stream[vector.Vector3]

Creates an unfiltered stream of rotation axis values in radians.

x_unit() vector.Vector3

The vector, with length 1, pointed in the part’s x axis relative to the part’s original orientation.

y_unit() vector.Vector3

The vector, with length 1, pointed in the part’s y axis relative to the part’s original orientation.

z_unit() vector.Vector3

The vector, with length 1, pointed in the part’s z axis relative to the part’s original orientation.

angular_velocity() vector.Vector3

Angular velocity relative to the sensors current orientation in radians per second. A velocity of <2 * tau,0,0> means the sensor is rotating around it’s own x axis twice per second. a slight filter is applied, see create_rotation_stream() for an unfiltered version.

angular_velocity_stream() streams.Stream[vector.Vector3]

Creates an stream of angular velocity values in radians per second.

class MotionData(rotation_axis: vector.Vector3, angular_velocity: vector.Vector3, acceleration: vector.Vector3)

Holds rotation, angular velocity, and acceleration data from the same sample.

motion_data_stream() streams.Stream[sensors.InertialMotionUnit.MotionData]

Creates a stream which contains samples from each data stream together such that in a given data object all samples were recorded simultaneously. The values all correspond directly to the other streams avaliable in this class.

The following example gets the motion data from a InertialMotionUnit on port 0 and outputs the velocity to a TextScreen on port 1.

from sensors import InertialMotionUnit
from matrix import Matrix4x4
from scriptruntime import Runtime
from vector import Vector3
from controllables import TextScreen

imu = InertialMotionUnit(0)
output = TextScreen(1)

stream = imu.motion_data_stream()
dt = Runtime.sample_duration()
velocity = Vector3(0,0,0)

while True:
    for sample in stream:
        acc          = sample.acceleration
        rot          = sample.rotation_axis
        rot_matrix   = Matrix4x4.from_rotation(rot)
        #rotate the acceleration to get a vector relative to a fixed frame
        dv           = rot_matrix * acc

        #use the sample duration here to do the integration
        velocity += dv * dt

    output.text = f"{velocity:+4.1f}"
name() str

Returns the user editable name of the controllable as found in the properties tab of the game.

from ports import PortReference

print(PortReference(0).name())