def subscriber_main(domain_id, sample_count): participant = dds.DomainParticipant(domain_id) wsqc_type = dds.QosProvider("waitset_cond.xml").type("wssc_lib", "Foo") topic = dds.DynamicData.Topic(participant, "Example Foo", wsqc_type) reader_qos = dds.QosProvider.default.datareader_qos reader = dds.DynamicData.DataReader(dds.Subscriber(participant), topic, reader_qos) # Get the StatusCondition associated with the reader and set the mask to get liveliness updates status_condition = dds.StatusCondition(reader) status_condition.enabled_statuses = dds.StatusMask.LIVELINESS_CHANGED # Create a ReadCondition to get any data read_condition = dds.ReadCondition(reader, dds.DataState.any_data) # Create WaitSet and attach conditions waitset = dds.WaitSet() waitset += status_condition waitset += read_condition count = Counter() while (sample_count == 0) or (count.value < sample_count): active = waitset.wait(4.0) # Check conditions after wait to see if anything triggered if status_condition in active: status_handler(reader) if read_condition in active: rc_handler(reader, count)
def _create_reader_status_condition(self, reader_name, reader): status_condition = dds.StatusCondition(reader) status_condition.enabled_statuses = ( dds.StatusMask.subscription_matched() | dds.StatusMask.liveliness_changed() | dds.StatusMask.requested_incompatible_qos()) return status_condition
def _create_writer_status_condition(self, writer_name, writer): status_condition = dds.StatusCondition(writer) status_condition.enabled_statuses = ( dds.StatusMask.publication_matched() | dds.StatusMask.liveliness_lost() | dds.StatusMask.offered_incompatible_qos()) return status_condition
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
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()
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()