From e2f419db4fdacdec835f794465b095a28276f509 Mon Sep 17 00:00:00 2001 From: Corwin Perren Date: Fri, 8 Dec 2017 12:12:07 -0800 Subject: [PATCH] Added new camera node, finished up most of UDEV with real names --- .../UDEV Rules/99-rover-cameras.rules | 8 +- .../UDEV Rules/99-rover-usb-serial.rules | 8 +- rover/rover_cameras/CMakeLists.txt | 197 ++++++++++++++++++ .../gimbal/gimbal_camera_360p.launch | 9 + .../gimbal/gimbal_camera_720p.launch | 12 ++ .../main_navigation_camera_360p.launch | 9 + .../main_navigation_camera_720p.launch | 9 + .../undercarriage_camera_360p.launch | 12 ++ .../undercarriage_camera_720p.launch | 12 ++ rover/rover_cameras/package.xml | 62 ++++++ rover/{rover => rover_main}/CMakeLists.txt | 0 rover/rover_main/launch/rover_main.launch | 19 ++ rover/{rover => rover_main}/package.xml | 0 rover/tutorial/CMakeLists.txt | 43 ++++ .../resized_publisher.h | 15 ++ .../resized_subscriber.h | 17 ++ rover/tutorial/msg/ResizedImage.msg | 3 + rover/tutorial/package.xml | 26 +++ rover/tutorial/resized_plugins.xml | 13 ++ rover/tutorial/src/manifest.cpp | 7 + rover/tutorial/src/my_publisher.cpp | 74 +++++++ rover/tutorial/src/my_subscriber.cpp | 29 +++ rover/tutorial/src/resized_publisher.cpp | 37 ++++ rover/tutorial/src/resized_subscriber.cpp | 18 ++ 24 files changed, 634 insertions(+), 5 deletions(-) create mode 100644 rover/rover_cameras/CMakeLists.txt create mode 100644 rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_360p.launch create mode 100644 rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_720p.launch create mode 100644 rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_360p.launch create mode 100644 rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_720p.launch create mode 100644 rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_360p.launch create mode 100644 rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_720p.launch create mode 100644 rover/rover_cameras/package.xml rename rover/{rover => rover_main}/CMakeLists.txt (100%) create mode 100644 rover/rover_main/launch/rover_main.launch rename rover/{rover => rover_main}/package.xml (100%) create mode 100644 rover/tutorial/CMakeLists.txt create mode 100644 rover/tutorial/include/image_transport_tutorial/resized_publisher.h create mode 100644 rover/tutorial/include/image_transport_tutorial/resized_subscriber.h create mode 100644 rover/tutorial/msg/ResizedImage.msg create mode 100644 rover/tutorial/package.xml create mode 100644 rover/tutorial/resized_plugins.xml create mode 100644 rover/tutorial/src/manifest.cpp create mode 100644 rover/tutorial/src/my_publisher.cpp create mode 100644 rover/tutorial/src/my_subscriber.cpp create mode 100644 rover/tutorial/src/resized_publisher.cpp create mode 100644 rover/tutorial/src/resized_subscriber.cpp diff --git a/environment_reference/UDEV Rules/99-rover-cameras.rules b/environment_reference/UDEV Rules/99-rover-cameras.rules index 3dbeb02..0ed9276 100644 --- a/environment_reference/UDEV Rules/99-rover-cameras.rules +++ b/environment_reference/UDEV Rules/99-rover-cameras.rules @@ -3,13 +3,13 @@ # SUBSYSTEM=="video4linux",ATTRS{idVendor}=="0x046d",ATTRS{idProduct}=="0x082d",NAME="videoext" # The zed camera -KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="2b03", ATTRS{idProduct}=="f580", SYMLINK+="rover/cam_zed" +KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="2b03", ATTRS{idProduct}=="f580", SYMLINK+="rover/camera_zed" # The first C920 Webcam -KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="082d", ATTRS{serial}=="B9A8A5FF", SYMLINK+="rover/cam_undercarriage" +KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="082d", ATTRS{serial}=="B9A8A5FF", SYMLINK+="rover/camera_undercarriage" # The second C920 Webcam -KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="082d", ATTRS{serial}=="A98AA5FF", SYMLINK+="rover/cam_grasper" +KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="082d", ATTRS{serial}=="A98AA5FF", SYMLINK+="rover/camera_gimbal" # The special main nav cam -KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="05a3", ATTRS{idProduct}=="9422", ATTRS{serial}=="SN0001", SYMLINK+="rover/cam_main_nav" +KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="05a3", ATTRS{idProduct}=="9422", ATTRS{serial}=="SN0001", SYMLINK+="rover/camera_main_navigation" diff --git a/environment_reference/UDEV Rules/99-rover-usb-serial.rules b/environment_reference/UDEV Rules/99-rover-usb-serial.rules index 553d26e..cb616b8 100644 --- a/environment_reference/UDEV Rules/99-rover-usb-serial.rules +++ b/environment_reference/UDEV Rules/99-rover-usb-serial.rules @@ -1 +1,7 @@ -SUBSYSTEM=="tty", ATTRS{serial}=="A5027JQW", SYMLINK+="rover/ttyARM" +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ATTRS{serial}=="FTU9EU0I", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="rover/ttyDEV0" + +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ATTRS{serial}=="FTU9EU0I", ENV{ID_USB_INTERFACE_NUM}=="01", SYMLINK+="rover/ttyDEV1" + +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ATTRS{serial}=="FTU9EU0I", ENV{ID_USB_INTERFACE_NUM}=="02", SYMLINK+="rover/ttyDEV2" + +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ATTRS{serial}=="FTU9EU0I", ENV{ID_USB_INTERFACE_NUM}=="03", SYMLINK+="rover/ttyDEV3" diff --git a/rover/rover_cameras/CMakeLists.txt b/rover/rover_cameras/CMakeLists.txt new file mode 100644 index 0000000..d0422e7 --- /dev/null +++ b/rover/rover_cameras/CMakeLists.txt @@ -0,0 +1,197 @@ +cmake_minimum_required(VERSION 2.8.3) +project(rover_cameras) + +## Compile as C++11, supported in ROS Kinetic and newer +# add_compile_options(-std=c++11) + +## Find catkin macros and libraries +## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) +## is used, also find other catkin packages +find_package(catkin REQUIRED COMPONENTS + rospy +) + +## System dependencies are found with CMake's conventions +# find_package(Boost REQUIRED COMPONENTS system) + + +## Uncomment this if the package has a setup.py. This macro ensures +## modules and global scripts declared therein get installed +## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html +# catkin_python_setup() + +################################################ +## Declare ROS messages, services and actions ## +################################################ + +## To declare and build messages, services or actions from within this +## package, follow these steps: +## * Let MSG_DEP_SET be the set of packages whose message types you use in +## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...). +## * In the file package.xml: +## * add a build_depend tag for "message_generation" +## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET +## * If MSG_DEP_SET isn't empty the following dependency has been pulled in +## but can be declared for certainty nonetheless: +## * add a run_depend tag for "message_runtime" +## * In this file (CMakeLists.txt): +## * add "message_generation" and every package in MSG_DEP_SET to +## find_package(catkin REQUIRED COMPONENTS ...) +## * add "message_runtime" and every package in MSG_DEP_SET to +## catkin_package(CATKIN_DEPENDS ...) +## * uncomment the add_*_files sections below as needed +## and list every .msg/.srv/.action file to be processed +## * uncomment the generate_messages entry below +## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...) + +## Generate messages in the 'msg' folder +# add_message_files( +# FILES +# Message1.msg +# Message2.msg +# ) + +## Generate services in the 'srv' folder +# add_service_files( +# FILES +# Service1.srv +# Service2.srv +# ) + +## Generate actions in the 'action' folder +# add_action_files( +# FILES +# Action1.action +# Action2.action +# ) + +## Generate added messages and services with any dependencies listed here +# generate_messages( +# DEPENDENCIES +# std_msgs # Or other packages containing msgs +# ) + +################################################ +## Declare ROS dynamic reconfigure parameters ## +################################################ + +## To declare and build dynamic reconfigure parameters within this +## package, follow these steps: +## * In the file package.xml: +## * add a build_depend and a run_depend tag for "dynamic_reconfigure" +## * In this file (CMakeLists.txt): +## * add "dynamic_reconfigure" to +## find_package(catkin REQUIRED COMPONENTS ...) +## * uncomment the "generate_dynamic_reconfigure_options" section below +## and list every .cfg file to be processed + +## Generate dynamic reconfigure parameters in the 'cfg' folder +# generate_dynamic_reconfigure_options( +# cfg/DynReconf1.cfg +# cfg/DynReconf2.cfg +# ) + +################################### +## catkin specific configuration ## +################################### +## The catkin_package macro generates cmake config files for your package +## Declare things to be passed to dependent projects +## INCLUDE_DIRS: uncomment this if your package contains header files +## LIBRARIES: libraries you create in this project that dependent projects also need +## CATKIN_DEPENDS: catkin_packages dependent projects also need +## DEPENDS: system dependencies of this project that dependent projects also need +catkin_package( +# INCLUDE_DIRS include +# LIBRARIES rover_cameras +# CATKIN_DEPENDS rospy +# DEPENDS system_lib +) + +########### +## Build ## +########### + +## Specify additional locations of header files +## Your package locations should be listed before other locations +include_directories( +# include + ${catkin_INCLUDE_DIRS} +) + +## Declare a C++ library +# add_library(${PROJECT_NAME} +# src/${PROJECT_NAME}/rover_cameras.cpp +# ) + +## Add cmake target dependencies of the library +## as an example, code may need to be generated before libraries +## either from message generation or dynamic reconfigure +# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Declare a C++ executable +## With catkin_make all packages are built within a single CMake context +## The recommended prefix ensures that target names across packages don't collide +# add_executable(${PROJECT_NAME}_node src/rover_cameras_node.cpp) + +## Rename C++ executable without prefix +## The above recommended prefix causes long target names, the following renames the +## target back to the shorter version for ease of user use +## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" +# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") + +## Add cmake target dependencies of the executable +## same as for the library above +# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) + +## Specify libraries to link a library or executable target against +# target_link_libraries(${PROJECT_NAME}_node +# ${catkin_LIBRARIES} +# ) + +############# +## Install ## +############# + +# all install targets should use catkin DESTINATION variables +# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html + +## Mark executable scripts (Python etc.) for installation +## in contrast to setup.py, you can choose the destination +# install(PROGRAMS +# scripts/my_python_script +# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark executables and/or libraries for installation +# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node +# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} +# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +# ) + +## Mark cpp header files for installation +# install(DIRECTORY include/${PROJECT_NAME}/ +# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} +# FILES_MATCHING PATTERN "*.h" +# PATTERN ".svn" EXCLUDE +# ) + +## Mark other files for installation (e.g. launch and bag files, etc.) +# install(FILES +# # myfile1 +# # myfile2 +# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} +# ) + +############# +## Testing ## +############# + +## Add gtest based cpp test target and link libraries +# catkin_add_gtest(${PROJECT_NAME}-test test/test_rover_cameras.cpp) +# if(TARGET ${PROJECT_NAME}-test) +# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) +# endif() + +## Add folders to be run by python nosetests +# catkin_add_nosetests(test) diff --git a/rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_360p.launch b/rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_360p.launch new file mode 100644 index 0000000..b3599e2 --- /dev/null +++ b/rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_360p.launch @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_720p.launch b/rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_720p.launch new file mode 100644 index 0000000..754fd87 --- /dev/null +++ b/rover/rover_cameras/launch/camera_launches/gimbal/gimbal_camera_720p.launch @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_360p.launch b/rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_360p.launch new file mode 100644 index 0000000..092db23 --- /dev/null +++ b/rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_360p.launch @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_720p.launch b/rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_720p.launch new file mode 100644 index 0000000..e3df6b0 --- /dev/null +++ b/rover/rover_cameras/launch/camera_launches/main_navigation/main_navigation_camera_720p.launch @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_360p.launch b/rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_360p.launch new file mode 100644 index 0000000..0e21498 --- /dev/null +++ b/rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_360p.launch @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_720p.launch b/rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_720p.launch new file mode 100644 index 0000000..51ed160 --- /dev/null +++ b/rover/rover_cameras/launch/camera_launches/undercarriage/undercarriage_camera_720p.launch @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/rover/rover_cameras/package.xml b/rover/rover_cameras/package.xml new file mode 100644 index 0000000..b12fd0c --- /dev/null +++ b/rover/rover_cameras/package.xml @@ -0,0 +1,62 @@ + + + rover_cameras + 0.0.0 + The rover_cameras package + + + + + nvidia + + + + + + TODO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + catkin + rospy + rospy + rospy + + + + + + + + diff --git a/rover/rover/CMakeLists.txt b/rover/rover_main/CMakeLists.txt similarity index 100% rename from rover/rover/CMakeLists.txt rename to rover/rover_main/CMakeLists.txt diff --git a/rover/rover_main/launch/rover_main.launch b/rover/rover_main/launch/rover_main.launch new file mode 100644 index 0000000..d6b38e1 --- /dev/null +++ b/rover/rover_main/launch/rover_main.launch @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/rover/rover/package.xml b/rover/rover_main/package.xml similarity index 100% rename from rover/rover/package.xml rename to rover/rover_main/package.xml diff --git a/rover/tutorial/CMakeLists.txt b/rover/tutorial/CMakeLists.txt new file mode 100644 index 0000000..b40fe74 --- /dev/null +++ b/rover/tutorial/CMakeLists.txt @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 2.8) +project(image_transport_tutorial) + +find_package(catkin REQUIRED COMPONENTS cv_bridge image_transport message_generation sensor_msgs) + +# add the resized image message +add_message_files(DIRECTORY msg + FILES ResizedImage.msg +) +generate_messages(DEPENDENCIES sensor_msgs) + +catkin_package(CATKIN_DEPENDS cv_bridge image_transport message_runtime sensor_msgs) + +find_package(OpenCV) + +include_directories(include ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS}) + +# add the publisher example +add_executable(my_publisher src/my_publisher.cpp) +add_dependencies(my_publisher ${catkin_EXPORTED_TARGETS} ${${PROJECT_NAME}_EXPORTED_TARGETS}) +target_link_libraries(my_publisher ${catkin_LIBRARIES} ${OpenCV_LIBRARIES}) + +# add the subscriber example +#add_executable(my_subscriber src/my_subscriber.cpp) +#add_dependencies(my_subscriber ${catkin_EXPORTED_TARGETS} ${${PROJECT_NAME}_EXPORTED_TARGETS}) +#target_link_libraries(my_subscriber ${catkin_LIBRARIES} ${OpenCV_LIBRARIES}) + +# add the plugin example +#add_library(resized_publisher src/manifest.cpp src/resized_publisher.cpp src/resized_subscriber.cpp) +#add_dependencies(resized_publisher ${catkin_EXPORTED_TARGETS} ${${PROJECT_NAME}_EXPORTED_TARGETS}) +#target_link_libraries(resized_publisher ${catkin_LIBRARIES} ${OpenCV_LIBRARIES}) + + +# Mark executables and/or libraries for installation +install(TARGETS my_publisher + ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} + LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} + RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +) + +#install(FILES resized_plugins.xml +# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} +#) diff --git a/rover/tutorial/include/image_transport_tutorial/resized_publisher.h b/rover/tutorial/include/image_transport_tutorial/resized_publisher.h new file mode 100644 index 0000000..63ab3eb --- /dev/null +++ b/rover/tutorial/include/image_transport_tutorial/resized_publisher.h @@ -0,0 +1,15 @@ +#include +#include + +class ResizedPublisher : public image_transport::SimplePublisherPlugin +{ +public: + virtual std::string getTransportName() const + { + return "resized"; + } + +protected: + virtual void publish(const sensor_msgs::Image& message, + const PublishFn& publish_fn) const; +}; diff --git a/rover/tutorial/include/image_transport_tutorial/resized_subscriber.h b/rover/tutorial/include/image_transport_tutorial/resized_subscriber.h new file mode 100644 index 0000000..94b55f5 --- /dev/null +++ b/rover/tutorial/include/image_transport_tutorial/resized_subscriber.h @@ -0,0 +1,17 @@ +#include +#include + +class ResizedSubscriber : public image_transport::SimpleSubscriberPlugin +{ +public: + virtual ~ResizedSubscriber() {} + + virtual std::string getTransportName() const + { + return "resized"; + } + +protected: + virtual void internalCallback(const typename image_transport_tutorial::ResizedImage::ConstPtr& message, + const Callback& user_cb); +}; diff --git a/rover/tutorial/msg/ResizedImage.msg b/rover/tutorial/msg/ResizedImage.msg new file mode 100644 index 0000000..d8c8fad --- /dev/null +++ b/rover/tutorial/msg/ResizedImage.msg @@ -0,0 +1,3 @@ +uint32 original_height +uint32 original_width +sensor_msgs/Image image diff --git a/rover/tutorial/package.xml b/rover/tutorial/package.xml new file mode 100644 index 0000000..668cd0a --- /dev/null +++ b/rover/tutorial/package.xml @@ -0,0 +1,26 @@ + + image_transport_tutorial + 0.0.0 + Tutorial for image_transport. This is useful for the tutorials at http://wiki.ros.org/image_transport/Tutorials/ + Vincent Rabaud + Vincent Rabaud + Apache 2.0 + + cv_bridge + image_transport + message_generation + opencv2 + sensor_msgs + + cv_bridge + image_transport + message_runtime + opencv2 + sensor_msgs + + catkin + + + + + diff --git a/rover/tutorial/resized_plugins.xml b/rover/tutorial/resized_plugins.xml new file mode 100644 index 0000000..bd2dff1 --- /dev/null +++ b/rover/tutorial/resized_plugins.xml @@ -0,0 +1,13 @@ + + + + This plugin publishes a decimated version of the image. + + + + + + This plugin rescales a decimated image to its original size. + + + diff --git a/rover/tutorial/src/manifest.cpp b/rover/tutorial/src/manifest.cpp new file mode 100644 index 0000000..ab20819 --- /dev/null +++ b/rover/tutorial/src/manifest.cpp @@ -0,0 +1,7 @@ +#include +#include +#include + +PLUGINLIB_EXPORT_CLASS(ResizedPublisher, image_transport::PublisherPlugin) + +PLUGINLIB_EXPORT_CLASS(ResizedSubscriber, image_transport::SubscriberPlugin) diff --git a/rover/tutorial/src/my_publisher.cpp b/rover/tutorial/src/my_publisher.cpp new file mode 100644 index 0000000..60f8ec9 --- /dev/null +++ b/rover/tutorial/src/my_publisher.cpp @@ -0,0 +1,74 @@ +#include +#include + +#include +#include +#include +#include +#include + +#include + +int main(int argc, char** argv) +{ + std::string capture_device_path; + int fps; + + int large_image_width; + int large_image_height; + int small_image_width; + int small_image_height; + + ros::init(argc, argv, "camera"); + ros::NodeHandle node_handle("~"); + + node_handle.param("device_path", capture_device_path, std::string("/dev/video0")); + node_handle.param("fps", fps, 30); + + node_handle.param("large_image_width", large_image_width, 1280); + node_handle.param("large_image_height", large_image_height, 720); + node_handle.param("small_image_width", small_image_width, 640); + node_handle.param("small_image_height", small_image_height, 360); + + cv::VideoCapture cap(capture_device_path); + + cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G')); + cap.set(CV_CAP_PROP_FRAME_WIDTH, large_image_width); + cap.set(CV_CAP_PROP_FRAME_HEIGHT, large_image_height); + cap.set(CV_CAP_PROP_FPS, fps); + + if(cap.isOpened() == false){ + return -1; + } + + image_transport::ImageTransport full_res_image_transport(node_handle); + image_transport::ImageTransport lower_res_image_transport(node_handle); + + image_transport::Publisher full_size_publisher = full_res_image_transport.advertise("image_720p", 1); + image_transport::Publisher lower_size_publisher = lower_res_image_transport.advertise("image_360p", 1); + + cv::Mat image; + cv::Mat image_smaller; + + ros::Rate loop_rate(fps + 5); + + + while (node_handle.ok()) { + + cap.read(image); + + if(!image.empty()){ + cv::resize(image, image_smaller, cv::Size(small_image_width, small_image_height)); + + sensor_msgs::ImagePtr full_res_message = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg(); + sensor_msgs::ImagePtr lower_res_message = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image_smaller).toImageMsg(); + + full_size_publisher.publish(full_res_message); + lower_size_publisher.publish(lower_res_message); + } + + ros::spinOnce(); + loop_rate.sleep(); + } +} + diff --git a/rover/tutorial/src/my_subscriber.cpp b/rover/tutorial/src/my_subscriber.cpp new file mode 100644 index 0000000..b6310f4 --- /dev/null +++ b/rover/tutorial/src/my_subscriber.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +void imageCallback(const sensor_msgs::ImageConstPtr& msg) +{ + try + { + cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image); + cv::waitKey(10); + } + catch (cv_bridge::Exception& 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; + cv::namedWindow("view"); + cv::startWindowThread(); + image_transport::ImageTransport it(nh); + image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback); + ros::spin(); + cv::destroyWindow("view"); +} diff --git a/rover/tutorial/src/resized_publisher.cpp b/rover/tutorial/src/resized_publisher.cpp new file mode 100644 index 0000000..178f482 --- /dev/null +++ b/rover/tutorial/src/resized_publisher.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +void ResizedPublisher::publish(const sensor_msgs::Image& message, + const PublishFn& publish_fn) const +{ + cv::Mat cv_image; + boost::shared_ptr tracked_object; + try + { + cv_image = cv_bridge::toCvShare(message, tracked_object, message.encoding)->image; + } + catch (cv::Exception &e) + { + ROS_ERROR("Could not convert from '%s' to '%s'.", message.encoding.c_str(), message.encoding.c_str()); + return; + } + + // Retrieve subsampling factor from the parameter server + double subsampling_factor; + std::string param_name; + nh().param("resized_image_transport_subsampling_factor", subsampling_factor, 2.0); + + // Rescale image + int new_width = cv_image.cols / subsampling_factor + 0.5; + int new_height = cv_image.rows / subsampling_factor + 0.5; + cv::Mat buffer; + cv::resize(cv_image, buffer, cv::Size(new_width, new_height)); + + // Set up ResizedImage and publish + image_transport_tutorial::ResizedImage resized_image; + resized_image.original_height = cv_image.rows; + resized_image.original_width = cv_image.cols; + resized_image.image = *(cv_bridge::CvImage(message.header, "bgr8", cv_image).toImageMsg()); + publish_fn(resized_image); +} diff --git a/rover/tutorial/src/resized_subscriber.cpp b/rover/tutorial/src/resized_subscriber.cpp new file mode 100644 index 0000000..299dc83 --- /dev/null +++ b/rover/tutorial/src/resized_subscriber.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +void ResizedSubscriber::internalCallback(const image_transport_tutorial::ResizedImage::ConstPtr& msg, + const Callback& user_cb) +{ + // This is only for optimization, not to copy the image + boost::shared_ptr tracked_object_tmp; + cv::Mat img_rsz = cv_bridge::toCvShare(msg->image, tracked_object_tmp)->image; + // Resize the image to its original size + cv::Mat img_restored; + cv::resize(img_rsz, img_restored, cv::Size(msg->original_width, msg->original_height)); + + // Call the user callback with the restored image + cv_bridge::CvImage cv_img(msg->image.header, msg->image.encoding, img_restored); + user_cb(cv_img.toImageMsg()); +};