Could anyone explain to me the following commands. They seem really simple, but I get confused most of the times as to what exactly they are doing:

rate = rospy.Rate(2)



Same way in c++

ros::Rate loop_rate(2)




When do i use the '.'(dot operator)and ' :: '(double colon operator). With a clear understanding this I think my programming skills would be much better. Thanks in advance for your response.

asked 23 May, 13:21

eashwar.erahan's gravatar image

accept rate: 0%

Hi @eashwar.erahan,

I suppose you're looking at the Python and C++ courses at the same time. This is not necessary; you just need to choose one, depending on which language you know better (Python or C++).

NOTE: we now have a course on Basic Python on the Academy:

That said:

  • . is used in Python for accessing properties and methods of an object or class, while :: is used for the same purpose in C++.
  • rospy.spin(), ros::spin() and ros::spinOnce() are responsible for handling communication events, e.g. arriving messages. If you are subscribing messages, services or actions you must call spin() to process the events. While spinOnce() handles the events and returns immediately, spin() blocks until ROS invokes a shutdown

  • rate = rospy.Rate(2) and ros::Rate loop_rate(2) are used for creating Rate objects in Python and C++ respectively.

  • rate.sleep() and loop_rate::sleep() are used for implementing the set Rate value in Python and C++ respectively.

But what is this Rate about?

According to ROS Wiki, rospy.Rate and ros::Rate are convenience classes that make a best effort at maintaining a particular rate for a loop. For example:

ros::Rate r(10); // 10 hz while (ros::ok()) { ... do some work ... r.sleep(); }

In the above example, the Rate instance will attempt to keep the loop at 10hz by accounting for the time used by the work done during the loop.

References and further reading:

permanent link

answered 27 May, 11:48's gravatar image

accept rate: 52%

Thanks a lot

(27 May, 12:51) eashwar.erahan

Problem is I need to program in both the languages based on the Hardware I get to work on. Hence the reason i'm trying to do both. It would be really helpful if you can give a simple 3 day or 5 day course on C++ for ROS

(27 May, 14:32) eashwar.erahan

I see. We could do that, but that would take some time. But if you have any good resource on learning C++, it would help as well, as "C++ for ROS" should uses C++ just like any C++ package. Here is what I think you will find useful: For Python, that could be For the shell, it's

(27 May, 14:57) bayodesegun@...
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 23 May, 13:21

question was seen: 160 times

last updated: 27 May, 14:57

powered by OSQA