Question
Exercise 3: Perform the steps in exercise 3. Prepare a document with screenshots from your Linux terminal to showcase that the steps were successfully followed.
Exercise 3:
Perform the steps in exercise 3. Prepare a document with screenshots from your Linux terminal to showcase that the steps were successfully followed. Use THREE numbers instead of two numbers in the document.
The scope of the third exercise is to understand the ROS architecture, nodes, and a communication among nodes using service. A node will send two numbers, the other node will add the numbers and send the output to the client node. 1: (First time only) (2) Create the skeleton for ROS package Following segments changes based on the home works and directories; user@hostname$ cd ~/catkin_ws/src (a project based on rospy package package dependency) user@hostname$ catkin_create_pkg ex3 rospy user@hostname$ ls user@hostname$ cd ex3 user@hostname$ ls (list and check the files within the directory) (3) Changes to the package.xml Edit the packages.xml in the directory ~/catkin_ws/src/ex3 as follows:
(4) Changes to CMakeLists.txt file Edit the CMakeLists.txt in the directory ~/catkin_ws/src/ex3 as follows: find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation ) Uncomment and make the following change: add_service_files( FILES AddService.srv # Service2.srv ) ## Generate added messages and services with any dependencies listed here Uncomment the following: generate_messages( DEPENDENCIES std_msgs # Or other packages containing msgs ) Uncomment and add the following: catkin_package( # INCLUDE_DIRS include # LIBRARIES ex3 CATKIN_DEPENDS roscpp rospy std_msgs message_runtime # DEPENDS system_lib ) (5) Create the directory for python files (directory for creating python scripts, later we will create other directories as srv, msg) user@hostname$ cd ~/catkin_ws/src/ex3 user@hostname$ mkdir scripts user@hostname$ ls -al you should see the scripts directory
(4) Create the Python file as a node representing client Open an editor for Python (I use Geany ), and store the following file as client.py in the directory scripts. (make sure indentation are proper for Python programs when you copy/paste) #!/usr/bin/env python # client.py # rosrun ex3 client.py num1 num2 # code from http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28python%29 import sys import rospy from ex3.srv import AddService def add_two_ints_client(x, y): rospy.wait_for_service('add_service') try: add_two_ints = rospy.ServiceProxy('add_service', AddService) resp1 = add_two_ints(x, y) return resp1.sum except rospy.ServiceException as e: print("Service call failed: %s"%e) def usage(): return "%s [x y]"%sys.argv[0] if __name__ == "__main__": if len(sys.argv) == 3: x = int(sys.argv[1]) y = int(sys.argv[2]) else: print(usage()) sys.exit(1) print("Requesting %s+%s"%(x, y)) print("%s + %s = %s"%(x, y, add_two_ints_client(x, y))) The client file will be inside the folder ~/catkin_ws/src/ex3/scripts user@hostname$ cd ~/catkin_ws/src/ex3/scripts Make the script executable user@hostname$ chmod +x client.py (5) Create the Python file as a node representing server Open an editor for Python, and store the following file as server.py in the directory scripts. (make sure indentation are proper for Python programs when you copy/paste)
#!/usr/bin/env python # server.py # rosrun ex3 server.py import sys import rospy from ex3.srv import AddService def add_num(request): print "Received: ", request return {'sum': request.num1+request.num2} def myServer(): rospy.init_node('service_server') s = rospy.Service('add_service', AddService, add_num) rospy.spin() if __name__ == '__main__': try: myServer() except rospy.ROSInterruptException: pass (6) Compile and build the Nodes Python Files Both client.py and server.py should be in the directory ~/catkin_ws/src/ex3/scripts user@hostname$ cd ~/catkin_ws/src/ex3/scripts user@hostname$ ls user@hostname$ chmod +x server.py Compile the packages so ROS can use it: user@hostname$ cd ~/catkin_ws user@hostname$ catkin_make (7) Run the code One terminal to run roscore (main ROS application): Open a terminal and run the following: user@hostname$ roscore Second terminal 2nd window to run the client node. If you set the setup.bash properly you can use the roscd to get to the package directory; then to the scripts ( cd into scripts) directory. Use the following command to start the listener node. user@hostname$ roscd ex3
user@hostname$ cd scripts user@hostname$ rosrun ex3 client.py 5 10 Third window to run the talker node. user@hostname$ roscd ex3 user@hostname$ cd scripts user@hostname$ rosrun ex3 server.py Observe the outputs
Note: Show The Output Screenshot of each Step
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started