From 692e7084906414b2966ded663d0b731c75ab1908 Mon Sep 17 00:00:00 2001 From: Alex Davies Date: Thu, 19 Sep 2024 10:05:27 -0300 Subject: [PATCH] Python demo now runs --- copier.yml | 41 +++++++++++++------ src/CMakeLists.txt.jinja | 6 +++ src/Dockerfile.jinja | 9 +--- .../{{PACKAGE_NAME}} => }/package.xml.jinja | 2 +- .../main.py | 20 +++++++++ 5 files changed, 58 insertions(+), 20 deletions(-) rename src/{include/{{PACKAGE_NAME}} => }/package.xml.jinja (98%) create mode 100644 src/src/{%if 'Python' in LANGUAGES%}scripts{%endif%}/main.py diff --git a/copier.yml b/copier.yml index 29cece5..eface75 100644 --- a/copier.yml +++ b/copier.yml @@ -1,16 +1,33 @@ _subdirectory: src _answers_file: .copier/answers.service-ros1-catkin.yml -PACKAGE_NAME: myproject -PACKAGE_DESCRIPTION: The myproject package -MAINTAINER_NAME: Spiri -MAINTAINER_EMAIL: noreply@spirirobotics.com +PACKAGE_NAME: + default: myproject + type: str + help: The folder your package lives in. Please avoid spaces. + +PACKAGE_DESCRIPTION: + default: The {{PACKAGE_NAME}} package + type: str + multiline: true + +MAINTAINER_NAME: No Reply +MAINTAINER_EMAIL: "{{MAINTAINER_NAME|slugify}}@spirirobotics.com" LICENSE: BSD #We use a custom launch script to ensure that rosrun logs to console and have it not accidently launch the master node itself, which can be hard to diagnose -RUN_COMMAND: /launch myproject mycommand -#Source can be either a local folder or a git repo, if you path starts with `git+` -SOURCE: ./ -PROJECT_NAME: My sample project -#This just checks if it can contact the ROS master, ideally we'd check for something specific to your ROS node. -#This can be tricky when you set ROS node paramaters from environment variables or the like, you may need to write a shell script. -# I'll try to make auto-generating health checks a bit easier in the future. -HEALTHCHECK: --start-period=60s --start-interval=1s CMD /ros_entrypoint.sh rostopic list +LANGUAGES: + type: str + multiselect: true + help: Which langauges do you want to use? + choices: + - Python + - CPP + +RUN_COMMAND: rosrun {{PACKAGE_NAME}} {{"main.py" if 'Python' in LANGUAGES elif "CPP" in LANGAUGES "main"}} + +HEALTHCHECK: + defualt: --start-period=60s --start-interval=1s CMD /ros_entrypoint.sh rostopic list + type: str + help: | + This health check command should return 0 when your service is running properly. + The default healthcheck just checks to see if we can reach the ROS master. + Providing a good health check is left as an excersize to the reader. diff --git a/src/CMakeLists.txt.jinja b/src/CMakeLists.txt.jinja index fdc4bf0..6e60be7 100644 --- a/src/CMakeLists.txt.jinja +++ b/src/CMakeLists.txt.jinja @@ -192,6 +192,12 @@ include_directories( # DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} # ) +{%if "Python" in LANGUAGES%} +catkin_install_python(PROGRAMS scripts/main.py + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +) +{% endif %} + ############# ## Testing ## ############# diff --git a/src/Dockerfile.jinja b/src/Dockerfile.jinja index 9f51ce7..d8047b9 100644 --- a/src/Dockerfile.jinja +++ b/src/Dockerfile.jinja @@ -10,12 +10,7 @@ RUN apt-get install --yes python3-rosdep python3-rosinstall python3-rosinstall-g WORKDIR /root/catkin_ws/src -#Copy source into the docker image -{% if SOURCE.startswith("git+") %} -RUN git clone {{SOURCE}} -{% elif SOURCE %} -COPY {{SOURCE}} . -{% endif %} +COPY ./ . WORKDIR /root/catkin_ws RUN /bin/bash -c "source /opt/ros/noetic/setup.bash && rosdep init" @@ -30,4 +25,4 @@ RUN apt-get clean HEALTHCHECK {{HEALTHCHECK}} # Command to run your application -CMD {{RUN_COMMAND}} +CMD {{RUN_COMMAND}} --wait --screen diff --git a/src/include/{{PACKAGE_NAME}}/package.xml.jinja b/src/package.xml.jinja similarity index 98% rename from src/include/{{PACKAGE_NAME}}/package.xml.jinja rename to src/package.xml.jinja index 641bbd7..2528b52 100644 --- a/src/include/{{PACKAGE_NAME}}/package.xml.jinja +++ b/src/package.xml.jinja @@ -2,7 +2,7 @@ {{PACKAGE_NAME}} 0.0.0 - {{PACKAGE_DECRIPTION}} + {{PACKAGE_DESCRIPTION}} diff --git a/src/src/{%if 'Python' in LANGUAGES%}scripts{%endif%}/main.py b/src/src/{%if 'Python' in LANGUAGES%}scripts{%endif%}/main.py new file mode 100644 index 0000000..981b286 --- /dev/null +++ b/src/src/{%if 'Python' in LANGUAGES%}scripts{%endif%}/main.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +import rospy +from std_msgs.msg import String + +def talker(): + pub = rospy.Publisher('chatter', String, queue_size=10) + rospy.init_node('talker', anonymous=True) + rate = rospy.Rate(10) # 10hz + while not rospy.is_shutdown(): + hello_str = "hello world %s" % rospy.get_time() + rospy.loginfo(hello_str) + pub.publish(hello_str) + rate.sleep() + +if __name__ == '__main__': + try: + talker() + except rospy.ROSInterruptException: + pass