ROS2 Simple Keyboard Publisher on Python
03 May 2018
|
ROS
Python
ROS2 Simple Keyboard Publisher on Python
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>simple_publisher</name>
<version>0.4.0</version>
<description>Simple Publisher on Python</description>
<maintainer email="snowdeer0314@gmail.com">snowdeer</maintainer>
<license>Apache License 2.0</license>
<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>simple_message</exec_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
setup.cfg
[develop]
script-dir=$base/bin/simple_publisher
[install]
install-scripts=$base/bin/simple_publisher
setup.py
from setuptools import setup
package_name = 'simple_publisher'
setup(
name=package_name,
version='0.4.0',
packages=[],
py_modules=[
'simple_publisher'],
install_requires=['setuptools'],
author='snowdeer ',
author_email='snowdeer0314@gmail.com',
maintainer='snowdeer',
maintainer_email='snowdeer0314@gmail.com',
keywords=['ROS'],
description='Simple Publisher on Python',
license='Apache License, Version 2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'simple_publisher = simple_publisher:main'
],
},
)
simple_publisher.py
import threading
import rclpy
import os
from simple_message.msg import SimpleMessage
NODE_NAME = "simple_publisher"
def handle_keyboard(publisher):
while True:
print('\n- Simple Publisher Menu -')
print(' 1. Command (Move along path 1)')
print(' 2. Command (Move along path 2)')
print(' 99. Exit')
menu = input('Input the menu: ')
if menu == '1':
msg = SimpleMessage()
msg.command_id = SimpleMessage.COMMAND_FOR_DEMO_1
publisher.publish(msg)
print(" \n>>> command is published : {0}".format(msg.command_id))
elif menu == '2':
msg = SimpleMessage()
msg.command_id = SimpleMessage.COMMAND_FOR_DEMO_2
publisher.publish(msg)
print(" \n>>> command is published : {0}".format(msg.command_id))
elif menu == '99':
rclpy.shutdown()
os._exit(1)
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node(NODE_NAME)
publisher = node.create_publisher(SimpleMessage, SimpleMessage.NAME)
th = threading.Thread(target=handle_keyboard, args=(publisher,))
th.start()
try:
rclpy.spin(node)
finally:
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()