Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to control rosbag record from a topic?

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!

Below is what I have tried so far. It can start recording a rosbag from the switch but it doesn't kill properly.

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

def callback(data):
    command = shlex.split("rosbag record -O /media/nvidia/C61C-EBE4/recording.bag /mavros/imu/data_raw")
    if (data.channels[5]>1200):
        rosbag_proc = subprocess.Popen(command)
        while (data.channels[5]>1200):
            time.sleep(1)
    try:
        rosbag_proc.send_signal(subprocess.signal.SIGINT)
        print('Stopped')

    except:
        None

def record_data():
    rospy.init_node('rosbag_recording_node', anonymous=True)
    rospy.Subscriber("/mavros/rc/in", RCIn, callback)
    rospy.spin()

if __name__ == '__main__':
    record_data()

How to control rosbag record from a topic?

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!

Below Edit: I have got something working now. The code is what I have tried so far. It below. Any suggestions for improvements however would be very welcome! Hopefully this can start recording a rosbag from be useful to others in the switch but it doesn't kill properly.future too.

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



class Monitor_Channel(object):
    def callback(data):
    command = shlex.split("rosbag __init__(self):
        self.is_recording = False
        self.loop_rate = rospy.Rate(0.5)
        rospy.Subscriber("/mavros/rc/in", RCIn, self.callback)

    def callback(self, data):
        self.switch_state = data.channels[5]
    if self.switch_state > 1200 and self.is_recording == False:
        rosbag_proc = subprocess.Popen(shlex.split("rosbag record -O /media/nvidia/C61C-EBE4/recording.bag /mavros/imu/data_raw")
/mavros/imu/data_raw"))
            self.is_recording = True
    if (data.channels[5]>1200):
        rosbag_proc = subprocess.Popen(command)
self.switch_state > 1200 and self.is_recording == True:
        print("Recording data")
    if self.switch_state < 1200 and self.is_recording == True:
        try:
                rosbag_proc.send_signal(subprocess.signal.SIGINT)
        except:
        None
        self.is_recording = False

    if self.switch_state < 1200 and self.is_recording == False:
            print("Recording stopped")

    def start(self):
        while (data.channels[5]>1200):
            time.sleep(1)
    try:
        rosbag_proc.send_signal(subprocess.signal.SIGINT)
        print('Stopped')

    except:
        None

def record_data():
    rospy.init_node('rosbag_recording_node', anonymous=True)
    rospy.Subscriber("/mavros/rc/in", RCIn, callback)
    rospy.spin()
not rospy.is_shutdown():
            self.loop_rate.sleep()

if __name__ == '__main__':
    record_data()
rospy.init_node("Monitor_Channel_6", anonymous=True)
    record_node = Monitor_Channel()
    record_node.start()

How to control rosbag record from a topic?

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!

Edit: I have got something almost working now. The updated code is below. The bag does not stop recording however.

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(0.5)
        rospy.Subscriber("/mavros/rc/in", RCIn, self.callback)

    def callback(self, data):
        self.switch_state = data.channels[5]
    if self.switch_state > 1200 and self.is_recording == False:
        rosbag_proc = subprocess.Popen(shlex.split("rosbag record -O /media/nvidia/C61C-EBE4/recording.bag /mavros/imu/data_raw"))
            self.is_recording = True
    if self.switch_state > 1200 and self.is_recording == True:
        print("Recording data")
    if self.switch_state < 1200 and self.is_recording == True:
        try:
                rosbag_proc.send_signal(subprocess.signal.SIGINT)
        except:
        None
        self.is_recording = False

    if self.switch_state < 1200 and 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()

How to control rosbag record from a topic?

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!

Edit: I have got something almost working now. The updated code is below. The bag does not stop recording however.

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(0.5)
        rospy.Subscriber("/mavros/rc/in", RCIn, self.callback)

    def callback(self, data):
        self.switch_state = data.channels[5]
    if self.switch_state > 1200 and self.is_recording == False:
        rosbag_proc = subprocess.Popen(shlex.split("rosbag record -O /media/nvidia/C61C-EBE4/recording.bag /mavros/imu/data_raw"))
/mavros/imu/data_raw __name:=bag_record"))
            self.is_recording = True
    if self.switch_state > 1200 and self.is_recording == True:
        print("Recording data")
    if self.switch_state < 1200 and self.is_recording == True:
        try:
                rosbag_proc.send_signal(subprocess.signal.SIGINT)
        except:
        None
subprocess.Popen(shlex.split("rosnode kill /bag_record"))
        self.is_recording = False

    if self.switch_state < 1200 and 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()

How to control rosbag record from a topic?

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
#

Edit: 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(0.5)
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]
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
    if self.switch_state > 1200 and self.is_recording == True:
        print("Recording data")
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")
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()

How to control rosbag record from a topic?

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()

How to control rosbag record from a topic?

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
#

____________________________________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()