Main Content

rosinit

Connect to ROS network

Description

rosinit starts the global ROS node with a default MATLAB® name and tries to connect to a ROS master running on localhost and port 11311. If the global ROS node cannot connect to the ROS master, rosinit also starts a ROS core in MATLAB, which consists of a ROS master, a ROS parameter server, and a rosout logging node.

Note

The first time you connect to a ROS network, you must install and setup Python.

  • From R2020b to R2021b, install Python® 2.7.

  • From R2022a or later, install Python 3.9.

To check your Python version in MATLAB and configure it for use with ROS Toolbox, see ROS Toolbox System Requirements.

example

rosinit(hostname) tries to connect to the ROS master at the host name or IP address specified by hostname. This syntax uses 11311 as the default port number.

example

rosinit(hostname,port) tries to connect to the host name or IP address specified by hostname and the port number specified by port.

rosinit(URI) tries to connect to the ROS master at the given resource identifier, URI, for example, "http://192.168.1.1:11311".

rosinit(___,Name,Value) provides additional options specified by one or more Name,Value pair arguments.

Using rosinit is a prerequisite for most ROS-related tasks in MATLAB because:

  • Communicating with a ROS network requires a ROS node connected to a ROS master.

  • By default, ROS functions in MATLAB operate on the global ROS node, or they operate on objects that depend on the global ROS node.

For example, after creating a global ROS node with rosinit, you can subscribe to a topic on the global ROS node. When another node on the ROS network publishes messages on that topic, the global ROS node receives the messages.

If a global ROS node already exists, then rosinit restarts the global ROS node based on the new set of arguments.

For more advanced ROS networks, connecting to multiple ROS nodes or masters is possible using the Node object.

example

Examples

collapse all

rosinit
Launching ROS Core...
Done in 0.46602 seconds.
Initializing ROS master on http://172.20.149.99:54819.
Initializing global node /matlab_global_node_09553 with NodeURI http://dcc786972glnxa64:38491/ and MasterURI http://localhost:54819.

When you are finished, shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_09553 with NodeURI http://dcc786972glnxa64:38491/ and MasterURI http://localhost:54819.
Shutting down ROS master on http://172.20.149.99:54819.
rosinit('192.168.17.128')
Initializing global node /matlab_global_node_57409 with NodeURI http://192.168.17.1:57782/

Shut down the ROS network when you are finished.

rosshutdown
Shutting down global node /matlab_global_node_57409 with NodeURI http://192.168.17.1:57782/
rosinit('192.168.17.128', 'NodeHost','192.168.17.1','NodeName','/test_node')
Initializing global node /test_node with NodeURI http://192.168.17.1:57633/

Shut down the ROS network when you are finished.

rosshutdown
Shutting down global node /test_node with NodeURI http://192.168.17.1:57633/

Input Arguments

collapse all

Host name or IP address, specified as a string scalar or character vector.

Port number used to connect to the ROS master, specified as a numeric scalar.

URI for ROS master, specified as a string scalar or character vector. Standard format for URIs is either http://ipaddress:port or http://hostname:port

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: "NodeHost","192.168.1.1"

Host name or IP address under which the node advertises itself to the ROS network, specified as the comma-separated pair consisting of "NodeHost" and a character vector.

Example: "comp-home"

Global node name, specified as the comma-separated pair consisting of "NodeName" and a character vector. The node that is created through rosinit is registered on the ROS network with this name.

Example: "NodeName","/test_node"

Version History

Introduced in R2019b