node¶
The node module is the main interface with ROS. The root component must always inherit from the ROSNode class and client side widgets should generally inherit from ROSWidget to make interacting with the library smoother. The following example shows how one may structure a service client application using these techniques.
from flexx import flx, config
from flexxros import node
from flexxros.node import ROSNode, ROSWidget
import rospy
from std_srvs.srv import SetBool, SetBoolResponse
node_name = "action_example_interface"
service_name = "simple_service"
config.hostname = "localhost"
config.port = 8097
class SimpleService(object):
def __init__(self, service_name):
self.service = rospy.Service(service_name, SetBool, self.handle_request)
def handle_request(self, req):
return SetBoolResponse(req.data, "flexxros!")
class ServiceClientWidget(ROSWidget):
def callback(self, resp):
self.receive_message.set_text(str(resp.success) + " " + resp.message)
@flx.reaction("send_message.pointer_click")
def _request_service(self, *events):
self.call_service(service_name, "std_srvs/SetBool", {"data": self.type_message.checked}, self.callback)
def init(self):
with flx.FormLayout(flex=1, maxsize=400):
self.type_message = flx.CheckBox(title="Checked?")
self.send_message = flx.Button(text="Request service")
self.receive_message = flx.Label(title="Response:", text="Waiting for response...")
class ServiceExampleInterface(ROSNode):
def init(self):
self.client = ServiceClientWidget()
rospy.init_node(node_name, anonymous=True)
service = SimpleService(service_name)
node.spin(ServiceExampleInterface)
-
class
flexxros.node.
ROSNode
(*init_args, **property_values)[source]¶ A PyComponent interface that the root node in the interface must always inherit from
-
CSS
= ''¶
-
action_clients
= {}¶
-
announce_action_client
¶ action – Announce ROS action client, must be called before publish
Parameters: - server_name – the ROS action server name
- server_type – the ROS action server type in form of string, e.g. ‘flexxros/Test’
-
announce_publish
¶ action – Announce publisher, must be called before publish
Parameters: - topic – the ROS topic name
- topic_type – the ROS message type in form of string, e.g. ‘std_msgs/Float32’
-
announce_reconfig
¶ action – Experimental
-
call_service
¶ action – Call a ROS service
Parameters: - server_name – the ROS service name
- server_type – the ROS service type, e.g. std_srvs/SetBool
- req – a dictionary version of message, e.g. {data: 0.32}
-
init
()[source]¶ Initializer method. This method can be overloaded when creating a custom class. It is called with this component as a context manager (i.e. it is the active component), and it receives any positional arguments that were passed to the constructor.
-
publish
¶ action – Publish a message to a topic, must call announce_publish before this
Parameters: - topic – the ROS topic name
- data – a dictionary version of message, e.g. {data: 0.32}
-
publishers
= {}¶
-
reconfig_clients
= []¶
-
send_action_goal
¶ action – Send a ROS action goal, need to call announce_action_client before
Parameters: - server_name – the ROS action server name
- msg – a dictionary version of the action goal message, e.g. {data: 0.32}
-
service_clients
= {}¶
-
set_config
¶ action – Experimental
-
subscribe
¶ action – Subscribe to a topic
Parameters: - topic – the ROS topic name
- topic_type – the ROS message type in form of string, e.g. ‘std_msgs/Float32’
-
subscribers
= {}¶
-
-
class
flexxros.node.
ROSWidget
(*init_args, **kwargs)[source]¶ flx.Widget subclass that a widget can inherit from for easy access to ros functionality
-
CSS
= ''¶
-
announce_action_client
(topic, topic_type)¶ action – Announce ROS action client, must be called before publish
Parameters: - topic – the ROS action client name
- topic_type – the ROS action server type in form of string, e.g. ‘flexxros/Test’
-
announce_publish
(topic, topic_type)¶ action – Announce publisher, must be called before publish
Parameters: - topic – the ROS topic name
- topic_type – the ROS message type in form of string, e.g. ‘std_msgs/Float32’
-
call_service
(server_name, server_type, req, cb)¶ action – Call a ROS service
Parameters: - server_name – the ROS service name
- server_type – the ROS service type, e.g. std_srvs/SetBool
- req – a dictionary version of message, e.g. {data: 0.32}
- cb – a callback handle, must be a method of a subclass of ROSWidget that takes result as arguments
-
publish
(topic, data)¶ action – Publish a message to a topic, must call announce_publish before this
Parameters: - topic – the ROS topic name
- data – a dictionary version of message, e.g. {data: 0.32}
-
send_action_goal
(topic, goal, feedback_cb, done_cb)¶ action – Send a ROS action goal, need to call announce_action_client before
Parameters: - topic – the ROS action server name
- goal – a dictionary version of the action goal message, e.g. {data: 0.32}
- feedback_cb – a callback handle, must be a method of a subclass of ROSWidget that takes status, and feedback as arguments
- done_cb – a callback handle, must be a method of a subclass of ROSWidget that takes result as argument
-
subscribe
(topic, topic_type, cb)¶ action – Subscribe to a topic
Parameters: - topic – the ROS topic name
- topic_type – the ROS message type in form of string, e.g. ‘std_msgs/Float32’
- cb – a callback handle, must be a method of a subclass of ROSWidget
-
-
flexxros.node.
init_and_spin
(node_name, app_type)[source]¶ The main way of starting your flexxros app
Parameters: - node_name – the ROS node name
- app_type – the root widget, must inherit from ROSNode