- Node initialisation
At the beginning of your code, the first thing to be done is to initialize the node. This runs the necessary constructors required for the node to work.
rospy.init_node( <node_name>, <anonymous>, <log_level>, <disable_signals> )
This constructs the node object.
- <anonymous>, default=False
Sets the flag to append random numbers to the name. Only set to true if duplicates of this node is not an issue.
- <log_level>, default=rospy.INFO
Sets the level when publishing messages to rosout (similar to std::cout)
- DEBUG
- INFO
- WARN
- ERR
- FATAL
- <disable_signals>, default=False
Prevents Ctrl + C from working.
- Publishing
rospy.Publisher( <topic_name>, <msg_type>, <subscriber_listener>, <tcp_nodelay>, <latch>, <headers>, <queue_size> )
This constructs a publisher object for a specified topic.
- <msg_type>
The message class derived from the .msg file.
- <subscriber_listener> default=rospy.SubscribeListener
Sets the publisher as either only a publisher, subscriber or both.
- <tcp_nodelay> default=False
Removes any networking delay from the publisher for lower latency at the cost of efficiency.
- <latch> default=False
Enables latching on the connection which will send the last published message to any new subscribers.
- <headers> default=None (dictionary)
Add additional key-value-pairs to headers for future connections.
- <queue_size> default=None (int)
Sets the size of the outgoing message. If value is not set, the message may be dropped.
rospy.Publisher.publish( <msg_member_type> )
This publishes a message through the publisher object.
- <msg_member_type>
This is specified in the .msg file imported.
- Subscribing
rospy.Subscriber( <topic_name>, <msg_type>, <callback> )
This constructs a subscriber object for a specified topic.
- <msg_type>
The message class derived from the .msg file.
- <callback>
User defined function to be ran upon receiving a message. This can be a function name, a class method or a functor.
- Service
rospy.Service( <service_name>, <service_class>, <handler>, <buffer_size> )
This constructs a service provider object.
- <service_class>
The service class derived from the .srv file.
- <handler>
User defined class function to be ran upon receiving a request.
The return types are:
- None (failure)
- ServiceResponse
- tuple or list
- dictionary
- <buffer_size> default=65536
The size of the incoming messages. Data portions greater than <buffer_size> will be dropped.
rospy.ServiceProxy( <service_name>, <service_class>, <persistent>, <headers> )
This constructs a proxy functor to a specified service. To send a request to the service,
simply call the functor with the function call operator () with the arguments corresponding to the service class.
- <service_class>
The service class derived from the .srv file.
- <persistent> default=False
Keeps the client and server connected after the initial request. Increases fragility of the service.
- <headers> default=None
Additional meta-data before the request is sent. Can be used similar to web browser cookies.
rospy.wait_for_service( <service_name>, <timeout> )
This causes the node to wait until the specified service is available.
- <timeout> default=None
- Time & Delays
rospy.Time( <secs>, <nsecs> )
This function constructs a Time object.
- <secs> default=0 (int)
The seconds counter.
- <nsecs> default=0 (int)
The nano seconds counter.
rospy.Time.now( <empty> )
This function returns a Time object that contains two data members
- secs
The seconds counter.
- nsecs
The nano seconds counter.
rospy.Time.from_sec( <float_secs> )
This function converts the input float seconds representation into a new Time object with the corresponding seconds and nano seconds values.
- <float_secs>
Floating point representation of time in seconds (similar to Python's time.time() representation).
rospy.get_time( <empty> )
This function returns time in seconds in the floating point representation.
rospy.Duration( <secs> )
This function creates a Duration object for timing use.
- <secs> (int)
Time in seconds.
rospy.Duration.from_sec( <float_secs> )
This function converts seconds from a floating point representation into a Duration object with the corresponding values.
- <float_secs>
Seconds in floating point representation.
# 60.1 is 60 and 1 tenth of a second.
rospy.sleep( <duration> )
This function causes the ROS node to sleep for an amount of time.
- <duration> (float | Duration)
Duration to sleep for. Can be in float or a Duration object.
- Loops
rospy.Rate( <hz> )
This function creates a Rate object that is used for loops.
- <hz>
Number of cycles per second.
rospy.Timer( <period>, <callback>, <oneshot> )
This function creates a timer object that will run a user defined function after a certain amount of time.
- <period>
Period between calls.
- <callback>
The user defined function to be called.
- <oneshot> default=False
Specifies whether the timer fires only once.
rospy.spin( <empty> )
This blocks the program from shutting down and progressing until the ROS network initiates a shutdown or CTRL + C.
- Exceptions
ROSException
Base exception class for ROS clients.
ROSSerializationException
Exception for message serialization errors.
ROSInitException
Exception for errors initializing ROS state.
ROSInterruptException
Exception for operations that interrupted. This is most commonly used with rospy.sleep() and rospy.Rate.
ROSInternalException
Base class for exceptions that occur due to internal rospy errors (i.e. bugs).
ServiceException
Errors related to communicate with ROS Services.