Skip to content

Commit

Permalink
update get_trial_score for new /vrx/task/info message
Browse files Browse the repository at this point in the history
  • Loading branch information
M1chaelM committed Jun 22, 2023
1 parent a4709f2 commit 841e1d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
9 changes: 5 additions & 4 deletions run_trial.bash
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fi

# Constants for containers
SERVER_CONTAINER_NAME=vrx-server-system
SERVER_USER=developer
ROS_DISTRO=humble
SERVER_IMG="vrx-server-${ROS_DISTRO}${image_nvidia}:latest"
LOG_DIR=/vrx/logs
Expand Down Expand Up @@ -188,10 +189,10 @@ echo "---------------------------------"

# Copy the ROS log files from the server's container.
echo "Copying ROS log files from server container..."
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$USER/.ros/log/latest $HOST_LOG_DIR/ros-server-latest
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$USER/.gazebo/ $HOST_LOG_DIR/gazebo-server
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$USER/vrx_rostopics.bag $HOST_LOG_DIR/
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$USER/verbose_output.txt $HOST_LOG_DIR/
#docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$SERVER_USER/.ros/log/latest $HOST_LOG_DIR/ros-server-latest
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$SERVER_USER/.gz/ $HOST_LOG_DIR/gz-server
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$SERVER_USER/vrx_rostopics.bag $HOST_LOG_DIR/
docker cp --follow-link ${SERVER_CONTAINER_NAME}:/home/$SERVER_USER/verbose_output.txt $HOST_LOG_DIR/

echo -e "${GREEN}OK${NOCOLOR}\n"

Expand Down
43 changes: 29 additions & 14 deletions utils/get_trial_score.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# get_trial_score.py: A Python script that gets the last message of a trial's rosbag file,
# get_trial_score.py: A Python script that gets the last message of a trial's ros2bag file,
# then records the final trial score in a text file
#
# eg. python get_trial_score.py <team_name> <task_name> <trial_number>

import os
import rosbag
import rosbag2_py
import sys
from rclpy.serialization import deserialize_message
from rosidl_runtime_py.utilities import get_message

# Get arguments
team_name = sys.argv[1]
Expand All @@ -17,25 +19,38 @@
trial_directory = dir_path + '/../generated/logs/' + team_name + '/' + task_name + '/' + trial_number

# Open ros bag
bag_name = trial_directory + '/vrx_rostopics.bag'
bag = rosbag.Bag(bag_name)
bag_path = trial_directory + '/vrx_rostopics.bag/vrx_rostopics.bag_0.db3'
storage_options = rosbag2_py.StorageOptions(uri=bag_path, storage_id='sqlite3')
converter_options = rosbag2_py.ConverterOptions(input_serialization_format='cdr',
output_serialization_format='cdr')

reader = rosbag2_py.SequentialReader()
reader.open(storage_options, converter_options)

topic_types = reader.get_all_topics_and_types()

type_map = {topic_types[i].name: topic_types[i].type for i in range(len(topic_types))}

storage_filter = rosbag2_py.StorageFilter(topics=['/vrx/task/info'])
reader.set_filter(storage_filter)

# Get last message
last_topic = None
last_msg = None
last_msg_data = None
last_t = None

for topic, msg, t in bag.read_messages(topics=['/vrx/task/info']):
last_topic = topic
last_msg = msg
last_t = t
while reader.has_next():
(last_topic, last_msg_data, last_t) = reader.read_next()

msg_type = get_message(type_map[last_topic])
last_msg = deserialize_message(last_msg_data, msg_type)
score = None
for field in last_msg.params:
if field.name == "score":
score = field.value.double_value
# Write trial score to file
trial_score_name = trial_directory + "/trial_score.txt"
f = open(trial_score_name, 'w+')
f.write("{}".format(last_msg.score))
with open(trial_score_name, 'w+') as f:
f.write("{}".format(score))

print("Successfully recorded trial score in {}".format(trial_score_name))
f.close()
bag.close()

0 comments on commit 841e1d7

Please sign in to comment.