def example_create_sequence(base): print("Creating Action for Sequence") angular_action = create_angular_action() cartesian_action = create_cartesian_action() print("Creating Sequence") sequence = Base_pb2.Sequence() sequence.name = "Example sequence" print("Appending Actions to Sequence") task_1 = sequence.tasks.add() task_1.group_identifier = 0 task_1.action.CopyFrom(angular_action) task_2 = sequence.tasks.add() task_2.group_identifier = 1 # sequence elements with same group_id are played at the same time task_2.action.CopyFrom(cartesian_action) print("Creating sequence on device and executing it") handle_sequence = base.CreateSequence(sequence) base.PlaySequence(handle_sequence) print("Waiting 30 seconds for movement to finish ...") time.sleep(30) print("Sequence completed")
def example_create_sequence(base, base_cyclic): print("Creating Action for Sequence") actuator_count = base.GetActuatorCount().count angular_action = create_angular_action(actuator_count) cartesian_action = create_cartesian_action(base_cyclic) print("Creating Sequence") sequence = Base_pb2.Sequence() sequence.name = "Example sequence" print("Appending Actions to Sequence") task_1 = sequence.tasks.add() task_1.group_identifier = 0 task_1.action.CopyFrom(cartesian_action) task_2 = sequence.tasks.add() task_2.group_identifier = 1 # sequence elements with same group_id are played at the same time task_2.action.CopyFrom(angular_action) e = threading.Event() notification_handle = base.OnNotificationSequenceInfoTopic( check_for_sequence_end_or_abort(e), Base_pb2.NotificationOptions()) print("Creating sequence on device and executing it") handle_sequence = base.CreateSequence(sequence) base.PlaySequence(handle_sequence) print("Waiting for movement to finish ...") finished = e.wait(TIMEOUT_DURATION) base.Unsubscribe(notification_handle) if not finished: print("Timeout on action notification wait") return finished
def example_manipulation_protobuf_list(): # In Google Protocol Buffer, 'repeated' is used to designate a list of indeterminate length. Once affected to a Python # variable they can be used in the same way as a standard list. # Note that a repeated message field doesn't have an append() function, it has an # add() function that create the new message object. # For this example we will use the following two messages # message Sequence { # SequenceHandle handle = 1 # string name = 2 # string application_data = 3 # repeated SequenceTask tasks = 4 # } # message SequenceTask { # uint32 group_identifier = 1; # Action action = 2; # string application_data = 3; # } # For a clearer example the attribute action in SequenceTask message will be kept to default value # Here's two ways to add to a repeated message field # Create the parent message sequence = Base_pb2.Sequence() sequence.name = "Sequence" # The 'extend' way sequence_task_1 = Base_pb2.SequenceTask() sequence_task_1.group_identifier = 10 action = sequence_task_1.action action = Base_pb2.Action() # Using Action default constructor sequence.tasks.extend([sequence_task_1]) # Extend expect an iterable # Created for the add() function unique to repeated message field sequence_task_2 = sequence.tasks.add() sequence_task_2.group_identifier = 20 action = sequence_task_2.action action = Base_pb2.Action() # Using Action default constructor # Since sequence.task is a list we can use all the Python features to # loop, iterate, interogate and print elements in that list. for i in range(len(sequence.tasks)): print("Sequence ID with index : {0}".format( sequence.tasks[i].group_identifier)) # Lists have the iterator property of a Python list, so you can directly iterate # throught element without creating a iterator as in the previous example for task in sequence.tasks: print("Sequence ID with object iterator : {0}".format( task.group_identifier))
def example_manipulation_protobuf_helpers(): # Google offers some helpers with Google Protocol Buffer, # some of which are covered in this section. # All the module google.protobuf documentation is available here: # https://developers.google.com/protocol-buffers/docs/reference/python/ # First, the function include with message instance. We will cover the next function # Clear # MergeFrom # CopyFrom # The Clear function is straightforward - it clears all the message attributes. # MergeFrom and CopyFrom have the same purpose: to duplicate data into another object. # The difference between them is that CopyFrom will do a Clear before a MergeFrom. # For its part MergeFrom will merge data if the new field is not empty. # In the case of repeated, the content received in a parameter will be appended. # For this example, we'll used the SSID message # message Ssid { # string identifier = 1; # } # First we'll create four of them, each with a unique string ssid_1 = Base_pb2.Ssid() ssid_1.identifier = "" ssid_2 = Base_pb2.Ssid() ssid_2.identifier = "123" ssid_3 = Base_pb2.Ssid() ssid_3.identifier = "@#$" # Now merge ssid_1 into ssid_2 and print the identifier of ssid_2 ssid_2.MergeFrom(ssid_1) print("Content ssid_2: {0}".format(ssid_2.identifier)) # output : Content ssid_2: 123 # Now copy ssid_1 into ssid_3 and print the identifier of ssid_3 ssid_3.CopyFrom(ssid_1) print("Content ssid_3: {0}".format(ssid_3.identifier)) # output : Content ssid_3: # For more functions consult the Class Message documentation # https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message.Message-class # From the Google Protocol Buffer library you can use the json_format module. # One useful function is the MessageToJson. # This function serializes the Google Protocol Buffer message to a JSON object. It is helpful when you want to print a large object into # a human-readable format. # Here's an example with the following two messages # # message Sequence { # SequenceHandle handle = 1 # string name = 2 # string application_data = 3 # repeated SequenceTask tasks = 4 # } # message SequenceTask { # uint32 group_identifier = 1; # Action action = 2; # string application_data = 3; # } # populate the message sequence = Base_pb2.Sequence() sequence.name = "A Name" for i in range(5): sequence_task = sequence.tasks.add() sequence_task.group_identifier = 10 * ( i + 1) # Some further function doesn't print if value = 0 action = sequence_task.action action = Base_pb2.Action() # Using Action default constructor # need to import the module from google.protobuf import json_format # now get the JSON object json_object = json_format.MessageToJson(sequence) # now print it print("Json object:") print(json_object) # output: # Json object # { # "name": "A Name", # "tasks": [ # { # "groupIdentifier": 10 # }, # { # "groupIdentifier": 20 # }, # { # "groupIdentifier": 30 # }, # { # "groupIdentifier": 40 # }, # { # "groupIdentifier": 50 # } # ] # } # From the Google Protocol Buffer library you can used the text_format module. # A useful function is the MessageToString. It has the same purpose as # MessageToJson - convert the message to a human readable format. # For the example we'll reuse the sequence object created in the previous example # First import the module from google.protobuf import text_format # Now print print("Text format:") print(text_format.MessageToString(sequence))