ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Some of the issues with your solution is that you're calling init_node() twice, your subscriber's scope is limited to the scope of the listener() function, your publisher's scope is limited to the scope of the talker() function, and your subscriber says that a function called callback() is the callback but you don't have a function called callback.

This is how it can be done in a very simple manner and I'll try to stick with the naming that you used and some inspiration from the subscriber/publisher tutorial. I'm going to give a high-level overview, but I can always update with more information.

# 1. Declare any libraries/messages that you need
import rospy

from std_msgs.msg import Int16
from project1_solution.msg import TwoInts

# 2. Define a callback in order to handle any incoming messages. This is where you process 
# the messages and is definitely needed. 
def callback(msg):
    sum = Int16()
    sum = msg.a + msg.b
    # Now, publish the message from the callback. I'll leave it for you to figure out how

# 3. This is where you will start your node, declare your publisher, 
# declare your subscriber, and spin()
def listener():
    # I'll leave this part up to you. Read the tutorial and see if you can finish