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

assignment 1- Add sangeet259_print_squares #42

Open
wants to merge 1 commit into
base: master
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
209 changes: 209 additions & 0 deletions assignment_1/sangeet259_print_squares/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
cmake_minimum_required(VERSION 2.8.3)
project(sangeet259_print_squares)

## Add support for C++11, supported in ROS Kinetic and newer
# add_definitions(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)


## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a run_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

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

## Generate services in the 'srv' folder
add_service_files(
FILES
Square.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
)

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a run_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
LIBRARIES sangeet259_print_squares
CATKIN_DEPENDS message_runtime roscpp rospy std_msgs
DEPENDS system_lib
)

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

## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
${catkin_INCLUDE_DIRS}
)
add_executable(sangeet259_numbers src/sangeet259_numbers.cpp)
target_link_libraries(sangeet259_numbers ${catkin_LIBRARIES})
add_dependencies(sangeet259_numbers sangeet259_print_squares_generate_messages_cpp)

add_executable(sangeet259_squares src/sangeet259_squares.cpp)
target_link_libraries(sangeet259_squares ${catkin_LIBRARIES})
add_dependencies(sangeet259_squares sangeet259_print_squares_generate_messages_cpp)

add_executable(sangeet259_print src/sangeet259_print.cpp)
target_link_libraries(sangeet259_print ${catkin_LIBRARIES})
add_dependencies(sangeet259_print sangeet259_print_squares_generate_messages_cpp)

## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/sangeet259_print_squares.cpp
# )

## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/sangeet259_print_squares_node.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
# ${catkin_LIBRARIES}
# )

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark executables and/or libraries for installation
# install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_sangeet259_print_squares.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
1 change: 1 addition & 0 deletions assignment_1/sangeet259_print_squares/msg/Num.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int64 num
58 changes: 58 additions & 0 deletions assignment_1/sangeet259_print_squares/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<package>
<name>sangeet259_print_squares</name>
<version>0.0.0</version>
<description>The sangeet259_print_squares package</description>

<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">sangeet</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>


<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/sangeet259_print_squares</url> -->


<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="[email protected]">Jane Doe</author> -->


<!-- The *_depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use run_depend for packages you need at runtime: -->
<!-- <run_depend>message_runtime</run_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>message_runtime</run_depend>
<run_depend>rospy</run_depend>
<run_depend>std_msgs</run_depend>


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

</export>
</package>
44 changes: 44 additions & 0 deletions assignment_1/sangeet259_print_squares/src/sangeet259_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "ros/ros.h"
//ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system.
#include "std_msgs/Int32.h"
//This includes the std_msgs/Int32 message, which resides in the std_msgs package.
//This is a header generated automatically from the Int32.msg file in that package.

#include <sstream>


int main(int argc, char **argv)
{

ros::init(argc, argv, "sangeet259_numbers"); // Initiliase ROS. Specify the name of our node as sangeet259_numbers .


ros::NodeHandle n; //Create a handle to this process' node. This will actually do the initialization of the node.


ros::Publisher numbers_pub = n.advertise<std_msgs::Int32>("topic_numbers", 777);
//Tell the master that we are going to be publish a message of type std_msgs/Int32 on the topic_numbers topic.
//777 is just the size of buffer nothing special about the number, by_the_way I am a MSD fan :) !
ros::Rate loop_rate(1); // It will send messages at the rate of 1Hz.


int number = 1; //Initilise the numbers to be sent , with 1 , obviously !
while (ros::ok()) //Until ctrl+C is not pressed
{

std_msgs::Int32 num; // create a Int32 type message object with name num to be sent as message across the topic.

num.data = number; //set the data attribute of our num message 'the number we want to send'
number++; //Increment the number by 1 in each loop.



numbers_pub.publish(num); //Publish the number of the current loop to the topic "topic_numbers"

ros::spinOnce(); //Need to call this funtion often to allow ROS process incoming messages

loop_rate.sleep(); //Makes the loop to sleep for 1 second before publishing the next message !
}

return 0;
}
37 changes: 37 additions & 0 deletions assignment_1/sangeet259_print_squares/src/sangeet259_print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "ros/ros.h"
//ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system.
#include "std_msgs/Int32.h"
//This includes the std_msgs/Int32 message, which resides in the std_msgs package.
//This is a header generated automatically from the Int32.msg file in that package.

void callback_topic_numbers(const std_msgs::Int32::ConstPtr& num)
//callback function for topic_numbers
{
ROS_INFO("topic_numbers : [%d]\n ",num->data);
//ROS_INFO is a kind of printf function equivalent to stdout/stderr, prints the number in the terminal
}

void callback_topic_squares(const std_msgs::Int32::ConstPtr& square)
//callback function for topic_squares
{
ROS_INFO("topic_squares : [%d]\n",square->data);
//ROS_INFO is a kind of printf function equivalent to stdout/stderr, prints the square in the terminal
}


int main(int argc,char **argv)
{
ros::init(argc, argv, "sangeet259_print");
// Initiliase ROS. Specify the name of our node as sangeet259_print
ros::NodeHandle N1, N2;
//Create two nodes for this process handle to handle two subscriptions
ros::Rate loop_rate(1);
//1 Hertz
ros::Subscriber sub1 = N1.subscribe("topic_numbers", 777, callback_topic_numbers);
//Subscribes to the topic "topic_numbers" and calls the callback function "callback_topic_numbers" whenever a new message arrives.
loop_rate.sleep();
ros::Subscriber sub2 = N2.subscribe("topic_squares", 777, callback_topic_squares);
//Subscribes to the topic "topic_squares" and calls the callback function "callback_topic_squares" whenever a new message arrives.
ros::spin();
return 0;
}
32 changes: 32 additions & 0 deletions assignment_1/sangeet259_print_squares/src/sangeet259_squares.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "ros/ros.h"
//ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system.
#include "std_msgs/Int32.h"
//This includes the std_msgs/Int32 message, which resides in the std_msgs package.
//This is a header generated automatically from the Int32.msg file in that package.

void topic_numbers_Callback(const std_msgs::Int32::ConstPtr& msg)
{
int n;
ros::NodeHandle sq1; // Created our first handle for this Node
ros::Publisher square_pub = sq1.advertise<std_msgs::Int32>("topic_squares", 777);
//Advertise to master that we are going to publish to a topic "topic_squares" .
n = msg->data; //set the data part of msg to be equal to n .
std_msgs::Int32 square; //created a "square" object of std_msgs/Int32.
square.data = n*n; //set the data in square as square of n.
square_pub.publish(square); // Now, publish the square on the topic "topic_squares".
ros::spin();
}


int main(int argc, char **argv)
{
ros::init(argc, argv, "sangeet259_squares");
// Initiliase ROS. Specify the name of our node as sangeet259_squares
ros::NodeHandle sq2; //This creates a second handle for this process' node
ros::Subscriber sub = sq2.subscribe("topic_numbers", 777, topic_numbers_Callback);
//Subscribes to the topic "topic_numbers" and calls the callback function "topic_numbers_Callback" whenever a new message arrives.

ros::spin();// As you are receiving callbacks,
// therfore if you were to add a subscription into this application, and did not have ros::spinOnce() here, your callbacks would never get called
return 0;
}
3 changes: 3 additions & 0 deletions assignment_1/sangeet259_print_squares/srv/Square.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int64 a
---
int64 square