Skip to content

Commit

Permalink
Merge pull request #1 from kavyadevd/ros_pub_sub
Browse files Browse the repository at this point in the history
Ros pub sub
  • Loading branch information
kavyadevd authored Nov 1, 2021
2 parents bb6ed17 + 96dd0ec commit 7c48b7a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 52 deletions.
4 changes: 4 additions & 0 deletions Results/cppcheckoutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Checking src/listener.cpp ...
1/2 files checked 47% done
Checking src/talker.cpp ...
2/2 files checked 100% done
2 changes: 2 additions & 0 deletions Results/cpplintoutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Done processing ./src/listener.cpp
Done processing ./src/talker.cpp
42 changes: 21 additions & 21 deletions src/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,19 @@
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
// %Tag(CALLBACK)%
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
void chatterCallback(const std_msgs::String::ConstPtr& msg) {
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
// %EndTag(CALLBACK)%

int main(int argc, char **argv)
{
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.
* 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.
Expand All @@ -55,8 +54,8 @@ int main(int argc, char **argv)

/**
* 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.
* The first NodeHandle constructed will fully initialize this node, and the
* last NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;

Expand All @@ -66,27 +65,28 @@ int main(int argc, char **argv)
* 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.
* 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.
* is the number of messages that will be buffered up before beginning to
* throw away the oldest ones.
*/
// %Tag(SUBSCRIBER)%
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
// %EndTag(SUBSCRIBER)%

/**
* 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.
* 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.
*/
// %Tag(SPIN)%
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%
// %EndTag(SPIN)%

return 0;
}
Expand Down
35 changes: 4 additions & 31 deletions src/talker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
* @brief Demonstrates simple sending of messages over the ROS system.
*/

// %Tag(FULLTEXT)%
// %Tag(ROS_HEADER)%
// Include required headers
#include <sstream>

#include "ros/ros.h"
// %EndTag(ROS_HEADER)%
// %Tag(MSG_HEADER)%
#include "std_msgs/String.h"
// %EndTag(MSG_HEADER)%

#include <sstream>

int main(int argc, char **argv) {
/**
Expand All @@ -47,18 +43,14 @@ int main(int argc, char **argv) {
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
// %Tag(INIT)%
ros::init(argc, argv, "talker");
// %EndTag(INIT)%

/**
* 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.
*/
// %Tag(NODEHANDLE)%
ros::NodeHandle n;
// %EndTag(NODEHANDLE)%

/**
* The advertise() function is how you tell ROS that you want to
Expand All @@ -77,58 +69,39 @@ int main(int argc, char **argv) {
* than we can send them, the number here specifies how many messages to
* buffer up before throwing some away.
*/
// %Tag(PUBLISHER)%
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
// %EndTag(PUBLISHER)%

// %Tag(LOOP_RATE)%
ros::Rate loop_rate(10);
// %EndTag(LOOP_RATE)%

/**
* A count of how many messages we have sent. This is used to create
* a unique string for each message.
*/
// %Tag(ROS_OK)%
int count = 0;
while (ros::ok()) {
// %EndTag(ROS_OK)%
/**
* This is a message object. You stuff it with data, and then publish it.
*/
// %Tag(FILL_MESSAGE)%
std_msgs::String msg;

std::stringstream ss;
ss << "hello world " << count;
ss << " The counter is set to : " << count << ". \tFear the turtle";
msg.data = ss.str();
// %EndTag(FILL_MESSAGE)%

// %Tag(ROSCONSOLE)%
ROS_INFO("%s", msg.data.c_str());
// %EndTag(ROSCONSOLE)%

/**
* The publish() function is how you send messages. The parameter
* is the message object. The type of this object must agree with the type
* given as a template parameter to the advertise<>() call, as was done
* in the constructor above.
*/
// %Tag(PUBLISH)%
chatter_pub.publish(msg);
// %EndTag(PUBLISH)%

// %Tag(SPINONCE)%
ros::spinOnce();
// %EndTag(SPINONCE)%

// %Tag(RATE_SLEEP)%
loop_rate.sleep();
// %EndTag(RATE_SLEEP)%
++count;
}

return 0;
}
// %EndTag(FULLTEXT)%

0 comments on commit 7c48b7a

Please sign in to comment.