Strange segfault when running node on raspberry pi 3
Hi all,
I have recently installed ROS kinetic on a raspberry pi 3 (raspbian jessie), as explained in this tutorial.
Everything worked fine (except rviz, but I don't need it). However, when I wanted to test some C++ nodes I wrote on my laptop, it didn't worked.
I just copied my packages in the raspi's catkin_ws, then catkin_maked it. I didn't meet any problem when compiling. The python-coded nodes worked fine, but not the C++ ones. Then I tried to make a very simple C++ node saying hello world, and I build it the same way. And it didn't work (Segmentation Fault). Then I discovered, that if, in the CMakeLists, I did not link the catkin/opencv libraries (with target_link_libraries
, which worked on my laptop), the program worked well, but it obviously couldn't interact with ROS/opencv.
Would you have any suggestion, feel free to answer ;)
Thanking you in advance
EDIT
I have included the codes below. The nodes aims at reading a webcam and sending OpenCV images. It works on my laptop (Ubuntu 16.04, ROS kinetic).
Here is the code :
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sstream>
int main(int argc, char** argv)
{
// Initialization of ROS node and ROS topic
ros::init(argc, argv, "i_Camera");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("/sensors/camera", 1);
int cam = 0;
nh.param<int>("camera_id",cam,0);
cv::VideoCapture cap(cam);
// Check if video device can be opened with the given index
if(!cap.isOpened()) return 1;
cv::Mat frame;
sensor_msgs::ImagePtr msg;
ros::Rate loop_rate(60);
// Main loop, grab frames and publish them on the topic
while (nh.ok()) {
cap >> frame;
// Check if grabbed frame is not empty
if(!frame.empty()) {
msg = cv_bridge::CvImage(std_msgs::Header(), sensor_msgs::image_encodings::BGR8, frame).toImageMsg();
pub.publish(msg);
cv::waitKey(1);
}
ros::spinOnce();
loop_rate.sleep();
}
}
And the CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(vision)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
geometry_msgs
roscpp
rospy
std_msgs
visualization_msgs
image_transport
cv_bridge
)
find_package(OpenCV)
catkin_package(
INCLUDE_DIRS include
# LIBRARIES vision
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs visualization_msgs image_transport cv_bridge
# DEPENDS system_lib
)
include_directories(
include
${OpenCV_INCLUDE_DIRS}
${catkin_INCLUDE_DIRS}
)
add_executable(i_Camera src/i_Camera.cpp)
target_link_libraries(i_Camera
${OpenCV_LIBS}
${catkin_LIBRARIES}
)