"""
.. module: Secondnode
:platform: unix
:synopsis: Extracting the velocity and position
.. moduleauthor:: Ehsan Fatemi
This ROS node is to receive the Odometry messages
from the *"/odom"* topic, extract the position and
linear velocity data from the received message,
and publish it as a custom message of type
``custom_msg`` to the *"chatter"* topic.
Subscribes to:
/odom
Publishes to:
chatter
"""
import rospy
from nav_msgs.msg import Odometry
from rt_assignment.msg import custom_msg
#this function is called when the node receives a message from the topic of /odom
# it publishes the message of the topic of /odom as a custom message
[docs]def cl_bck(data):
"""
This function is the callback function that
is called each time a new Odometry message is
received on the *"/odom"* topic. The function
extracts position and linear velocity data from
the message and assigns it to the corresponding
fields of the ``custom_msg`` message. Then, it prints
the message to the console and publishes it to
the *"chatter"* topic using the publisher pub.
The velocity and position is published as ``custom_msg``
message, which is subscribed in :mod:`scripts.Fourthnode`
"""
# Initialize a publisher for the "chatter" topic with a custom message type "custom_msg"
pub = rospy.Publisher('chatter', custom_msg, queue_size=50)
# Create an instance of the custom message
message=custom_msg()
# Extract the position and linear velocity data from the received odometry message and assign it to the custom message
message.x = data.pose.pose.position.x
message.vel_x = data.twist.twist.linear.x
message.y = data.pose.pose.position.y
message.vel_y = data.twist.twist.linear.y
# Print the custom message
print(message)
# Publish the custom message
pub.publish(message)
# Initialize a new ROS node named 'secondnode' and create a Subscriber to listen to the "/odom" topic
# The Odometry message type is passed to the callback function which will
# be called everytime a new message is received on the topic
[docs]def Initialize():
"""
This function initializes the node with the
name "secondnode" using the *rospy.init_node()*
method. It also creates a subscriber to listen
to the *"/odom"* topic with the message type
*Odometry*
"""
rospy.init_node('Secondnode')
rospy.Subscriber("/odom", Odometry, cl_bck)
rospy.spin()
# main
if __name__ == '__main__':
Initialize()