ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

ROS1 to ROS2 MIGRATION ("Pythonic" ~/ros2ws/src/simple_node_pkg/simple_node_pkg/simple_node.py ):

#!/usr/bin/env python3

# First create Python ROS2 package:
# cd ~/ros2_ws/src        
# ros2 pkg create --build-type ament_python [--node-name simple_node] simple_node_pkg
# edit this file into ~/ros2ws/src/simple_node_pkg/simple_node_pkg/simple_node.py
# add entry point to ~/ros2ws/src/simple_node_pkg/setup.py
#     'console_scripts': [
#            'simple_node = simple_node_pkg.simple_node:main',

# source install/setup.bash
# ros2 run simple_node_pkg simple_node

import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class Simple_Node(Node):

    def __init__(self):
        super().__init__('simple_node')

        # Create a timer that will gate the node actions twice a second
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.node_callback)

    # 
    def node_callback(self):
        self.get_logger().info('simple_node is alive')



def main(args=None):
    rclpy.init(args=args)

    my_node = Simple_Node()   # instantiate my simple_node

    rclpy.spin(my_node)       # execute simple_node 

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    my_node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

my thoughts at least - I'm just learning ROS2

ROS1 to ROS2 MIGRATION ("Pythonic" ~/ros2ws/src/simple_node_pkg/simple_node_pkg/simple_node.py ):

#!/usr/bin/env python3

# First create Python ROS2 package:
# cd ~/ros2_ws/src        
# ros2 pkg create --build-type ament_python [--node-name simple_node] --node-name simple_node simple_node_pkg
# edit this file into ~/ros2ws/src/simple_node_pkg/simple_node_pkg/simple_node.py
# add entry point to ~/ros2ws/src/simple_node_pkg/setup.py
#     'console_scripts': [
#            'simple_node = simple_node_pkg.simple_node:main',

# source install/setup.bash
# ros2 run simple_node_pkg simple_node

import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class Simple_Node(Node):

    def __init__(self):
        super().__init__('simple_node')

        # Create a timer that will gate the node actions twice a second
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.node_callback)

    # 
    def node_callback(self):
        self.get_logger().info('simple_node is alive')



def main(args=None):
    rclpy.init(args=args)

    my_node = Simple_Node()   # instantiate my simple_node

    rclpy.spin(my_node)       # execute simple_node 

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    my_node.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

my thoughts at least - I'm just learning ROS2 ROS2