-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from anubhavparas/ros_pub_sub
Ros pub sub
- Loading branch information
Showing
16 changed files
with
356 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/build | ||
/.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
MIT License | ||
|
||
Copyright (c) 2021 Anubhav Paras | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Simple ROS Publisher Subscriber | ||
This is a simple example to run publisher and subscriber in ROS Melodic. | ||
|
||
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) | ||
--- | ||
## Dependencies | ||
- Ubuntu 18.04 (LTS) | ||
- ROS Melodic | ||
|
||
## Instructions to build and run the code | ||
- Make sure you have ROS Melodic installed in your computer. If not refer to [site](http://wiki.ros.org/melodic/Installation/Ubuntu). | ||
|
||
- Create a workspace: | ||
``` | ||
mkdir -p ~/pubsub_ws/src | ||
cd ~/pubsub_ws/src | ||
``` | ||
- Clone the repository into the workspace: | ||
``` | ||
git clone https://github.com/anubhavparas/beginner_tutorials.git | ||
``` | ||
- Build the workspace: | ||
``` | ||
cd ~/pubsub_ws | ||
catkin_make or catkin build (preferred) | ||
source devel/setup.bash | ||
``` | ||
|
||
- Start ros master node in a separate terminal: | ||
``` | ||
roscore | ||
``` | ||
|
||
- To run the publisher run the following in a new terminal: | ||
``` | ||
cd ~/pubsub_ws/ | ||
source devel/setup.bash | ||
rosrun beginner_tutorials talker | ||
``` | ||
|
||
- To run the subscriber run the following in a new terminal: | ||
``` | ||
cd ~/pubsub_ws/ | ||
source devel/setup.bash | ||
rosrun beginner_tutorials listener | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @file ros_publisher.hpp | ||
* @author Anubhav Paras ([email protected]) | ||
* @brief Class to publish fixed message over /chatter topic | ||
* @version 0.1 | ||
* @date 2021-11-01 | ||
* | ||
* @copyright Copyright (c) 2021 | ||
* | ||
*/ | ||
|
||
#ifndef SRC_BEGINNER_TUTORIALS_INCLUDE_BEGINNER_TUTORIALS_ROS_PUBLISHER_HPP_ | ||
#define SRC_BEGINNER_TUTORIALS_INCLUDE_BEGINNER_TUTORIALS_ROS_PUBLISHER_HPP_ | ||
|
||
#include <ros/ros.h> | ||
#include <std_msgs/String.h> | ||
|
||
#include <sstream> | ||
#include <string> | ||
|
||
|
||
|
||
/** | ||
* @brief ROSPublisher class to publish messages | ||
* | ||
*/ | ||
class ROSPublisher { | ||
public: | ||
/** | ||
* @brief Construct a new ROSPublisher object | ||
* | ||
* @param ros_node_h | ||
*/ | ||
explicit ROSPublisher(ros::NodeHandle ros_node_h); | ||
|
||
/** | ||
* @brief Destroy the ROSPublisher object | ||
* | ||
*/ | ||
virtual ~ROSPublisher(); | ||
|
||
/** | ||
* @brief to run the publisher node and publish messages at some fixed rate. | ||
* The loop rate is set to 10 | ||
* The message that is to be sent is also fixed in this case: | ||
* Message is: "Hey, I am chatting. I am saying:: <count>" | ||
* | ||
*/ | ||
virtual void run_publisher(); | ||
|
||
private: | ||
/** | ||
* 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 ros_node_h; | ||
ros::Publisher chatter_pub; | ||
}; | ||
|
||
#endif // SRC_BEGINNER_TUTORIALS_INCLUDE_BEGINNER_TUTORIALS_ROS_PUBLISHER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @file ros_subscriber.hpp | ||
* @author Anubhav Paras ([email protected]) | ||
* @brief Class to subscribe and read messages from /chatter topic | ||
* @version 0.1 | ||
* @date 2021-11-01 | ||
* | ||
* @copyright Copyright (c) 2021 | ||
* | ||
*/ | ||
|
||
#ifndef SRC_BEGINNER_TUTORIALS_INCLUDE_BEGINNER_TUTORIALS_ROS_SUBSCRIBER_HPP_ | ||
#define SRC_BEGINNER_TUTORIALS_INCLUDE_BEGINNER_TUTORIALS_ROS_SUBSCRIBER_HPP_ | ||
|
||
#include <ros/ros.h> | ||
#include <std_msgs/String.h> | ||
|
||
#include <sstream> | ||
#include <string> | ||
|
||
|
||
|
||
/** | ||
* @brief ROSSubscriber class to publish messages | ||
* | ||
*/ | ||
class ROSSubscriber { | ||
public: | ||
/** | ||
* @brief Construct a new ROSSubscriber object | ||
* | ||
* @param ros_node_h | ||
*/ | ||
explicit ROSSubscriber(ros::NodeHandle ros_node_h); | ||
|
||
/** | ||
* @brief Destroy the ROSSubscriber object | ||
* | ||
*/ | ||
virtual ~ROSSubscriber(); | ||
|
||
/** | ||
* @brief to run the publisher node and publish messages at some fixed rate. | ||
* The loop rate is set to 10 | ||
* The message that is to be sent is also fixed in this case: | ||
* Message is: "Hey, I am chatting. I am saying:: <count>" | ||
* | ||
*/ | ||
virtual void run_subscriber(); | ||
|
||
private: | ||
/** | ||
* 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 ros_node_h; | ||
ros::Subscriber chatter_sub; | ||
|
||
/** | ||
* @brief callback method for the subscriber | ||
* | ||
* @param msg message read by the subsriber | ||
*/ | ||
void chatter_call_back(const std_msgs::String::ConstPtr& msg); | ||
}; | ||
|
||
#endif // SRC_BEGINNER_TUTORIALS_INCLUDE_BEGINNER_TUTORIALS_ROS_SUBSCRIBER_HPP_ |
Oops, something went wrong.