#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Example Python node to publish on a specific topic.
"""

# Import required Python code.
import rospy

# Give ourselves the ability to run a dynamic reconfigure server.
from dynamic_reconfigure.server import Server as DynamicReconfigureServer

# Import custom message data and dynamic reconfigure variables.
from node_example.msg import NodeExampleData
from node_example.cfg import nodeExampleConfig as ConfigType

class NodeExample(object):
    '''
    Node example class.
    '''
    def __init__(self):
        # Get the private namespace parameters from the parameter server:
        # set from either command line or launch file.
        rate = rospy.get_param('~rate', 1.0)
        rospy.loginfo('rate = %f', rate)
        # Create a dynamic reconfigure server.
        self.server = DynamicReconfigureServer(ConfigType, self.reconfigure_cb)
        # Create a publisher for our custom message.
        self.pub = rospy.Publisher('example', NodeExampleData, queue_size=10)
        # Initialize message variables.
        self.enable = rospy.get_param('~enable', True)
        self.int_a = rospy.get_param('a', 1)
        self.int_b = rospy.get_param('b', 2)
        self.message = rospy.get_param('~message', 'hello')

        # Create a timer to go to a callback. This is more accurate than
        # sleeping for a specified time.
        rospy.Timer(rospy.Duration(1 / rate), self.timer_cb)

        # Allow ROS to go to all callbacks.
        rospy.spin()

    def timer_cb(self, _event):
        """
        Called at a specified interval. Publishes message.
        """
        if not self.enable:
            return

        # Set the message to publish as our custom message.
        msg = NodeExampleData()
        # Fill in custom message variables with values updated from dynamic
        # reconfigure server.
        msg.message = self.message
        msg.a = self.int_a
        msg.b = self.int_b
        # Publish our custom message.
        self.pub.publish(msg)

    def reconfigure_cb(self, config, dummy):
        '''
        Create a callback function for the dynamic reconfigure server.
        '''
        # Fill in local variables with values received from dynamic reconfigure
        # clients (typically the GUI).
        self.message = config["message"]
        self.int_a = config["a"]
        self.int_b = config["b"]
        self.enable = config["enable"]
        # Return the new variables.
        return config

# Main function.
if __name__ == '__main__':
    # Initialize the node and name it.
    rospy.init_node('pytalker')
    # Go to class functions that do all the heavy lifting. Do error checking.
    try:
        NodeExample()
    except rospy.ROSInterruptException:
        pass

@@ -1116,16 +1116,139 @@
 , True)%0A
+        # Define int_a and int_b also as private variables%0A        # such that they can be specified from the command line%0A
         
@@ -1277,16 +1277,17 @@
 _param('
+~
 a', 1)%0A 
@@ -1323,16 +1323,17 @@
 _param('
+~
 b', 2)%0A 
@@ -1392,16 +1392,199 @@
 ello')%0A%0A
+        # Add ros log information display%0A        rospy.loginfo('a = %25d', self.int_a)%0A        rospy.loginfo('b = %25d', self.int_b)%0A        rospy.loginfo('message = %25s', self.message)%0A%0A
         
@@ -2190,21 +2190,259 @@
     
-msg.message =
+# Change message expressions such that they can be also specified using%0A        # $ rosparam set /pytalker/a 100%0A        # $ rosparam set /pytalker/b 200%0A        # $ rosparam set /pytalker/message %22Hi%22%0A        msg.message = rospy.get_param('~message',
  sel
@@ -2450,16 +2450,17 @@
 .message
+)
 %0A       
@@ -2467,16 +2467,38 @@
  msg.a =
+ rospy.get_param('~a',
  self.in
@@ -2500,16 +2500,17 @@
 lf.int_a
+)
 %0A       
@@ -2518,26 +2518,613 @@
 msg.b = 
-self.int_b
+rospy.get_param('~b', self.int_b)%0A%0A        # If changes are detected, display ros log informing the changes%0A        if self.message != msg.message:%0A            rospy.loginfo('new message = %25s', msg.message)%0A        if self.int_a != msg.a:%0A            rospy.loginfo('new a = %25d', msg.a)%0A        if self.int_b != msg.b:%0A            rospy.loginfo('new b = %25d', msg.b)%0A%0A        # Assign private variables to newly assigned values such that%0A        # the notification about change won't come up in every callback%0A        self.message = msg.message%0A        self.int_a = msg.a%0A        self.int_b = msg.b%0A
 %0A       

change NodeExample's a b to private, and msg.data accessible from CLI