"""
Implement communications from Basic-Robotics to ROS 1 and ROS 2.
This is a very basic implementation of communications, and may be slower than more tightly integrated methods.
This is as planned, as the intent is simply to provide a basic interface that can be used without much effort.
It is expected that those who would desire a more tight integration (such as multithreaded pub/sub) would know
how to enable such features on their own, and would have no need of this bridging module on its own.
"""
READY = False
try:
import rospy
import std_msgs.msg as r1msg
READY = True
except ImportError:
pass
try:
import rclpy
from rclpy.node import Node
import std_msgs.msg as r2msg
READY = True
except ImportError:
pass
[docs]
class ROSPub:
"""Create a new ROS Publisher Instance."""
[docs]
def __init__(self, name : str, node_type) -> 'ROSPub':
"""
Create a new ROS Publisher Instance.
Args:
name (str): Name of the publisher
node_type (Any): Datatype of the publisher
Returns:
ROSPub: New publisher instance.
"""
self.node_type = node_type
self.name = name
[docs]
def send(self, message) -> None:
"""
Send a message.
Args:
message (Any): Message data to send.
"""
self.publisher.publish(message)
def __eq__(self, o) -> bool:
"""
Test for equality between ROSPub instances.
Args:
o (Any): Other object
Returns:
bool: Equality boolean. True is equal.
"""
if o == None:
return False
if self.name == o:
return True
if isinstance(o, ROSPub):
if self.name == o.name:
return True
return False
[docs]
class ROS1Pub(ROSPub):
"""Create a new ROS1 Publisher Instance."""
[docs]
def __init__(self, name, node_type):
"""
Create a new ROS1 Publisher Instance.
Args:
name (str): Name of the publisher
node_type (Any): Datatype of the publisher
Returns:
ROS1Pub: New publisher instance.
"""
super().__init__(name, node_type)
self.publisher = rospy.Publisher(name, node_type)
[docs]
def send(self, message):
"""
Send a message.
Args:
message (Any): Message data to send.
"""
self.publisher.publish(message)
[docs]
class ROS2Pub(ROSPub):
"""Create a new ROS2 Publisher Instance."""
[docs]
def __init__(self, name : str, node_type, host_node : 'ROS2Bridge') -> 'ROS2Pub':
"""
Create a new ROS2 Publisher Instance.
Args:
name (str): Name of the publisher
node_type (Any): Datatype of the publisher
host_node (ROS2Bridge): Reference to the ROS2 Bridge Node host.
Returns:
ROS2Pub: New publisher instance.
"""
super().__init__(name, node_type)
self.host_node = host_node
self.publisher = host_node.create_publisher(node_type, name, host_node.r)
[docs]
def send(self, message) -> None:
"""
Send a message.
Args:
message (Any): Message data to send.
"""
new_message = self.node_type()
new_message.data = message
self.publisher.publish(new_message)
[docs]
class ROSSub:
"""Create a new ROS Subscriber Handle."""
[docs]
def __init__(self, name, node_type, host_node, call_back=None):
"""
Create a new ROS Subscriber Handle.
Args:
name (str): Name of the Subscription Node
node_type (Any): Message type of the subscription node
call_back (Any, optional): Callback function for subscriber. Defaults to None.
Returns:
ROSSub: New ROS2 Subscriber Handle.
"""
self.name = name
self.node_type = node_type
self.host_node = host_node
self.call_back_handle = call_back
if self.call_back_handle is not None:
self.setCallBack(self.call_back_handle)
self.mostRecent = None
[docs]
def setCallBack(self, call_back) -> None:
"""
Set callback function to external handle.
Args:
call_back (Any): External function handle.
"""
self.callBack = call_back
[docs]
def callBack(self, data) -> None:
"""
Execute default callback on most recent data.
Args:
data (Any): data from Ros subscriber.
"""
self.mostRecent = data.data
[docs]
def getUpdate(self):
"""
Get the most recent data.
Returns:
Any: Most recent data.
"""
return self.mostRecent
def __eq__(self, o) -> bool:
"""
Test for equality between ROSSub instances.
Args:
o (Any): Other object
Returns:
bool: Equality boolean. True is equal.
"""
if o == None:
return False
if self.name == o:
return True
if isinstance(o, ROSSub):
if self.name == o.name:
return True
return False
[docs]
class ROS1Sub(ROSSub):
"""Create a new ROS1 Subscriber Handle."""
[docs]
def __init__(self, name : str, node_type, call_back = None) -> 'ROS1Sub':
"""
Create a new ROS1 Subscriber Handle.
Args:
name (str): Name of the Subscription Node
node_type (Any): Message type of the subscription node
call_back (Any, optional): Callback function for subscriber. Defaults to None.
Returns:
ROS1Sub: New ROS2 Subscriber Handle.
"""
super().__init__(name, node_type, None, call_back)
self.subscriber = rospy.Subscriber(name, node_type, self.callBack)
[docs]
class ROS2Sub(ROSSub):
"""Create a new ROS2 Subscriber Handle."""
[docs]
def __init__(self, name : str, node_type,
host_node : 'ROS2Bridge', call_back = None) -> 'ROS2Sub':
"""
Create a new ROS2 Subscriber Handle.
Args:
name (str): Name of the Subscription Node
node_type (Any): Message type of the subscription node
host_node (ROS2Bridge): Instance of ROS2Bridge inheriting (NODE) properties.
call_back (Any, optional): Callback function for subscriber. Defaults to None.
Returns:
ROS2Sub: New ROS2 Subscriber Handle.
"""
super().__init__(name, node_type, host_node, call_back)
self.subscriber = host_node.create_subscription(
node_type, name, self.callBack, host_node.r)
[docs]
class Uplink:
"""Create a new Uplink from a function to a ROS topic."""
[docs]
def __init__(self, pub : ROSPub, call) -> 'Uplink':
"""
Create a new Uplink from a function to a ROS topic.
Args:
pub (ROSPub): Ros topic subscriber
call (Any): method call to populate
Returns:
Uplink: New Uplink
"""
self.pub = pub
self.call = call
[docs]
def update(self) -> None:
"""Update the uplink's internal publisher."""
msg = self.call()
self.pub.send(msg)
[docs]
class Downlink:
"""Create a downlink from a ROS topic to a function."""
[docs]
def __init__(self, sub : ROSSub, call) -> 'Downlink':
"""
Create a new downlink from a ROS topic to a function.
Args:
sub (ROSSub): Ros topic subscriber
call (Any): method call to populate
Returns:
Downlink: New Downlink
"""
self.sub = sub
self.call = call
self.sub.callBack = call
[docs]
def update(self):
"""
Update the downlink's internal subscriber.
Returns:
Any: Most recent data.
"""
msg = self.sub.mostRecent
self.call(msg)
return msg
[docs]
class ROSBridge:
"""Create a bridge between Basic Robotics and ROS."""
[docs]
def __init__(self, name = 'faserNode', rate = 10):
"""
Create a new ROSBridge instance.
Args:
name (str, optional): Name of this Bridge Node. Defaults to 'faserNode'.
rate (int, optional): Updates per second. Defaults to 10.
"""
super().__init__()
self.r = rate
self.pub_list = []
self.sub_list = []
self.updateables = []
[docs]
def newPub(self, name, node_type):
"""
Create a new Publisher handle.
Args:
name (str): Name of the publisher.
node_type (Any): Message type of the publisher.
Returns:
ROSPub: New Publisher node.
"""
pass
[docs]
def newSub(self, name, node_type, func):
"""
Create a new Subscriber handle.
Args:
name (str): Name of the subscriber
node_type (Any): Message type of the subscriber
func (Any): Function handler for subscriber to bind to
Returns:
ROSSub : New Subscriber Node
"""
pass
[docs]
def bindUplink(self, name : str, node_type, func) -> None:
"""
Bind a new uplink to the bridge, pulling data from a function and publishing over ROS.
Args:
name (str): Name of the upload handle.
node_type (Any): Datatype for the upload handle.
func (Any): Function handle to hold on to.
"""
new_uplink = None
if name in self.pub_list:
new_uplink = Uplink(self.pub_list[self.pub_list.index(name)], func)
else:
tpub = self.newPub(name, node_type)
self.pub_list.append(tpub)
new_uplink = Uplink(tpub, func)
self.updateables.append(new_uplink)
[docs]
def bindDownlink(self, name : str, node_type, func) -> None:
"""
Bind a new downlink to the bridge, pulling data from ROS and calling a function.
Args:
name (str): Name of download handle.
node_type (Any): Datatype for the download handle.
func (Any): Function to hold on to.
"""
new_downlink = None
if name in self.sub_list:
new_downlink = Downlink(self.sub_list[self.sub_list.index(name)], func)
else:
tsub = self.newSub(name, node_type, func)
self.sub_list.append(tsub)
new_downlink = Downlink(tsub, func)
self.updateables.append(new_downlink)
[docs]
def spin(self):
"""Start a continous update cycle."""
pass
try:
[docs]
class ROS2Bridge(Node, ROSBridge):
"""Create a bridge between Basic Robotics and ROS 2."""
[docs]
def __init__(self, name : str = 'faserNode', rate : int = 10):
"""
Create a new ROS2 Bridge instance.
Args:
name (str, optional): Name of this Bridge Node. Defaults to 'faserNode'.
rate (int, optional): Updates per second. Defaults to 10.
"""
super().__init__(name, rate)
[docs]
def newPub(self, name : str, node_type) -> ROS2Pub:
"""
Create a new Publisher handle.
Args:
name (str): Name of the publisher.
node_type (Any): Message type of the publisher.
Returns:
ROS2Pub: New Publisher node.
"""
return ROS2Pub(name, node_type, self)
[docs]
def newSub(self, name : str, node_type, func) -> ROS2Sub:
"""
Create a new Subscriber handle.
Args:
name (str): Name of the subscriber
node_type (Any): Message type of the subscriber
func (Any): Function handler for subscriber to bind to
Returns:
ROS2Sub : New Subscriber Node
"""
return ROS2Sub(name, node_type, self, func)
[docs]
def spin(self):
"""Start a continous update cycle."""
rclpy.spin(self)
rclpy.shutdown()
except:
pass
[docs]
class ROS1Bridge(ROSBridge):
"""Create a bridge between Basic Robotics and ROS 1."""
[docs]
def __init__(self, name : str = 'faserNode', rate : int = 10) -> ROSBridge:
"""
Create a new ROS1 Bridge instance.
Args:
name (str, optional): Name of this Bridge Node. Defaults to 'faserNode'.
rate (int, optional): Updates per second. Defaults to 10.
"""
super().__init__(name, rate)
[docs]
def newPub(self, name : str, node_type) -> ROS1Pub:
"""
Create a new Publisher handle.
Args:
name (str): Name of the publisher.
node_type (Any): Message type of the publisher.
Returns:
ROS1Pub: New Publisher node.
"""
return ROS1Pub(name, node_type)
[docs]
def newSub(self, name : str, node_type, func) -> ROS1Sub:
"""
Create a new Subscriber handle.
Args:
name (str): Name of the subscriber
node_type (Any): Message type of the subscriber
func (Any): Function handler for subscriber to bind to
Returns:
ROS1Sub : New Subscriber Node
"""
return ROS1Sub(name, node_type, func)
[docs]
def spin(self) -> None:
"""Start a continous update cycle."""
while True:
for item in self.updateables:
item.update()
self.r.sleep()
[docs]
def makeROSBridge(node_name : str,
node_rate : int = 10, ros_ver :int = 1, exargs = None) -> ROSBridge:
"""
Make a new ROS Bridge.
Args:
node_name (string): Name of this ROS node.
node_rate (int, optional): updates per second. Defaults to 10.
ros_ver (int, optional): ROS Bridge version to build. Defaults to 1.
exargs (Any, optional): Extra arguments for ROS2. Defaults to None.
Returns:
ROSbridge: New ROS Bridging Function
"""
if not READY:
print("Ros1 or Ros2 not available. Please check source, install, and rospy/rclpy install.")
return
if ros_ver == 1:
return ROS1Bridge(node_name, node_rate)
else:
rclpy.init(args=exargs)
return ROS2Bridge(node_name, node_rate)
[docs]
def getMsgHandle(ros_ver : int = 1):
"""
Get a handle to this ROS version's form of std_msg.msg.
Args:
ros_ver (int, optional): ROS Version. Defaults to 1.
Returns:
std_msg.msg: ROS message types
"""
if ros_ver == 1:
return r1msg
else:
return r2msg
[docs]
def getRosHandle(ros_ver :int = 1):
"""
Return a handle to the ROS python installation.
Args:
ros_ver (int, optional): ROS Version to return. Defaults to 1.
Returns:
Ros Handle : Handle to a ROS installation.
"""
if ros_ver == 1:
return rospy
else:
return rclpy