You are on page 1of 4

ROBOT PROGRAMMING LAB 3

NAME : VARSHINI J
REG NO: 21BRS1126

enter three input values a, b and c in publisher and print values


of (a+b+c)^2 through subscriber

PROCEDURE:
Use the vim terminal to enter the codes of publisher and subscriber and run them on
different terminals using python3 filename.
VIM:
:x and esc to save & exit
i to edit
esc to get back to normal
:q! to exit without saving

CODE:

PUBLISHER:

#!/usr/bin/env python3

import rospy
from std_msgs.msg import Float32,Int32

def publisher():
rospy.init_node('publisher_
pub = rospy.Publisher('sum_square', Float32, queue_size=10)
a = float(input("Enter value for a: "))
b = float(input("Enter value for b: "))
c = float(input("Enter value for c: "))

sum_squared = (a + b + c)**2

rospy.loginfo("Publishing: %s", sum_squared)


while not rospy.is_shutdown():
pub.publish(sum_squared)

if __name__ == '__main__':
try:
publisher()
except rospy.ROSInterruptException:
pass

SUBSCRIBER:

#!/usr/bin/env python3

import rospy
from std_msgs.msg import Float32,Int32

def callback(data):
rospy.loginfo("Received: %s", data.data)

def subscriber():
rospy.init_node('subscriber_
rospy.Subscriber('sum_square', Float32, callback)
rospy.spin()

if __name__ == '__main__':
subscriber()

publisher:
subscriber:

OUTPUT:
RESULT:
Thus the program was executed successfully.

You might also like