def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
def timer_callback(self):
msg = String()
msg.data = 'Hello World: %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1
There was a example publisher code on the ROS site and I tried to study it, but I was confused at the def __init__(self)
.
If you see the self.timer_callback
in def __init__(self)
part, the codes needs self.i
but the self.i
is bellow the self.timer
, and it works without errors. I don't know how it works.
Could anyone help me understanding this.
The only possible explanation is that the self.create_timer
doesn't call the callback self.timer_callback
instantly. It might wait for the timer_period
(or some other time) before it calls the callback the first time, that's why the line self.i = 0
executes before the self.timer_callback
executes for the first time.