Skip to content

Commit

Permalink
Merge pull request #2 from sakshikakde/ros_pub_sub
Browse files Browse the repository at this point in the history
Ros pub sub
  • Loading branch information
sakshikakde authored Oct 31, 2021
2 parents 2c270c8 + ea4fa4c commit c923b72
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 126 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)

## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs)

## Declare a catkin package
catkin_package()
catkin_package(
INCLUDE_DIRS include
)

## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(talker src/talker.cpp)
add_executable(talker src/talker.cpp src/talker_node.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})

add_executable(listener src/listener.cpp)
add_executable(listener src/listener.cpp src/listener_node.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
# beginner_tutorials
## Introduction
This repository provides a simple example to run ROS publisher and sibscriber

## File Structure
- include
-- listener.hpp
-- talker.hpp

- src
-- listener.cpp
-- talker.cpp
-- listener_node.cpp
-- talker_node.cpp

listener_node.cpp and talker_node.cpp are the files where the objectes for the classes Listener and Talker are created.

## Building the package
1) Create a catkin workspace catkin_ws
2) Clone the package inside catkin_ws/src using

```
git clone https://github.com/sakshikakde/beginner_tutorials.git
```
4) Change the directory
```
cd catkin_ws
```
5) run

```
catkin build
```

## How to run the code
1) Change the directory

```
cd catkin_ws
```
2)Source the workspace

```
sourcedevel/setup.bash
```
3) In a terminal, run
```
roscore
```
5) In a new terminal, run the talker by using

```
rosrun beginner_tutorials talker
```
5) In a new terminal, run the listener by using
```
rosrun beginner_tutorials listener
```






58 changes: 58 additions & 0 deletions include/beginner_tutorials/listener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @file listener.hpp
* @author Sakshi Kakde
* @brief A class to subscribe to a topic of type string
* @version 0.1
* @date 2021-10-31
*
* @copyright Copyright (c) 2021
*
*/
#ifndef INCLUDE_BEGINNER_TUTORIALS_LISTENER_HPP_
#define INCLUDE_BEGINNER_TUTORIALS_LISTENER_HPP_

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sstream>
#include <string>

class Listener {
public:
/**
* @brief Construct a new Listener object
*
*/
Listener();
/**
* @brief Destroy the Listener object
*
*/
~Listener();
/**
* @brief waits for publisher to publish
*
*/
void runNode();
ros::NodeHandle* nh_p; // nodehandle

private:
std::string subscriber_topic_name; // ROS subscriber topic name
ros::Subscriber chatter_sub; // ROS Subscriber object
/**
* @brief Function to init params
*
*/
void initParams();
/**
* @brief Function to init subscribers
*
*/
void initSubscribers();
/**
* @brief Callback function for subscriber
*
* @param msg Message received from publisher
*/
void chatter_callback(const std_msgs::String::ConstPtr& msg);
};
#endif // INCLUDE_BEGINNER_TUTORIALS_LISTENER_HPP_
53 changes: 53 additions & 0 deletions include/beginner_tutorials/talker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @file talker.hpp
* @author Sakshi Kakde
* @brief A class to publish string data on a topic
* @version 0.1
* @date 2021-10-31
*
* @copyright Copyright (c) 2021
*
*/
#ifndef INCLUDE_BEGINNER_TUTORIALS_TALKER_HPP_
#define INCLUDE_BEGINNER_TUTORIALS_TALKER_HPP_

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <sstream>
#include <string>

class Talker {
public:
/**
* @brief Construct a new Talker object
*
*/
Talker();
/**
* @brief Destroy the Talker object
*
*/
~Talker();
/**
* @brief Runs the ros::ok loop and publishes data at a predefined rate
*
*/
void runNode();
ros::NodeHandle* nh_p; // nodehandle

private:
std::string publisher_topic_name; // ROS publisher topic name
ros::Publisher chatter_pub; // ROS publisher object
int publisher_rate; // rate of publishing
/**
* @brief Function to init params
*
*/
void initParams();
/**
* @brief Function to init publishers
*
*/
void initPublishers();
};
#endif // INCLUDE_BEGINNER_TUTORIALS_TALKER_HPP_
5 changes: 5 additions & 0 deletions results/cppcheck_process.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[include/beginner_tutorials/listener.hpp:19]: (style) class 'Listener' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.
[src/talker.cpp:13]: (warning) Member variable 'Talker::publisher_rate' is not initialized in the constructor.
[src/talker.cpp:19]: (warning) Member variable 'Talker::publisher_rate' is not initialized in the constructor.
[src/talker.cpp:20]: (style) Value of pointer 'nh_p', which points to allocated memory, is copied in copy constructor instead of allocating new memory.
[include/beginner_tutorials/talker.hpp:19]: (warning) The class 'Talker' has 'copy constructor' but lack of 'operator='.
8 changes: 8 additions & 0 deletions results/cppcheck_result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Checking src/listener.cpp ...
1/4 files checked 31% done
Checking src/listener_node.cpp ...
2/4 files checked 44% done
Checking src/talker.cpp ...
3/4 files checked 86% done
Checking src/talker_node.cpp ...
4/4 files checked 100% done
6 changes: 6 additions & 0 deletions results/cpplint_result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Done processing ./include/beginner_tutorials/listener.hpp
Done processing ./include/beginner_tutorials/talker.hpp
Done processing ./src/listener.cpp
Done processing ./src/listener_node.cpp
Done processing ./src/talker.cpp
Done processing ./src/talker_node.cpp
2 changes: 2 additions & 0 deletions run_cppcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cppcheck --enable=all --std=c++11 -I include/ --suppress=missingIncludeSystem $( find . -name *.cpp | grep -vE -e "^./build/" -e "^./vendor/") --output-file=results/cppcheck_process.txt > results/cppcheck_result.txt
echo "Done Processing. Results are stored in results/cppcheck_process.txt, results/cppcheck_result.txt"
2 changes: 2 additions & 0 deletions run_cpplint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cpplint $( find . -name *.cpp | grep -vE -e "^./build/" -e "^./vendor/") $( find . -name *.hpp | grep -vE -e "^./build/" -e "^./vendor/") > results/cpplint_result.txt
echo "Done Processing. Results are stored in results/cpplint_result.txt"
80 changes: 30 additions & 50 deletions src/listener.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
#include "ros/ros.h"
#include "std_msgs/String.h"

/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
* @file listener.cpp
* @author Sakshi Kakde
* @brief A class to subscribe to a topic of type string
* @version 0.1
* @date 2021-10-31
*
* @copyright Copyright (c) 2021
*
*/
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
#include <beginner_tutorials/listener.hpp>

Listener::Listener() {
this->nh_p = new ros::NodeHandle("~");
initParams();
initSubscribers();
}

int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line.
* For programmatic remappings you can use a different version of init() which takes
* remappings directly, but for most command-line programs, passing argc and argv is
* the easiest way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
ros::init(argc, argv, "listener");
Listener::~Listener() {
}

/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
void Listener::initParams() {
this->nh_p->param<std::string>("subscriber_topic_name",
this->subscriber_topic_name, "/chatter");
}

/**
* The subscribe() call is how you tell ROS that you want to receive messages
* on a given topic. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. Messages are passed to a callback function, here
* called chatterCallback. subscribe() returns a Subscriber object that you
* must hold on to until you want to unsubscribe. When all copies of the Subscriber
* object go out of scope, this callback will automatically be unsubscribed from
* this topic.
*
* The second parameter to the subscribe() function is the size of the message
* queue. If messages are arriving faster than they are being processed, this
* is the number of messages that will be buffered up before beginning to throw
* away the oldest ones.
*/
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
void Listener::initSubscribers() {
this->chatter_sub = this->nh_p->subscribe(this->subscriber_topic_name,
1, &Listener::chatter_callback, this);
}

/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
ros::spin();
void Listener::chatter_callback(const std_msgs::String::ConstPtr& msg) {
ROS_INFO("Yes, I heard [%s]", msg->data.c_str());
}

return 0;
}
void Listener::runNode() {
ros::spin();
}
18 changes: 18 additions & 0 deletions src/listener_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file listener_node.cpp
* @author Sakshi Kakde
* @brief Code to subscibe to a topic
* @version 0.1
* @date 2021-10-31
*
* @copyright Copyright (c) 2021
*
*/
#include <beginner_tutorials/listener.hpp>

int main(int argc, char **argv) {
ros::init(argc, argv, "listener_node");
Listener listener;
listener.runNode();
return 0;
}
Loading

0 comments on commit c923b72

Please sign in to comment.