Ask Your Question
0

How to set rosbag recording to stop when robot reaches a goal?

asked 2021-11-06 04:10:46 -0500

Hello all, I am working on a project in 3D space, I am giving command to robot through topic to reach a goal, I want to record a specific robot moving topic ( /UAV/goal), for only when robot start moving to reach goal. is it possible through rosbag record arguments?? (PS. I do not know how much time it takes to reach goal, so i can not set timing through rosbag record arguments) (I am planning to verify 1. how much time taken to reach the goal, 2. Actual travelled distance by robot.)

currently I am recording using rosbag and using matlab to find the specific time and distance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-06 14:46:25 -0500

osilva gravatar image

This answer is in Python but you could use something for Matlab. The source of the code: https://gist.github.com/marco-tranzat...

#!/usr/bin/env python

import rospy
import subprocess
import os
import signal


class RosbagRecord:
    def __init__(self):

        if rospy.has_param('~record_script') and rospy.has_param('~record_folder'):
            self.record_script = rospy.get_param('~record_script')
            self.record_folder = rospy.get_param('~record_folder')
            rospy.on_shutdown(self.stop_recording_handler)

            # Start recording.
            command = "source " + self.record_script
            self.p = subprocess.Popen(command, stdin=subprocess.PIPE, shell=True, cwd=self.record_folder,
                                      executable='/bin/bash')

            # Wait for shutdown signal to close rosbag record
            rospy.spin()
        else:
            rospy.signal_shutdown(rospy.get_name() + ' no record script or folder specified.')

    def terminate_ros_node(self, s):
        # Adapted from http://answers.ros.org/question/10714/start-and-stop-rosbag-within-a-python-script/
        list_cmd = subprocess.Popen("rosnode list", shell=True, stdout=subprocess.PIPE)
        list_output = list_cmd.stdout.read()
        retcode = list_cmd.wait()
        assert retcode == 0, "List command returned %d" % retcode
        for str in list_output.split("\n"):
            if (str.startswith(s)):
                os.system("rosnode kill " + str)

    def stop_recording_handler(self):
        rospy.loginfo(rospy.get_name() + ' stop recording.')
        self.terminate_ros_node("/record")

if __name__ == '__main__':
    rospy.init_node('rosbag_record')
    rospy.loginfo(rospy.get_name() + ' start')

    # Go to class functions that do all the heavy lifting. Do error checking.
    try:
        rosbag_record = RosbagRecord()
    except rospy.ROSInterruptException:
        pass

It can be used as ROS node, so once you reach the goal, then you can publish to stop recording. I searched in prior answers and couldn't find something more concise to do this. Hope this helps.

edit flag offensive delete link more

Comments

Measuring the time between when you start and stop recording can be done separately or by slightly modifying this script.

osilva gravatar image osilva  ( 2021-11-06 14:48:05 -0500 )edit

It seems I can start recording by this script but stopping the recording should also should be done automatically, when robot reaches a goal. Say If i stop the rosbag record manually it is difficult to find exact time spent to reach the goal.

ganesh112 gravatar image ganesh112  ( 2021-11-06 15:50:56 -0500 )edit

When you know that is time to stop then you publish to stop rosbag and get a time stamp

osilva gravatar image osilva  ( 2021-11-06 16:02:48 -0500 )edit

list_cmd = subprocess.Popen("rosnode list", shell=True, stdout=subprocess.PIPE)

Kills rosbag

osilva gravatar image osilva  ( 2021-11-06 16:07:50 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2021-11-06 04:10:46 -0500

Seen: 17 times

Last updated: Nov 06 '21