libstp.step.motion.heading_reference

Classes

MarkHeadingReference

Captures the absolute IMU heading as a reference for future absolute turns.

Functions

mark_heading_reference(→ MarkHeadingReference)

Mark the current IMU heading as a reference point for absolute turns.

turn_to_heading(→ libstp.step.logic.defer.Defer)

Turn to an absolute heading relative to the marked reference.

Module Contents

class libstp.step.motion.heading_reference.MarkHeadingReference

Bases: libstp.step.Step

Captures the absolute IMU heading as a reference for future absolute turns.

libstp.step.motion.heading_reference.mark_heading_reference() MarkHeadingReference

Mark the current IMU heading as a reference point for absolute turns.

Captures the robot’s current absolute IMU heading and stores it as a reference. Subsequent calls to turn_to_heading() will compute turn angles relative to this stored reference, enabling absolute heading control even after the robot has moved and turned through other motion steps.

The reference uses the raw IMU heading which is unaffected by odometry resets that occur during normal motion steps.

Multiple calls overwrite the previous reference.

Returns:

A MarkHeadingReference step that records the heading when executed.

Example:

from libstp.step.motion import mark_heading_reference, turn_to_heading

# Mark current heading as 0-degree reference
mark_heading_reference()

# ... robot drives around ...

# Turn to face 180 degrees from where we marked
turn_to_heading(180)
libstp.step.motion.heading_reference.turn_to_heading(degrees: float, speed: float = 1.0) libstp.step.logic.defer.Defer

Turn to an absolute heading relative to the marked reference.

Computes the shortest rotation from the robot’s current heading to the target heading (reference + degrees) at execution time, then delegates to turn_left() or turn_right() accordingly. The turn direction is chosen automatically to minimize rotation.

Requires mark_heading_reference() to have been called earlier in the mission.

Parameters:
  • degrees – Target heading in degrees relative to the reference. 0 returns to the reference heading, 90 faces 90 degrees counter-clockwise from it, -90 faces 90 degrees clockwise, etc.

  • speed – Fraction of max angular speed, 0.0 to 1.0 (default 1.0).

Returns:

A deferred step that computes and executes the turn at runtime.

Raises:

RuntimeError – If no heading reference has been set.

Example:

from libstp.step.motion import mark_heading_reference, turn_to_heading

# Mark heading at start of mission
mark_heading_reference()

# Drive around, turn, etc.
drive_forward(30)
turn_left(45)
drive_forward(20)

# Return to original heading
turn_to_heading(0)

# Face the opposite direction from reference
turn_to_heading(180)