Skip to content

Commit

Permalink
Improve record_rosbag.sh (#16)
Browse files Browse the repository at this point in the history
Improve rosbags naming and usage of the `record_rosbag.sh` script

- Change default bag name from `<timestamp>_sensor_recording` to
`<timestamp>_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
  • Loading branch information
hect95 authored Sep 18, 2024
1 parent 3b917c6 commit 1840f10
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 27 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions config/recording_presets/calibration_lspf_r.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions config/recording_presets/calibration_lspr_l.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions config/recording_presets/calibration_rsp_l.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions config/recording_presets/calibration_rspf_l.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions config/recording_presets/calibration_rspr_r.txt
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
32 changes: 32 additions & 0 deletions scripts/container_tools/_tools_autocomplete.sh
Original file line number Diff line number Diff line change
@@ -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
67 changes: 41 additions & 26 deletions scripts/container_tools/record_rosbag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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}"

0 comments on commit 1840f10

Please sign in to comment.