Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Perform the steps in exercise 2. Prepare a document with screenshots from your Linux terminal to showcase that the steps were successfully followed. Use THREE

Perform the steps in exercise 2. 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.

Exercise 2:

The scope of the second exercise is to understand the ROS architecture, nodes, and a communication among nodes using different message types. A node will send two numbers and a boolean, based on the boolean value, the other node will add or multiply the numbers. 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 ex2 rospy user@hostname$ ls user@hostname$ cd ex2 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/ex2 as follows: roscpp rospy std_msgs message_generation roscpp rospy std_msgs message_runtime (4) Create a msg file user@hostname$ cd ~/catkin_ws/src/ex2 user@hostname$ mkdir msg Create a file number_msg.msg with the following contents: string operation_name float32 number_1 float32 number_2 bool is_addition

(4) Changes to CMakeLists.txt file Edit the CMakeLists.txt in the directory ~/catkin_ws/src/ex2 as follows: find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs message_generation ) Uncomment and make the following change: add_message_files( FILES number_msg.msg # Message2.msg ) ## 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 ex2 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/ex2 user@hostname$ mkdir scripts user@hostname$ ls -al you should see the scripts directory

(4) Create the Python file as a node representing talker - Publisher Open an editor for Python (I use Geany ), and store the following file as talker.py in the directory scripts. (make sure indentation are proper for Python programs when you copy/paste) #!/usr/bin/env python # talker.py import rospy from std_msgs.msg import String from ex2.msg import number_msg # number_msg.msg: # number_msg is the name of our package. # The file is within a directory "msg". # number_msg: # Our actual message is saved as number_msg.msg, but we don't use the ".msg" extension here. import random def talker(): pub = rospy.Publisher('msg_ex', number_msg, queue_size=10) rospy.init_node('talker', anonymous=True) rate = rospy.Rate(1) # 1hz while not rospy.is_shutdown(): ''' string operation_name float32 number_1 float32 number_2 bool is_addition ''' myMessage = number_msg() myMessage.number_1 = random.random()*100 myMessage.number_2 = random.random()*100 k = random.randint(0, 1) if (k == 0): myMessage.operation_name = 'addition' myMessage.is_addition = True else: myMessage.operation_name = 'multiply' myMessage.is_addition = False rospy.loginfo(myMessage) pub.publish(myMessage) rate.sleep() if __name__ == '__main__': try: talker() except rospy.ROSInterruptException:

pass The talker file will be inside the folder ~/catkin_ws/src/ex2/scripts user@hostname$ cd ~/catkin_ws/src/ex2/scripts Make the script executable user@hostname$ chmod +x talker.py (5) Create the Python file as a node representing listener - Subscriber Open an editor for Python, and store the following file as listener.py in the directory scripts. (make sure indentation are proper for Python programs when you copy/paste) #!/usr/bin/env python # listener.py import rospy from std_msgs.msg import String from ex2.msg import number_msg def math_callback(data): ''' string operation_name float32 number_1 float32 number_2 bool is_addition ''' rospy.loginfo(rospy.get_caller_id() + " This is : %s", data.operation_name) if (data.is_addition): rospy.loginfo("\t Number_1: {0}; \t Number_2:{1}".format(data.number_1,data.number_2)) rospy.loginfo("\t Addition: %f", data.number_1 + data.number_2) else: rospy.loginfo("\t Number_1: {0}; \t Number_2:{1}".format(data.number_1,data.number_2)) rospy.loginfo("\t Multiply: %f", data.number_1 * data.number_2) def listener(): rospy.init_node('listener', anonymous=True) rospy.Subscriber("msg_ex", number_msg, math_callback) # spin() simply keeps python from exiting until this node is stopped rospy.spin() if __name__ == '__main__': listener()

(6) Compile and build the Nodes Python Files Both talker.py and listener.py should be in the directory ~/catkin_ws/src/ex1/scripts user@hostname$ cd ~/catkin_ws/src/ex2/scripts user@hostname$ ls user@hostname$ chmod +x listener.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 listener 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 ex2 user@hostname$ cd scripts user@hostname$ rosrun ex2 listener.py Third window to run the talker node. user@hostname$ roscd ex2 user@hostname$ cd scripts user@hostname$ rosrun ex2 talker.py Observe the outputs

<--------------------------------------------------->

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions