Cpp executeables only work with roslaunch, not rosrun, how can I fix this
I have had multiple workspaces/packages that I have been working with, and for a reason I can't explain, as of this morning, executeables built from cpp files only work with roslaunch and not rosrun. Specifically, if called with roslaunch, they can publish/subscribe/everything in realtime as intended, but when I call them with rosrun, even ones that worked before, they do not execute until I hit ctrl-C, where they run once and close. This problem is common across all workspaces and packages therein.
For an example:
grey_pub.cpp: (shortened to include the relevant parts only)
#include <ros/ros.h>
#include <cstdio>
int main(int argc, char** argv){
printf("Script called");
ros::init(argc,argv, "grey_pub");
printf("Node initialized");
//pub mainobject;
//printf("Object initialized");
ros::spin();
return(0);
}
catkin_make for the workspace:
cmake_minimum_required(VERSION 2.8.3)
project(odom_reporter)
add_compile_options(-std=c++11)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
geometry_msgs
nav_msgs
std_msgs
message_generation
image_transport
cv_bridge
sensor_msgs
opencv_apps
message_filters
)
find_package(OpenCV REQUIRED)
add_message_files(
FILES
IWR.msg
)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
opencv_apps # Or other packages containing msgs
)
catkin_package(
CATKIN_DEPENDS message_runtime
)
include_directories(
${OpenCV_INCLUDE_DIRS}
${catkin_INCLUDE_DIRS}
)
add_executable (grey_pub scripts/grey_pub.cpp)
target_link_libraries (grey_pub ${OpenCV_LIBS} ${catkin_LIBRARIES})
When run, it just hangs endlessly, doing nothing. This was a new script, so although it may not work perfectly the first time, the first line in main is a printf command, and yet nothing happens until I ctrl-C
command line output:
rosrun odom_reporter grey_pub
^CScript calledNode initializeddechering@dechering-OptiPlex-7440-AIO:~$
As you can see, the print statements only appear AFTER I have called ctrl-C. In addition, in omitted code there are publisher setups, yet when running rqt there is no topic, just rosout.
When running launch files on very similarly structured nodes, the topics set up properly, functions are performed, etc, but when I use rosrun to individually run those same nodes, rqt graph shows no subscriptions, publishers, etc. This seems like a very odd error, and I am at a loss as to why roslaunch/rosrun would handle these differently.
I have already relaunched the roscore/terminal window, re-sourced my devel/setup file, checked my main .bashrc file, restarted the computer even, and the problem persists. Any help anyone can give is much appreciated. I run kinetic on a linux 16.04 system. This is my first time posting on answers.ros.org, so any input on formatting this question is also appreciated.
Asked by user_handle_9918 on 2019-09-06 11:12:01 UTC
Comments
I edited your question to put the code snippets in preformatted text tags. This can be done with the
101010
button, or by pressingCtrl-k
.Asked by jarvisschultz on 2019-09-06 11:58:19 UTC
I'd check whether you've installed any
apt
updates in the meantime (note: this can happen automatically, if you have that configured).Check that you have no outstanding updates to any ROS packages (partial upgrades are not supported), then remove the
build
anddevel
folders from your Catkin workspaces (and theinstall
one, if you have that).source
the appropriatesetup.bash
(either the main/opt/ros/kinetic/setup.bash
or the one from an underlay if you're overlaying) and rebuild each workspace.Then
source
the workspace and try again.Asked by gvdhoorn on 2019-09-07 04:30:41 UTC
Thanks @jarvisschultz, I will keep this in mind for future posts! @gvdhoorn, I don't see any recently installed packages or instances of any apt commands (used
HISTTIMEFORMAT="%d/%m/%y %T " history | grep '[a]pt'
), and I removed the build, devel, and install folders, resourced the /opt/... setup.bash file, and rebuilt, but nothing changed, sorry. I checked c++ compiling on my computer just in case and it works just fine. I appreciate your suggestion, though.Asked by user_handle_9918 on 2019-09-07 12:07:44 UTC
I built a helloworld program and added features one by one as a test - adding \n to my printf commands seems to have fixed it. I don't understand why not having it would delay execution, but problem solved. Thanks to both of you for your time!
Asked by user_handle_9918 on 2019-09-07 12:47:54 UTC
printf()
doesn't add end-of-line characters for you, so you'd have to add those yourself always in any case.What was probably happening was the output streams were not flushed to your terminal, causing nothing to appear. As soon as you terminate the process, all streams are flushed, causing whatever was buffered there to be written to the screen.
Asked by gvdhoorn on 2019-09-08 10:37:00 UTC