Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROS Task by 210983 #60

Open
wants to merge 9 commits into
base: ros
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions 210983/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# How to Run

1). Create a catkin workspace using **catkin_make** in a suitable directory say, catkin_ws
2). Source the setup file using **source devel/setup.bash** or **source devel/setup.zsh** depending on your terminal.
3). Copy the **math** folder into the src folder of your workspace.
4). In your workspace, build the package using **catkin_make**.
4). To run the nodes, in a new terminal run **roscore**.
5). In new terminals, run **rosrun math input_node**, **rosrun math output_node** and **rosrun math add_two_numbers_server**.

# Steps Followed
1). First created a custom msg which holds a Float64 with a header(used for Time Sync in output node) and a serivce file called add_two_numbers.srv.
2). Wrote the code for the input node in C++ which publishes two msgs to two topics(topic1 and topic2) and makes a request to the previously created service.
3). Wrote the output node to subscribe to the two topics log the sum and also publish the sum to another topic(topic3) which currently has no subscribers. I had to use **message_filters** to syncronize the messages from the two topics to use a single callback function.
4). Wrote the server for add_two_ints service.
5). Added the neccesary lines in CMakeLists.txt to make the package successfully.
1 change: 1 addition & 0 deletions 210983/src/CMakeLists.txt
21 changes: 21 additions & 0 deletions 210983/src/math/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"configurations": [
{
"browse": {
"databaseFilename": "${default}",
"limitSymbolsToIncludedHeaders": false
},
"includePath": [
"/opt/ros/noetic/include/**",
"/home/shivam/ROS/Assignment1/catkin_ws/src/math/include/**",
"/usr/include/**"
],
"name": "ROS",
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "c++14"
}
],
"version": 4
}
11 changes: 11 additions & 0 deletions 210983/src/math/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.autoComplete.extraPaths": [
"/opt/ros/noetic/lib/python3/dist-packages"
],
"files.associations": {
"array": "cpp",
"string": "cpp",
"string_view": "cpp",
"ratio": "cpp"
}
}
76 changes: 76 additions & 0 deletions 210983/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.0.2)
project(math)

find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_filters
)

## System dependencies are found with CMake's conventions
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_filters
message_generation
genmsg)

## Generate messages in the 'msg' folder
add_message_files(
FILES
number.msg
)

## Generate services in the 'srv' folder
add_service_files(
FILES
add_two_numbers.srv
)

## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )

## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
# message_filters
)

catkin_package(
INCLUDE_DIRS include
LIBRARIES math
CATKIN_DEPENDS roscpp rospy std_msgs message_filters message_runtime
DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include
${catkin_INCLUDE_DIRS}
)

add_executable(input_node src/input_node.cpp)
target_link_libraries(input_node ${catkin_LIBRARIES})
add_dependencies(input_node math_generate_messages_cpp)
add_dependencies(input_node math_gencpp)


add_executable(output_node src/output_node.cpp)
target_link_libraries(output_node ${catkin_LIBRARIES})
add_dependencies(output_node math_generate_messages_cpp)

add_executable(add_two_numbers_server src/add_two_numbers_server.cpp)
target_link_libraries(add_two_numbers_server ${catkin_LIBRARIES})
add_dependencies(add_two_numbers_server math_gencpp)
2 changes: 2 additions & 0 deletions 210983/src/math/msg/number.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Header header
float64 num
33 changes: 33 additions & 0 deletions 210983/src/math/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<package format="2">
<name>math</name>
<version>0.1.0</version>
<description>The math package</description>

<maintainer email="sshivam21@iitk.ac.in">shivam</maintainer>

<license>BSD</license>

<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_filters</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>message_filters</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_filters</exec_depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>


<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->

</export>
</package>
20 changes: 20 additions & 0 deletions 210983/src/math/src/add_two_numbers_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "ros/ros.h"
#include "math/add_two_numbers.h"

bool add(math::add_two_numbers::Request &req, math::add_two_numbers::Response &res){
res.sum.num = req.num1.num + req.num2.num;
ROS_INFO("request: x=%f, y=%f", req.num1.num, req.num2.num);
ROS_INFO("sending back response: [%f]", res.sum.num);
return true;
}

int main(int argc, char **argv){
ros::init(argc, argv, "add_two_numbers_server");
ros::NodeHandle n;

ros::ServiceServer service = n.advertiseService("add_two_numbers", add);
ROS_INFO("Ready to add");
ros::spin();

return 0;
}
38 changes: 38 additions & 0 deletions 210983/src/math/src/input_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include "math/number.h"
#include "math/add_two_numbers.h"
#include<stdlib.h>

using namespace math;

int main(int arc, char ** argv){
ros::init(arc, argv, "input_node");
ros::NodeHandle n;
ros::Publisher topic1_pub = n.advertise<math::number>("topic1", 1000);
ros::Publisher topic2_pub = n.advertise<math::number>("topic2", 1000);
ros::ServiceClient client = n.serviceClient<math::add_two_numbers>("add_two_numbers");
math::add_two_numbers srv;

ros::Rate loop_rate(10);
while(ros::ok()){
math::number num1;
math::number num2;
num1.num = rand();
num2.num = rand();
srv.request.num1.num = num1.num;
srv.request.num2.num = num2.num;
ROS_INFO("Publishing %f to topic 1", num1.num);
topic1_pub.publish(num1);
ROS_INFO("Publishing %f to topic 2", num2.num);
topic2_pub.publish(num2);
if(client.call(srv)){
ROS_INFO("Sum: %f", srv.response.sum.num);
}
ros::spinOnce();
loop_rate.sleep();
}


return 0;
}
31 changes: 31 additions & 0 deletions 210983/src/math/src/output_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "ros/ros.h"
#include "std_msgs/Float64.h"
#include "math/number.h"
#include "message_filters/subscriber.h"
#include "message_filters/time_synchronizer.h"

// using namespace message_filters;
using namespace math;

ros::Publisher topic3_pubg;

void callback(const math::number::ConstPtr& num1, const math::number::ConstPtr& num2){
ROS_INFO("I'm publishing %f", (num1->num) + (num2->num));
math::number num3;
num3.num = (num1->num) + (num2->num);
topic3_pubg.publish(num3);
}

int main(int argc, char **argv){
ros::init(argc, argv, "output_node");
ros::NodeHandle n;
message_filters::Subscriber<math::number> topic1_sub(n, "topic1", 1);
message_filters::Subscriber<math::number> topic2_sub(n, "topic2", 1);
ros::Publisher topic3_pub = n.advertise<math::number>("topic3", 1000);
topic3_pubg = topic3_pub;
message_filters::TimeSynchronizer<math::number, math::number> sync(topic1_sub, topic2_sub, 10);
sync.registerCallback(boost::bind(&callback, _1, _2));
ros::spin();

return 0;
}
4 changes: 4 additions & 0 deletions 210983/src/math/srv/add_two_numbers.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
number num1
number num2
---
number sum