Sei sulla pagina 1di 8

Image Transport between two ROSsupported machines

Launch a scene in gazebo : $ roslaunch gazebo_worlds simple_world.launch

Spawn PR2 : $ roslaunch pr2_gazebo pr2_empty_world.launch

Run rviz to visualize the model : $ rosrun rviz rviz

Now click on add and add the grid. Further add the robot model, you will see pr2 in rviz. By adding laser scan, point cloud, point cloud 2 in display we can see in the rviz which part of the scence robot can see. We will get this by just playing with the fields of these components. Now we want to capture the image of what robot can see. For this add image in display Select the image topic whatever you want, you will see the image corresponding to that topic.

Now to save this image run the command and set topic for the image $ rosrun image_view image_view image:=/wide_stereo/left/image_rect_color Now you will see the window pop up which shows the required image,this image saves in your home folder,we can also change the saving location.

Now we want to send this image to other ROS-supported machine where image processing can be done. For this we have to publish this image.

Change your directory to ros_workspace: $ cd ~/ros_workspace Create the package and add dependcies : $ roscreate-pkg pr2_image_app image_transport opencv2 cv_bridge $ rosmake pr2_image_app $ roscd pr2_image_app Make src/image_publisher.cpp #include <ros/ros.h> #include <image_transport/image_transport.h> #include <opencv/cvwimage.h> #include <opencv/highgui.h> #include <cv_bridge/CvBridge.h> int main(int argc, char** argv) { ros::init(argc, argv, "image_publisher"); ros::NodeHandle nh; image_transport::ImageTransport it(nh); image_transport::Publisher pub = it.advertise("camera/image", 1); cv::WImageBuffer3_b image( cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR) ); sensor_msgs::ImagePtr msg = sensor_msgs::CvBridge::cvToImgMsg(image.Ipl(), "bgr8"); ros::Rate loop_rate(5); while (nh.ok()) { pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); } }

Add the following line to your CMakeLists.txt file: rosbuild_add_executable(image_publisher src/image_publisher.cpp) And do $ make Now enter in to the super user mode Run Master : $ roscore Set the ROS_MASTER_URI and display $ export ROS_MASTER_URI=http://172.16.9.35:11311 $ export DISPLAY=:0.0 Publish image : rosrun pr2_image_app image_publisher /path/image.jpg

Login to another machine $ ssh xyz@172.16.9.74 Enter in superuser mode Change your directory to ros_workspace: $ cd ~/ros_workspace Create the package and add dependcies : $ roscreate-pkg pr2_image_app image_transport opencv2 cv_bridge $ rosmake pr2_image_app

$ roscd pr2_image_app Make src/image_subscriber.cpp #include <ros/ros.h> #include <image_transport/image_transport.h> #include <opencv/cv.h> #include <opencv/highgui.h> #include <cv_bridge/CvBridge.h> void imageCallback(const sensor_msgs::ImageConstPtr& msg) { sensor_msgs::CvBridge bridge; try { cvShowImage("view", bridge.imgMsgToCv(msg, "bgr8")); } catch (sensor_msgs::CvBridgeException& e) { ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str()); } }

int main(int argc, char **argv) { ros::init(argc, argv, "image_listener"); ros::NodeHandle nh; cvNamedWindow("view"); cvStartWindowThread(); image_transport::ImageTransport it(nh); image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback); ros::spin(); cvDestroyWindow("view"); }

Add the following line to your CMakeLists.txt file: rosbuild_add_executable(my_subscriber src/my_subscriber.cpp) and do $ make set the ROS_MASTER_URI and display $ export ROS_MASTER_URI=http://172.16.9.35:11311 $ export DISPLAY=:0.0

Subscribe the image : $rosrun pr2_image_app image_subscriber You will see the image in window pop-up in other machine.

Potrebbero piacerti anche