Difference between revisions of "Writing a Simpler Service and Client (C++)"
(Created page with "Close all previous terminals. 1) In a terminal, navigate to the beginner_tutorials folder: <nowiki>cd ~/catkin_ws/src/beginner_tutorials</nowiki> 2) Check to see if the sr...") |
m |
||
Line 24: | Line 24: | ||
'''Writing a Service Node''' | '''Writing a Service Node''' | ||
+ | |||
1) Navigate to the beginner_tutorials folder: | 1) Navigate to the beginner_tutorials folder: | ||
<nowiki>cd ~/catkin_ws/src/beginner_tutorials</nowiki> | <nowiki>cd ~/catkin_ws/src/beginner_tutorials</nowiki> | ||
Line 56: | Line 57: | ||
'''Writing a Client Node''' | '''Writing a Client Node''' | ||
+ | |||
1) Navigate to the beginner_tutorials folder: | 1) Navigate to the beginner_tutorials folder: | ||
<nowiki>cd ~/catkin_ws/src/beginner_tutorials</nowiki> | <nowiki>cd ~/catkin_ws/src/beginner_tutorials</nowiki> | ||
Line 95: | Line 97: | ||
'''Building your Nodes''' | '''Building your Nodes''' | ||
+ | |||
1) Navigate to the beginner_tutorials folder: | 1) Navigate to the beginner_tutorials folder: | ||
<nowiki>~/catkin_ws/src/beginner_tutorials</nowiki> | <nowiki>~/catkin_ws/src/beginner_tutorials</nowiki> |
Revision as of 11:19, 22 October 2014
Close all previous terminals. 1) In a terminal, navigate to the beginner_tutorials folder:
cd ~/catkin_ws/src/beginner_tutorials
2) Check to see if the srv or msg folders exist:
ls
srv will be listed on the terminal if it exists.
3) If the srv or msg folders exist, use Home Folder and delete either or both folders.
4) Re-create the folder srv:
mkdir srv
5) Re-create the folder msg:
mkdir msg
5) Make the following file:
roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
This should create the file AddTwoInts.srv in the srv folder.
6) Create the following file:
echo "int64 num" > msg/Num.msg
This creates a copy of int64 in the msg folder and names it Num.msg
Writing a Service Node
1) Navigate to the beginner_tutorials folder:
cd ~/catkin_ws/src/beginner_tutorials
2) Create the file add_two_ints_server.cpp:
gedit src/add_two_ints_server.cpp
Inside the editor, copy and paste the following:
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" bool add(beginner_tutorials::AddTwoInts::Request &req, beginner_tutorials::AddTwoInts::Response &res) { res.sum = req.a + req.b; ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b); ROS_INFO("sending back response: [%ld]", (long int)res.sum); return true; } int main(int argc, char **argv) { ros::init(argc, argv, "add_two_ints_server"); ros::NodeHandle n; ros::ServiceServer service = n.advertiseService("add_two_ints", add); ROS_INFO("Ready to add two ints."); ros::spin(); return 0; }
Save the file and close the editor. For information on the code, see this link
Writing a Client Node
1) Navigate to the beginner_tutorials folder:
cd ~/catkin_ws/src/beginner_tutorials
2) Create the file add_two_ints_client.cpp:
gedit src/add_two_ints_client.cpp
Inside the editor, copy and paste the following:
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" #include <cstdlib> int main(int argc, char **argv) { ros::init(argc, argv, "add_two_ints_client"); if (argc != 3) { ROS_INFO("usage: add_two_ints_client X Y"); return 1; } ros::NodeHandle n; ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints"); beginner_tutorials::AddTwoInts srv; srv.request.a = atoll(argv[1]); srv.request.b = atoll(argv[2]); if (client.call(srv)) { ROS_INFO("Sum: %ld", (long int)srv.response.sum); } else { ROS_ERROR("Failed to call service add_two_ints"); return 1; } return 0; }
Save the file and close the editor. For information on the code, see this link
Building your Nodes
1) Navigate to the beginner_tutorials folder:
~/catkin_ws/src/beginner_tutorials
2) Edit the CMakeLists.txt file:
gedit CMakeLists.txt
Erase the CMakeLists file contents and paste in the contents from this link
3) Navigate to your workspace:
cd ~/catkin_ws
4) Build your package:
catkin_make