-1
$\begingroup$

As the title say I have to spawn three turtles and have them follow y=5cosx and y=5sin(4x).

I have tried it multiple times but always keep getting errors which I'm unable to solve.

import rclpy
from rclpy.executors import MultiThreadedExecutor
from rclpy.node import Node
from turtlesim.msg import Pose
from geometry_msgs.msg import Twist
import math

class TurtleController(Node):
    def __init__(self, turtle_name, curve_equation, interval):
        super().__init__(turtle_name)
        self.turtle_name = turtle_name
        self.curve_equation = curve_equation
        self.interval = interval
        self.pose_subscriber = self.create_subscription(Pose, f'/{turtle_name}/pose', self.pose_callback, 10)
        self.velocity_publisher = self.create_publisher(Twist, f'/{turtle_name}/cmd_vel', 10)
        self.pose = None

    def pose_callback(self, msg):
        self.pose = msg

    def move_turtle(self):
        if self.pose is None:
            return

        x = self.pose.x
        y = self.pose.y
        theta = self.pose.theta

        # Calculate the desired y-coordinate based on the curve equation and interval
        desired_y = self.curve_equation(x, self.interval)

        # Calculate the error between the desired y-coordinate and the current y-coordinate
        error = desired_y - y

        # Calculate the angular velocity based on the error
        angular_velocity = error * 0.5

        # Publish the velocity command
        velocity_msg = Twist()
        velocity_msg.linear.x = 0.5
        velocity_msg.angular.z = angular_velocity
        self.velocity_publisher.publish(velocity_msg)

def cosine_curve(x, interval):
    return 5.0 * math.cos(x)

def sine_curve(x, interval):
    return 5.0 * math.sin(4.0 * (x - interval[0]) / (interval[1] - interval[0]))

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

    # Create the TurtleController instances
    turtle1 = TurtleController('turtle1', cosine_curve, [0.0, 2.0 * math.pi])
    turtle2 = TurtleController('turtle2', sine_curve, [2.0 * math.pi, 4.0 * math.pi])
    turtle3 = TurtleController('turtle3', sine_curve, [4.0 * math.pi, 6.0 * math.pi])

   # Create an executor
    executor = MultiThreadedExecutor()

    # Add the nodes to the executor
    executor.add_node(turtle1)
    executor.add_node(turtle2)
    executor.add_node(turtle3)

    try:
        # Spin the nodes until interrupted
        executor.spin()
    finally:
        # Shutdown the nodes
        executor.shutdown()
        turtle1.destroy_node()
        turtle2.destroy_node()
        turtle3.destroy_node()
        rclpy.shutdown()

if __name__ == '__main__':
    main()

This is the code I used.

I'll also write the error I'm receiving below

Traceback (most recent call last):
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/__init__.py", line 220, in spin
    executor.add_node(node)
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 254, in add_node
    if node not in self._nodes:
TypeError: unhashable type: 'list'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/harsh/turtle_ws/install/cooperative_turtles/lib/cooperative_turtles/turtle_maneuver", line 33, in <module>
    sys.exit(load_entry_point('cooperative-turtles==0.0.0', 'console_scripts', 'turtle_maneuver')())
  File "/home/harsh/turtle_ws/install/cooperative_turtles/lib/python3.10/site-packages/cooperative_turtles/turtle_maneuver.py", line 58, in main
    rclpy.spin([turtle1, turtle2, turtle3])
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/__init__.py", line 224, in spin
    executor.remove_node(node)
  File "/opt/ros/humble/local/lib/python3.10/dist-packages/rclpy/executors.py", line 270, in remove_node
    self._nodes.remove(node)
TypeError: unhashable type: 'list'

Please help me solve this and complete my project.

$\endgroup$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.