Example of a Package for ROS2 with Important Basic Principles
Zur Navigation springen
Zur Suche springen
The Python Programm/Script
The following source-code tries to be a basic example of a python-file in a ROS2-package. It would be stored in a workspace-folder such as ros2_ws/src/<package_name>/<package_name>/MyFirstPythonROSProgram.py [yes, there are 2 times folders with the <package_name>]
#!/usr/bin/env python # this specifies the interpreter of this script
# ros-core-library for standard ros2-functionality (this is the one for python)
import rclpy
# use ROS-nodes
from rclpy.node import Node
# create a Node "HeartbeatNode", that inherits all functionality from "Node"
class HeartbeatNode(Node):
def __init__(self, rover_name, timer_period=0.2):
# call super() in the constructor to initialize the Node object
# the parameter we pass is the node name
self._rover_name = rover_name
super().__init__(self._rover_name)
# create a timer sending two parameters:
# - the duration between two callbacks (0.2 seconds)
# - the timer function (timer_callback)
self.create_timer(timer_period, self.timer_callback)
def timer_callback(self):
ros_time_stamp = self.get_clock().now()
self.get_logger().info(self._rover_name +" is alive..."+str(ros_time_stamp))
# define the Standard-MAIN
def main(args=None):
# initialize the ROS2 communication
rclpy.init(args=args)
# declare the node constructor
node = HeartbeatNode(rover_name="mars_rover_1", timer_period=1.0)
# keeps the node alive, waits for a request to kill the node (ctrl+c)
# The spin() function is used to tell ROS2 to process and manage incoming callbacks within the given ROS2 node.
# spin() acts as as a highly efficient INFINITE WHILE LOOP
rclpy.spin(node)
# shutdown the ROS2 communication
rclpy.shutdown()
# define another MAIN2
def main2(args=None):
# initialize the ROS2 communication
rclpy.init(args=args)
# declare the node constructor
node = HeartbeatNode(rover_name="mars_rover_2", timer_period=1.0)
# keeps the node alive, waits for a request to kill the node (ctrl+c)
# The spin() function is used to tell ROS2 to process and manage incoming callbacks within the given ROS2 node.
# spin() acts as as a highly efficient INFINITE WHILE LOOP
rclpy.spin(node)
# shutdown the ROS2 communication
rclpy.shutdown()
# define another main_shutdown
def main_shutdown(args=None):
# initialize ROS2-communication and systems
rclpy.init(args=args)
# print message
print("Shutting down Mars rover 1...")
# close all connections
rclpy.shutdown()
# execute the main()-method above
if __name__ == '__main__':
main() # call the main function
Setup.py
This file is automatically created. We have to edit the fields marked with ######## text... ########
from setuptools import find_packages, setup
######## input the following two imports ########
import os
from glob import glob
package_name = 'mars_rover_systems'
setup(
name=package_name,
version='0.0.0',
packages=find_packages(exclude=['test']),
######## edit the last line of data_fields so that ros2 finds the launch-files ########
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name), glob('launch/*.launch.py'))
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='user',
maintainer_email='user@todo.todo',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
######## edit the executables here to enable ros2 to find them ########
entry_points={
'console_scripts': [
'heartbeat_executable = mars_rover_systems.heartbeat:main',
'heartbeat_executable2 = mars_rover_systems.heartbeat:main2',
'heartbeat_shutdown = mars_rover_systems.heartbeat:main_shutdown',
],
},
)