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
Empty file.
42 changes: 42 additions & 0 deletions Shivang/node_for_tertlesim/node_for_tertlesim/move_in_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import rclpy
from rclpy.node import Node
from turtlesim.srv import TeleportAbsolute
from geometry_msgs.msg import Twist
import time
import math

class RotateTurtleNode(Node):
def __init__(self,x,y,z,a):
super().__init__('teleport_turtle_node')
self.publisher = self.create_publisher(Twist, '/turtle1/cmd_vel', 10)
self.x=x
self.y=y
self.z=z
self.a=a
self.timer=self.create_timer(1,self.move_in_circle) #for maintaining continuous movement

def move_in_circle(self):
message=Twist()
message.linear.x=self.x
message.linear.y=self.y
message.linear.z=self.z
message.angular.z=self.a
self.publisher.publish(message)


def main(args=None):
speed_x = float(input("Enter speed in x: "))
speed_z = float(input("Enter speed in z: "))
speed_y = float(input("Enter speed in y: "))
radius = float(input("Enter radius: "))
rclpy.init(args=args)
rotate_turtle_node = RotateTurtleNode(speed_x,speed_y,speed_z,(speed_x**2+speed_y**2+speed_z**2)**(1/2)/radius)


rclpy.spin(rotate_turtle_node)

rotate_turtle_node.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()
138 changes: 138 additions & 0 deletions Shivang/node_for_tertlesim/node_for_tertlesim/move_to_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# import rclpy
# from rclpy.node import Node
# from turtlesim.srv import TeleportAbsolute
# from turtlesim.msg import Pose
# import math

# class TeleportTurtleNode(Node):
# def __init__(self):
# super().__init__('teleport_turtle_node')
# self.teleport_client = self.create_client(TeleportAbsolute, 'turtle1/teleport_absolute')
# self.pose_subscription = self.create_subscription(Pose, 'turtle1/pose', self.pose_callback, 10)
# self.teleported_pose = None

# def teleport_turtle(self, x, y, theta):
# """Teleports the turtle to the given position and orientation."""
# while not self.teleport_client.wait_for_service(timeout_sec=1.0):
# self.get_logger().info('service not available, waiting again...')

# request = TeleportAbsolute.Request()
# request.x = float(x)
# request.y = float(y)
# request.theta = float(theta)

# future = self.teleport_client.call_async(request)
# rclpy.spin_until_future_complete(self, future)

# if future.result() is not None:
# self.get_logger().info('Turtle teleported successfully!')
# self.teleported_pose = Pose()
# self.teleported_pose.x = float(x)
# self.teleported_pose.y = float(y)
# self.teleported_pose.theta = float(theta)
# return True
# else:
# self.get_logger().error('Failed to call service teleport_absolute: %r' % future.exception())
# return False

# def pose_callback(self, pose_msg):
# """Callback function to verify the turtle's pose."""
# if self.teleported_pose is not None:
# if (abs(pose_msg.x - self.teleported_pose.x) < 0.01 and
# abs(pose_msg.y - self.teleported_pose.y) < 0.01 and
# abs(pose_msg.theta - self.teleported_pose.theta) < 0.01):
# self.get_logger().info('Turtle pose verified!')
# rclpy.shutdown() # Stop the node
# else:

# self.get_logger().warn(f'Turtle pose does not match teleported pose.\ndelta_x={pose_msg.x - self.teleported_pose.x}\ndelta_y={pose_msg.y - self.teleported_pose.y}\ndelta_theta={pose_msg.theta - self.teleported_pose.theta}\n{pose_msg.theta}')

# def main(args=None):
# rclpy.init(args=args)
# teleport_turtle_node = TeleportTurtleNode()

# x = float(input("Enter x coordinate: "))
# y = float(input("Enter y coordinate: "))
# theta = float(input("Enter theta (orientation): "))

# if teleport_turtle_node.teleport_turtle(x, y, theta):
# # rclpy.spin(teleport_turtle_node)
# teleport_turtle_node.get_logger().info("Taken the node")

# teleport_turtle_node.destroy_node()
# rclpy.shutdown()

# if __name__ == '__main__':
# main()









import rclpy
from rclpy.node import Node
from turtlesim.srv import TeleportAbsolute
from turtlesim.msg import Pose
import math

class TeleportTurtleNode(Node):
def __init__(self):
super().__init__('teleport_turtle_node')
self.teleport_client = self.create_client(TeleportAbsolute, 'turtle1/teleport_absolute')
# self.pose_subscription = self.create_subscription(Pose, 'turtle1/pose', self.pose_callback, 10)
self.teleported_pose = None

def teleport_turtle(self, x, y, theta):
"""Teleports the turtle to the given position and orientation."""
while not self.teleport_client.wait_for_service(timeout_sec=1.0):
self.get_logger().info('service not available, waiting again...')

request = TeleportAbsolute.Request()
request.x = float(x)
request.y = float(y)
request.theta = float(theta)

future = self.teleport_client.call_async(request)
rclpy.spin_until_future_complete(self, future)

if future.result() is not None:
self.get_logger().info('Turtle teleported successfully!')
self.teleported_pose = Pose()
self.teleported_pose.x = float(x)
self.teleported_pose.y = float(y)
self.teleported_pose.theta = float(theta)
return True
else:
self.get_logger().error('Failed to call service teleport_absolute: %r' % future.exception())
return False



def main(args=None):
rclpy.init(args=args)
teleport_turtle_node = TeleportTurtleNode()

x = float(input("Enter x coordinate: "))
y = float(input("Enter y coordinate: "))
theta = float(input("Enter theta (orientation): "))

is_moved_succesfully=teleport_turtle_node.teleport_turtle(x, y, theta)

while is_moved_succesfully:
# rclpy.spin(teleport_turtle_node)
x = float(input("Enter x coordinate: "))
y = float(input("Enter y coordinate: "))
theta = float(input("Enter theta (orientation): "))

is_moved_succesfully=teleport_turtle_node.teleport_turtle(x, y, theta)


teleport_turtle_node.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()
22 changes: 22 additions & 0 deletions Shivang/node_for_tertlesim/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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>node_for_tertlesim</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="shivang@todo.todo">shivang</maintainer>
<license>TODO: License declaration</license>

<depend>rclpy</depend>
<depend>std_msgs</depend>
<depend>example_interfaces</depend>

<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 Shivang/node_for_tertlesim/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/node_for_tertlesim
[install]
install_scripts=$base/lib/node_for_tertlesim
27 changes: 27 additions & 0 deletions Shivang/node_for_tertlesim/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from setuptools import find_packages, setup

package_name = 'node_for_tertlesim'

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='shivang',
maintainer_email='shivang@todo.todo',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
"mtl=node_for_tertlesim.move_to_location:main",
"mic=node_for_tertlesim.move_in_circle:main"
],
},
)
25 changes: 25 additions & 0 deletions Shivang/node_for_tertlesim/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 Shivang/node_for_tertlesim/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 Shivang/node_for_tertlesim/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'