diff --git a/assignment_1/g4gekkouga_print_squares/print_squares/CMakeLists.txt b/assignment_1/g4gekkouga_print_squares/print_squares/CMakeLists.txt new file mode 100644 index 0000000..a8e64b6 --- /dev/null +++ b/assignment_1/g4gekkouga_print_squares/print_squares/CMakeLists.txt @@ -0,0 +1,35 @@ +cmake_minimum_required(VERSION 2.8.3) +project(print_squares) + + +find_package(catkin REQUIRED COMPONENTS + roscpp + rospy + std_msgs +) + + +catkin_package( +# INCLUDE_DIRS include +# LIBRARIES print_squares +# CATKIN_DEPENDS roscpp rospy std_msgs +# DEPENDS system_lib +) + + +include_directories( + include + ${catkin_INCLUDE_DIRS} +) + +add_executable(g4gekkouga_numbers src/g4gekkouga_numbers.cpp) +target_link_libraries(g4gekkouga_numbers ${catkin_LIBRARIES}) +add_dependencies(g4gekkouga_numbers beginner_tutorials_generate_messages_cpp) + +add_executable(g4gekkouga_squares src/g4gekkouga_squares.cpp) +target_link_libraries(g4gekkouga_squares ${catkin_LIBRARIES}) +add_dependencies(g4gekkouga_squares beginner_tutorials_generate_messages_cpp) + +add_executable(g4gekkouga_print src/g4gekkouga_print.cpp) +target_link_libraries(g4gekkouga_print ${catkin_LIBRARIES}) +add_dependencies(g4gekkouga_print beginner_tutorials_generate_messages_cpp) diff --git a/assignment_1/g4gekkouga_print_squares/print_squares/package.xml b/assignment_1/g4gekkouga_print_squares/print_squares/package.xml new file mode 100644 index 0000000..c607880 --- /dev/null +++ b/assignment_1/g4gekkouga_print_squares/print_squares/package.xml @@ -0,0 +1,27 @@ + + + print_squares + 0.1.0 + The print_squares package + + Amshumaan + + BSD + + catkin + roscpp + rospy + std_msgs + message_generation + roscpp + rospy + std_msgs + roscpp + rospy + std_msgs + message_runtime + + + + + diff --git a/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_numbers.cpp b/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_numbers.cpp new file mode 100644 index 0000000..43ea545 --- /dev/null +++ b/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_numbers.cpp @@ -0,0 +1,36 @@ +/* Publisher Node to Publish numbers from 1 onwards to the topic topic_numbers */ + +#include "ros/ros.h" +#include "std_msgs/Int64.h" //This is the type of message that is used for communation btw the nodes + +int main(int argc, char **argv) { + + ros::init(argc, argv, "g4gekkouga_numbers"); // + + ros::NodeHandle n; //access point for the communation with the system + + ros::Publisher topic_Numbers = n.advertise("topic_numbers", 1000); // Now topic_Numbers can be used to communicate with the ROS to publish to topic_numbers + + ros::Rate loop_rate(1); // Setting the number generation rate to 1HZ + + int count = 0; + + while (ros::ok()) { // returns false on ctrl C in the console + + count++; //Starting from 1 and all following numbers + + std_msgs::Int64 msg; // For publishing to the topic + msg.data = count; + + topic_Numbers.publish(msg); // Publish the message to topic topic_numbers + + ROS_INFO("Number Published : %d", msg.data); // Displays the number published to the topic in the correspoing console of this node + + loop_rate.sleep(); // To maintain the 1Hz Frequency rate + } + + return 0; +} + + + diff --git a/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_print.cpp b/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_print.cpp new file mode 100644 index 0000000..0128ce4 --- /dev/null +++ b/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_print.cpp @@ -0,0 +1,40 @@ +/* Take the msg from topic_numbers and topic_squares and prints them accordingly */ + +#include "ros/ros.h" +#include "std_msgs/Int64.h" ////This is the type of message that is used for communation btw the nodes + + +ros::Subscriber topic_Numbers ; // to suscribe to topic_numbers + +ros::Subscriber topic_Squares ; // to suscribe to topic_numbers + +void getSquare(const std_msgs::Int64::ConstPtr& msgr) { + + ROS_INFO("Received from topic_squares : %d \n", msgr->data); // as this fn is called when input received from topic_squares + + return ; + +} + +void getNum(const std_msgs::Int64::ConstPtr& msgr) { + + ROS_INFO("Received from topic_numbers : %d \n", msgr->data); // as this fn is called when input received from topic_numbers + + return ; +} + +int main(int argc, char **argv){ + + ros::init(argc, argv, "g4gekkouga_print"); + + ros::NodeHandle n; //access point for the communation with the system + + + topic_Numbers = n.subscribe("topic_numbers", 1000, getNum); // To receive msgs from topic_numbers + + topic_Squares = n.subscribe("topic_squares", 1000, getSquare); // To receive msgs from topic_squares + + ros::spin(); //required as there are callBacks to the functions + + return 0; +} \ No newline at end of file diff --git a/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_squares.cpp b/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_squares.cpp new file mode 100644 index 0000000..2feefc4 --- /dev/null +++ b/assignment_1/g4gekkouga_print_squares/print_squares/src/g4gekkouga_squares.cpp @@ -0,0 +1,41 @@ +/* Computes the square of the numbers published to topic_numbers and published them to another topic topic_squares */ + +#include "ros/ros.h" +#include "std_msgs/Int64.h" //This is the type of message that is used for communation btw the nodes + +ros::Publisher topic_Squares; // Made Global as to be used in callBack function also + +void squareCalc(const std_msgs::Int64::ConstPtr& msgr) { + + ROS_INFO("Number Received : %d \n", msgr->data); // Msg received from topic_numbers and displayed in corresponding terminal + + std_msgs::Int64 msgp; + msgp.data = (msgr->data)*(msgr->data); //new msg type var to store the computed square of the input + + topic_Squares.publish(msgp); // Publishing the computed square to topic_squares + +/* Similarly here also the publishing rate is at 1Hz as the function is +called at this rate and publishes at the same rate */ + + ROS_INFO("Number Published : %d \n", msgp.data); // Msg Published to topic_squares and displayed in corresponding terminal + + } + + int main(int argc, char **argv) { + + ros::init(argc, argv, "g4gekkouga_squares"); + + ros::NodeHandle n; //access point for the communation with the system + + topic_Squares = n.advertise("topic_squares", 1000); // Declared Globally and initializesd here to communicate with the ROS to publish to topic_squares + + ros::Subscriber topic_Numbers = n.subscribe("topic_numbers", 1000, squareCalc); // + +/* There is no need for maintaing frequency manually as the publishing rate to topic_numbers is 1Hz , + the callBack function will also take input at the rate of 1Hz */ + + + ros::spin(); // Required as there are callBacks as long as the _numbers node is publishing or Ctrl C is encountered + + return 0; +} \ No newline at end of file