Let's say you know how a function call works as in the following example:
int main(int argc, char **argv) {
ros::Rate loop(50);
while(ros::ok()) {
callmyfunction(); // <= do that until the end of the world or ros kaputt
loop.sleep();
}
}
where
void callmyfunction() {
// function body
}
is your function defined somewhere in your code. That's the most usual way
Ok, now let's say you have a situation where you need to do other things in your main while() or for some reasons you don't want to call that function every loop (maybe because it has another rate or frequency). An example could be a function that publishes points to RViz. It does every 0.5 seconds but yur main routine (your robot) works much more faster (with a frequency of 100 Hz for istance).
In this case you can use timers and your code changes like following:
ros::Timer my_desired_frequency;
int main(int argc, char **argv) {
my_desired_frequency = nh_.createTimer( ros::Duration(2.0), callmyfunction); // Automatically calls every 2.0 seconds the function
ros::Rate loop(50);
while(ros::ok()) {
// Here the main code for your Terminator T1000
/* callmyfunction(); */ // Not needed anymore since it runs indipendently
ros::spinOnce; // NEEDED since callbacks for timers, publishers and listeners must be executed
loop.sleep();
}
}
void callmyfunction(const ros::TimerEvent& event) { // <= New signature here... for the same function
// function body same as above
}
Using that timer the function is going to be called indipendently and at another (or you want the same) frequency than your main routine. It doesn't need to be with the main synchronized. Keep it simple: you can think it is running in background and you don't need to call the function directly in your code.
UPDATE: this is not the only way to use a timer. You can create a timer as a member function/variable in a class or as a functor. For the right syntax can you check the following link and ask if yu have questions.
Hope my explanation helps.
Regards
Which timer and which tutorial do you mean?