diff --git a/240590/Readme.md b/240590/Readme.md new file mode 100644 index 0000000..51403ca --- /dev/null +++ b/240590/Readme.md @@ -0,0 +1,10 @@ +To run the assignment, you need to + +1. run the turtle and turtlesim using + `ros2 run turtlesim turtlesim_node` +2. run the `colcon build` command in `240590` folder +3. run `ros2 run turtle_pub circle_server` in `240590/` folder +4. install ros2 topics using `sudo apt install ros-humble-ros2topic` +5. Run the command using `os2 topic pub --once /draw_circle_params geometry_msgs/msg/Vector3 "{x: 5, y: 5, z: 2.0}"` + +This will run the desired output \ No newline at end of file diff --git a/240590/src/turtle_pub/package.xml b/240590/src/turtle_pub/package.xml new file mode 100644 index 0000000..68e1544 --- /dev/null +++ b/240590/src/turtle_pub/package.xml @@ -0,0 +1,17 @@ + + + + turtle_pub + 0.0.0 + TODO: Package description + lakshya + TODO: License declaration + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + + + ament_python + + diff --git a/240590/src/turtle_pub/resource/turtle_pub b/240590/src/turtle_pub/resource/turtle_pub new file mode 100644 index 0000000..e69de29 diff --git a/240590/src/turtle_pub/setup.cfg b/240590/src/turtle_pub/setup.cfg new file mode 100644 index 0000000..a84b69e --- /dev/null +++ b/240590/src/turtle_pub/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script_dir=$base/lib/turtle_pub +[install] +install_scripts=$base/lib/turtle_pub diff --git a/240590/src/turtle_pub/setup.py b/240590/src/turtle_pub/setup.py new file mode 100644 index 0000000..9cfad07 --- /dev/null +++ b/240590/src/turtle_pub/setup.py @@ -0,0 +1,32 @@ +from setuptools import find_packages, setup +import os +from glob import glob + +package_name = 'turtle_pub' + +setup( + name=package_name, + version='0.0.0', + packages=find_packages(exclude=['test']), + data_files=[ + ('share/ament_index/resource_index/packages', + ['resource/' + package_name]), + ('share/' + package_name, ['package.xml']), + ], + install_requires=['setuptools'], + zip_safe=True, + maintainer='lakshya', + maintainer_email='lakshyagarg911@gmail.com', + description='TODO: Package description', + license='TODO: License declaration', + extras_require={ + 'test': [ + 'pytest', + ], + }, + entry_points={ + 'console_scripts': [ + 'circle_server = turtle_pub.circle_server:main' + ], + }, +) diff --git a/240590/src/turtle_pub/test/test_copyright.py b/240590/src/turtle_pub/test/test_copyright.py new file mode 100644 index 0000000..97a3919 --- /dev/null +++ b/240590/src/turtle_pub/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/240590/src/turtle_pub/test/test_flake8.py b/240590/src/turtle_pub/test/test_flake8.py new file mode 100644 index 0000000..27ee107 --- /dev/null +++ b/240590/src/turtle_pub/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/240590/src/turtle_pub/test/test_pep257.py b/240590/src/turtle_pub/test/test_pep257.py new file mode 100644 index 0000000..b234a38 --- /dev/null +++ b/240590/src/turtle_pub/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' diff --git a/240590/src/turtle_pub/turtle_pub/__init__.py b/240590/src/turtle_pub/turtle_pub/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/240590/src/turtle_pub/turtle_pub/circle_server.py b/240590/src/turtle_pub/turtle_pub/circle_server.py new file mode 100644 index 0000000..1b6b998 --- /dev/null +++ b/240590/src/turtle_pub/turtle_pub/circle_server.py @@ -0,0 +1,77 @@ +import rclpy +from rclpy.node import Node +from geometry_msgs.msg import Twist, Vector3 +from turtlesim.srv import TeleportAbsolute +import math +import time + +class CircleSubscriberNode(Node): + def __init__(self): + super().__init__('circle_subscriber_node') + self.get_logger().info("Circle drawing subscriber is now active.") + + # Subscriber to get the circle parameters from cmd line when the user sends it + self.subscription = self.create_subscription( + Vector3, + 'draw_circle_params', + self.draw_circle_callback, + 10) + + # To control the turtle + self.publisher_ = self.create_publisher(Twist, '/turtle1/cmd_vel', 10) + + # teleport the turtle + self.teleport_client = self.create_client(TeleportAbsolute, '/turtle1/teleport_absolute') + while not self.teleport_client.wait_for_service(timeout_sec=1.0): + self.get_logger().info('Teleport service not available, waiting again...') + + def draw_circle_callback(self, msg): + center_x = msg.x + center_y = msg.y + radius = msg.z + + self.get_logger().info(f"Received request to draw a circle at ({center_x:.2f}, {center_y:.2f}) with radius {radius:.2f}") + + teleport_request = TeleportAbsolute.Request() + teleport_request.x = float(center_x) + teleport_request.y = float(center_y) + teleport_request.theta = float(math.pi / 2.0) + self.teleport_client.call_async(teleport_request) + + time.sleep(1) + + twist_msg = Twist() + twist_msg.linear.y = -radius + start_time = self.get_clock().now() + duration = 1 + while rclpy.ok() and (self.get_clock().now() - start_time).nanoseconds / 1e9 < duration: + self.publisher_.publish(twist_msg) + time.sleep(0.05) + + twist_msg = Twist() + twist_msg.linear.x = radius + twist_msg.angular.z = 1.0 # (1 rad/s) + + # 2*pi / omega + duration = 2 * math.pi / twist_msg.angular.z + start_time = self.get_clock().now() + + # Publish velocity commands + while rclpy.ok() and (self.get_clock().now() - start_time).nanoseconds / 1e9 < duration: + self.publisher_.publish(twist_msg) + time.sleep(0.05) + + stop_msg = Twist() + self.publisher_.publish(stop_msg) + self.get_logger().info("Circle drawing complete.") + + +def main(args=None): + rclpy.init(args=args) + node = CircleSubscriberNode() + rclpy.spin(node) + node.destroy_node() + rclpy.shutdown() + +if __name__ == '__main__': + main()