Skip to content

Commit 3e7299e

Browse files
committed
Updated CMakeLists.txt for newer ROS. Added simple tutorial that uses object oriented classes.
1 parent 8febc34 commit 3e7299e

File tree

9 files changed

+236
-107
lines changed

9 files changed

+236
-107
lines changed

action_tut/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ include_directories(include
3232
)
3333

3434
add_executable(example_action_server src/example_action_server.cpp)
35-
add_dependencies(example_action_server example_action_gencpp)
35+
#add_dependencies(example_action_server example_action_gencpp)
36+
add_dependencies(example_action_server ${${PROJECT_NAME}_EXPORTED_TARGETS})
3637
target_link_libraries(
3738
example_action_server
3839
${catkin_LIBRARIES}
3940
)
4041

4142
add_executable(example_action_client src/example_action_client.cpp)
42-
add_dependencies(example_action_client example_action_gencpp)
43+
#add_dependencies(example_action_client example_action_gencpp)
44+
add_dependencies(example_action_client ${${PROJECT_NAME}_EXPORTED_TARGETS})
4345
target_link_libraries(
4446
example_action_client
4547
${catkin_LIBRARIES}

beginner_tut/CMakeLists.txt

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
cmake_minimum_required(VERSION 2.8.3)
22
project(beginner_tut)
33

4-
## Find catkin macros and libraries
5-
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
6-
## is used, also find other catkin packages
74
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation)
85

9-
## System dependencies are found with CMake's conventions
10-
# find_package(Boost REQUIRED COMPONENTS system)
11-
12-
13-
## Uncomment this if the package has a setup.py. This macro ensures
14-
## modules and global scripts declared therein get installed
15-
## See http://ros.org/doc/groovy/api/catkin/html/user_guide/setup_dot_py.html
16-
# catkin_python_setup()
17-
18-
#######################################
19-
## Declare ROS messages and services ##
20-
#######################################
21-
226
## Generate messages in the 'msg' folder
237
add_message_files(
248
FILES
@@ -37,100 +21,22 @@ generate_messages(
3721
std_msgs
3822
)
3923

40-
###################################
41-
## catkin specific configuration ##
42-
###################################
43-
## The catkin_package macro generates cmake config files for your package
44-
## Declare things to be passed to dependent projects
45-
## LIBRARIES: libraries you create in this project that dependent projects also need
46-
## CATKIN_DEPENDS: catkin_packages dependent projects also need
47-
## DEPENDS: system dependencies of this project that dependent projects also need
4824
catkin_package(
49-
INCLUDE_DIRS include
25+
# INCLUDE_DIRS include
5026
# LIBRARIES beginner_tut
5127
# CATKIN_DEPENDS roscpp rospy std_msgs
5228
# DEPENDS system_lib
5329
CATKIN_DEPENDS message_runtime
5430
)
5531

56-
###########
57-
## Build ##
58-
###########
59-
60-
## Specify additional locations of header files
61-
## Your package locations should be listed before other locations
6232
include_directories(include
6333
${catkin_INCLUDE_DIRS}
6434
)
6535

66-
## Declare a cpp library
67-
# add_library(beginner_tut
68-
# src/${PROJECT_NAME}/beginner_tut.cpp
69-
# )
70-
71-
## Declare a cpp executable
72-
# add_executable(beginner_tut_node src/beginner_tut_node.cpp)
7336
add_executable(talker src/talker.cpp)
7437
target_link_libraries(talker ${catkin_LIBRARIES})
7538
add_dependencies(talker beginner_tut_gencpp)
7639

7740
add_executable(listener src/listener.cpp)
7841
target_link_libraries(listener ${catkin_LIBRARIES})
7942
add_dependencies(listener beginner_tut_gencpp)
80-
81-
## Add cmake target dependencies of the executable/library
82-
## as an example, message headers may need to be generated before nodes
83-
# add_dependencies(beginner_tut_node beginner_tut_generate_messages_cpp)
84-
85-
## Specify libraries to link a library or executable target against
86-
# target_link_libraries(beginner_tut_node
87-
# ${catkin_LIBRARIES}
88-
# )
89-
90-
#############
91-
## Install ##
92-
#############
93-
94-
# all install targets should use catkin DESTINATION variables
95-
# See http://ros.org/doc/groovy/api/catkin/html/adv_user_guide/variables.html
96-
97-
## Mark executable scripts (Python etc.) for installation
98-
## in contrast to setup.py, you can choose the destination
99-
# install(PROGRAMS
100-
# scripts/my_python_script
101-
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
102-
# )
103-
104-
## Mark executables and/or libraries for installation
105-
# install(TARGETS beginner_tut beginner_tut_node
106-
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
107-
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
108-
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
109-
# )
110-
111-
## Mark cpp header files for installation
112-
# install(DIRECTORY include/${PROJECT_NAME}/
113-
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
114-
# FILES_MATCHING PATTERN "*.h"
115-
# PATTERN ".svn" EXCLUDE
116-
# )
117-
118-
## Mark other files for installation (e.g. launch and bag files, etc.)
119-
# install(FILES
120-
# # myfile1
121-
# # myfile2
122-
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
123-
# )
124-
125-
#############
126-
## Testing ##
127-
#############
128-
129-
## Add gtest based cpp test target and link libraries
130-
# catkin_add_gtest(${PROJECT_NAME}-test test/test_beginner_tut.cpp)
131-
# if(TARGET ${PROJECT_NAME}-test)
132-
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
133-
# endif()
134-
135-
## Add folders to be run by python nosetests
136-
# catkin_add_nosetests(test)

class_tut/CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
################################################################################
2+
# Author: Joshua Weaver
3+
# Date: 8-11-15
4+
################################################################################
5+
cmake_minimum_required(VERSION 2.8.3)
6+
project(class_tut)
7+
add_definitions(--std=c++11)
8+
9+
################################################################################
10+
# Find necessary ros packages needed for class_demo
11+
################################################################################
12+
find_package(catkin REQUIRED COMPONENTS
13+
roscpp
14+
std_msgs
15+
message_generation
16+
)
17+
18+
################################################################################
19+
# Setup catkin package to setup ros dependencies.
20+
################################################################################
21+
catkin_package(
22+
INCLUDE_DIRS include
23+
# LIBRARIES class_demo
24+
CATKIN_DEPENDS roscpp std_msgs
25+
# DEPENDS system_lib
26+
)
27+
28+
include_directories(
29+
include
30+
${catkin_INCLUDE_DIRS}
31+
)
32+
33+
################################################################################
34+
# Build class_demo executable
35+
################################################################################
36+
set(BINNAME class_tut)
37+
38+
FILE(GLOB SRC
39+
src/*.cpp
40+
)
41+
add_executable(${BINNAME} ${SRC})
42+
43+
target_link_libraries(${BINNAME}
44+
${catkin_LIBRARIES}
45+
)
46+
47+
###############################################################
48+
# Install executable
49+
###############################################################
50+
install(TARGETS ${BINNAME}
51+
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
52+
)
53+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @author Joshua Weaver
3+
* @date 6/22/17
4+
*
5+
* @brief Define a simple object class for an example of use within ROS
6+
*
7+
* A simple class to be used within ROS. Class is used to explain more complex
8+
* object oriented uses within ROS which is more similar to real applications
9+
* for projects than the listener and talker tutorials.
10+
*/
11+
#pragma once
12+
13+
#include <ros/ros.h>
14+
#include "std_msgs/String.h"
15+
16+
namespace ros_tutorials
17+
{
18+
class ClassTut
19+
{
20+
public:
21+
ClassTut();
22+
~ClassTut();
23+
24+
private:
25+
ros::NodeHandle nh; /// Nodehandle for ROS Node
26+
ros::NodeHandle pnh; /// Private nodehandle to access Node/namespace specfics
27+
28+
/// All subscriptions under node
29+
struct Subscriptions
30+
{
31+
ros::Subscriber listener;
32+
} mSubscriptions;
33+
34+
/// All publishers under node
35+
struct Publishers
36+
{
37+
ros::Publisher talker;
38+
} mPublishers;
39+
40+
/// Parameter to hold message for sending
41+
std::string mMessageToSend;
42+
43+
/** Callback that handles receipt of listener messages
44+
*
45+
* Simple callback
46+
*
47+
* @param msg ROS message holding std_msgs String with data field
48+
*/
49+
void listenerCallback(const std_msgs::String::ConstPtr& msg);
50+
51+
/** Function to hold run loop of application
52+
*
53+
* Holds while loop with ROS sleep to handle main loop of application.
54+
*/
55+
void runNode();
56+
};
57+
}

class_tut/package.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<package>
3+
<name>class_tut</name>
4+
<version>0.0.0</version>
5+
<description>Simple tutorial on how to use classes with ROS</description>
6+
7+
<maintainer email="epsilonorion@gmail.com">Joshua Weaver</maintainer>
8+
9+
<license>BSD</license>
10+
11+
<!-- <url type="website">http://wiki.ros.org/class_tut</url> -->
12+
13+
<author email="epsilonorion@gmail.com">Joshua Weaver</author>
14+
15+
<buildtool_depend>catkin</buildtool_depend>
16+
17+
<build_depend>roscpp</build_depend>
18+
<build_depend>std_msgs</build_depend>
19+
<build_depend>message_generation</build_depend>
20+
21+
<run_depend>roscpp</run_depend>
22+
<run_depend>std_msgs</run_depend>
23+
<run_depend>message_runetime</run_depend>
24+
25+
<export>
26+
27+
</export>
28+
</package>

class_tut/src/class_tut.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @author Joshua Weaver
3+
* @date 8/11/15
4+
*
5+
* @brief Define a simple object class for an example of use within ROS
6+
*
7+
* A simple class to be used within ROS. Class is used to explain more complex
8+
* object oriented uses within ROS which is more similar to real applications
9+
* for projects than the listener and talker tutorials.
10+
*/
11+
#include "class_tut/class_tut.h"
12+
13+
using namespace ros_tutorials;
14+
15+
ClassTut::ClassTut():
16+
pnh("~")
17+
{
18+
/// Setup parameters
19+
pnh.param<std::string>("message", mMessageToSend, "not_set");
20+
21+
/// Setup publishers
22+
mPublishers.talker = nh.advertise<std_msgs::String>( "talker", 10, true);
23+
24+
/// Setup subscribers
25+
mSubscriptions.listener = nh.subscribe("listener", 10, &ClassTut::listenerCallback, this);
26+
27+
ROS_INFO("Class tutorial initialized");
28+
29+
runNode();
30+
}
31+
32+
ClassTut::~ClassTut()
33+
{
34+
35+
}
36+
37+
void ClassTut::listenerCallback(const std_msgs::String::ConstPtr& msg)
38+
{
39+
ROS_INFO("Received: %s", msg->data.c_str());
40+
}
41+
42+
void ClassTut::runNode()
43+
{
44+
ROS_INFO("Starting class tutorial node");
45+
46+
/// Set loop rate of main node to 1 Hz
47+
ros::Rate loopRate(1);
48+
49+
while(ros::ok())
50+
{
51+
/// Build ROS Message for talker publisher
52+
std_msgs::String msg;
53+
msg.data = mMessageToSend;
54+
55+
mPublishers.talker.publish(msg);
56+
57+
/// Spin ROS node at above loopRate
58+
ros::spinOnce();
59+
loopRate.sleep();
60+
}
61+
}

class_tut/src/main.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author Joshua Weaver
3+
* @date 8/11/15
4+
*
5+
* @brief Main file for starting ROS node on Class Tutorials
6+
*/
7+
#include "class_tut/class_tut.h"
8+
9+
int main(int argc, char **argv)
10+
{
11+
/// Initialize ROS Node
12+
ROS_INFO("Class tutorial initializing...");
13+
ros::init(argc, argv, "class_tut");
14+
15+
/// Create class object
16+
ros_tutorials::ClassTut classTut;
17+
18+
/// ROS Spin is only needed if the class object doesn't have a loop (which it
19+
/// does) or if it is using a separately created thread to handle looping.
20+
/// Leaving this here for reference.
21+
ros::Rate r(10.0);
22+
ros::spin();
23+
24+
ROS_INFO("Class tutorial shutting down...");
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)