From 1840f1086a32d0044549cf8bfef30a5d2f5829ae Mon Sep 17 00:00:00 2001 From: Hector Cruz <37127013+hect95@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:16:59 +0100 Subject: [PATCH] Improve record_rosbag.sh (#16) Improve rosbags naming and usage of the `record_rosbag.sh` script - Change default bag name from `_sensor_recording` to `_all_sensors_recording` - Add `--all` flag to record all available topics - Add ` -f | --file` to explicitly specify the topic list file - Add autocomplete for `-f | --file` flag to automatically list available topics list files - Rename `sensor_topics.txt` to `all_sensors_topics.txt` - Record topics listed in `all_sensors_topic.txt` by default - Fix magic numbers --- Dockerfile | 5 +- .../all_sensor_topics.txt} | 6 ++ .../calibration_fsp_l.txt} | 0 .../recording_presets/calibration_lspf_r.txt | 8 +++ .../recording_presets/calibration_lspr_l.txt | 8 +++ .../recording_presets/calibration_rsp_l.txt | 8 +++ .../recording_presets/calibration_rspf_l.txt | 8 +++ .../recording_presets/calibration_rspr_r.txt | 8 +++ .../front_camera_top_lidar.txt | 0 .../container_tools/_tools_autocomplete.sh | 32 +++++++++ scripts/container_tools/record_rosbag.sh | 67 ++++++++++++------- 11 files changed, 123 insertions(+), 27 deletions(-) rename config/{sensor_topics.txt => recording_presets/all_sensor_topics.txt} (87%) rename config/{calibration_front.txt => recording_presets/calibration_fsp_l.txt} (100%) create mode 100644 config/recording_presets/calibration_lspf_r.txt create mode 100644 config/recording_presets/calibration_lspr_l.txt create mode 100644 config/recording_presets/calibration_rsp_l.txt create mode 100644 config/recording_presets/calibration_rspf_l.txt create mode 100644 config/recording_presets/calibration_rspr_r.txt rename config/{ => recording_presets}/front_camera_top_lidar.txt (100%) create mode 100644 scripts/container_tools/_tools_autocomplete.sh diff --git a/Dockerfile b/Dockerfile index 2607b34..448adc4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,6 +47,9 @@ ENV RCUTILS_COLORIZED_OUTPUT=1 COPY scripts/container_tools $ROS_WS/container_tools COPY config $ROS_WS/config +# Set tools autocomplete +RUN echo "source $ROS_WS/container_tools/_tools_autocomplete.sh" >> /root/.bashrc + # Add tools to PATH RUN echo "export PATH=$ROS_WS/container_tools:$PATH " >> /root/.bashrc &&\ # Add sourcing local workspace command to bashrc for @@ -107,4 +110,4 @@ CMD ["bash"] FROM base AS runtime # Start recording a rosbag by default -CMD ["/opt/ros_ws/container_tools/record_rosbag.sh", "/opt/ros_ws/config/sensor_topics.txt"] +CMD ["/opt/ros_ws/container_tools/record_rosbag.sh"] diff --git a/config/sensor_topics.txt b/config/recording_presets/all_sensor_topics.txt similarity index 87% rename from config/sensor_topics.txt rename to config/recording_presets/all_sensor_topics.txt index 5b7dad2..c9b6052 100644 --- a/config/sensor_topics.txt +++ b/config/recording_presets/all_sensor_topics.txt @@ -19,21 +19,27 @@ /robot_description /sensor/camera/fsp_l/camera_info /sensor/camera/fsp_l/image_raw +/sensor/camera/fsp_l/image_rect_color/compressed /sensor/camera/fsp_l/meta /sensor/camera/lspf_r/camera_info /sensor/camera/lspf_r/image_raw +/sensor/camera/lspf_r/image_rect_color/compressed /sensor/camera/lspf_r/meta /sensor/camera/lspr_l/camera_info /sensor/camera/lspr_l/image_raw +/sensor/camera/lspr_l/image_rect_color/compressed /sensor/camera/lspr_l/meta /sensor/camera/rsp_l/camera_info /sensor/camera/rsp_l/image_raw +/sensor/camera/rsp_l/image_rect_color/compressed /sensor/camera/rsp_l/meta /sensor/camera/rspf_l/camera_info /sensor/camera/rspf_l/image_raw +/sensor/camera/rspf_l/image_rect_color/compressed /sensor/camera/rspf_l/meta /sensor/camera/rspr_r/camera_info /sensor/camera/rspr_r/image_raw +/sensor/camera/rspr_r/image_rect_color/compressed /sensor/camera/rspr_r/meta /sensor/gps/bestpos /sensor/gps/bestvel diff --git a/config/calibration_front.txt b/config/recording_presets/calibration_fsp_l.txt similarity index 100% rename from config/calibration_front.txt rename to config/recording_presets/calibration_fsp_l.txt diff --git a/config/recording_presets/calibration_lspf_r.txt b/config/recording_presets/calibration_lspf_r.txt new file mode 100644 index 0000000..f3577d1 --- /dev/null +++ b/config/recording_presets/calibration_lspf_r.txt @@ -0,0 +1,8 @@ +/joint_states +/robot_description +/sensor/camera/lspf_r/camera_info +/sensor/camera/lspf_r/image_rect_color/compressed +/sensor/camera/lspf_r/meta +/sensor/lidar/top/points +/tf +/tf_static diff --git a/config/recording_presets/calibration_lspr_l.txt b/config/recording_presets/calibration_lspr_l.txt new file mode 100644 index 0000000..20c0ea0 --- /dev/null +++ b/config/recording_presets/calibration_lspr_l.txt @@ -0,0 +1,8 @@ +/joint_states +/robot_description +/sensor/camera/lspr_l/camera_info +/sensor/camera/lspr_l/image_rect_color/compressed +/sensor/camera/lspr_l/meta +/sensor/lidar/top/points +/tf +/tf_static diff --git a/config/recording_presets/calibration_rsp_l.txt b/config/recording_presets/calibration_rsp_l.txt new file mode 100644 index 0000000..3282e5a --- /dev/null +++ b/config/recording_presets/calibration_rsp_l.txt @@ -0,0 +1,8 @@ +/joint_states +/robot_description +/sensor/camera/rsp_l/camera_info +/sensor/camera/rsp_l/image_rect_color/compressed +/sensor/camera/rsp_l/meta +/sensor/lidar/top/points +/tf +/tf_static diff --git a/config/recording_presets/calibration_rspf_l.txt b/config/recording_presets/calibration_rspf_l.txt new file mode 100644 index 0000000..92ad8b0 --- /dev/null +++ b/config/recording_presets/calibration_rspf_l.txt @@ -0,0 +1,8 @@ +/joint_states +/robot_description +/sensor/camera/rspf_l/camera_info +/sensor/camera/rspf_l/image_rect_color/compressed +/sensor/camera/rspf_l/meta +/sensor/lidar/top/points +/tf +/tf_static diff --git a/config/recording_presets/calibration_rspr_r.txt b/config/recording_presets/calibration_rspr_r.txt new file mode 100644 index 0000000..bd2b486 --- /dev/null +++ b/config/recording_presets/calibration_rspr_r.txt @@ -0,0 +1,8 @@ +/joint_states +/robot_description +/sensor/camera/rspr_r/camera_info +/sensor/camera/rspr_r/image_rect_color/compressed +/sensor/camera/rspr_r/meta +/sensor/lidar/top/points +/tf +/tf_static diff --git a/config/front_camera_top_lidar.txt b/config/recording_presets/front_camera_top_lidar.txt similarity index 100% rename from config/front_camera_top_lidar.txt rename to config/recording_presets/front_camera_top_lidar.txt diff --git a/scripts/container_tools/_tools_autocomplete.sh b/scripts/container_tools/_tools_autocomplete.sh new file mode 100644 index 0000000..99e3a7f --- /dev/null +++ b/scripts/container_tools/_tools_autocomplete.sh @@ -0,0 +1,32 @@ +_record_rosbag_autocomplete() { + local cur prev opts + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + # Directory containing topic lists presets + topic_list_dir="/opt/ros_ws/config/recording_presets" + + COMPREPLY=() + + # Check if the previous argument was -f or --file + if [[ "$prev" == "-f" || "$prev" == "--file" ]]; then + # List only the filenames (not full paths) + local files=$(ls "$topic_list_dir"/*.txt 2>/dev/null | xargs -n1 basename) + + # If the current input (`cur`) starts with the full path, strip the path + if [[ "$cur" == "$topic_list_dir/"* ]]; then + cur="${cur##*/}" # Remove the path, leaving only the filename part + fi + + # Autocomplete the filenames + COMPREPLY=( $(compgen -W "$files" -- "$cur") ) + + # If the user selects a file, prepend the full path + if [[ ${#COMPREPLY[@]} -eq 1 ]]; then + COMPREPLY[0]="$topic_list_dir/${COMPREPLY[0]}" + fi + fi +} + +# Register autocomplete function +complete -F _record_rosbag_autocomplete record_rosbag.sh diff --git a/scripts/container_tools/record_rosbag.sh b/scripts/container_tools/record_rosbag.sh index be93fb0..adb0f4d 100755 --- a/scripts/container_tools/record_rosbag.sh +++ b/scripts/container_tools/record_rosbag.sh @@ -13,27 +13,34 @@ DATE_PREFIX=$(date "+%Y_%m_%d-%H_%M_%S") # Function to display help help() { - echo "Usage: record_rosbag.sh [options] [path-to-topics-list-file.txt]" - echo "" - echo "Options:" - echo " -n, --name Set the rosbag name (default is 'sensor_recording')." - echo " -h, --help Display this help message and exit." - echo "" - echo "Examples:" - echo " record_rosbag.sh -n custom_name topics_list.txt" - echo " record_rosbag.sh --name custom_name topics_list.txt" - echo " record_rosbag.sh topics_list.yaml" - echo " record_rosbag.sh (record all topics)" + echo "Usage: record_rosbag.sh [options] + + Options: + --all Record all available topics + -f, --file TOPIC_LIST.txt + Specify topic list file (Default: all_sensor_topics.txt) + -n, --name ROSBAG_NAME + Set the rosbag name (Default: all_sensors_recording). + -h, --help Display this help message and exit. + " exit 0 } -# Initialize variables -TOPICS_LIST_FILE="" -ROSBAG_SUFFIX="sensor_recording" +TOPICS_LIST_FILE="$ROS_WS/config/recording_presets/all_sensor_topics.txt" +ROSBAG_SUFFIX="all_sensors_recording" +RECORD_ALL="" +MAX_CACHE_SIZE="5000000000" +MAX_BAG_SIZE="10740000000" -# Parse command-line arguments while [[ "$#" -gt 0 ]]; do case $1 in + --all) + RECORD_ALL="YES" + ;; + -f|--file) + TOPICS_LIST_FILE="$2" + shift + ;; -n|--name) ROSBAG_SUFFIX="$2" shift @@ -42,19 +49,24 @@ while [[ "$#" -gt 0 ]]; do help ;; *) - TOPICS_LIST_FILE="$1" + echo "Unsupported flag" + help ;; esac shift done -# Define default behavior if no file is provided -if [ -z "$TOPICS_LIST_FILE" ]; then - echo -e "No topics list file provided. ${CYAN}Recording all topics.${NO_COLOR}" - ros2 bag record -s mcap --all --max-cache-size 5000000000 \ +if [ ! -z "$RECORD_ALL" ]; then + echo -e "${CYAN}Recording all topics.${NO_COLOR}" + + ROSBAG_PATH="$OUTPUT_DIR/${DATE_PREFIX}_all_topics" + + ros2 bag record -s mcap --all --max-cache-size $MAX_CACHE_SIZE \ --storage-config-file "$ROS_WS/config/mcap_cfg.yaml" \ - -b 10740000000 \ - -o "$OUTPUT_DIR/${DATE_PREFIX}_all_topics" + -b $MAX_BAG_SIZE \ + -o "$ROSBAG_PATH" + + echo -e "Recording saved in: ${CYAN}${ROSBAG_PATH}${NO_COLOR}" exit 0 fi @@ -67,14 +79,17 @@ fi # Read topics into an array readarray -t TOPICS < "$TOPICS_LIST_FILE" -# Start recording the topics +ROSBAG_PATH="${OUTPUT_DIR}/${DATE_PREFIX}_${ROSBAG_SUFFIX}" + if [ ${#TOPICS[@]} -eq 0 ]; then echo "No topics found in the file. Stopping recording." exit 1 else echo -e "Recording topics from ${MAGENTA}$TOPICS_LIST_FILE${NO_COLOR}" - ros2 bag record -s mcap --max-cache-size 5000000000 \ + ros2 bag record -s mcap --max-cache-size $MAX_CACHE_SIZE \ --storage-config-file "$ROS_WS/config/mcap_cfg.yaml" \ - -b 10740000000 \ - -o "$OUTPUT_DIR/${DATE_PREFIX}_${ROSBAG_SUFFIX}" "${TOPICS[@]}" + -b $MAX_BAG_SIZE \ + -o $ROSBAG_PATH "${TOPICS[@]}" fi + +echo -e "Recording saved in: ${CYAN}${ROSBAG_PATH}${NO_COLOR}"