ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I have create a package (my_pkg) with two nodes, trying to reproduce your situation.. It is like that:
catkin_create_pkg my_pkg rospy std_msgs message_generation message_runtime
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(my_pkg)
find_package(catkin REQUIRED COMPONENTS
message_generation
message_runtime
rospy
std_msgs
)
add_message_files(
FILES
two_ints.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES my_pkg
# CATKIN_DEPENDS message_generation message_runtime rospy std_msgs
# DEPENDS system_lib
)
include_directories(
${catkin_INCLUDE_DIRS}
)
package.xml
<?xml version="1.0"?>
<package>
<name>my_pkg</name>
<version>0.0.0</version>
<description>The my_pkg package</description>
<maintainer email="user@todo.todo">user</maintainer>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>message_generation</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<run_depend>message_runtime</run_depend>
<run_depend>rospy</run_depend>
<run_depend>std_msgs</run_depend>
<export>
</export>
</package>
The message file msg/two_ints.msg
int16 a
int16 b
The publisher node (that publishes the two_ints message) scripts/publisher_two_ints.py:
#!/usr/bin/env python
import rospy
from my_pkg.msg import two_ints
def main():
rospy.init_node("two_ints_publisher")
pub = rospy.Publisher("two_ints", two_ints, queue_size=1)
r = rospy.Rate(1)
msg = two_ints()
while not rospy.is_shutdown():
msg.a = 2
msg.b = 15
pub.publish(msg)
r.sleep()
if __name__ == "__main__":
try:
main()
except rospy.ROSInterruptException:
pass
And finally, the one you are interested, the subscriber, which calculates the sum and publishes the result into the /sum topic: (subscriber_two_ints.py)
#!/usr/bin/env python
import rospy
from my_pkg.msg import two_ints
from std_msgs.msg import Int16
result = Int16()
def callback(msg):
result.data = msg.a + msg.b
rospy.loginfo(result)
def main():
rospy.init_node("two_ints_subscriber")
pub = rospy.Publisher("sum", Int16, queue_size=1)
sub = rospy.Subscriber("two_ints", two_ints, callback)
r = rospy.Rate(1)
while not rospy.is_shutdown():
pub.publish(result)
r.sleep()
if __name__ == "__main__":
try:
main()
except rospy.ROSInterruptException:
pass
Basically, you have a publisher and a publisher+subscriber. It's common to have a node with both, you have to handle the loop rate and the callback methods and be careful to not freeze inside your loop.
I hope it can help you!
2 | No.2 Revision |
I have create created a package (my_pkg) with two nodes, trying to reproduce your situation.. It is like that:
catkin_create_pkg my_pkg rospy std_msgs message_generation message_runtime
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(my_pkg)
find_package(catkin REQUIRED COMPONENTS
message_generation
message_runtime
rospy
std_msgs
)
add_message_files(
FILES
two_ints.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES my_pkg
# CATKIN_DEPENDS message_generation message_runtime rospy std_msgs
# DEPENDS system_lib
)
include_directories(
${catkin_INCLUDE_DIRS}
)
package.xml
<?xml version="1.0"?>
<package>
<name>my_pkg</name>
<version>0.0.0</version>
<description>The my_pkg package</description>
<maintainer email="user@todo.todo">user</maintainer>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>message_generation</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<run_depend>message_runtime</run_depend>
<run_depend>rospy</run_depend>
<run_depend>std_msgs</run_depend>
<export>
</export>
</package>
The message file msg/two_ints.msg
int16 a
int16 b
The publisher node (that publishes the two_ints message) scripts/publisher_two_ints.py:
#!/usr/bin/env python
import rospy
from my_pkg.msg import two_ints
def main():
rospy.init_node("two_ints_publisher")
pub = rospy.Publisher("two_ints", two_ints, queue_size=1)
r = rospy.Rate(1)
msg = two_ints()
while not rospy.is_shutdown():
msg.a = 2
msg.b = 15
pub.publish(msg)
r.sleep()
if __name__ == "__main__":
try:
main()
except rospy.ROSInterruptException:
pass
And finally, the one you are interested, the subscriber, which calculates the sum and publishes the result into the /sum topic: (subscriber_two_ints.py)
#!/usr/bin/env python
import rospy
from my_pkg.msg import two_ints
from std_msgs.msg import Int16
result = Int16()
def callback(msg):
result.data = msg.a + msg.b
rospy.loginfo(result)
def main():
rospy.init_node("two_ints_subscriber")
pub = rospy.Publisher("sum", Int16, queue_size=1)
sub = rospy.Subscriber("two_ints", two_ints, callback)
r = rospy.Rate(1)
while not rospy.is_shutdown():
pub.publish(result)
r.sleep()
if __name__ == "__main__":
try:
main()
except rospy.ROSInterruptException:
pass
Basically, you have a publisher and a publisher+subscriber. It's common to have a node with both, you have to handle the loop rate and the callback methods and be careful to not freeze inside your loop.
You can check the development, step-by-step in this video: https://youtu.be/H6bw5aw9mOQ
I hope it can help you!