diff --git a/assignment_1/basher666_print_squares/CMakeLists.txt b/assignment_1/basher666_print_squares/CMakeLists.txt new file mode 100644 index 0000000..0ff347f --- /dev/null +++ b/assignment_1/basher666_print_squares/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 2.8.3) +project(basher666_print_squares) + + +find_package(catkin REQUIRED COMPONENTS + roscpp + std_msgs +) + + +catkin_package() + +include_directories( + ${catkin_INCLUDE_DIRS} +) + + +include_directories(include ${catkin_INCLUDE_DIRS}) + +add_executable(basher666_numbers src/basher666_numbers.cpp) +target_link_libraries(basher666_numbers ${catkin_LIBRARIES}) + +add_executable(basher666_squares src/basher666_squares.cpp) +target_link_libraries(basher666_squares ${catkin_LIBRARIES}) + +add_executable(basher666_print src/basher666_print.cpp) +target_link_libraries(basher666_print ${catkin_LIBRARIES}) diff --git a/assignment_1/basher666_print_squares/package.xml b/assignment_1/basher666_print_squares/package.xml new file mode 100644 index 0000000..2044e04 --- /dev/null +++ b/assignment_1/basher666_print_squares/package.xml @@ -0,0 +1,20 @@ + + + basher666_print_squares + 0.0.0 + The basher666_print_squares package + + swastik + + MIT + + catkin + roscpp + std_msgs + roscpp + std_msgs + + + + + diff --git a/assignment_1/basher666_print_squares/src/basher666_numbers.cpp b/assignment_1/basher666_print_squares/src/basher666_numbers.cpp new file mode 100644 index 0000000..ffdacb7 --- /dev/null +++ b/assignment_1/basher666_print_squares/src/basher666_numbers.cpp @@ -0,0 +1,24 @@ +#include "ros/ros.h" +#include "std_msgs/Int64.h" + +int main(int argc,char **argv) +{ + ros::init(argc,argv,"basher666_numbers"); //initializing the node basher666_numbers + + ros::NodeHandle n; + + ros::Publisher num_pub=n.advertise("topic_numbers",1000); //setting the topic_numbers as the topic + ros::Rate loop_rate(1); //setting loop_rate as 1 hz + int count=1; + while(ros::ok()) + { + std_msgs::Int64 x; + x.data=count; + num_pub.publish(x); //publishing the data to the topic + ROS_INFO("%ld sent to topic_numbers\n",x.data); + ros::spinOnce(); + loop_rate.sleep(); + count++; + } + return 0; +} diff --git a/assignment_1/basher666_print_squares/src/basher666_print.cpp b/assignment_1/basher666_print_squares/src/basher666_print.cpp new file mode 100644 index 0000000..92b682b --- /dev/null +++ b/assignment_1/basher666_print_squares/src/basher666_print.cpp @@ -0,0 +1,27 @@ +#include "ros/ros.h" +#include "std_msgs/Int64.h" + +void sqcallback(const std_msgs::Int64::ConstPtr& sqr) //callback function for topic_squares +{ + ROS_INFO("%ld received from topic_squares",sqr->data); + return; +} + +void numcallback(const std_msgs::Int64::ConstPtr& num) //callback function for topic_numbers +{ + ROS_INFO("%ld received from topic_numbers",num->data); + return; +} + +int main(int argc,char **argv) +{ + ros::init(argc,argv,"basher666_print"); + ros::NodeHandle n; + ros::Rate loop_rate(1); //for 1 hz rate + + ros::Subscriber sub1=n.subscribe("topic_numbers",1000,numcallback); //subscribing to the topic_numbers + ros::Subscriber sub2=n.subscribe("topic_squares",1000,sqcallback); // subscribing to the topic_squares + ros::spin(); + loop_rate.sleep(); + return 0; +} diff --git a/assignment_1/basher666_print_squares/src/basher666_squares.cpp b/assignment_1/basher666_print_squares/src/basher666_squares.cpp new file mode 100644 index 0000000..21e1fd2 --- /dev/null +++ b/assignment_1/basher666_print_squares/src/basher666_squares.cpp @@ -0,0 +1,31 @@ +#include "ros/ros.h" +#include "std_msgs/Int64.h" + +ros::Publisher sq_pub; //creating the publisher object globally for convenience +void numberscallback(const std_msgs::Int64::ConstPtr& num) +{ + ROS_INFO("heard %ld from topic_numbers \n",num->data); + std_msgs::Int64 sq; + sq.data=(num->data)*(num->data); + + if(ros::ok()) //publishing the square only once for each number from topic_numbers + { + sq_pub.publish(sq); + ROS_INFO("sent %ld to topic_squares\n",sq.data); + ros::spinOnce(); + //loop_rate.sleep(); + } + +} +int main(int argc,char **argv) +{ + ros::init(argc,argv,"basher666_squares"); + ros::NodeHandle n; + ros::NodeHandle n2; + ros::Rate loop_rate(1); + sq_pub=n2.advertise("topic_squares",1000); //selecting topic_squares as the topic to publish + ros::Subscriber square=n.subscribe("topic_numbers",1000,numberscallback); //selecting topic_numbers as the topic to subscribe + ros::spin(); + + return 0; +}