ROS related: use rospy to write ros programs and use rosbag to store data

Why use rospy

ROS supports C++ and Python. Since the bottom layer of ROS is written in C++, most ROS programs use C++, but the Python language interface is simple and easier to write. And you can use python to combine with some deep learning frameworks such as Caffe, TensorFlow, Theano, etc. So, going with python is the better choice. This article only summarizes some details of rospy use and the use of rosbag

Advantages of rospy

In addition to the above-mentioned simple interface, easy to write, and compatibility with deep learning frameworks, another point is that programs written in python can be run without catkin_make. For children's shoes who have used ros, I am afraid that in order to compile successfully, the CMakeLists.txt file needs to be modified, which is very troublesome and often fails to compile. Then using python to write ROS programs will find it super easy. Run directly after writing.

How rospy handles images

This program is taken from stackoverflow

# rospy for the subscriber
import rospy
# ROS Image message
from sensor_msgs.msg import Image
# ROS Image message -> OpenCV2 image converter
from cv_bridge import CvBridge, CvBridgeError
# OpenCV2 for saving an image
import cv2

# Instantiate CvBridge
bridge = CvBridge()

def image_callback(msg):
    print("Received an image!")
    try:
        # Convert your ROS Image message to OpenCV2
        cv2_img = bridge.imgmsg_to_cv2(msg, "bgr8")
    except CvBridgeError, e:
        print(e)
    else:
        # Save your OpenCV2 image as a jpeg 
        cv2.imwrite('camera_image.jpeg', cv2_img)

def main():
    rospy.init_node('image_listener')
    # Define your image topic
    image_topic = "/cameras/left_hand_camera/image"
    # Set up your subscriber and define its callback
    rospy.Subscriber(image_topic, Image, image_callback)
    # Spin until ctrl + c
    rospy.spin()

if __name__ == '__main__':
    main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

Basically from the above code you can see the way to use rospy programming.

How to import messages

Sometimes there are some specific messages that need to be imported, usually there is a msg file.

So, for example, to import the message type of turtlesim/Pose, then in python it looks like this:

from turtlesim.msg import Pose
  • 1

More generally, if you want to import a specific format xxx/kkk written by yourself, then the code is:

from xxx.msg import kkk
  • 1

Use of rosbag

rosbag is used to record data, there are two methods, one is in the command line. for example:

rosbag record -O dataset /turtle1/pose /turtle1/cmd_vel
  • 1

Another way is to use rospy.

import rospy
import rosbag
from turtlesim.msg import Pose
from turtlesim.msg import Color

bag = rosbag.Bag('dataset.bag','w')

def processColorData(data):
    bag.write('Color',data)

def processPoseData(data):
    bag.write('Pose',data)

def main():
    rospy.init_node('drone_track_data_saver')
    rospy.Subscriber('/turtle1/color_sensor',Color,processColorData)
    rospy.Subscriber('/turtle1/pose',Pose,processPoseData)

    rate = rospy.Rate(1) # 10hz
    while not rospy.is_shutdown():
        rate.sleep()
    else:
        bag.close()

if __name__ == '__main__':
        main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

The effect of both is the same. If written in python, you can add some specific controls, such as time intervals.

Reading the contents of the rosbag using python is also very simple:

import rosbag

rbag = rosbag.Bag('dataset.bag')
numbers = 100
for topic,msg,t in rbag.read_messages():
        if numbers < 1:
            break
        numbers -= 1
        print topic,msg
rbag.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/songrotek/article/details/50789450

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691174&siteId=291194637