commit 96afe5b656d6313e7b3cb773ba7e0393e1085dda Author: Diogo Peralta Cordeiro Date: Sun Oct 2 18:17:20 2022 +0100 Add solution for Home Work 1 of Intelligent Robotics Prompt: Make a program that makes the rosturtle in turtlesim run at velocity=v=0.5 m/s and turn with angular_velocity=ω=t/100 where t is time (in seconds); don't worry about the units too much. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1345657 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/ +install/ +log/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7a3094a --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +Version 2, December 2004 + +Copyright (C) 2004 Sam Hocevar + +Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. + +DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/README.md b/README.md new file mode 100644 index 0000000..42febfb --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +## Home Work 1 +Make a program that makes the rosturtle in turtlesim run at velocity=v=0.5 m/s and turn with angular_velocity=ω=t/100 where t is time (in seconds); don't worry about the units too much. + +[Solution](ros2_turtlesim_shapes/publisher_member_function.py) diff --git a/launch/turtlesim_draw.launch.py b/launch/turtlesim_draw.launch.py new file mode 100644 index 0000000..416dbbf --- /dev/null +++ b/launch/turtlesim_draw.launch.py @@ -0,0 +1,17 @@ +from launch import LaunchDescription +from launch_ros.actions import Node + +def generate_launch_description(): + return LaunchDescription([ + Node( + package='turtlesim', + executable='turtlesim_node' + ), + Node( + package='ros2_turtlesim_shapes', + executable='talker', + name='cmd_vel_publisher', + output='screen' + ), + + ]) diff --git a/package.xml b/package.xml new file mode 100644 index 0000000..1782fc5 --- /dev/null +++ b/package.xml @@ -0,0 +1,22 @@ + + + + ros2_turtlesim_shapes + 0.0.0 + Simple package to draw an ellipse with ROS turtle. + Diogo Peralta Cordeiro + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + + rclpy + std_msgs + ros2launch + + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + + + ament_python + + diff --git a/resource/ros2_turtlesim_shapes b/resource/ros2_turtlesim_shapes new file mode 100644 index 0000000..e69de29 diff --git a/ros2_turtlesim_shapes/__init__.py b/ros2_turtlesim_shapes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ros2_turtlesim_shapes/publisher_member_function.py b/ros2_turtlesim_shapes/publisher_member_function.py new file mode 100644 index 0000000..09e6523 --- /dev/null +++ b/ros2_turtlesim_shapes/publisher_member_function.py @@ -0,0 +1,70 @@ +import rclpy +from rclpy.node import Node +from geometry_msgs.msg import Twist + +from turtlesim.srv import TeleportAbsolute +from turtlesim.srv import SetPen + +delta_t = 0.1 +v = 0.5 +t = 0 + + +def robot_angular_v(tt): + return tt / 100 + + +def timed_callback(): + global t, node, pub + t += delta_t + + w = robot_angular_v(t) + + msg = Twist() + msg.linear.x = v + msg.angular.z = w + pub.publish(msg) + + +def main(args=None): + rclpy.init(args=args) + global node, pub + + node = Node('my_favourite_turtle') + + cli = node.create_client(SetPen, "/turtle1/set_pen") + req = SetPen.Request() + req.off = True + while not cli.wait_for_service(timeout_sec=1.0): + node.get_logger().info('1: set_pen service not available, waiting again...') + future = cli.call_async(req) + rclpy.spin_until_future_complete(node, future) + + cli = node.create_client(TeleportAbsolute, "/turtle1/teleport_absolute") + req = TeleportAbsolute.Request() + req.x = 0.0 + req.y = 0.0 + req.theta = 0.0 + while not cli.wait_for_service(timeout_sec=1.0): + node.get_logger().info('2: teleport_absolute service not available, waiting again...') + future = cli.call_async(req) + rclpy.spin_until_future_complete(node, future) + + cli = node.create_client(SetPen, "/turtle1/set_pen") + req = SetPen.Request() + req.off = False + req.r = 255 + while not cli.wait_for_service(timeout_sec=1.0): + node.get_logger().info('3: set_pen service not available, waiting again...') + future = cli.call_async(req) + rclpy.spin_until_future_complete(node, future) + + pub = node.create_publisher(Twist, '/turtle1/cmd_vel', 10) + node.create_timer(delta_t, timed_callback) + + rclpy.spin(node) + rclpy.shutdown() + + +if __name__ == '__main__': + main() diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b8501c7 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script_dir=$base/lib/ros2_turtlesim_shapes +[install] +install_scripts=$base/lib/ros2_turtlesim_shapes diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7f84884 --- /dev/null +++ b/setup.py @@ -0,0 +1,29 @@ +from setuptools import setup +import os +from glob import glob + +package_name = 'ros2_turtlesim_shapes' + +setup( + name=package_name, + version='0.0.0', + packages=[package_name], + data_files=[ + ('share/ament_index/resource_index/packages', + ['resource/' + package_name]), + ('share/' + package_name, ['package.xml']), + (os.path.join('share', package_name, 'launch'), glob('launch/*.py')), + ], + install_requires=['setuptools'], + zip_safe=True, + maintainer='Diogo Peralta Cordeiro', + maintainer_email='mail@diogo.site', + description='Simple package to draw an ellipse with ROS turtle.', + license='DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE', + tests_require=['pytest'], + entry_points={ + 'console_scripts': [ + 'talker = ros2_turtlesim_shapes.publisher_member_function:main', + ], + }, +) diff --git a/test/test_copyright.py b/test/test_copyright.py new file mode 100644 index 0000000..97a3919 --- /dev/null +++ b/test/test_copyright.py @@ -0,0 +1,25 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_copyright.main import main +import pytest + + +# Remove the `skip` decorator once the source file(s) have a copyright header +@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.') +@pytest.mark.copyright +@pytest.mark.linter +def test_copyright(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors' diff --git a/test/test_flake8.py b/test/test_flake8.py new file mode 100644 index 0000000..27ee107 --- /dev/null +++ b/test/test_flake8.py @@ -0,0 +1,25 @@ +# Copyright 2017 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_flake8.main import main_with_errors +import pytest + + +@pytest.mark.flake8 +@pytest.mark.linter +def test_flake8(): + rc, errors = main_with_errors(argv=[]) + assert rc == 0, \ + 'Found %d code style errors / warnings:\n' % len(errors) + \ + '\n'.join(errors) diff --git a/test/test_pep257.py b/test/test_pep257.py new file mode 100644 index 0000000..b234a38 --- /dev/null +++ b/test/test_pep257.py @@ -0,0 +1,23 @@ +# Copyright 2015 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ament_pep257.main import main +import pytest + + +@pytest.mark.linter +@pytest.mark.pep257 +def test_pep257(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found code style errors / warnings'