Quantcast
Channel: ROS Answers: Open Source Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 475

How to control rosbag record from a topic?

$
0
0
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 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.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: subprocess.Popen(shlex.split("rosbag record -O /media/nvidia/C61C-EBE4/recording.bag /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: 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()

Viewing all articles
Browse latest Browse all 475

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>