Skip to content

Commit

Permalink
Merge branch 'Week10_HW' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshikakde committed Nov 8, 2021
2 parents 2b5910d + 471bb0f commit 16b41b9
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 22 deletions.
20 changes: 19 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ 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)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
roslaunch
)

add_service_files(
FILES
AddTwoInts.srv
)

generate_messages(
DEPENDENCIES
std_msgs
)

## Declare a catkin package
catkin_package(
Expand All @@ -14,6 +30,8 @@ include_directories(include ${catkin_INCLUDE_DIRS})

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

add_executable(listener src/listener.cpp src/listener_node.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_gencpp)
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,3 @@ rosrun beginner_tutorials talker
```
rosrun beginner_tutorials listener
```






hiii
10 changes: 9 additions & 1 deletion include/beginner_tutorials/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <std_msgs/String.h>
#include <sstream>
#include <string>
#include "beginner_tutorials/AddTwoInts.h"

class Listener {
public:
Expand Down Expand Up @@ -42,8 +43,10 @@ class Listener {
ros::NodeHandle* nh_p; // nodehandle

private:
std::string subscriber_topic_name; // ROS subscriber topic name
std::string subscriber_topic_name,
service_name; // ROS subscriber topic name
ros::Subscriber chatter_sub; // ROS Subscriber object
ros::ServiceClient client; // ROS service client object
/**
* @brief Function to init params
*
Expand All @@ -54,6 +57,11 @@ class Listener {
*
*/
void initSubscribers();
/**
* @brief Function to init service clients
*
*/
void initServiceClient();
/**
* @brief Callback function for subscriber
*
Expand Down
19 changes: 18 additions & 1 deletion include/beginner_tutorials/talker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <std_msgs/String.h>
#include <sstream>
#include <string>
#include "beginner_tutorials/AddTwoInts.h"

class Talker {
public:
Expand Down Expand Up @@ -42,8 +43,9 @@ class Talker {
ros::NodeHandle* nh_p; // nodehandle

private:
std::string publisher_topic_name; // ROS publisher topic name
std::string publisher_topic_name, service_name; // ROS publisher topic name
ros::Publisher chatter_pub; // ROS publisher object
ros::ServiceServer service; // ROS service object
int publisher_rate; // rate of publishing
/**
* @brief Function to init params
Expand All @@ -55,5 +57,20 @@ class Talker {
*
*/
void initPublishers();
/**
* @brief Function to init ros services
*
*/
void initServices();
/**
* @brief Service callback function
*
* @param req request parameters
* @param res response parameters
* @return true if the service operations are successful
* @return false if the service operations fail
*/
bool add(beginner_tutorials::AddTwoInts::Request &req, // NOLINT
beginner_tutorials::AddTwoInts::Response &res); // NOLINT
};
#endif // INCLUDE_BEGINNER_TUTORIALS_TALKER_HPP_
24 changes: 24 additions & 0 deletions launch/talker_chatter.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<launch>
<arg name="platform_name" default="sakshi" />
<arg name="publisher_topic_name" default="/chatter" />
<arg name="publisher_rate" default="20" />
<arg name="service_name" default="/add_two_ints" />
<arg name="subscriber_topic_name" default="/chatter" />
<group>
<node pkg="beginner_tutorials" type="talker" name="talker_$(arg platform_name)">
<param name="publisher_topic_name" value="$(arg publisher_topic_name)"/>
<param name="publisher_rate" value="$(arg publisher_rate)"/>
<param name="service_name" value="$(arg service_name)"/>
</node>
</group>

<group>
<node pkg="beginner_tutorials" type="listener" name="listener_$(arg platform_name)">
<param name="subscriber_topic_name" value="$(arg subscriber_topic_name)"/>
<param name="service_name" value="$(arg service_name)"/>
</node>
</group>


</launch>
Binary file added logs/rqt_console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="sakshi@todo.todo">sakshi</maintainer>
<maintainer email="sakshi@umd.edu">sakshi</maintainer>


<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<license>MIT</license>


<!-- Url tags are optional, but multiple are allowed, one per tag -->
Expand Down Expand Up @@ -52,6 +52,8 @@
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
Expand Down
6 changes: 1 addition & 5 deletions results/cppcheck_process.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
[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='.
(information) Cppcheck cannot find all the include files (use --check-config for details)
4 changes: 2 additions & 2 deletions results/cppcheck_result.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Checking src/listener.cpp ...
1/4 files checked 31% done
1/4 files checked 34% done
Checking src/listener_node.cpp ...
2/4 files checked 44% done
Checking src/talker.cpp ...
3/4 files checked 86% done
3/4 files checked 90% done
Checking src/talker_node.cpp ...
4/4 files checked 100% done
20 changes: 19 additions & 1 deletion src/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Listener::Listener(ros::NodeHandle* nh_p) {
this->nh_p = nh_p;
initParams();
initSubscribers();
initServiceClient();
}

Listener::~Listener() {
Expand All @@ -25,17 +26,34 @@ Listener::~Listener() {
void Listener::initParams() {
this->nh_p->param<std::string>("subscriber_topic_name",
this->subscriber_topic_name, "/chatter");
this->nh_p->param<std::string>("service_name",
this->service_name, "/talker_node/add_two_ints");
}

void Listener::initSubscribers() {
this->chatter_sub = this->nh_p->subscribe(this->subscriber_topic_name,
1, &Listener::chatter_callback, this);
}

void Listener::initServiceClient() {
this->client = this->nh_p->serviceClient<beginner_tutorials::AddTwoInts>(
this->service_name,
this);
}
void Listener::chatter_callback(const std_msgs::String::ConstPtr& msg) {
ROS_INFO("Yes, I heard [%s]", msg->data.c_str());
ROS_INFO_STREAM("Yes, I heard " << msg->data.c_str());
ROS_INFO_STREAM("Can you add two numbers for me?");
beginner_tutorials::AddTwoInts srv;
srv.request.a = 10;
srv.request.b = -10;
if (client.call(srv)) {
ROS_INFO_STREAM("Yes! The sum is : " << srv.response.sum);
} else {
ROS_ERROR_STREAM("Failed to call service add_two_ints");
}
}

void Listener::runNode() {
ROS_DEBUG_ONCE("Talker node activated.");
ros::spin();
}
32 changes: 30 additions & 2 deletions src/talker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Talker::Talker(ros::NodeHandle* nh_p) {
this->nh_p = nh_p;
initParams();
initPublishers();
initServices();
}

Talker::~Talker() {
Expand All @@ -28,6 +29,8 @@ void Talker::initParams() {
this->publisher_topic_name, "/chatter");
this->nh_p->param<int>("publisher_rate",
this->publisher_rate, 10);
this->nh_p->param<std::string>("service_name",
this->service_name, "add_two_ints");
}

void Talker::initPublishers() {
Expand All @@ -36,18 +39,43 @@ void Talker::initPublishers() {
this->publisher_rate, this);
}

void Talker::initServices() {
this->service = this->nh_p->advertiseService(this->service_name,
&Talker::add,
this);
ROS_INFO_STREAM("Ready to add two ints.");
}

bool Talker::add(beginner_tutorials::AddTwoInts::Request &req,
beginner_tutorials::AddTwoInts::Response &res) {
if ((req.a < 0) || (req.b < 0)) {
ROS_WARN_STREAM("One of the numbers is negative");
}
res.sum = req.a + req.b;
if (res.sum == 0) {
ROS_ERROR_STREAM("Sum is 0!");
}
ROS_INFO_STREAM("request: x = " << req.a << " y = " << req.b);
ROS_INFO_STREAM("sending back response: " << res.sum);
return true;
}

void Talker::runNode() {
int count = 0;
ros::Rate loop_rate(10);
ros::Rate loop_rate(this->publisher_rate);
while (ros::ok()) {
ROS_DEBUG_STREAM("Talker node is active");
std_msgs::String msg;
std::stringstream ss;
ss << "Can you hear " << count << " ?";
msg.data = ss.str();

ROS_INFO("%s", msg.data.c_str());
this->chatter_pub.publish(msg);

uint32_t n_sub = this->chatter_pub.getNumSubscribers();
if (n_sub == 0) {
ROS_FATAL_STREAM("NO SUBSCRIBERS!");
}
ros::spinOnce();
loop_rate.sleep();
++count;
Expand Down
4 changes: 4 additions & 0 deletions srv/AddTwoInts.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int64 a
int64 b
---
int64 sum

0 comments on commit 16b41b9

Please sign in to comment.