C++ ROS Tutorial Chatter
Hello everyone, I'm trying to do this simple tutorial using C++ but for some reason i'm having difficulty with it since no executable files are generated under devel/lib
files:
Publisher:
#include "ros/ros.h"
#include "std_msgs/Int32.h"
#include <iostream>
int main (int argc, char **argv)
{
ros::init(argc, argv,"demo_topic_publisher");
ros::NodeHandle node_obj;
ros::Publisher number_publisher = node_obj.advertise<std_msgs::Int32>("/numbers",10);
ros::Rate loop_rate(10);
int number_count = 0;
while (ros::ok())
{
std_msgs::Int32 msg;
msg.data = number_count;
ROS_INFO("%d",msg.data);
number_publisher.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++number_count;
}
return 0;
}
Subscriber:
#include "ros/ros.h"
#include "std_msgs/Int32.h"
#include <iostream>
void number_callback(const std_msgs::Int32::ConstPtr& msg)
{
ROS_INFO("Received [%d]",msg->data);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "demo_topic_subscriber");
ros::NodeHandle node_obj;
ros::Subscriber number_subscriber = node_obj.subscribe("/numbers", 10, number_callback);
ros::spin();
return 0;
}
CMakeList.txt:
cmake_minimum_required(VERSION 2.8.3)
project(book_mr)
find_package(catkin REQUIRED COMPONENTS
actionlib
actionlib_msgs
roscpp
std_msgs
)
catkin_package()
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
add_executable(demo_topic_publisher src/demo_topic_publisher.cpp)
add_executable(demo_topic_subscriber src/demo_topic_subscriber.cpp)
add_dependencies(demo_topic_publisher book_mr_generate_messages_cpp)
add_dependencies(demo_topic_subscriber book_mr_generate_messages_cpp)
target_link_libraries(demo_topic_publisher ${catkin_LIBRARIES})
target_link_libraries(demo_topic_subscriber ${catkin_LIBRARIES})
update 1:
When I use this command to properly initialise the workspace:
$ catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
This error comes up:
(base) banana@Science:~/catkin_ws$ catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3
Base path: /home/banana/catkin_ws
Source space: /home/banana/catkin_ws/src
Build space: /home/banana/catkin_ws/build
Devel space: /home/banana/catkin_ws/devel
Install space: /home/banana/catkin_ws/install
####
#### Running command: "cmake /home/banana/catkin_ws/src -DPYTHON_EXECUTABLE=/usr/bin/python3 -DCATKIN_DEVEL_PREFIX=/home/banana/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/banana/catkin_ws/install -G Unix Makefiles" in "/home/banana/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/banana/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/banana/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/banana/catkin_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.6.9", minimum required is "2")
-- Using PYTHON_EXECUTABLE: /usr/bin/python3
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/banana/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python3 (found version "3.6.9")
-- Using Python nosetests: /usr/bin/nosetests-2.7
ImportError: "from catkin_pkg.package import parse_package" failed: No module named 'catkin_pkg'
Make sure that you have installed "catkin_pkg", it is up to date and on the PYTHONPATH.
CMake Error at /opt/ros/melodic/share/catkin/cmake/safe_execute_process.cmake:11 (message):
execute_process(/usr/bin/python3
"/opt/ros/melodic/share/catkin/cmake/parse_package_xml.py"
"/opt/ros/melodic/share/catkin/cmake/../package.xml"
"/home/banana/catkin_ws/build/catkin/catkin_generated/version/package.cmake")
returned error code 1
Call Stack (most recent call first):
/opt/ros/melodic/share/catkin/cmake/catkin_package_xml.cmake:74 (safe_execute_process)
/opt/ros/melodic/share/catkin/cmake/all.cmake:168 (_catkin_package_xml)
/opt/ros/melodic/share/catkin/cmake/catkinConfig.cmake:20 (include)
CMakeLists.txt:56 (find_package)
-- Configuring incomplete, errors ...
Could you please remove the boilerplate comments from the
CMakeLists.txt
?And please do not link to external sites for these small snippets. Just copy-paste it verbatim into your question. I've done that for you this time, but please keep it in mind.
Thank you very much, and noted!
Hi @KforBanana
First of all why do you have as a required component
actionlib
? since you are using only subscriber pusblisher paradigm you will not needactionlib
andactionlib_msgs
. Same whith boost${Boost_INCLUDE_DIRS}
Also you do not need to add dependencies to
generate_messages_cpp
since you are not using a custom message.Tested and working:
There is any eror during catkin_make?
I don't see anything wrong. The only thing i think is strange is that you are not using any headers, but i think it should work like that.
Did you done properly the workspace creation? (http://wiki.ros.org/catkin/Tutorials/...)
@Weasfas well it should work either way, It's there because the tutorial uses it later on @Solrac3589
ok, so i checked this and when I run
The Python3 works great but the CMake craps out. I'll update the post with the errors and error logs. Thanks for everyone's help. Hope to figure this out since I don't want to just purge everything and re install every time something like this happens it's a learning experience.
The error message is pretty clear I would think:
catkin_pkg
needs to be installed for the Python 3 version you are using (how to install it depends on the platform you are on which you didn't mention).On Ubuntu you will need to install
python3-catkin-pkg-modules
. Just keep in mind that Python 3 is only officially supported by the to-be-released ROS distro Noetic (May 2020). All older ROS distros use Python 2.ok that, fixed it and no more errors on
catkin_make
, but also the original problem wasnt solved, which is nothing is being compiled indevel/lib
, so whenever i try to run the package, nothing works