Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 240590/Readme.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions 240590/src/turtle_pub/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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>turtle_pub</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">lakshya</maintainer>
<license>TODO: License declaration</license>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>

<export>
<build_type>ament_python</build_type>
</export>
</package>
Empty file.
4 changes: 4 additions & 0 deletions 240590/src/turtle_pub/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/turtle_pub
[install]
install_scripts=$base/lib/turtle_pub
32 changes: 32 additions & 0 deletions 240590/src/turtle_pub/setup.py
Original file line number Diff line number Diff line change
@@ -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='[email protected]',
description='TODO: Package description',
license='TODO: License declaration',
extras_require={
'test': [
'pytest',
],
},
entry_points={
'console_scripts': [
'circle_server = turtle_pub.circle_server:main'
],
},
)
25 changes: 25 additions & 0 deletions 240590/src/turtle_pub/test/test_copyright.py
Original file line number Diff line number Diff line change
@@ -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'
25 changes: 25 additions & 0 deletions 240590/src/turtle_pub/test/test_flake8.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions 240590/src/turtle_pub/test/test_pep257.py
Original file line number Diff line number Diff line change
@@ -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'
Empty file.
77 changes: 77 additions & 0 deletions 240590/src/turtle_pub/turtle_pub/circle_server.py
Original file line number Diff line number Diff line change
@@ -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()