-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transport layer and the wrapped ROS middleware seem to working
- Loading branch information
Showing
4 changed files
with
101 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import rospy | ||
import sys | ||
import time | ||
import string | ||
import publishermanager as pm | ||
import json | ||
from rospy_message_converter import message_converter as mc | ||
from connection import Connection | ||
from geometry_msgs.msg import Point | ||
|
||
|
||
NODE_NAME = "jammi" | ||
|
||
class JammiNode(object): | ||
|
||
def __init__(self, host, port, name, topics): | ||
self.host = host | ||
self.port = port | ||
self.name = name | ||
self.conn = Connection(host, port, name) | ||
self.conn.start() | ||
self.subs = dict() | ||
self.topics = topics | ||
self.pub_man = pm.PublisherManager() | ||
|
||
def run(self): | ||
for topic, msg_type in self.topics: | ||
self.create_subscriber(topic, msg_type) | ||
r = rospy.Rate(30) | ||
while not rospy.is_shutdown(): | ||
updates = self.conn.updates() | ||
for v in updates.values(): | ||
self.pub_man.publish(v) | ||
r.sleep() | ||
|
||
|
||
def create_subscriber(self, topic, msg_type): | ||
namespace, msg_name = msg_type.split("/") | ||
mod = __import__(namespace + ".msg") | ||
msg_cls = getattr(mod.msg, msg_name) | ||
cb = self.create_callback(topic, msg_type) | ||
self.subs[topic] = rospy.Subscriber(topic, msg_cls, cb) | ||
return self | ||
|
||
def create_callback(self, topic, msg_type): | ||
def callback(msg): | ||
data = dict() | ||
data["to"] = "*" | ||
data["from"] = self.name | ||
data["topic"] = "/{}{}".format(self.name, topic) | ||
data["type"] = msg_type | ||
data["stamp"] = time.time() | ||
data["msg"] = mc.convert_ros_message_to_dictionary(msg) | ||
payload = json.dumps(data) | ||
self.conn.send_message(payload) | ||
return callback | ||
|
||
|
||
if __name__ == "__main__": | ||
rospy.init_node(sys.argv[1], anonymous=False) | ||
topics = [("/state", "geometry_msgs/Point")] | ||
jn = JammiNode("localhost", 9000, sys.argv[1], topics) | ||
jn.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import rospy | ||
import json | ||
from rospy_message_converter import message_converter | ||
|
||
|
||
class PublisherManager(object): | ||
|
||
def __init__(self): | ||
self.pubs = dict() | ||
|
||
def create_msg(self, msg_dict, msg_type): | ||
namespace, msg_name = msg_type.split("/") | ||
mod = __import__(namespace + ".msg") | ||
msg_cls = getattr(mod.msg, msg_name) | ||
msg = message_converter.convert_dictionary_to_ros_message( | ||
msg_type, msg_dict) | ||
return msg, msg_cls | ||
|
||
def publish(self, payload): | ||
data = json.loads(payload) | ||
msg, msg_cls = self.create_msg(data["msg"], data["type"]) | ||
topic = data["topic"] | ||
if not topic in self.pubs.keys(): | ||
self.pubs[topic] = rospy.Publisher(topic, msg_cls, queue_size=2) | ||
self.pubs[topic].publish(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters