Examples: Access Vicon Data Through o80ΒΆ

Note

All examples on this page requires an o80 back end to be running. This is done by executing vicon_o80_standalone in a separate terminal before running the example.

The example o80_get_robot_pose.py shows a minimal example on how to use the o80 front end to get the pose of the robot base from Vicon:

# SPDX-License-Identifier: BSD-3-Clause

"""Use o80 front end to get the pose of the robot base from Vicon.

The back end is expected to be run in a separate process (e.g. by
``vicon_o80_standalone``) using the same segment ID.  It needs to be started before
initialising the front end.
"""
import contextlib
import signal_handler
from spatial_transformation import Transformation
from pam_vicon.o80 import FrontEnd, Subjects


def main() -> None:
    SEGMENT_ID = "vicon"
    frontend = FrontEnd(SEGMENT_ID)
    frontend.reset_next_index()

    signal_handler.init()  # for detecting ctrl+c
    with contextlib.suppress(KeyboardInterrupt, SystemExit):
        while not signal_handler.has_received_sigint():
            obs = frontend.wait_for_next()
            vicon_frame = obs.get_extended_state()

            robot_base = vicon_frame.subjects[Subjects.ROBOT1_BASE]

            # convert pose to native Python class (makes it more convenient to use).
            robot_pose = Transformation.from_cpp(robot_base.global_pose)

            print(f"Robot Base Pose: {robot_pose}")


if __name__ == "__main__":
    main()

The script vicon_o80_print_data.py shows a slightly more complex example, printing all subject data and using get_subject_names() to get the name of each subject:

#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
"""Connect to a shared memory set up by an o80 back end and print the received data.

The back end is expected to be run in a separate process (e.g. by
``vicon_o80_standalone``) using the same segment ID.  It needs to be started before
``vicon_o80_print_data``.
"""
import argparse
import logging
import sys
import typing

import signal_handler

from pam_vicon.o80 import (
    FrontEnd,
    FixedSizeViconFrame,
    Subjects,
    get_subject_names,
)


DEFAULT_SEGMENT_ID = "vicon"


def print_frame_data(
    frame: FixedSizeViconFrame, subject_names: typing.Sequence[str]
) -> None:
    """Print Vicon frame data including subject names.

    Args:
        frame: The Vicon frame received through o80.
        subject_names: List of subject names in same order as ``frame.subjects``.
    """
    print("Frame Number: {}".format(frame.frame_number))
    print("Frame Rate: {}".format(frame.frame_rate))
    print("Latency: {}".format(frame.latency))
    print("Timestamp: {}".format(frame.time_stamp))
    print("Subjects ({}):".format(len(frame.subjects)))
    for i, subject in enumerate(frame.subjects):
        print("  {}:".format(Subjects(i).name))
        print("    Name: {}".format(subject_names[i]))
        print("    Visible: {}".format(subject.is_visible))
        print("    Translation: {}".format(subject.global_pose.translation))
        print("    Rotation: {}".format(subject.global_pose.get_rotation()))
        print("    Quality: {}".format(subject.quality))


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--segment-id",
        "-s",
        type=str,
        default=DEFAULT_SEGMENT_ID,
        help="Shared memory segment ID. Default: '%(default)s'",
    )
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO)

    frontend = FrontEnd(args.segment_id)
    iteration = frontend.latest().get_iteration()

    subject_names = get_subject_names()

    signal_handler.init()  # for detecting ctrl+c
    try:
        while not signal_handler.has_received_sigint():
            iteration += 1
            obs = frontend.read(iteration)
            vicon_frame = obs.get_extended_state()

            print("----")
            print_frame_data(vicon_frame, subject_names)

    except (KeyboardInterrupt, SystemExit):
        pass
    except Exception as e:
        print("Error:", e)

    return 0


if __name__ == "__main__":
    sys.exit(main())