How to control rosbag record from a topic?

asked 2019-02-23 08:00:31 -0500

sk12345 gravatar image

updated 2019-02-23 10:48:49 -0500

Hello ROS Community,

I am learning how to use ROS for controlling a UAV and have not been able to find any examples on how I might approach this properly. I'm not from a software engineering background so I have much to learn about programming still.

I would like to use a switch on my RC transmitter to start and stop recording a rosbag. I have the switch state being published under /mavros/rc/in. I have been able to access the channel and print out it's values with a node, however, I am having difficulty working out how to get the rosbag to initiate and stop correctly.

Is a node the best tool to use for this or would something like a service be more appropriate? Any suggestions on how best to start recording a rosbag when a topic is above a threshold and stop when it is below a threshold?

Any help would be greatly appreciated. Thanks in advance!

____________________________________UPDATE___________________________________________

I have got something working now. The updated code is below. Any suggestions for improvements however would be very welcome! Hopefully this can be useful to others in the future too.

#!/usr/bin/env python
import rospy
import rosbag
from mavros_msgs.msg import RCIn
import subprocess, shlex

class Monitor_Channel(object):
    def __init__(self):
        self.is_recording = False
        self.loop_rate = rospy.Rate(100)
        rospy.Subscriber("/mavros/rc/in", RCIn, self.callback)
        self.prev_switch_state = 0

    def callback(self, data):
        try:
            self.prev_switch_state = self.switch_state
            self.switch_state = data.channels[5] #5 is actually RC channel 6
        except:
            self.switch_state = self.prev_switch_state

    if self.switch_state > 1200 and self.is_recording == False:
        subprocess.Popen(shlex.split("rosbag record -O /media/nvidia/C61C-EBE4/recording.bag /mavros/imu/data_raw /camF/camera /camF/camera_info /camRR/camera /camRR/camera_info /camRL/camera /camRL/camera_info __name:=bag_record"))
            self.is_recording = True
            print("Recording data...")

    if self.switch_state < 1200 and self.is_recording == True:
        subprocess.Popen(shlex.split("rosnode kill /bag_record"))
        self.is_recording = False
        print("Recording stopped...")

    def start(self):
        while not rospy.is_shutdown():
            self.loop_rate.sleep()

if __name__ == '__main__':
    rospy.init_node("Monitor_Channel_6", anonymous=True)
    record_node = Monitor_Channel()
    record_node.start()
edit retag flag offensive close merge delete

Comments

Instead of spawning and killing rosbag processes, I would suggest using the rosbag API to record directly within the node. Check it out here. Just remember to close the rosbag when exiting the node to prevent data corruption.

renangm gravatar imagerenangm ( 2019-02-24 00:20:15 -0500 )edit

Duplicates / related:

Also: check osrf/nodelet_rosbag.

gvdhoorn gravatar imagegvdhoorn ( 2019-02-24 05:38:31 -0500 )edit