For this example, we will use ROS Standart String messages to link a NEP Publisher with ROS. We will define a bridge script that creates a NEP Subscriber and ROS Publisher.
Main idea behind connecting NEP Publishers and ROS Subscribers in a direct method
The code of the bridge script is shown below.
Using correctly rospy.Rate will require more complex code, we will update an example soon.
Bridge ROS Publishers to NEP Subscribers in Python
import nep
import time
import rospy
from std_msgs.msg import String
ros_pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
# Create a new nep node
node = nep.node("receiver")
# Set a direct connection using port <3000> and in <'one2many'> mode
# Important: You need to change the IP address <'127.0.0.1'> by
# the IP address of you PC running the publisher node
conf = node.direct(ip = "127.0.0.1", port = 3000, mode ="one2many")
# Create a new nep subscriber with the topic <'test'>
sub = node.new_sub("test_subcriber", "json", conf)
while True:
# Read data in a non-blocking mode
s, msg = sub.listen()
# if s == True, then there is data in the socket
if s:
print(msg)
# msg is a python dictionary
# we convert the value of msg["result"] to string
ros_pub.publish(str(msg["result"]))
else:
# An small sleep will reduce computational load
time.sleep(.0001)