-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle_controller.py
110 lines (88 loc) · 3.73 KB
/
turtle_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
import math
import rclpy
from functools import partial
from rclpy.node import Node
from turtlesim.msg import Pose
from geometry_msgs.msg import Twist
from my_robot_interfaces.msg import Turtle
from my_robot_interfaces.msg import TurtleArray
from my_robot_interfaces.srv import CatchTurtle
class TurtleControllerNode(Node):
def __init__(self):
super().__init__("turtle_controller")
self.declare_parameter("catch_closest_turtle_first", True)
self.catch_closest_turtle_first_ = self.get_parameter("catch_closest_turtle_first").value
self.turtle_to_catch_ = None
self.pose_ = None
self.cmd_vel_publisher_ = self.create_publisher(Twist, "turtle1/cmd_vel", 10)
self.pose_subscriber_ = self.create_subscription(Pose, "turtle1/pose",
self.callback_turtle_pose, 10)
self.alive_turtles_subscriber_ = self.create_subscription(TurtleArray, "alive_turtles",
self.callback_alive_turtles, 10)
self.control_loop_timer_ = self.create_timer(0.01, self.control_loop)
def callback_turtle_pose(self, msg):
self.pose_ = msg
def callback_alive_turtles(self, msg):
if len(msg.turtles) > 0:
if self.catch_closest_turtle_first_:
closest_turtle = None
closest_turtle_distance = None
for turtle in msg.turtles:
dist_x = turtle.x - self.pose_.x
dist_y = turtle.y - self.pose_.y
distance = math.sqrt(dist_x*dist_x + dist_y*dist_y)
if closest_turtle == None or distance < closest_turtle_distance:
closest_turtle = turtle
closest_turtle_distance = distance
self.turtle_to_catch_ = closest_turtle
else:
self.turtle_to_catch_ = msg.turtles[0]
def control_loop(self):
if self.pose_ == None or self.turtle_to_catch_ == None:
return
dist_x = self.turtle_to_catch_.x - self.pose_.x
dist_y = self.turtle_to_catch_.y - self.pose_.y
distance = math.sqrt(dist_x * dist_x + dist_y * dist_y)
msg = Twist()
if distance > 0.5:
# position
msg.linear.x = 2*distance
# orientation
goal_theta = math.atan2(dist_y, dist_x)
diff = goal_theta - self.pose_.theta
if diff > math.pi:
diff -= 2*math.pi
elif diff < -math.pi:
diff += 2*math.pi
msg.angular.z = 6*diff
else:
# target reached
msg.linear.x = 0.0
msg.angular.z = 0.0
self.call_catch_turtle_server(self.turtle_to_catch_.name)
self.turtle_to_catch_ = None
self.cmd_vel_publisher_.publish(msg)
def call_catch_turtle_server(self, turtle_name):
client = self.create_client(CatchTurtle, "catch_turtle")
while not client.wait_for_service(1.0):
self.get_logger().warn("Waiting for Server...")
request = CatchTurtle.Request()
request.name = turtle_name
future = client.call_async(request)
future.add_done_callback(
partial(self.callback_call_catch_turtle, turtle_name=turtle_name))
def callback_call_catch_turtle(self, future, turtle_name):
try:
response = future.result()
if not response.success:
self.get_logger().error("Turtle " + str(turtle_name) + " could not be caught")
except Exception as e:
self.get_logger().error("Service call failed %r" % (e,))
def main(args=None):
rclpy.init(args=args)
node = TurtleControllerNode()
rclpy.spin(node)
rclpy.shutdown()
if __name__ == "__main__":
main()