본문 바로가기
카테고리 없음

parameter(python)

by sonysame 2023. 11. 4.
  • 패키지 생성
ros2 pkg create --build-type ament_python python_parameters --dependencies rclpy

 

  • setup.py
entry_points={
        'console_scripts': [
        	'minimal_param_node = python_parameters.python_parameters_node:main',
        ],
    },

 

  • 코드
import rclpy
import rclpy.node

class MinimalParam(rclpy.node.Node):
    def __init__(self):
        super().__init__('minimal_param_node')

        self.declare_parameter('my_parameter', 'world')

        self.timer = self.create_timer(1, self.timer_callback)

    def timer_callback(self):
        my_param = self.get_parameter('my_parameter').get_parameter_value().string_value

        self.get_logger().info('Hello %s!' % my_param)

        my_new_param = rclpy.parameter.Parameter(
            'my_parameter',
            rclpy.Parameter.Type.STRING,
            'world'
        )
        all_new_parameters = [my_new_param]
        self.set_parameters(all_new_parameters)

def main():
    rclpy.init()
    node = MinimalParam()
    rclpy.spin(node)

if __name__ == '__main__':
    main()

 

  • Add ParameterDescriptor
from rcl_interfaces.msg import ParameterDescriptor
my_parameter_descriptor = ParameterDescriptor(description='This parameter is mine!')
self.declare_parameter('my_parameter', 'world', my_parameter_descriptor)

 

<빌드 & 실행>

cbp python_parameters

source install/setup.bash
ros2 run python_parameters minimal_param_node

source install/setup.bash
ros2 param set /minimal_param_node my_parameter earth