Преглед изворни кода

add a simple simulator node

Rodja Trappe пре 1 година
родитељ
комит
27799797eb

+ 1 - 1
examples/ros2/docker-compose.yml

@@ -7,4 +7,4 @@ services:
       - 8080:8080
     volumes:
       - ./ros2_ws/src:/ros2_ws/src
-    command: ros2 run gui nicegui_node
+    command: ros2 launch gui main_launch.py

+ 19 - 0
examples/ros2/ros2_ws/src/gui/launch/main_launch.py

@@ -0,0 +1,19 @@
+from launch import LaunchDescription
+from launch_ros.actions import Node
+
+
+def generate_launch_description():
+    return LaunchDescription([
+        Node(
+            package='gui',
+            executable='nicegui_node',
+            name='example_gui',
+            output='screen',
+        ),
+        Node(
+            package='simulator',
+            executable='simulator_node',
+            name='example_simulator',
+            output='screen',
+        ),
+    ])

+ 1 - 1
examples/ros2/ros2_ws/src/gui/package.xml

@@ -4,7 +4,7 @@
   <name>gui</name>
   <version>1.0.0</version>
   <description>This is an example of NiceGUI in a ROS2 node based that uses a joystick to control turtlesim.</description>
-  <maintainer email="nicegui@zauberzeug.com">jens</maintainer>
+  <maintainer email="nicegui@zauberzeug.com">Zauberzeug GmbH</maintainer>
   <license>MIT License</license>
   
   <depend>rclpy</depend>

+ 5 - 1
examples/ros2/ros2_ws/src/gui/setup.py

@@ -1,16 +1,20 @@
 import glob
 import os
+from pathlib import Path
 
 from setuptools import setup
 
 package_name = 'gui'
 
+data = Path('share') / package_name
 setup(
     name=package_name,
     version='1.0.0',
     packages=[package_name],
     data_files=[
-        ('share/' + package_name, ['package.xml']),
+        (str(data), ['package.xml']),
+        (str(data / 'launch'), ['launch/main_launch.py']),
+        # (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*')))
     ],
     install_requires=['setuptools'],
     zip_safe=True,

+ 16 - 0
examples/ros2/ros2_ws/src/simulator/package.xml

@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
+<package format="3">
+  <name>simulator</name>
+  <version>1.0.0</version>
+  <description>A very simple simulator which receives geometry_msgs/Twist messages and publishes geometry_msgs/Pose messages on the 'pose' topic.</description>
+  <maintainer email="nicegui@zauberzeug.com">Zauberzeug GmbH</maintainer>
+  <license>MIT License</license>
+  
+  <depend>rclpy</depend>
+  <depend>geometry_msgs</depend>
+
+  <export>
+    <build_type>ament_python</build_type>
+  </export>
+</package>

+ 4 - 0
examples/ros2/ros2_ws/src/simulator/setup.cfg

@@ -0,0 +1,4 @@
+[develop]
+script_dir=$base/lib/simulator
+[install]
+install_scripts=$base/lib/simulator

+ 25 - 0
examples/ros2/ros2_ws/src/simulator/setup.py

@@ -0,0 +1,25 @@
+import xml.etree.ElementTree as ET
+from pathlib import Path
+
+from setuptools import setup
+
+package_xml = ET.parse('package.xml').getroot()
+package_name = package_xml.find('name').text
+setup(
+    name=package_name,
+    version=package_xml.find('version').text,
+    packages=[package_name],
+    maintainer=package_xml.find('license').text,
+    maintainer_email=package_xml.find('maintainer').attrib['email'],
+    license=package_xml.find('license').text,
+    data_files=[
+        (str(Path('share') / package_name), ['package.xml']),
+    ],
+    install_requires=['setuptools'],
+    zip_safe=True,
+    entry_points={
+        'console_scripts': [
+            'simulator_node = simulator.node:main',
+        ],
+    },
+)

+ 0 - 0
examples/ros2/ros2_ws/src/simulator/simulator/__init__.py


+ 34 - 0
examples/ros2/ros2_ws/src/simulator/simulator/node.py

@@ -0,0 +1,34 @@
+import rclpy
+from geometry_msgs.msg import Pose, Twist
+from rclpy.node import Node
+
+
+class Simulator(Node):
+
+    def __init__(self):
+        super().__init__('simple_simulator')
+        self.pose_publisher_ = self.create_publisher(Pose, 'pose', 10)
+        self.subscription = self.create_subscription(
+            Twist,
+            'cmd_vel',
+            self.listener_callback,
+            10)
+
+    def listener_callback(self, msg):
+        pose = Pose()
+        pose.position.x += msg.linear.x
+        pose.position.y += 0
+        pose.orientation.z += msg.angular.z
+        self.pose_publisher_.publish(pose)
+
+
+def main(args=None):
+    rclpy.init(args=args)
+    simulator = Simulator()
+    rclpy.spin(simulator)
+    simulator.destroy_node()
+    rclpy.shutdown()
+
+
+if __name__ == '__main__':
+    main()