Ejemplo n.º 1
0
def run_example(domain_id, sample_count):

    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # Create a DomainParticipant with default Qos
    with dds.DomainParticipant(domain_id) as participant:
        # A QosProvider is used here to get the type from the file
        # HelloWorld.xml
        provider = dds.QosProvider(FILE)

        provider_type = provider.type("HelloWorld")

        # A Topic has a name and a datatype. Create a Topic named
        # "HelloWorld Topic" with type HelloWorld
        topic = dds.DynamicData.Topic(
            participant, "Example HelloWorld", provider_type
        )

        # A Subscriber allows an application to create one or more DataReaders
        # Subscriber QoS is configured in USER_QOS_PROFILES.xml
        subscriber = dds.Subscriber(participant)

        # This DataReader will read data of type HelloWorld on Topic
        # "HelloWorld Topic". DataReader QoS is configured in
        # USER_QOS_PROFILES.xml
        reader = dds.DynamicData.DataReader(subscriber, topic)

        # Obtain the DataReader's Status Condition
        status_condition = dds.StatusCondition(reader)

        # Enable the 'data available' status.
        status_condition.enabled_statuses = dds.StatusMask.data_available()

        # Initialize samples_read to zero
        samples_read = 0

        # Associate a handler with the status condition. This will run when the
        # condition is triggered, in the context of the dispatch call (see below)
        def handler(_):  # condition argument is not used
            nonlocal samples_read
            nonlocal reader
            samples_read += process_data(reader)

        status_condition.set_handler(handler)

        # Create a WaitSet and attach the StatusCondition
        waitset = dds.WaitSet()
        waitset += status_condition

        # Catch control c interrupt
        try:
            while samples_read < sample_count:
                # Dispatch will call the handlers associated to the WaitSet conditions
                # when they activate
                print(f"Hello World subscriber sleeping for 4 seconds...")

                waitset.dispatch(dds.Duration(4))  # Wait up to 4s each time
        except KeyboardInterrupt:
            pass
Ejemplo n.º 2
0
def test_set_qos_exception():
    p = dds.DomainParticipant(DOMAIN_ID, dds.DomainParticipantQos())
    qos = p.qos
    d = dds.Duration(77)
    db = dds.Database()
    db.shutdown_cleanup_period = d
    qos << db
    with pytest.raises(dds.ImmutablePolicyError):
        p.qos = qos
Ejemplo n.º 3
0
def run_example(domain_id, sample_count, sensor_id):

    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # Create a DomainParticipant with default Qos
    participant = dds.DomainParticipant(domain_id)

    # A Topic has a name and a datatype. Create a Topic named
    # "ChocolateTemperature" with type Temperature
    temperature_type = dds.QosProvider(FILE).type("Temperature")
    topic = dds.DynamicData.Topic(participant, "ChocolateTemperature",
                                  temperature_type)

    # A Subscriber allows an application to create one or more DataReaders
    # Subscriber QoS is configured in USER_QOS_PROFILES.xml
    subscriber = dds.Subscriber(participant)

    # This DataReader reads data of type Temperature on Topic
    # "ChocolateTemperature". DataReader QoS is configured in
    # USER_QOS_PROFILES.xml
    reader = dds.DynamicData.DataReader(subscriber, topic)

    # Obtain the DataReader's Status Condition
    status_condition = dds.StatusCondition(reader)

    # Enable the 'data available' status.
    status_condition.enabled_statuses = dds.StatusMask.data_available()

    # Associate a handler with the status condition. This will run when the
    # condition is triggered, in the context of the dispatch call (see below)
    samples_read = 0

    def handler(_):
        nonlocal samples_read
        nonlocal reader
        samples_read += process_data(reader)

    status_condition.set_handler(handler)

    # Create a WaitSet and attach the StatusCondition
    waitset = dds.WaitSet()
    waitset += status_condition

    try:
        # Dispatch will call the handlers associated to the WaitSet conditions
        # when they activate
        while sample_count is None or samples_read < sample_count:
            print("ChocolateTemperature subcriber sleeping for 4 sec...")
            waitset.dispatch(dds.Duration(4))  # Wait up to 4s each time
    except KeyboardInterrupt:
        pass
def run_example(domain_id, sensor_id):
    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
    participant = dds.DomainParticipant(domain_id)

    # A Topic has a name and a datatype. Create Topics.
    # Topic names are constants defined in the XML file.
    temperature_topic = dds.DynamicData.Topic(participant,
                                              CHOCOLATE_TEMPERATURE_TOPIC,
                                              TEMPERATURE_TYPE)
    lot_state_topic = dds.DynamicData.Topic(participant,
                                            CHOCOLATE_LOT_STATE_TOPIC,
                                            CHOCOLATE_LOT_TYPE)

    # A Publisher allows an application to create one or more DataWriters
    # Publisher QoS is configured in USER_QOS_PROFILES.xml
    publisher = dds.Publisher(participant)

    # Create DataWriters of Topics "ChocolateTemperature" & "ChocolateLotState"
    # DataWriter QoS is configured in USER_QOS_PROFILES.xml
    temperature_writer = dds.DynamicData.DataWriter(publisher,
                                                    temperature_topic)
    lot_state_writer = dds.DynamicData.DataWriter(publisher, lot_state_topic)

    # A Subscriber allows an application to create one or more DataReaders
    # Subscriber QoS is configured in USER_QOS_PROFILES.xml
    subscriber = dds.Subscriber(participant)

    # Create DataReader of Topic "ChocolateLotState".
    # DataReader QoS is configured in USER_QOS_PROFILES.xml
    lot_state_reader = dds.DynamicData.DataReader(subscriber, lot_state_topic)

    # Obtain the DataReader's Status Condition
    status_condition = dds.StatusCondition(lot_state_reader)
    # Enable the 'data available' status.
    status_condition.enabled_statuses = dds.StatusMask.data_available()

    # Associate a handler with the status condition. This will run when the
    # condition is triggered, in the context of the dispatch call (see below)
    def handler(_):
        nonlocal lot_state_reader
        nonlocal lot_state_writer
        process_lot(lot_state_reader, lot_state_writer)

    # Create a WaitSet and attach the StatusCondition
    status_condition.set_handler(handler)

    waitset = dds.WaitSet()
    waitset += status_condition

    # Create a thread to periodically publish the temperature
    print(f"ChocolateTemperature Sensor with ID: {sensor_id} starting")
    temperature_thread = threading.Thread(target=publish_temperature,
                                          args=(temperature_writer, sensor_id))
    temperature_thread.start()
    try:
        while True:
            # Wait for ChocolateLotState
            print("Waiting for lot")
            waitset.dispatch(dds.Duration(10))  # Wait up to 10s for update
    except KeyboardInterrupt:
        pass

    temperature_thread.join()
Ejemplo n.º 5
0
def run_example(domain_id, lots_to_process, sensor_id):

    # A DomainParticipant allows an application to begin communicating in
    # a DDS domain. Typically there is one DomainParticipant per application.
    # DomainParticipant QoS is configured in USER_QOS_PROFILES.xml
    participant = dds.DomainParticipant(domain_id)

    # A Topic has a name and a datatype. Create a Topic with type
    # ChocolateLotState.  Topic name is a constant defined in the XML file.
    topic = dds.DynamicData.Topic(participant, CHOCOLATE_LOT_STATE_TOPIC,
                                  CHOCOLATE_LOT_TYPE)

    # Exercise #4.1: Add a Topic for Temperature to this application
    temperature_topic = dds.DynamicData.Topic(participant,
                                              CHOCOLATE_TEMPERATURE_TOPIC,
                                              provider.type("Temperature"))

    # A Publisher allows an application to create one or more DataWriters
    # Publisher QoS is configured in USER_QOS_PROFILES.xml
    publisher = dds.Publisher(participant)

    # This DataWriter writes data on Topic "ChocolateLotState"
    # DataWriter QoS is configured in USER_QOS_PROFILES.xml
    writer = dds.DynamicData.DataWriter(publisher, topic)

    # A Subscriber allows an application to create one or more DataReaders
    # Subscriber QoS is configured in USER_QOS_PROFILES.xml
    subscriber = dds.Subscriber(participant)

    # Create DataReader of Topic "ChocolateLotState".
    # DataReader QoS is configured in USER_QOS_PROFILES.xml
    reader = dds.DynamicData.DataReader(subscriber, topic)

    # Exercise #4.2: Add a DataReader for Temperature to this application
    temperature_reader = dds.DynamicData.DataReader(subscriber,
                                                    temperature_topic)

    # Obtain the DataReader's Status Condition
    status_condition = dds.StatusCondition(reader)
    # Enable the 'data available' status.
    status_condition.enabled_statuses = dds.StatusMask.data_available()

    # Associate a handler with the status condition. This will run when the
    # condition is triggered, in the context of the dispatch call (see below)
    lots_processed = 0

    def handler(_):
        nonlocal lots_processed
        nonlocal reader
        lots_processed += monitor_lot_state(reader)

    status_condition.set_handler(handler)

    temperature_status_condition = dds.StatusCondition(reader)

    temperature_status_condition.enabled_statuses = (
        dds.StatusMask.data_available())

    def temperature_handler(_):
        nonlocal temperature_reader
        monitor_lot_state(temperature_reader)

    temperature_status_condition.set_handler(temperature_handler)

    # Create a WaitSet and attach the StatusCondition
    waitset = dds.WaitSet()
    waitset += status_condition

    # Exercise #4.3: Add the new DataReader's StatusCondition to the Waitset
    waitset += temperature_status_condition

    # Create a thread to periodically publish the temperature
    start_lot_thread = threading.Thread(target=publish_start_lot,
                                        args=(writer, lots_to_process))
    start_lot_thread.start()
    try:
        while lots_to_process is None or lots_processed < lots_to_process:
            # Dispatch will call the handlers associated to the WaitSet conditions
            # when they activate
            waitset.dispatch(dds.Duration(4))  # Wait for up to 4s each time
    except KeyboardInterrupt:
        pass

    start_lot_thread.join()