libstp.robot.geometry

Robot geometry calculations for sensor and wheel positions.

This module provides dataclasses for representing sensor and wheel positions, and a mixin class for computing distances and angles between elements.

Example usage:

from libstp.robot.geometry import RobotGeometry, SensorPosition, WheelPosition

class Robot(GenericRobot, RobotGeometry):

width_cm = 15.0 length_cm = 22.0 rotation_center_forward_cm = 0.0 rotation_center_strafe_cm = 0.0

_sensor_positions = {

defs.front_left_ir_sensor: SensorPosition(forward_cm=8.8, strafe_cm=3.75),

}

_wheel_positions = {

defs.front_left_motor: WheelPosition(forward_cm=6.0, strafe_cm=9.5),

}

robot = Robot() pos = robot.sensor_position(robot.defs.front_left_ir_sensor) print(f”Sensor at: {pos.forward_cm}cm forward, {pos.strafe_cm}cm left”)

wheel_pos = robot.wheel_position(robot.defs.front_left_motor) print(f”Wheel at: {wheel_pos.forward_cm}cm forward”)

Classes

SensorPosition

Position of a sensor relative to robot geometric center.

WheelPosition

Position of a wheel relative to robot geometric center.

RobotGeometry

Mixin class providing geometry calculations for robot positioning.

Module Contents

class libstp.robot.geometry.SensorPosition

Position of a sensor relative to robot geometric center.

forward_cm

Distance forward from center (positive = front of robot)

strafe_cm

Distance left from center (positive = left side of robot)

clearance_cm

Sensor clearance/detection radius

forward_cm: float
strafe_cm: float
clearance_cm: float = 0.0
class libstp.robot.geometry.WheelPosition

Position of a wheel relative to robot geometric center.

forward_cm

Distance forward from center (positive = front of robot)

strafe_cm

Distance left from center (positive = left side of robot)

forward_cm: float
strafe_cm: float
class libstp.robot.geometry.RobotGeometry

Mixin class providing geometry calculations for robot positioning.

This class should be used as a mixin with GenericRobot. Subclasses must define the following class attributes (typically generated by codegen):

  • width_cm: Robot width in centimeters

  • length_cm: Robot length in centimeters

  • rotation_center_forward_cm: Offset from geometric center to rotation center (forward axis)

  • rotation_center_strafe_cm: Offset from geometric center to rotation center (strafe axis)

  • _sensor_positions: Dict mapping sensor objects to SensorPosition instances

  • _wheel_positions: Dict mapping motor objects to WheelPosition instances

The mixin provides computed methods for: - Getting sensor/wheel positions - Computing distances between sensors - Computing distances from sensors to rotation center - Computing angles from center to sensors

width_cm: float = 0.0
length_cm: float = 0.0
rotation_center_forward_cm: float = 0.0
rotation_center_strafe_cm: float = 0.0
sensor_position(sensor: Any) SensorPosition | None

Get the position of a sensor relative to robot center.

Parameters:

sensor – The sensor object (pass defs.sensor_name)

Returns:

SensorPosition if found, None otherwise

wheel_position(motor: Any) WheelPosition | None

Get the position of a wheel by its motor object.

Parameters:

motor – The motor object (pass defs.motor_name)

Returns:

WheelPosition if found, None otherwise

distance_between_sensors(sensor_a: Any, sensor_b: Any) float

Calculate the Euclidean distance between two sensors.

Parameters:
  • sensor_a – First sensor object

  • sensor_b – Second sensor object

Returns:

Distance in centimeters

Raises:

ValueError – If either sensor is not found in geometry

sensor_to_rotation_center(sensor: Any) float

Calculate the distance from a sensor to the rotation center.

This is useful for arc calculations during turns.

Parameters:

sensor – The sensor object

Returns:

Distance in centimeters

Raises:

ValueError – If sensor is not found in geometry

sensor_to_geometric_center(sensor: Any) float

Calculate the distance from a sensor to the geometric center.

Parameters:

sensor – The sensor object

Returns:

Distance in centimeters

Raises:

ValueError – If sensor is not found in geometry

sensor_angle_from_center(sensor: Any) float

Calculate the angle from geometric center to a sensor.

The angle is measured in radians, with 0 being forward and positive angles going counter-clockwise (to the left).

Parameters:

sensor – The sensor object

Returns:

Angle in radians (0 = forward, π/2 = left, -π/2 = right)

Raises:

ValueError – If sensor is not found in geometry

sensor_angle_from_rotation_center(sensor: Any) float

Calculate the angle from rotation center to a sensor.

The angle is measured in radians, with 0 being forward and positive angles going counter-clockwise (to the left).

Parameters:

sensor – The sensor object

Returns:

Angle in radians (0 = forward, π/2 = left, -π/2 = right)

Raises:

ValueError – If sensor is not found in geometry

all_sensors() Dict[Any, SensorPosition]

Get all sensor positions.

Returns:

Dictionary mapping sensor objects to their positions

all_wheels() Dict[Any, WheelPosition]

Get all wheel positions.

Returns:

Dictionary mapping motor objects to their positions