ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 10 week HW (3) action programming : action server node
    ROS2 Study 2021. 11. 9. 17:48
    728x90
    반응형

    1. make package

     

    2. make python file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    import rclpy
     
    # import ActionServer class to make actionserver
    from rclpy.action import ActionServer
    from rclpy.node import Node
     
    # import Fibonacci interface type
    from my_interface_example.action import Fibonacci
     
    class FibonacciActionServer(Node):
     
        def __init__(self):
            # Initialize FibonacciActionServer class & make node name
            super().__init__('fibonacci_action_server')
            self._action_server = ActionServer(
                self,
                Fibonacci,
                'fibonacci',
                self.execute_callback)
     
        def execute_callback(self, goal_handle):
            self.get_logger().info('Executing goal...')
     
            sequence = [01]
     
            for i in range(1, goal_handle.request.order):
                sequence.append(sequence[i] + sequence[i-1])
     
            # succeed() function indicates the goal was successful
            goal_handle.succeed()
     
            result = Fibonacci.Result()
     
            # return the requested Fibonacci sequence
            result.sequence = sequence
            return result
     
    def main(args=None):
        rclpy.init(args=args)
     
        fibonacci_action_server = FibonacciActionServer()
     
        rclpy.spin(fibonacci_action_server)
     
    if __name__ == '__main__':
        main()
    cs


     

    3. play

    python은 인터프리터 언어로 빌드가 필요없다.

     

    현재까지의 코드는 피드백이 없는 상태

    피드백을 위해서는 코드를 수정해야 한다.

     

    4. add the feedback

    [revise code]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    import time
    import rclpy
     
    # import ActionServer class to make actionserver
    from rclpy.action import ActionServer
    from rclpy.node import Node
     
    # import Fibonacci interface type
    from my_interface_example.action import Fibonacci
     
    class FibonacciActionServer(Node):
     
        def __init__(self):
            # Initialize FibonacciActionServer class & make node name
            super().__init__('fibonacci_action_server')
            self._action_server = ActionServer(
                self,
                Fibonacci,
                'fibonacci',
                self.execute_callback)
     
        def execute_callback(self, goal_handle):
            self.get_logger().info('Executing goal...')
     
            # Define the feedback
            feedback_msg = Fibonacci.Feedback()
            feedback_msg.partial_sequence = [01]
     
            for i in range(1, goal_handle.request.order):
                feedback_msg.partial_sequence.append(
                    feedback_msg.partial_sequence[i] + feedback_msg.partial_sequence[i-1])
                self.get_logger().info('Feedback: {0}'.format(feedback_msg.partial_sequence))
     
                # publish Feedback msg
                goal_handle.publish_feedback(feedback_msg)
                time.sleep(1)
     
            # succeed() function indicates the goal was successful
            goal_handle.succeed()
     
            result = Fibonacci.Result()
     
            # return the requested Fibonacci sequence
            result.sequence = feedback_msg.partial_sequence
            return result
     
    def main(args=None):
        rclpy.init(args=args)
     
        fibonacci_action_server = FibonacciActionServer()
     
        rclpy.spin(fibonacci_action_server)
     
    if __name__ == '__main__':
        main()
    cs

     

    5. play

     

     

    next time we will add action client code

    728x90
    반응형

    댓글 0

Designed by Tistory.